Previous topic

Sample Service Provider

Next topic

Sample publication

This Page

Sample service

Here we cover two services. ServiceOne, that do not needs publication and ServiceTwo, that needs publication.

This sample should be enought to guide you through the creation of a new service.

Download sample

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# -*- coding: utf-8 -*-

#
# Copyright (c) 2012 Virtual Cable S.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, 
# are permitted provided that the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice, 
#      this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, 
#      this list of conditions and the following disclaimer in the documentation 
#      and/or other materials provided with the distribution.
#    * Neither the name of Virtual Cable S.L. nor the names of its contributors 
#      may be used to endorse or promote products derived from this software 
#      without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'''
.. moduleauthor:: Adolfo Gómez, dkmaster at dkmon dot com
'''

from django.utils.translation import ugettext_noop as translatable, ugettext as _
from uds.core.services import Service
from SamplePublication import SamplePublication
from SampleUserDeploymentOne import SampleUserDeploymentOne
from SampleUserDeploymentTwo import SampleUserDeploymentTwo

from uds.core.ui import gui

import logging

logger = logging.getLogger(__name__)

class ServiceOne(Service):
    '''
    Basic service, the first part (variables) include the description of the service.
    
    Remember to fill all variables needed, but at least you must define:
        * typeName
        * typeType
        * typeDescription
        * iconFile (defaults to service.png)
        * publicationType, type of publication in case it needs publication. 
          If this is not provided, core will assume that the service do not 
          needs publishing.
        * deployedType, type of deployed user service. Do not forget this!!!
        
    The rest of them can be ommited, but its recommended that you fill all
    declarations shown in this sample (that in fact, are all)
    
    This description informs the core what this service really provides,
    and how this is done. Look at description of class variables for more
    information.
    
    '''
    #: Name to show the administrator. This string will be translated BEFORE
    #: sending it to administration interface, so don't forget to
    #: mark it as translatable (using ugettext_noop)
    typeName = translatable('Sample Service One') 
    #: Type used internally to identify this provider
    typeType = 'SampleService1'
    #: Description shown at administration interface for this provider
    typeDescription = translatable('Sample (and dummy) service ONE')
    #: Icon file used as icon for this provider. This string will be translated 
    #: BEFORE sending it to administration interface, so don't forget to
    #: mark it as translatable (using ugettext_noop)
    iconFile = 'service.png'
    
    # Functional related data
    
    #: If the service provides more than 1 "deployed user" (-1 = no limit, 
    #: 0 = ???? (do not use it!!!), N = max number to deploy
    maxDeployed = -1
    #: If we need to generate "cache" for this service, so users can access the 
    #: provided services faster. Is usesCache is True, you will need also 
    #: set publicationType, do take care about that!
    usesCache = False 
    #: Tooltip shown to user when this item is pointed at admin interface, none 
    #: because we don't use it
    cacheTooltip = translatable('None')
    #: If we need to generate a "Level 2" cache for this service (i.e., L1 
    #: could be running machines and L2 suspended machines) 
    usesCache_L2 = False 
    #: Tooltip shown to user when this item is pointed at admin interface, None 
    #: also because we don't use it
    cacheTooltip_L2 = translatable('None') 
      
    #: If the service needs a s.o. manager (managers are related to agents 
    #: provided by services itselfs, i.e. virtual machines with actors)
    needsManager = False 
    #: If true, the system can't do an automatic assignation of a deployed user 
    #: service from this service
    mustAssignManually = False 

    #: Types of publications (preparated data for deploys) 
    #: In our case, we do no need a publication, so this is None
    publicationType = None
    #: Types of deploys (services in cache and/or assigned to users)
    deployedType = SampleUserDeploymentOne
    
    # Now the form part, this service will have only two "dummy" fields
    # If we don't indicate an order, the output order of fields will be
    # "random"
    
    colour = gui.ChoiceField(order = 1,
                 label = translatable('Colour'),
                 tooltip = translatable('Colour of the field'),
                 # In this case, the choice can have none value selected by default
                 required = True, 
                 values = [ gui.choiceItem('red', 'Red'),
                     gui.choiceItem('green', 'Green'),
                     gui.choiceItem('blue', 'Blue'),
                     gui.choiceItem('nonsense', 'Blagenta')
                 ],
                 defvalue = '1' # Default value is the ID of the choicefield
             )
    
    passw = gui.PasswordField(order = 2,
                label = translatable('Password'),
                tooltip = translatable('Password for testing purposes'),
                required = True,
                defvalue = '1234' #: Default password are nonsense?? :-)
            )

    baseName = gui.TextField(order = 3,
                          label = translatable('Services names'),
                          tooltip = translatable('Base name for this user services'),
                          # In this case, the choice can have none value selected by default
                          required = True, 
                          defvalue = '' # Default value is the ID of the choicefield
             )
    
    def initialize(self, values):
        '''
        We check here form values to see if they are valid.
        
        Note that we check them throught FROM variables, that already has been
        initialized by __init__ method of base class, before invoking this.
        '''
        
        # We don't need to check anything, bat because this is a sample, we do
        # As in provider, we receive values only at new Service creation,
        # so we only need to validate params if values is not None
        if values is not None:
            if self.colour.value == 'nonsense':
                raise Service.ValidationException('The selected colour is invalid!!!')
        
        
    # Services itself are non testeable right now, so we don't even have
    # to provide one!!!
        

    # Congratulations!!!, the needed part of your first simple service is done!
    # Now you can go to administration panel, and check it
    #
    # From now onwards, we implement our own methods, that will be used by, 
    # for example, services derived from this provider
    
    def getColour(self):
        '''
        Simply returns colour, for deployed user services.
        
        Remember that choiceField.value returns the id part of the ChoiceItem
        '''
        return self.colour.value
    
    def getPassw(self):
        '''
        Simply returns passwd, for deloyed user services
        '''
        return self.passw.value
    
    def getBaseName(self):
        '''
        '''
        return self.baseName.value
    
     

class ServiceTwo(Service):
    '''
    Just a second service, no comments here (almost same that ServiceOne
    '''
    typeName = translatable('Sample Service Two') 
    typeType = 'SampleService2'
    typeDescription = translatable('Sample (and dummy) service ONE+ONE')
    iconFile = 'provider.png' #: We reuse provider icon here :-)
    
    # Functional related data
    maxDeployed = 5
    usesCache = True
    cacheTooltip = translatable('L1 cache for dummy elements')
    usesCache_L2 = True
    cacheTooltip_L2 = translatable('L2 cache for dummy elements')
      
    needsManager = False
    mustAssignManually = False

    #: Types of publications. In this case, we will include a publication
    #: type for this one  
    #: Note that this is a MUST if you indicate that needPublication
    publicationType = SamplePublication
    #: Types of deploys (services in cache and/or assigned to users)
    deployedType = SampleUserDeploymentTwo
    
    
    # Gui, we will use here the EditableList field
    names = gui.EditableList(label=translatable('List of names'))
    
    def __init__(self, environment, parent, values = None):
        '''
        We here can get a HUGE list from client.
        Right now, this is treated same as other fields, in a near
        future we will se how to handle this better
        '''
        super(ServiceTwo, self).__init__(environment, parent, values)
        
        # No checks here
        
    def getNames(self):
        '''
        For using at deployed services, really nothing
        '''
        return self.names.value