s4-sites: Document, fix under optimal coding, use exceptions
[metze/samba/wip.git] / source4 / scripting / python / samba / netcmd / sites.py
1
2 #!/usr/bin/env python
3 #
4 # sites management
5 #
6 # Copyright Matthieu Patou <mat@matws.net> 2011
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21
22
23
24 import os
25 from samba import sites
26 from samba.samdb import SamDB
27 from samba.auth import system_session
28 from samba.netcmd import (
29     Command,
30     CommandError,
31     SuperCommand
32     )
33
34
35 class cmd_sites_create(Command):
36     """Create a new site"""
37
38     synopsis = "%prog <site> [options]"
39
40     takes_args = ["sitename"]
41
42     def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
43         lp = sambaopts.get_loadparm()
44         creds = credopts.get_credentials(lp, fallback_machine=True)
45         url =  lp.private_path("sam.ldb")
46
47         if not os.path.exists(url):
48             raise CommandError("secret database not found at %s " % url)
49         samdb = SamDB(url=url, session_info=system_session(),
50                       credentials=creds, lp=lp)
51
52         samdb.transaction_start()
53         try:
54             ok = sites.create_site(samdb, samdb.get_config_basedn(), sitename)
55             samdb.transaction_commit()
56         except sites.SiteAlreadyExistsException, e:
57             samdb.transaction_cancel()
58             raise CommandError("Error while creating site %s, error: %s" % (sitename, str(e)))
59
60         self.outf.write("Site %s created !\n" % sitename)
61
62 class cmd_sites_delete(Command):
63     """Delete a new site"""
64
65     synopsis = "%prog <site> [options]"
66
67     takes_args = ["sitename"]
68
69     def run(self, sitename, sambaopts=None, credopts=None, versionopts=None):
70         lp = sambaopts.get_loadparm()
71         creds = credopts.get_credentials(lp, fallback_machine=True)
72         url =  lp.private_path("sam.ldb")
73
74         if not os.path.exists(url):
75             raise CommandError("secret database not found at %s " % url)
76         samdb = SamDB(url=url, session_info=system_session(),
77             credentials=creds, lp=lp)
78
79         samdb.transaction_start()
80         try:
81             ok = sites.delete_site(samdb, samdb.get_config_basedn(), sitename)
82             samdb.transaction_commit()
83         except sites.SiteException, e:
84             samdb.transaction_cancel()
85             raise CommandError("Error while removing site %s, error: %s" % (sitename, str(e)))
86
87         self.outf.write("Site %s removed!\n" % sitename)
88
89
90
91 class cmd_sites(SuperCommand):
92     """Sites management"""
93
94     subcommands = {}
95     subcommands["create"] = cmd_sites_create()
96     subcommands["remove"] = cmd_sites_delete()