smbd: Simplify callers of notify_filter_string
[Samba.git] / python / samba / sites.py
bloba59e9985c3ba8a9bbf50ae12d7240cfe1ad0b05b
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, LdbError
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 "%s: %s" % (self.__class__.__name__, self.value)
34 class SiteNotFoundException(SiteException):
35 """Raised when the site is not found and it's expected to exists."""
38 class SiteAlreadyExistsException(SiteException):
39 """Raised when the site is not found and it's expected not to exists."""
42 class SiteServerNotEmptyException(SiteException):
43 """Raised when the site still has servers attached."""
46 def create_site(samdb, configDn, siteName):
47 """
48 Create a site
50 :param samdb: A samdb connection
51 :param configDn: The DN of the configuration partition
52 :param siteName: Name of the site to create
53 :return: True upon success
54 :raise SiteAlreadyExists: if the site to be created already exists.
55 """
57 ret = samdb.search(base=configDn, scope=ldb.SCOPE_SUBTREE,
58 expression='(&(objectclass=Site)(cn=%s))' % siteName)
59 if len(ret) != 0:
60 raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName)
62 m = ldb.Message()
63 m.dn = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
64 m["objectclass"] = ldb.MessageElement("site", FLAG_MOD_ADD, "objectclass")
66 samdb.add(m)
68 m2 = ldb.Message()
69 m2.dn = ldb.Dn(samdb, "Cn=NTDS Site Settings,%s" % str(m.dn))
70 m2["objectclass"] = ldb.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD, "objectclass")
72 samdb.add(m2)
74 m3 = ldb.Message()
75 m3.dn = ldb.Dn(samdb, "Cn=Servers,%s" % str(m.dn))
76 m3["objectclass"] = ldb.MessageElement("serversContainer", FLAG_MOD_ADD, "objectclass")
78 samdb.add(m3)
80 return True
83 def delete_site(samdb, configDn, siteName):
84 """
85 Delete a site
87 :param samdb: A samdb connection
88 :param configDn: The DN of the configuration partition
89 :param siteName: Name of the site to delete
90 :return: True upon success
91 :raise SiteNotFoundException: if the site to be deleted do not exists.
92 :raise SiteServerNotEmpty: if the site has still servers in it.
93 """
95 dnsite = ldb.Dn(samdb, "CN=Sites")
96 try:
97 dnsite.add_base(configDn)
98 except ldb.LdbError:
99 raise SiteException("dnsite.add_base() failed")
100 try:
101 dnsite.add_child("CN=X")
102 except ldb.LdbError:
103 raise SiteException("dnsite.add_child() failed")
104 dnsite.set_component(0, "CN", siteName)
106 dnservers = ldb.Dn(samdb, "CN=Servers")
107 dnservers.add_base(dnsite)
109 try:
110 ret = samdb.search(base=dnsite, scope=ldb.SCOPE_BASE,
111 expression="objectClass=site")
112 if len(ret) != 1:
113 raise SiteNotFoundException('Site %s does not exist' % siteName)
114 except LdbError as e:
115 (enum, estr) = e.args
116 if enum == ldb.ERR_NO_SUCH_OBJECT:
117 raise SiteNotFoundException('Site %s does not exist' % siteName)
119 ret = samdb.search(base=dnservers, scope=ldb.SCOPE_ONELEVEL,
120 expression='(objectclass=server)')
121 if len(ret) != 0:
122 raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName)
124 samdb.delete(dnsite, ["tree_delete:0"])
126 return True