PEP8: fix E128: continuation line under-indented for visual indent
[amitay/samba.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, LdbError
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 "%s: %s" % (self.__class__.__name__, self.value)
32
33
34 class SiteNotFoundException(SiteException):
35     """Raised when the site is not found and it's expected to exists."""
36
37
38 class SiteAlreadyExistsException(SiteException):
39     """Raised when the site is not found and it's expected not to exists."""
40
41
42 class SiteServerNotEmptyException(SiteException):
43     """Raised when the site still has servers attached."""
44
45
46 def create_site(samdb, configDn, siteName):
47     """
48     Create a site
49
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.
55     """
56
57     ret = samdb.search(base=configDn, scope=ldb.SCOPE_SUBTREE,
58                        expression='(&(objectclass=Site)(cn=%s))' % siteName)
59     if len(ret) != 0:
60         raise SiteAlreadyExistsException('A site with the name %s already exists' % siteName)
61
62     m = ldb.Message()
63     m.dn = ldb.Dn(samdb, "Cn=%s,CN=Sites,%s" % (siteName, str(configDn)))
64     m["objectclass"] = ldb.MessageElement("site", FLAG_MOD_ADD, "objectclass")
65
66     samdb.add(m)
67
68     m2 = ldb.Message()
69     m2.dn = ldb.Dn(samdb, "Cn=NTDS Site Settings,%s" % str(m.dn))
70     m2["objectclass"] = ldb.MessageElement("nTDSSiteSettings", FLAG_MOD_ADD, "objectclass")
71
72     samdb.add(m2)
73
74     m3 = ldb.Message()
75     m3.dn = ldb.Dn(samdb, "Cn=Servers,%s" % str(m.dn))
76     m3["objectclass"] = ldb.MessageElement("serversContainer", FLAG_MOD_ADD, "objectclass")
77
78     samdb.add(m3)
79
80     return True
81
82
83 def delete_site(samdb, configDn, siteName):
84     """
85     Delete a site
86
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.
93     """
94
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)
101
102     dnservers = ldb.Dn(samdb, "CN=Servers")
103     dnservers.add_base(dnsite)
104
105     try:
106         ret = samdb.search(base=dnsite, scope=ldb.SCOPE_BASE,
107                            expression="objectClass=site")
108         if len(ret) != 1:
109             raise SiteNotFoundException('Site %s does not exist' % siteName)
110     except LdbError as e:
111         (enum, estr) = e.args
112         if enum == ldb.ERR_NO_SUCH_OBJECT:
113             raise SiteNotFoundException('Site %s does not exist' % siteName)
114
115     ret = samdb.search(base=dnservers, scope=ldb.SCOPE_ONELEVEL,
116                        expression='(objectclass=server)')
117     if len(ret) != 0:
118         raise SiteServerNotEmptyException('Site %s still has servers in it, move them before removal' % siteName)
119
120     samdb.delete(dnsite, ["tree_delete:0"])
121
122     return True