Move python modules from source4/scripting/python/ to python/.
[bbaumbach/samba-autobuild/.git] / python / samba / sites.py
1 # python site manipulation code
2 # Copyright Matthieu Patou <mat@matws.net> 2011
3 #
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.
8 #
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.
13 #
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/>.
16 #
17
18 """Manipulating sites."""
19
20 import ldb
21 from ldb import FLAG_MOD_ADD
22
23
24 class SiteException(Exception):
25     """Base element for Sites errors"""
26
27     def __init__(self, value):
28         self.value = value
29
30     def __str__(self):
31         return "SiteException: " + self.value
32
33
34 class SiteNotFoundException(SiteException):
35     """Raised when the site is not found and it's expected to exists."""
36
37     def __init__(self, value):
38         self.value = value
39
40     def __str__(self):
41         return "SiteNotFoundException: " + self.value
42
43 class SiteAlreadyExistsException(SiteException):
44     """Raised when the site is not found and it's expected not to exists."""
45
46     def __init__(self, value):
47         self.value = value
48
49     def __str__(self):
50         return "SiteAlreadyExists: " + self.value
51
52 class SiteServerNotEmptyException(SiteException):
53     """Raised when the site still has servers attached."""
54
55     def __init__(self, value):
56         self.value = value
57
58     def __str__(self):
59         return "SiteServerNotEmpty: " + self.value
60
61 def create_site(samdb, configDn, siteName):
62     """
63     Create a site
64
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.
70     """
71
72     ret = samdb.search(base=configDn, scope=ldb.SCOPE_SUBTREE,
73                     expression='(&(objectclass=Site)(cn=%s))' % siteName)
74     if len(ret) != 0:
75         raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName)
76
77     m = ldb.Message()
78     m.dn = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
79     m["objectclass"] = ldb.MessageElement("site", FLAG_MOD_ADD, "objectclass")
80
81     samdb.add(m)
82
83     m2 = ldb.Message()
84     m2.dn = ldb.Dn(samdb, "Cn=NTDS Site Settings,%s" % str(m.dn))
85     m2["objectclass"] = ldb.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD, "objectclass")
86
87     samdb.add(m2)
88
89     m3 = ldb.Message()
90     m3.dn = ldb.Dn(samdb, "Cn=Servers,%s" % str(m.dn))
91     m3["objectclass"] = ldb.MessageElement("serversContainer", FLAG_MOD_ADD, "objectclass")
92
93     samdb.add(m3)
94
95     return True
96
97 def delete_site(samdb, configDn, siteName):
98     """
99     Delete a site
100
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.
107     """
108
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))
112
113     ret = samdb.search(base=dnsites, scope=ldb.SCOPE_ONELEVEL,
114                     expression='(dn=%s)' % str(dnsite))
115     if len(ret) != 1:
116         raise SiteNotFoundException('Site %s do not exists' % siteName)
117
118     ret = samdb.search(base=dnserver, scope=ldb.SCOPE_ONELEVEL,
119                     expression='(objectclass=server)')
120     if len(ret) != 0:
121         raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName)
122
123     samdb.delete(dnsite, ["tree_delete:0"])
124
125     return True