Source code for motey.models.service

import uuid

from motey.models.image import Image
from motey.models.service_state import ServiceState


[docs]class Service(object): """ Model object. Represent a service. A service can have multiple states, action types and service types. """ def __init__(self, service_name, images, id=uuid.uuid4().hex, state=ServiceState.INITIAL, state_message=''): """ Constructor of the service model. :param service_name: the name of the service :type service_name: str :param images: list of images which are asociated with the service :type images: list :param id: autogenerated id of the service :type id: uuid :param state: current state of the service. Default `INITIAL`. :type state: motey.models.service_state.ServiceState :param state_message: message for the current service state :type state_message: str """ self.id = id self.service_name = service_name self.images = images self.state = state self.state_message = state_message def __iter__(self): yield 'id', self.id yield 'service_name', self.service_name yield 'images', [dict(image) for image in self.images] yield 'state', self.state yield 'state_message', self.state_message
[docs] @staticmethod def transform(data): """ Static method to translate the service dict data into a service model. :param data: service dict to be transformed :type data: dict :return: the translated service model, None if something went wrong """ if 'service_name' not in data or 'images' not in data: return None return Service( id=data['id'] if 'id' in data else uuid.uuid4().hex, service_name=data['service_name'], images=[Image.transform(image) for image in data['images']], state=data['state'] if 'state' in data else ServiceState.INITIAL, state_message=data['state_message'] if 'state_message' in data else '' )