s4 provision/dns: Move secretsdb_setup_dns to the AD DNS specific setup
[Samba/gebeck_regimport.git] / source4 / scripting / python / samba / sites.py
blobf18441cbbb1a1e24d1868cf9b2374e7e2cd2e433
1 #!/usr/bin/env python
3 # python site manipulation code
4 # Copyright Matthieu Patou <mat@matws.net> 2011
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """Manipulating sites."""
22 import ldb
23 from ldb import FLAG_MOD_ADD
26 class SiteException(Exception):
27 """Base element for Sites errors"""
29 def __init__(self, value):
30 self.value = value
32 def __str__(self):
33 return "SiteException: " + self.value
36 class SiteNotFoundException(SiteException):
37 """Raised when the site is not found and it's expected to exists."""
39 def __init__(self, value):
40 self.value = value
42 def __str__(self):
43 return "SiteNotFoundException: " + self.value
45 class SiteAlreadyExistsException(SiteException):
46 """Raised when the site is not found and it's expected not to exists."""
48 def __init__(self, value):
49 self.value = value
51 def __str__(self):
52 return "SiteAlreadyExists: " + self.value
54 class SiteServerNotEmptyException(SiteException):
55 """Raised when the site still has servers attached."""
57 def __init__(self, value):
58 self.value = value
60 def __str__(self):
61 return "SiteServerNotEmpty: " + self.value
63 def create_site(samdb, configDn, siteName):
64 """
65 Create a site
67 :param samdb: A samdb connection
68 :param configDn: The DN of the configuration partition
69 :param siteName: Name of the site to create
70 :return: True upon success
71 :raise SiteAlreadyExists: if the site to be created already exists.
72 """
74 ret = samdb.search(base=configDn, scope=ldb.SCOPE_SUBTREE,
75 expression='(&(objectclass=Site)(cn=%s))' % siteName)
76 if len(ret) != 0:
77 raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName)
79 m = ldb.Message()
80 m.dn = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
81 m["objectclass"] = ldb.MessageElement("site", FLAG_MOD_ADD, "objectclass")
83 samdb.add(m)
85 m2 = ldb.Message()
86 m2.dn = ldb.Dn(samdb, "Cn=NTDS Site Settings,%s" % str(m.dn))
87 m2["objectclass"] = ldb.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD, "objectclass")
89 samdb.add(m2)
91 m3 = ldb.Message()
92 m3.dn = ldb.Dn(samdb, "Cn=Servers,%s" % str(m.dn))
93 m3["objectclass"] = ldb.MessageElement("serversContainer", FLAG_MOD_ADD, "objectclass")
95 samdb.add(m3)
97 return True
99 def delete_site(samdb, configDn, siteName):
101 Delete a site
103 :param samdb: A samdb connection
104 :param configDn: The DN of the configuration partition
105 :param siteName: Name of the site to delete
106 :return: True upon success
107 :raise SiteNotFoundException: if the site to be deleted do not exists.
108 :raise SiteServerNotEmpty: if the site has still servers in it.
111 dnsites = ldb.Dn(samdb, "CN=Sites,%s" % (str(configDn)))
112 dnsite = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
113 dnserver = ldb.Dn(samdb, "Cn=Servers,%s" % str(dnsite))
115 ret = samdb.search(base=dnsites, scope=ldb.SCOPE_ONELEVEL,
116 expression='(dn=%s)' % str(dnsite))
117 if len(ret) != 1:
118 raise SiteNotFoundException('Site %s do not exists' % siteName)
120 ret = samdb.search(base=dnserver, scope=ldb.SCOPE_ONELEVEL,
121 expression='(objectclass=server)')
122 if len(ret) != 0:
123 raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName)
125 samdb.delete(dnsite, ["tree_delete:0"])
127 return True