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."""
21 from ldb
import FLAG_MOD_ADD
24 class SiteException(Exception):
25 """Base element for Sites errors"""
27 def __init__(self
, value
):
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
):
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
):
50 return "SiteAlreadyExists: " + self
.value
52 class SiteServerNotEmptyException(SiteException
):
53 """Raised when the site still has servers attached."""
55 def __init__(self
, value
):
59 return "SiteServerNotEmpty: " + self
.value
61 def create_site(samdb
, configDn
, siteName
):
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.
72 ret
= samdb
.search(base
=configDn
, scope
=ldb
.SCOPE_SUBTREE
,
73 expression
='(&(objectclass=Site)(cn=%s))' % siteName
)
75 raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName
)
78 m
.dn
= ldb
.Dn(samdb
, "Cn=%s,CN=Sites,%s" % (siteName
, str(configDn
)))
79 m
["objectclass"] = ldb
.MessageElement("site", FLAG_MOD_ADD
, "objectclass")
84 m2
.dn
= ldb
.Dn(samdb
, "Cn=NTDS Site Settings,%s" % str(m
.dn
))
85 m2
["objectclass"] = ldb
.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD
, "objectclass")
90 m3
.dn
= ldb
.Dn(samdb
, "Cn=Servers,%s" % str(m
.dn
))
91 m3
["objectclass"] = ldb
.MessageElement("serversContainer", FLAG_MOD_ADD
, "objectclass")
97 def delete_site(samdb
, configDn
, siteName
):
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
))
116 raise SiteNotFoundException('Site %s do not exists' % siteName
)
118 ret
= samdb
.search(base
=dnserver
, scope
=ldb
.SCOPE_ONELEVEL
,
119 expression
='(objectclass=server)')
121 raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName
)
123 samdb
.delete(dnsite
, ["tree_delete:0"])