Exemplo n.º 1
0
 def _send_service_request(self, headers=[('X_Broker_Api_Version', '2.12')], params={}):
     service = ServiceRequest(service_id=self.test_service.id, plan_id=self.test_plan.id,
                              organization_guid='org', space_guid='space', parameters=params)
     query_string = [('accept_incomplete', False)]
     print('Sending service instantiation content of:\n {content}'.format(content=json.dumps(service)))
     response = self.client.open('/v2/service_instances/{instance_id}'.format(instance_id=self.instance_id),
                                 method='PUT',
                                 data=json.dumps(service),
                                 content_type='application/json',
                                 query_string=query_string,
                                 headers=headers)
     return response
Exemplo n.º 2
0
 def test_create_instance_with_nonexistant_plan(self):
     service = ServiceRequest(service_id=self.test_service.id, plan_id='WRONG_ONE',
                              organization_guid='org', space_guid='space')
     query_string = [('accept_incomplete', False)]
     headers = [('X_Broker_Api_Version', '2.12')]
     print('Sending service instantiation content of:\n {content}'.format(content=json.dumps(service)))
     response = self.client.open('/v2/service_instances/{instance_id}'.format(instance_id=self.instance_id),
                                 method='PUT',
                                 data=json.dumps(service),
                                 content_type='application/json',
                                 query_string=query_string,
                                 headers=headers)
     self.assert404(response, "Response body is : " + response.data.decode('utf-8'))
def create_service_instance(instance_id, service, accept_incomplete=False):
    """
    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 connexion.request.is_json:
            service_inst_req = ServiceRequest.from_dict(
                connexion.request.get_json())
        else:
            return "Supplied body content is not or is mal-formed JSON", 400

        entity = {
            'entity_id': instance_id,
            'entity_req': service_inst_req,
            'entity_res': None
        }
        context = {'STORE': STORE, 'RM': RM}

        if accept_incomplete:
            AsychExe([CreateInstance(entity, context)]).start()
            return 'accepted', 201
        else:
            # we have the result of the operation in entity, good/bad status in context
            entity, context = CreateInstance(entity, context).start()
            if config.esm_measure_insts == 'YES':
                # entity, context = #
                MeasureInstance(entity, context).start()
            # Response is ServiceResponse not a service instance
            return entity['entity_res'], context['status'][1]
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