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
25 def create_site(samdb
, configDn
, siteName
):
26 ret
= samdb
.search(base
=configDn
, scope
=ldb
.SCOPE_SUBTREE
,
27 expression
='(&(objectclass=Site)(cn=%s))' % siteName
)
29 raise Exception('A site with the name %s already exists' % siteName
)
32 m
.dn
= ldb
.Dn(samdb
, "Cn=%s,CN=Sites,%s" % (siteName
, str(configDn
)))
33 m
["objectclass"] = ldb
.MessageElement("site", FLAG_MOD_ADD
, "objectclass")
38 m2
.dn
= ldb
.Dn(samdb
, "Cn=NTDS Site Settings,%s" % str(m
.dn
))
39 m2
["objectclass"] = ldb
.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD
, "objectclass")
44 m3
.dn
= ldb
.Dn(samdb
, "Cn=Servers,%s" % str(m
.dn
))
45 m3
["objectclass"] = ldb
.MessageElement("serversContainer", FLAG_MOD_ADD
, "objectclass")
51 def delete_site(samdb
, configDn
, siteName
):
53 dnsite
= ldb
.Dn(samdb
, "Cn=%s,CN=Sites,%s" % (siteName
, str(configDn
)))
54 dnserver
= ldb
.Dn(samdb
, "Cn=Servers,%s" % str(dnsite
))
56 ret
= samdb
.search(base
=dnserver
, scope
=ldb
.SCOPE_ONELEVEL
,
57 expression
='(objectclass=server)')
59 raise Exception('Site %s still has servers in it, move them before removal' % siteName
)
61 samdb
.delete(dnsite
, ["tree_delete:0"])