Ejemplo n.º 1
0
    def run(self):

        if len(self.store.get_service_instance(instance_id=self.instance_id)) == 1:
            self.context['status'] = ('Service instance with id {id} already exists'.format(id=self.instance_id), 409)
            return self.entity, self.context

        # get the manifest for the service/plan
        LOG.debug("Executing run()... Looking for service_id: {}".format(self.entity_req.service_id))
        svc_type = self.store.get_service(self.entity_req.service_id)[0]
        if svc_type is None:
            self.context['status'] = ('Unrecognised service requested to be instantiated', 404)
            return self.entity, self.context

        plans = svc_type.plans
        plan = [p for p in plans if p.id == self.entity_req.plan_id]
        if len(plan) <= 0:
            self.context['status'] = ('Plan {p_id} found.'.format(p_id=self.entity_req.plan_id), 404)
            return self.entity, self.context

        # mani = self.store.get_manifest(plan_id=plan[0].id)  # TODO revert once underlying bug is fixed
        manis = self.store.get_manifest()
        mani = [m for m in manis if m.plan_id == plan[0].id]

        if not len(mani) == 1:
            self.context['status'] = ('no manifest for service {plan} found.'.format(plan=self.entity_req.plan_id), 404)
            return self.entity, self.context


        mani = mani[0]

        # stored within the service instance doc
        last_op = LastOperation(state='in progress', description='service instance is being created')

        # store the instance Id with manifest id
        srv_inst = ServiceInstance(service_type=svc_type, state=last_op,
                                   context={'id': self.instance_id, 'manifest_id': mani.id})

        self.store.add_service_instance(srv_inst)

        self.rm.create(instance_id=self.instance_id, content=mani.manifest_content,
                       c_type=mani.manifest_type, parameters=self.entity_req.parameters)

        # should loop or receive call back to when the stack is done

        svc_up = self.store.get_service_instance(instance_id=self.instance_id)[0]
        # TODO this does not reflect real status (svc up)
        LOG.debug("svcup result {} \n{}".format(type(svc_up), svc_up))

        svc_up.state.state = 'succeeded'
        svc_up.state.description = 'service instance is created'

        self.store.add_service_instance(svc_up)

        self.entity['entity_res'] = srv_inst
        self.context['status'] = ('created', 200)
        return self.entity, self.context
Ejemplo n.º 2
0
 def add_last_operation(self, instance_id: str, last_operation: LastOperation) -> None:
     last_op = {'id': instance_id, 'last_op': last_operation}
     if last_op not in self.ESM_DB.last_operations:
         self.ESM_DB.last_operations.append(last_op)
     else:
         LOG.info('An existing last operation for service instance {id} is found. '
                  'Updating with {content}'.format(id=instance_id, content=last_operation.to_str()))
         lo = [lo for lo in self.ESM_DB.last_operations if lo['id'] == instance_id]
         lo[0]['last_op'].state = last_operation.state
         lo[0]['last_op'].description = last_operation.description
Ejemplo n.º 3
0
    def get_last_operation(self,
                           instance_id: str = None) -> List[LastOperation]:
        if instance_id:
            if self.ESM_DB.last_operations.count({'id': instance_id}) == 1:
                return [
                    LastOperation.from_dict(
                        self.ESM_DB.last_operations.find_one(
                            {'id': instance_id}))
                ]
            else:
                LOG.warn('Requested last operation not found: {id}'.format(
                    id=instance_id))
                return []
        else:
            last_ops = []

            for lo in self.ESM_DB.last_operations.find():
                last_ops.append(LastOperation().from_dict(lo))

            return last_ops
Ejemplo n.º 4
0
 def add_last_operation(self, instance_id: str,
                        last_operation: LastOperation) -> tuple:
     result = self.ESM_DB.last_operations.insert_one({
         'id':
         instance_id,
         'last_op':
         last_operation.to_dict()
     })
     if not result.acknowledged:
         return 'there was an issue saving the service status to the DB', 500
     return 'ok', 200
Ejemplo n.º 5
0
    def add_last_operation(self, instance_id: str,
                           last_operation: LastOperation) -> None:
        last_op = {'id': instance_id, 'last_op': last_operation}

        lo = [
            lo for lo in self.ESM_DB.last_operations if lo['id'] == instance_id
        ]

        if len(lo) > 0:
            LOG.info(
                'An existing last operation for service instance {id} is found. '
                'Updating with {content}'.format(
                    id=instance_id, content=last_operation.to_str()))
            lo[0]['last_op'].state = last_operation.state
            lo[0]['last_op'].description = last_operation.description
        else:
            self.ESM_DB.last_operations.append(last_op)
            LOG.info(
                'Adding a new last operation. '
                'Content supplied: {content}'.format(content=str(last_op)))