423a0593f3bbff148fef858ed04a2a55bb220d86
[bbaumbach/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 __future__ import print_function
20 from samba import sites, subnets
21 from samba.samdb import SamDB
22 import samba.getopt as options
23 from samba.auth import system_session
24 from samba.netcmd import (
25     Command,
26     CommandError,
27     SuperCommand,
28     Option,
29 )
30
31
32 class cmd_sites_create(Command):
33     """Create a new site."""
34
35     synopsis = "%prog <site> [options]"
36
37     takes_args = ["sitename"]
38
39     takes_optiongroups = {
40         "sambaopts": options.SambaOptions,
41         "versionopts": options.VersionOptions,
42         "credopts": options.CredentialsOptions,
43     }
44
45     takes_options = [
46         Option("-H", "--URL", help="LDB URL for database or target server",
47                type=str, metavar="URL", dest="H"),
48     ]
49
50     def run(self, sitename, H=None, sambaopts=None, credopts=None,
51             versionopts=None):
52         lp = sambaopts.get_loadparm()
53         creds = credopts.get_credentials(lp, fallback_machine=True)
54         samdb = SamDB(url=H, session_info=system_session(),
55                       credentials=creds, lp=lp)
56
57         samdb.transaction_start()
58         try:
59             sites.create_site(samdb, samdb.get_config_basedn(), sitename)
60             samdb.transaction_commit()
61         except sites.SiteAlreadyExistsException as e:
62             samdb.transaction_cancel()
63             raise CommandError("Error while creating site %s, error: %s" %
64                                (sitename, str(e)))
65
66         self.outf.write("Site %s created !\n" % sitename)
67
68
69 class cmd_sites_delete(Command):
70     """Delete an existing site."""
71
72     synopsis = "%prog <site> [options]"
73
74     takes_args = ["sitename"]
75
76     takes_optiongroups = {
77         "sambaopts": options.SambaOptions,
78         "versionopts": options.VersionOptions,
79         "credopts": options.CredentialsOptions,
80     }
81
82     takes_options = [
83         Option("-H", "--URL", help="LDB URL for database or target server",
84                type=str, metavar="URL", dest="H"),
85     ]
86
87     def run(self, sitename, H=None, sambaopts=None, credopts=None,
88             versionopts=None):
89         lp = sambaopts.get_loadparm()
90         creds = credopts.get_credentials(lp, fallback_machine=True)
91         samdb = SamDB(url=H, session_info=system_session(),
92                       credentials=creds, lp=lp)
93
94         samdb.transaction_start()
95         try:
96             sites.delete_site(samdb, samdb.get_config_basedn(), sitename)
97             samdb.transaction_commit()
98         except sites.SiteException as e:
99             samdb.transaction_cancel()
100             raise CommandError(
101                 "Error while removing site %s, error: %s" % (sitename, str(e)))
102
103         self.outf.write("Site %s removed!\n" % sitename)
104
105
106 class cmd_sites_subnet_create(Command):
107     """Create a new subnet."""
108     synopsis = "%prog <subnet> <site-of-subnet> [options]"
109     takes_args = ["subnetname", "site_of_subnet"]
110
111     takes_optiongroups = {
112         "sambaopts": options.SambaOptions,
113         "versionopts": options.VersionOptions,
114         "credopts": options.CredentialsOptions,
115     }
116
117     takes_options = [
118         Option("-H", "--URL", help="LDB URL for database or target server",
119                type=str, metavar="URL", dest="H"),
120     ]
121
122     def run(self, subnetname, site_of_subnet, H=None, sambaopts=None,
123             credopts=None, versionopts=None):
124         lp = sambaopts.get_loadparm()
125         creds = credopts.get_credentials(lp)
126         samdb = SamDB(url=H, session_info=system_session(),
127                       credentials=creds, lp=lp)
128
129         samdb.transaction_start()
130         try:
131             subnets.create_subnet(samdb, samdb.get_config_basedn(), subnetname,
132                                   site_of_subnet)
133             samdb.transaction_commit()
134         except subnets.SubnetException as e:
135             samdb.transaction_cancel()
136             raise CommandError("Error while creating subnet %s: %s" %
137                                (subnetname, e))
138
139         self.outf.write("Subnet %s created !\n" % subnetname)
140
141
142 class cmd_sites_subnet_delete(Command):
143     """Delete an existing subnet."""
144
145     synopsis = "%prog <subnet> [options]"
146
147     takes_args = ["subnetname"]
148
149     takes_optiongroups = {
150         "sambaopts": options.SambaOptions,
151         "versionopts": options.VersionOptions,
152         "credopts": options.CredentialsOptions,
153     }
154
155     takes_options = [
156         Option("-H", "--URL", help="LDB URL for database or target server",
157                type=str, metavar="URL", dest="H"),
158     ]
159
160     def run(self, subnetname, H=None, sambaopts=None, credopts=None,
161             versionopts=None):
162         lp = sambaopts.get_loadparm()
163         creds = credopts.get_credentials(lp)
164         samdb = SamDB(url=H, session_info=system_session(),
165                       credentials=creds, lp=lp)
166
167         samdb.transaction_start()
168         try:
169             subnets.delete_subnet(samdb, samdb.get_config_basedn(), subnetname)
170             samdb.transaction_commit()
171         except subnets.SubnetException as e:
172             samdb.transaction_cancel()
173             raise CommandError("Error while removing subnet %s, error: %s" %
174                                (subnetname, e))
175
176         self.outf.write("Subnet %s removed!\n" % subnetname)
177
178
179 class cmd_sites_subnet_set_site(Command):
180     """Assign a subnet to a site."""
181     synopsis = "%prog <subnet> <site-of-subnet> [options]"
182     takes_args = ["subnetname", "site_of_subnet"]
183
184     takes_optiongroups = {
185         "sambaopts": options.SambaOptions,
186         "versionopts": options.VersionOptions,
187         "credopts": options.CredentialsOptions,
188     }
189
190     takes_options = [
191         Option("-H", "--URL", help="LDB URL for database or target server",
192                type=str, metavar="URL", dest="H"),
193     ]
194
195     def run(self, subnetname, site_of_subnet, H=None, sambaopts=None,
196             credopts=None, versionopts=None):
197         lp = sambaopts.get_loadparm()
198         creds = credopts.get_credentials(lp)
199         samdb = SamDB(url=H, session_info=system_session(),
200                       credentials=creds, lp=lp)
201
202         samdb.transaction_start()
203         try:
204             subnets.set_subnet_site(samdb, samdb.get_config_basedn(),
205                                     subnetname, site_of_subnet)
206             samdb.transaction_commit()
207         except subnets.SubnetException as e:
208             samdb.transaction_cancel()
209             raise CommandError("Error assigning subnet %s to site %s: %s" %
210                                (subnetname, site_of_subnet, e))
211
212         print(("Subnet %s shifted to site %s" %
213                (subnetname, site_of_subnet)), file=self.outf)
214
215
216 class cmd_sites_subnet(SuperCommand):
217     """Subnet management subcommands."""
218     subcommands = {
219         "create": cmd_sites_subnet_create(),
220         "remove": cmd_sites_subnet_delete(),
221         "set-site": cmd_sites_subnet_set_site(),
222     }
223
224 class cmd_sites(SuperCommand):
225     """Sites management."""
226     subcommands = {}
227     subcommands["create"] = cmd_sites_create()
228     subcommands["remove"] = cmd_sites_delete()
229     subcommands["subnet"] = cmd_sites_subnet()