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