root / lib / pyzinn / zone / vsite.py @ 92161d985777e77f04cfdd7bf9efc0a78ff292d8

View | Annotate | Download (5.1 KB)

1
# Copyright (c) 2009 Lost Oasis, IELO <info@ielo.net>
2
#
3
# This file is part of ZINN.
4
# 
5
# ZINN is free software: you can redistribute it and/or modify it under
6
# the terms of the GNU General Public License as published by the Free
7
# Software Foundation, either version 3 of the License, or (at your
8
# option) any later version.
9
# 
10
# ZINN is distributed in the hope that it will be useful, but WITHOUT
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13
# for more details.
14
# 
15
# You should have received a copy of the GNU General Public License
16
# along with ZINN.  If not, see <http://www.gnu.org/licenses/>.
17
18
import copy
19
import formencode
20
import sqlobject
21
from sqlobject import *
22
from twisted.web import xmlrpc
23
from twisted.web.xmlrpc import *
24
25
import gettext
26
_ = gettext.gettext
27
28
import pyzinn.database as db
29
30
from pysinn.site.abstract import service
31
from pyzinn.zone.abstract import vsite
32
33
import pyzinn.sitemgr as sitemgr
34
35
import client
36
import sitegroup
37
38
class VSite(vsite.AVSite, object):
39
    def __init__(self):
40
        self.name = self.__class__.__name__
41
        self.db = db
42
        super(VSite, self).__init__()
43
44
    def xmlrpc_create(self, label, attributes=None):
45
        sg = None
46
        cl = None
47
        if isinstance(attributes, dict):
48
            cl = client.Client().byLabel(attributes.get('client'))
49
            sg = sitegroup.SiteGroup().byLabel(attributes.get('siteGroup'))
50
        else:
51
            msg = _('Missing arguments')
52
            raise Fault(self.INVALID_ATTR, msg)
53
54
        self.user.raise_can_client(cl)
55
56
        # FIXME: sitegroup is mandatory ? So attributes needs to be
57
        default = {
58
            'label'     : label,
59
            'siteGroup' : sg,
60
            'client'    : cl
61
        }
62
        args = self.AttributesFilter(attributes, default)
63
        try:
64
            db.VSite(**args)
65
        except formencode.Invalid, e:
66
            raise Fault(self.INVALID_ATTR, str(e))
67
        except dberrors.DuplicateEntryError:
68
            msg = _('Vsite %s already exists.') % label
69
            raise Fault(self.ALREADY_EXIST_ERROR, msg)
70
        else:
71
            sitemgr.RegisterService(label, label, self.__class__.__name__,
72
                    attributes)
73
74
    xmlrpc_create.__dict__ = copy.deepcopy(
75
            vsite.AVSite.xmlrpc_create.__dict__
76
            )
77
78
    def xmlrpc_list(self, label=None):
79
        if not label:
80
            return [v.label for v in self.getAll() if self.user.can(v, 'list',
81
                    v.label, self.__class__.__name__)]
82
        else:
83
            vsite = self.byLabel(label)
84
85
            self.user.raise_can(vsite, 'list', vsite.label, self.__class__.__name__)
86
87
            d = {
88
                'client'      : vsite.client.label,
89
                'siteGroup'   : vsite.siteGroup.label,
90
                'services'    : {},
91
            }
92
            if vsite.services:
93
                for i in vsite.services:
94
                    if i.family.label != self.__class__.__name__:
95
                        if i.family.label not in d['services']:
96
                            d['services'][i.family.label] = []
97
                        d['services'][i.family.label].append(i.label)
98
        for a in self.getAttributes(onlyMutables=False):
99
                d[a] = getattr(vsite, a)
100
        return [d]
101
102
    xmlrpc_list.__dict__ = copy.deepcopy(
103
            vsite.AVSite.xmlrpc_list.__dict__
104
            )
105
106
107
    def xmlrpc_accounting(self, label):
108
        vsite = self.byLabel(label)
109
        self.user.raise_can(vsite, 'list', vsite.label, self.__class__.__name__)
110
    
111
        d = {}
112
        if vsite.services:
113
            for i in vsite.services:
114
                if i.family.label != self.__class__.__name__:
115
                    if i.family.label not in d:
116
                        d[i.family.label] = {}
117
                    if i.family.label == 'VMachine':
118
                        d[i.family.label][i.label] = sitemgr.SitePool().Call(
119
                                'machine.accounting', 
120
                                vsite.siteGroup.label, '%s.%s' % (vsite.label,
121
                                    i.label))
122
                    if i.family.label == 'Interface':
123
                        d[i.family.label][i.label] = sitemgr.SitePool().Call(
124
                                'interface.accounting', 
125
                                vsite.siteGroup.label, '%s.%s' % (vsite.label, 
126
                                   i.label))
127
128
        return d
129
130
    xmlrpc_list.__dict__ = copy.deepcopy(
131
            vsite.AVSite.xmlrpc_accounting.__dict__
132
            )
133
134
135
136
    def xmlrpc_delete(self, label):
137
        vsite = self.byLabel(label)
138
139
        self.user.raise_can(vsite, 'delete', vsite.label)
140
141
        for s in vsite.services:
142
            if s.family.label != self.__class__.__name__:
143
                msg = _('Vsite %s is in use!')
144
                raise Fault(self.ALREADY_ATTACHED_ERROR, msg % label)
145
        sitemgr.UnRegisterService(label, label, self.__class__.__name__)
146
        vsite.destroySelf()
147
148
    xmlrpc_delete.__dict__ = copy.deepcopy(
149
            vsite.AVSite.xmlrpc_delete.__dict__
150
            )
151
152
	
153
# vim: tabstop=4 expandtab shiftwidth=4 textwidth=79