Exemplo n.º 1
0
    def update_endpoint_templates(self, request, template_id):
        """
        Update an API endpoint template already in the system.

        .. note:: A template by the same id must already exist in the system.

        .. note:: Either the service-id must be specified in the header or
            a Service Name by the same name must already exist. Otherwise
            a Not Found (404) will be returned.

        `OpenStack Identity v2 OS-KSCATALOG Update Endpoint Template
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        try:
            content = json_from_request(request)
        except ValueError:
            return json.dumps(bad_request("Invalid JSON request body", request))

        try:
            if content["id"] != template_id:
                return json.dumps(conflict("Template ID in URL does not match that of the JSON body", request))

            endpoint_template_instance = EndpointTemplateStore.deserialize(content)
        except (InvalidEndpointTemplateMissingKey, KeyError) as ex:
            # KeyError is for the content['id'] line
            return json.dumps(
                bad_request("JSON body does not contain the required parameters: " + text_type(ex), request)
            )

        service_id = request.getHeader(b"serviceid")
        if service_id is None:
            for api_id in self.core.get_external_apis():
                api = self.core.get_external_api(api_id)
                if api.has_template(template_id):
                    service_id = api.uuid_key
        else:
            service_id = service_id.decode("utf-8")

        try:
            service = self.core.get_external_api(service_id)
        except ServiceDoesNotExist:
            return json.dumps(not_found("Service API for endoint template not found", request))

        try:
            service.update_template(endpoint_template_instance)
        except (InvalidEndpointTemplateServiceType, InvalidEndpointTemplateId):
            return json.dumps(
                conflict("Endpoint already exists and service id or service type " "does not match.", request)
            )
        except EndpointTemplateDoesNotExist:
            return json.dumps(
                not_found(
                    "Unable to update non-existent template. Template must " "first be added before it can be updated.",
                    request,
                )
            )
        else:
            request.setResponseCode(201)
            return b""
Exemplo n.º 2
0
    def create_external_api_service(self, request):
        """
        Create a new external api service that endpoint templates
        may be added to.

        .. note:: Only requires 'name' and 'type' fields in the JSON. If the 'id'
            or 'description' fields are present, then they will be used;
            otherwise a UUID4 will be assigned to the 'id' field and the
            'description' will be given a generic value.
        `OpenStack Identity v2 OS-KSADM Create Service
        <http://developer.openstack.org/api-ref/identity/v2-ext/index.html#create-service-admin-extension>`_
        """
        try:
            content = json_from_request(request)
        except ValueError:
            return json.dumps(bad_request("Invalid JSON request body",
                                          request))

        try:
            service_name = content['name']
            service_type = content['type']
        except KeyError:
            return json.dumps(
                bad_request(
                    "Invalid Content. 'name' and 'type' fields are required.",
                    request))

        try:
            service_id = content['id']
        except KeyError:
            service_id = text_type(uuid.uuid4())

        try:
            service_description = content['description']
        except KeyError:
            service_description = u"External API referenced by Mimic"

        if service_id in self.core.get_external_apis():
            return json.dumps(
                conflict(
                    "Conflict: Service with the same uuid already exists.",
                    request))

        try:
            self.core.add_api(
                ExternalApiStore(service_id,
                                 service_name,
                                 service_type,
                                 description=service_description))
        except ServiceNameExists:
            return json.dumps(
                conflict(
                    "Conflict: Service with the same name already exists.",
                    request))
        else:
            request.setResponseCode(201)
            return b''
Exemplo n.º 3
0
    def create_external_api_service(self, request):
        """
        Create a new external api service that endpoint templates
        may be added to.

        .. note:: Only requires 'name' and 'type' fields in the JSON. If the 'id'
            or 'description' fields are present, then they will be used;
            otherwise a UUID4 will be assigned to the 'id' field and the
            'description' will be given a generic value.
        `OpenStack Identity v2 OS-KSADM Create Service
        <http://developer.openstack.org/api-ref/identity/v2-ext/index.html#create-service-admin-extension>`_
        """
        try:
            content = json_from_request(request)
        except ValueError:
            return json.dumps(bad_request("Invalid JSON request body", request))

        try:
            service_name = content["name"]
            service_type = content["type"]
        except KeyError:
            return json.dumps(bad_request("Invalid Content. 'name' and 'type' fields are required.", request))

        try:
            service_id = content["id"]
        except KeyError:
            service_id = text_type(uuid.uuid4())

        try:
            service_description = content["description"]
        except KeyError:
            service_description = "External API referenced by Mimic"

        if service_id in self.core.get_external_apis():
            return json.dumps(conflict("Conflict: Service with the same uuid already exists.", request))

        try:
            self.core.add_api(ExternalApiStore(service_id, service_name, service_type, description=service_description))
        except ServiceNameExists:
            return json.dumps(conflict("Conflict: Service with the same name already exists.", request))
        else:
            request.setResponseCode(201)
            return b""
Exemplo n.º 4
0
    def delete_external_api_service(self, request, service_id):
        """
        Delete/Remove an existing  external service api. It must not have
        any endpoint templates assigned to it for success.

        `OpenStack Identity v2 OS-KSADM Delete Service
        <http://developer.openstack.org/api-ref/identity/v2-ext/index.html#delete-service-admin-extension>`_
        """
        try:
            self.core.remove_external_api(service_id)
        except ServiceDoesNotExist:
            return json.dumps(not_found("Service not found. Unable to remove.", request))
        except ServiceHasTemplates:
            return json.dumps(conflict("Service still has endpoint templates.", request))
        else:
            request.setResponseCode(204)
            return b""
Exemplo n.º 5
0
    def delete_external_api_service(self, request, service_id):
        """
        Delete/Remove an existing  external service api. It must not have
        any endpoint templates assigned to it for success.

        `OpenStack Identity v2 OS-KSADM Delete Service
        <http://developer.openstack.org/api-ref/identity/v2-ext/index.html#delete-service-admin-extension>`_
        """
        try:
            self.core.remove_external_api(service_id)
        except ServiceDoesNotExist:
            return json.dumps(
                not_found("Service not found. Unable to remove.", request))
        except ServiceHasTemplates:
            return json.dumps(
                conflict("Service still has endpoint templates.", request))
        else:
            request.setResponseCode(204)
            return b''
Exemplo n.º 6
0
    def add_endpoint_templates(self, request):
        """
        Add an API endpoint template to the system. By default the API
        described by the template will disabled for all users.

        .. note:: Either the service-id must be specified in the header or
            a Service Name by the same name must already exist. Otherwise
            a Not Found (404) will be returned.

        .. note:: A template has certain required parametes. For Mimic the
            id, name, type, and region parameters are required. See
            EndpointTemplateStore.required_mapping for details. Other
            implementations may have different requirements.

        `OpenStack Identity v2 OS-KSCATALOG Create Endpoint Template
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        try:
            content = json_from_request(request)
        except ValueError:
            return json.dumps(bad_request("Invalid JSON request body",
                                          request))

        try:
            endpoint_template_instance = EndpointTemplateStore.deserialize(
                content)
        except InvalidEndpointTemplateMissingKey as ex:
            return json.dumps(
                bad_request(
                    "JSON body does not contain the required parameters: " +
                    text_type(ex), request))

        # Access the Service ID that tells which External API
        # is to support this template.
        service_id = request.getHeader(b'serviceid')
        if service_id is not None:
            service_id = service_id.decode('utf-8')

        # Check all existing External APIs for the API ID
        # to ensure that none of them contain it already. The
        # value must be unique.
        for api_id in self.core.get_external_apis():
            api = self.core.get_external_api(api_id)
            if api.has_template(endpoint_template_instance.id_key):
                return json.dumps(
                    conflict(
                        "ID value is already assigned to an existing template",
                        request))

            # While we're at it, if we need to look up the service ID
            # and find the External API that will ultimately provide it
            # then grab that too instead of repeating the search.
            elif api.name_key == endpoint_template_instance.name_key:
                if service_id is None:
                    service_id = api.uuid_key

        try:
            service = self.core.get_external_api(service_id)
        except ServiceDoesNotExist:
            return json.dumps(
                not_found("Service API for endoint template not found",
                          request))

        try:
            service.add_template(endpoint_template_instance)
        except (EndpointTemplateAlreadyExists,
                InvalidEndpointTemplateServiceType):
            return json.dumps(
                conflict(
                    "Endpoint already exists or service type does not match.",
                    request))
        else:
            request.setResponseCode(201)
            return b''
Exemplo n.º 7
0
    def update_endpoint_templates(self, request, template_id):
        """
        Update an API endpoint template already in the system.

        .. note:: A template by the same id must already exist in the system.

        .. note:: Either the service-id must be specified in the header or
            a Service Name by the same name must already exist. Otherwise
            a Not Found (404) will be returned.

        `OpenStack Identity v2 OS-KSCATALOG Update Endpoint Template
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        try:
            content = json_from_request(request)
        except ValueError:
            return json.dumps(bad_request("Invalid JSON request body",
                                          request))

        try:
            if content['id'] != template_id:
                return json.dumps(
                    conflict(
                        "Template ID in URL does not match that of the JSON body",
                        request))

            endpoint_template_instance = EndpointTemplateStore.deserialize(
                content)
        except (InvalidEndpointTemplateMissingKey, KeyError) as ex:
            # KeyError is for the content['id'] line
            return json.dumps(
                bad_request(
                    "JSON body does not contain the required parameters: " +
                    text_type(ex), request))

        service_id = request.getHeader(b'serviceid')
        if service_id is None:
            for api_id in self.core.get_external_apis():
                api = self.core.get_external_api(api_id)
                if api.has_template(template_id):
                    service_id = api.uuid_key
        else:
            service_id = service_id.decode('utf-8')

        try:
            service = self.core.get_external_api(service_id)
        except ServiceDoesNotExist:
            return json.dumps(
                not_found("Service API for endoint template not found",
                          request))

        try:
            service.update_template(endpoint_template_instance)
        except (InvalidEndpointTemplateServiceType, InvalidEndpointTemplateId):
            return json.dumps(
                conflict(
                    "Endpoint already exists and service id or service type "
                    "does not match.", request))
        except EndpointTemplateDoesNotExist:
            return json.dumps(
                not_found(
                    "Unable to update non-existent template. Template must "
                    "first be added before it can be updated.", request))
        else:
            request.setResponseCode(201)
            return b''
Exemplo n.º 8
0
    def add_endpoint_templates(self, request):
        """
        Add an API endpoint template to the system. By default the API
        described by the template will disabled for all users.

        .. note:: Either the service-id must be specified in the header or
            a Service Name by the same name must already exist. Otherwise
            a Not Found (404) will be returned.

        .. note:: A template has certain required parametes. For Mimic the
            id, name, type, and region parameters are required. See
            EndpointTemplateStore.required_mapping for details. Other
            implementations may have different requirements.

        `OpenStack Identity v2 OS-KSCATALOG Create Endpoint Template
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        try:
            content = json_from_request(request)
        except ValueError:
            return json.dumps(bad_request("Invalid JSON request body", request))

        try:
            endpoint_template_instance = EndpointTemplateStore.deserialize(content)
        except InvalidEndpointTemplateMissingKey as ex:
            return json.dumps(
                bad_request("JSON body does not contain the required parameters: " + text_type(ex), request)
            )

        # Access the Service ID that tells which External API
        # is to support this template.
        service_id = request.getHeader(b"serviceid")
        if service_id is not None:
            service_id = service_id.decode("utf-8")

        # Check all existing External APIs for the API ID
        # to ensure that none of them contain it already. The
        # value must be unique.
        for api_id in self.core.get_external_apis():
            api = self.core.get_external_api(api_id)
            if api.has_template(endpoint_template_instance.id_key):
                return json.dumps(conflict("ID value is already assigned to an existing template", request))

            # While we're at it, if we need to look up the service ID
            # and find the External API that will ultimately provide it
            # then grab that too instead of repeating the search.
            elif api.name_key == endpoint_template_instance.name_key:
                if service_id is None:
                    service_id = api.uuid_key

        try:
            service = self.core.get_external_api(service_id)
        except ServiceDoesNotExist:
            return json.dumps(not_found("Service API for endoint template not found", request))

        try:
            service.add_template(endpoint_template_instance)
        except (EndpointTemplateAlreadyExists, InvalidEndpointTemplateServiceType):
            return json.dumps(conflict("Endpoint already exists or service type does not match.", request))
        else:
            request.setResponseCode(201)
            return b""