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."""
23 from ldb
import FLAG_MOD_ADD
26 class SiteException(Exception):
27 """Base element for Sites errors"""
29 def __init__(self
, value
):
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
):
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
):
52 return "SiteAlreadyExists: " + self
.value
54 class SiteServerNotEmptyException(SiteException
):
55 """Raised when the site still has servers attached."""
57 def __init__(self
, value
):
61 return "SiteServerNotEmpty: " + self
.value
63 def create_site(samdb
, configDn
, siteName
):
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.
74 ret
= samdb
.search(base
=configDn
, scope
=ldb
.SCOPE_SUBTREE
,
75 expression
='(&(objectclass=Site)(cn=%s))' % siteName
)
77 raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName
)
80 m
.dn
= ldb
.Dn(samdb
, "Cn=%s,CN=Sites,%s" % (siteName
, str(configDn
)))
81 m
["objectclass"] = ldb
.MessageElement("site", FLAG_MOD_ADD
, "objectclass")
86 m2
.dn
= ldb
.Dn(samdb
, "Cn=NTDS Site Settings,%s" % str(m
.dn
))
87 m2
["objectclass"] = ldb
.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD
, "objectclass")
92 m3
.dn
= ldb
.Dn(samdb
, "Cn=Servers,%s" % str(m
.dn
))
93 m3
["objectclass"] = ldb
.MessageElement("serversContainer", FLAG_MOD_ADD
, "objectclass")
99 def delete_site(samdb
, configDn
, siteName
):
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
))
118 raise SiteNotFoundException('Site %s do not exists' % siteName
)
120 ret
= samdb
.search(base
=dnserver
, scope
=ldb
.SCOPE_ONELEVEL
,
121 expression
='(objectclass=server)')
123 raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName
)
125 samdb
.delete(dnsite
, ["tree_delete:0"])