libcli/cldap: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / source4 / scripting / python / samba / sites.py
blob76c57dd11cfe0a94ed47ce33b9983d7c14d8668d
1 # python site manipulation code
2 # Copyright Matthieu Patou <mat@matws.net> 2011
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 """Manipulating sites."""
20 import ldb
21 from ldb import FLAG_MOD_ADD
24 class SiteException(Exception):
25 """Base element for Sites errors"""
27 def __init__(self, value):
28 self.value = value
30 def __str__(self):
31 return "SiteException: " + self.value
34 class SiteNotFoundException(SiteException):
35 """Raised when the site is not found and it's expected to exists."""
37 def __init__(self, value):
38 self.value = value
40 def __str__(self):
41 return "SiteNotFoundException: " + self.value
43 class SiteAlreadyExistsException(SiteException):
44 """Raised when the site is not found and it's expected not to exists."""
46 def __init__(self, value):
47 self.value = value
49 def __str__(self):
50 return "SiteAlreadyExists: " + self.value
52 class SiteServerNotEmptyException(SiteException):
53 """Raised when the site still has servers attached."""
55 def __init__(self, value):
56 self.value = value
58 def __str__(self):
59 return "SiteServerNotEmpty: " + self.value
61 def create_site(samdb, configDn, siteName):
62 """
63 Create a site
65 :param samdb: A samdb connection
66 :param configDn: The DN of the configuration partition
67 :param siteName: Name of the site to create
68 :return: True upon success
69 :raise SiteAlreadyExists: if the site to be created already exists.
70 """
72 ret = samdb.search(base=configDn, scope=ldb.SCOPE_SUBTREE,
73 expression='(&(objectclass=Site)(cn=%s))' % siteName)
74 if len(ret) != 0:
75 raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName)
77 m = ldb.Message()
78 m.dn = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
79 m["objectclass"] = ldb.MessageElement("site", FLAG_MOD_ADD, "objectclass")
81 samdb.add(m)
83 m2 = ldb.Message()
84 m2.dn = ldb.Dn(samdb, "Cn=NTDS Site Settings,%s" % str(m.dn))
85 m2["objectclass"] = ldb.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD, "objectclass")
87 samdb.add(m2)
89 m3 = ldb.Message()
90 m3.dn = ldb.Dn(samdb, "Cn=Servers,%s" % str(m.dn))
91 m3["objectclass"] = ldb.MessageElement("serversContainer", FLAG_MOD_ADD, "objectclass")
93 samdb.add(m3)
95 return True
97 def delete_site(samdb, configDn, siteName):
98 """
99 Delete a site
101 :param samdb: A samdb connection
102 :param configDn: The DN of the configuration partition
103 :param siteName: Name of the site to delete
104 :return: True upon success
105 :raise SiteNotFoundException: if the site to be deleted do not exists.
106 :raise SiteServerNotEmpty: if the site has still servers in it.
109 dnsites = ldb.Dn(samdb, "CN=Sites,%s" % (str(configDn)))
110 dnsite = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
111 dnserver = ldb.Dn(samdb, "Cn=Servers,%s" % str(dnsite))
113 ret = samdb.search(base=dnsites, scope=ldb.SCOPE_ONELEVEL,
114 expression='(dn=%s)' % str(dnsite))
115 if len(ret) != 1:
116 raise SiteNotFoundException('Site %s do not exists' % siteName)
118 ret = samdb.search(base=dnserver, scope=ldb.SCOPE_ONELEVEL,
119 expression='(objectclass=server)')
120 if len(ret) != 0:
121 raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName)
123 samdb.delete(dnsite, ["tree_delete:0"])
125 return True