def _get_instance(srv_inst): # get the latest info mani_id = srv_inst.context['manifest_id'] mani = STORE.get_manifest(manifest_id=mani_id) if len(mani) < 1: return 'no manifest found.', 404 # Get the latest info of the instance # could also use STORE.get_service_instance(srv_inst) but will not have all details inst_info = RM.info(instance_id=srv_inst.context['id'], manifest_type=mani[0].manifest_type) if inst_info['srv_inst.state.state'] == 'failed': # try epm.delete(instance_id=instance_id)? return 'There has been a failure in creating the service instance.', 500 srv_inst.state.state = inst_info['srv_inst.state.state'] srv_inst.state.description = inst_info['srv_inst.state.description'] # don't need you any more, buh-bye! del inst_info['srv_inst.state.state'] del inst_info['srv_inst.state.description'] # merge the two context dicts srv_inst.context = {**srv_inst.context, **inst_info} # update the service instance record - there should be an asynch method doing the update - event based STORE.add_service_instance(srv_inst) return srv_inst
def list_manifests(): """ returns a list of manifests registered at with the ESM hi! :rtype: List[Manifest] """ manifests = STORE.get_manifest() return manifests, 200
def get_manifest(manifest_id): """ returns a specific of manifest registered at with the ESM hi! :param manifest_id: The manifest_id of a manifest to be associated with a plan of a servicetype. :type manifest_id: str :rtype: Manifest """ manifest = STORE.get_manifest(manifest_id) if len(manifest) > 0: return manifest, 200 else: return 'Manifest {id} could not be found'.format(id=manifest_id), 404
def deprovision_service_instance(instance_id, service_id, plan_id, accept_incomplete=None): """ Deprovisions a service instance. 'When a broker receives a deprovision request from a client, it should delete any resources it created during the provision. Usually this means that all resources are immediately reclaimed for future provisions.' :param instance_id: 'The instance_id of a service instance is provided by the client. This ID will be used for future requests (bind and deprovision), so the broker must use it to correlate the resource it creates.' :type instance_id: str :param service_id: service ID to be deprovisioned :type service_id: str :param plan_id: plan ID of the service to be deprovisioned :type plan_id: str :param accept_incomplete: Indicates that the client is supporting asynchronous operations :type accept_incomplete: bool :rtype: UpdateOperationResponse """ ok, message, code = _version_ok() if not ok: return message, code else: # XXX if there's bindings remove first? # XXX what about undo? # check that the instance exists first instance = STORE.get_service_instance(instance_id=instance_id) if len(instance) == 1: mani_id = instance[0].context['manifest_id'] mani = STORE.get_manifest(manifest_id=mani_id) if len(mani) < 1: return 'no service manifest found.', 404 RM.delete(instance_id=instance_id, manifest_type=mani[0].manifest_type) STORE.delete_service_instance(instance_id) # we don't delete the last_operation explicitly as its embedded in the service_instance document # STORE.delete_last_operation(instance_id) return Empty(), 200 else: return Empty(), 404
def create_service_instance(instance_id, service, accept_incomplete=None): """ Provisions a service instance When the broker receives a provision request from a client, it should synchronously take whatever action is necessary to create a new service resource for the developer. The result of provisioning varies by service type, although there are a few common actions that work for many services. Supports asynchronous operations.' :param instance_id: 'The instance_id of a service instance is provided by the client. This ID will be used for future requests (bind and deprovision), so the broker must use it to correlate the resource it creates.' :type instance_id: str :param service: Service information. :type service: dict | bytes :param accept_incomplete: Indicates that the client is supporting asynchronous operations :type accept_incomplete: bool :rtype: ServiceResponse """ ok, message, code = _version_ok() if not ok: return message, code else: if len(STORE.get_service_instance(instance_id=instance_id)) == 1: return 'Service instance with id {id} already exists'.format(id=instance_id), 409 if connexion.request.is_json: service = ServiceRequest.from_dict(connexion.request.get_json()) else: return "Supplied body content is not or is mal-formed JSON", 400 # look up manifest based on plan id # based on the manifest type, select the driver # send the manifest for creation to the target system # store the ID along with refs to service, plan and manifest # get the manifest for the service/plan svc_type = STORE.get_service(service.service_id)[0] if svc_type is None: return 'Unrecognised service requested to be instantiated', 404 plans = svc_type.plans plan = [p for p in plans if p.id == service.plan_id] if len(plan) <= 0: return 'Plan {p_id} found.'.format(p_id=service.plan_id), 404 mani = STORE.get_manifest(plan_id=plan[0].id) if len(mani) <= 0: return 'no manifest for service {plan} found.'.format(plan=service.plan_id), 404 mani = mani[0] if accept_incomplete: # given docker-compose runs in detached mode this is not needed - only timing can verify # XXX put this in a thread to allow for asynch processing? RM.create(instance_id=instance_id, content=mani.manifest_content, c_type=mani.manifest_type, parameters=service.parameters) else: RM.create(instance_id=instance_id, content=mani.manifest_content, c_type=mani.manifest_type, parameters=service.parameters) last_op = LastOperation( # stored within the service instance doc state='creating', 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': instance_id, 'manifest_id': mani.id, } ) STORE.add_service_instance(srv_inst) if accept_incomplete: STORE.add_last_operation(instance_id=instance_id, last_operation=last_op) return 'created', 200