Esempio n. 1
0
    def remove_endpoint_for_tenant(self, request, tenant_id, template_id):
        """
        Disable a given endpoint template for a given tenantid if it's been
        enabled. This does not affect an endpoint template that has been
        globally enabled.

        `OpenStack Identity v2 OS-KSCATALOG Delete Endpoint for Tenant
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        for api_id in self.core.get_external_apis():
            api = self.core.get_external_api(api_id)
            if api.has_template(template_id):
                try:
                    api.disable_endpoint_for_tenant(tenant_id, template_id)
                except EndpointTemplateDisabledForTenant:
                    return json.dumps(
                        not_found("Template not enabled for tenant", request))
                else:
                    request.setResponseCode(204)
                    return b''

        return json.dumps(
            not_found(
                "Unable to locate an External API with the given Template ID.",
                request))
Esempio n. 2
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""
Esempio n. 3
0
    def create_endpoint_for_tenant(self, request, tenant_id):
        """
        Enable a given endpoint template for a given tenantid.

        `OpenStack Identity v2 OS-KSCATALOG Create Endpoint for Tenant
        <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:
            template_id = content['OS-KSCATALOG:endpointTemplate']['id']
        except KeyError:
            return json.dumps(
                bad_request(
                    "Invalid Content. OS-KSCATALOG:endpointTemplate:id is "
                    "required.", request))

        for api_id in self.core.get_external_apis():
            api = self.core.get_external_api(api_id)
            if api.has_template(template_id):
                api.enable_endpoint_for_tenant(tenant_id, template_id)
                request.setResponseCode(201)
                return b''

        return json.dumps(
            not_found(
                "Unable to locate an External API with the given Template ID.",
                request))
Esempio n. 4
0
    def list_endpoints_for_tenant(self, request, tenant_id):
        """
        List the available endpoints for a given tenant-id.

        .. note:: Marker/Limit capability not implemented here.

        `OpenStack Identity v2 OS-KSCATALOG List Endpoints for Tenant
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        # caller may provide a specific API to list by setting the
        # serviceid header
        external_apis_to_list = []
        service_id = request.getHeader(b'serviceid')
        if service_id is not None:
            external_apis_to_list = [service_id.decode('utf-8')]
        else:
            external_apis_to_list = [
                api_id for api_id in self.core.get_external_apis()
            ]

        try:
            data = []
            request.setResponseCode(200)
            for api_id in external_apis_to_list:
                api = self.core.get_external_api(api_id)
                for endpoint_template in api.list_tenant_templates(tenant_id):
                    data.append(endpoint_template.serialize(tenant_id))

            return json.dumps({"endpoints": data, "endpoints_links": []})
        except ServiceDoesNotExist:
            request.setResponseCode(404)
            return json.dumps(
                not_found("Unable to find the requested API", request))
Esempio n. 5
0
    def delete_endpoint_templates(self, request, template_id):
        """
        Delete an endpoint API template from 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 Delete Endpoint Template
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        service_id = request.getHeader(b'serviceid')
        if service_id is not None:
            api = self.core.get_external_api(service_id.decode('utf-8'))
            if api.has_template(template_id):
                api.remove_template(template_id)
                request.setResponseCode(204)
                return b''
        else:
            for api_id in self.core.get_external_apis():
                api = self.core.get_external_api(api_id)
                if api.has_template(template_id):
                    api.remove_template(template_id)
                    request.setResponseCode(204)
                    return b''

        return json.dumps(
            not_found(
                "Unable to locate an External API with the given Template ID.",
                request))
Esempio n. 6
0
    def create_endpoint_for_tenant(self, request, tenant_id):
        """
        Enable a given endpoint template for a given tenantid.

        `OpenStack Identity v2 OS-KSCATALOG Create Endpoint for Tenant
        <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:
            template_id = content["OS-KSCATALOG:endpointTemplate"]["id"]
        except KeyError:
            return json.dumps(bad_request("Invalid Content. OS-KSCATALOG:endpointTemplate:id is " "required.", request))

        for api_id in self.core.get_external_apis():
            api = self.core.get_external_api(api_id)
            if api.has_template(template_id):
                api.enable_endpoint_for_tenant(tenant_id, template_id)
                request.setResponseCode(201)
                return b""

        return json.dumps(not_found("Unable to locate an External API with the given Template ID.", request))
Esempio n. 7
0
    def list_endpoints_for_tenant(self, request, tenant_id):
        """
        List the available endpoints for a given tenant-id.

        .. note:: Marker/Limit capability not implemented here.

        `OpenStack Identity v2 OS-KSCATALOG List Endpoints for Tenant
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        # caller may provide a specific API to list by setting the
        # serviceid header
        external_apis_to_list = []
        service_id = request.getHeader(b"serviceid")
        if service_id is not None:
            external_apis_to_list = [service_id.decode("utf-8")]
        else:
            external_apis_to_list = [api_id for api_id in self.core.get_external_apis()]

        try:
            data = []
            request.setResponseCode(200)
            for api_id in external_apis_to_list:
                api = self.core.get_external_api(api_id)
                for endpoint_template in api.list_tenant_templates(tenant_id):
                    data.append(endpoint_template.serialize(tenant_id))

            return json.dumps({"endpoints": data, "endpoints_links": []})
        except ServiceDoesNotExist:
            request.setResponseCode(404)
            return json.dumps(not_found("Unable to find the requested API", request))
Esempio n. 8
0
    def delete_endpoint_templates(self, request, template_id):
        """
        Delete an endpoint API template from 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 Delete Endpoint Template
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        service_id = request.getHeader(b"serviceid")
        if service_id is not None:
            api = self.core.get_external_api(service_id.decode("utf-8"))
            if api.has_template(template_id):
                api.remove_template(template_id)
                request.setResponseCode(204)
                return b""
        else:
            for api_id in self.core.get_external_apis():
                api = self.core.get_external_api(api_id)
                if api.has_template(template_id):
                    api.remove_template(template_id)
                    request.setResponseCode(204)
                    return b""

        return json.dumps(not_found("Unable to locate an External API with the given Template ID.", request))
Esempio n. 9
0
    def remove_endpoint_for_tenant(self, request, tenant_id, template_id):
        """
        Disable a given endpoint template for a given tenantid if it's been
        enabled. This does not affect an endpoint template that has been
        globally enabled.

        `OpenStack Identity v2 OS-KSCATALOG Delete Endpoint for Tenant
        <http://developer.openstack.org/api-ref-identity-v2-ext.html>`_
        """
        for api_id in self.core.get_external_apis():
            api = self.core.get_external_api(api_id)
            if api.has_template(template_id):
                try:
                    api.disable_endpoint_for_tenant(tenant_id, template_id)
                except EndpointTemplateDisabledForTenant:
                    return json.dumps(not_found("Template not enabled for tenant", request))
                else:
                    request.setResponseCode(204)
                    return b""

        return json.dumps(not_found("Unable to locate an External API with the given Template ID.", request))
Esempio n. 10
0
    def rax_kskey_apikeycredentials(self, request, user_id):
        """
        Support, such as it is, for the apiKeysCredentials call.

        reference: https://developer.rackspace.com/docs/cloud-identity/v2/api-reference/users-operations/#get-user-credentials  # noqa
        """
        if user_id in self.core.sessions._userid_to_session:
            username = self.core.sessions._userid_to_session[user_id].username
            apikey = self.make_apikey()
            return json.dumps({"RAX-KSKEY:apiKeyCredentials": {"username": username, "apiKey": apikey}})
        else:
            return json.dumps(not_found("User " + user_id + " not found", request))
Esempio n. 11
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""
Esempio n. 12
0
    def rax_kskey_apikeycredentials(self, request, user_id):
        """
        Support, such as it is, for the apiKeysCredentials call.

        reference: https://developer.rackspace.com/docs/cloud-identity/v2/api-reference/users-operations/#get-user-credentials  # noqa
        """
        if user_id in self.core.sessions._userid_to_session:
            username = self.core.sessions._userid_to_session[user_id].username
            apikey = self.make_apikey()
            return json.dumps({
                'RAX-KSKEY:apiKeyCredentials': {
                    'username': username,
                    'apiKey': apikey
                }
            })
        else:
            return json.dumps(
                not_found('User ' + user_id + ' not found', request))
Esempio n. 13
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''
Esempio n. 14
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''
Esempio n. 15
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''
Esempio n. 16
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""