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
, LdbError
24 class SiteException(Exception):
25 """Base element for Sites errors"""
27 def __init__(self
, value
):
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
):
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.
57 ret
= samdb
.search(base
=configDn
, scope
=ldb
.SCOPE_SUBTREE
,
58 expression
='(&(objectclass=Site)(cn=%s))' % siteName
)
60 raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName
)
63 m
.dn
= ldb
.Dn(samdb
, "Cn=%s,CN=Sites,%s" % (siteName
, str(configDn
)))
64 m
["objectclass"] = ldb
.MessageElement("site", FLAG_MOD_ADD
, "objectclass")
69 m2
.dn
= ldb
.Dn(samdb
, "Cn=NTDS Site Settings,%s" % str(m
.dn
))
70 m2
["objectclass"] = ldb
.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD
, "objectclass")
75 m3
.dn
= ldb
.Dn(samdb
, "Cn=Servers,%s" % str(m
.dn
))
76 m3
["objectclass"] = ldb
.MessageElement("serversContainer", FLAG_MOD_ADD
, "objectclass")
83 def delete_site(samdb
, configDn
, siteName
):
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.
95 dnsite
= ldb
.Dn(samdb
, "CN=Sites")
96 if dnsite
.add_base(configDn
) == False:
97 raise SiteException("dnsites.add_base() failed")
98 if dnsite
.add_child("CN=X") == False:
99 raise SiteException("dnsites.add_child() failed")
100 dnsite
.set_component(0, "CN", siteName
)
102 dnservers
= ldb
.Dn(samdb
, "CN=Servers")
103 dnservers
.add_base(dnsite
)
106 ret
= samdb
.search(base
=dnsite
, scope
=ldb
.SCOPE_BASE
,
107 expression
="objectClass=site")
109 raise SiteNotFoundException('Site %s does not exist' % siteName
)
110 except LdbError
as (enum
, estr
):
111 if enum
== ldb
.ERR_NO_SUCH_OBJECT
:
112 raise SiteNotFoundException('Site %s does not exist' % siteName
)
114 ret
= samdb
.search(base
=dnservers
, scope
=ldb
.SCOPE_ONELEVEL
,
115 expression
='(objectclass=server)')
117 raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName
)
119 samdb
.delete(dnsite
, ["tree_delete:0"])