def test_serialize_tenant(self):
        """
        Serializing for a tenant with the full data will give the dict object.
        """
        tenant_id = "some-tenant"
        data = {
            "id": "some-id",
            "region": "some-region",
            "type": "some-type",
            "name": "some-name",
            "enabled": True,
            "publicURL": "http://public.url",
            "internalURL": "http://internal.url",
            "adminURL": None,
            "RAX-AUTH:tenantAlias": "{{tenant_id}}",
            "versionId": "http://some.url/version",
            "versionInfo": "http://some.url/version/info",
            "versionList": "http://some.url/version/list"
        }
        expected_result = {
            "id": data['id'],
            "tenantId": tenant_id,
            "region": data['region'],
            "type": data['type'],
            "publicURL": data['publicURL'],
            "internalURL": data['internalURL'],
        }

        epts = EndpointTemplateStore.deserialize(data)
        serialized_data = epts.serialize(tenant_id=tenant_id)
        self.assertEqual(expected_result, serialized_data)
Exemple #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""
 def test_basic_with_minimal_dict(self, key_to_remove):
     """
     Check setting up the template with the minimal mappings.
     """
     data = {
         "id": "some-id",
         "region": "some-region",
         "type": "some-type",
         "name": "some-name"
     }
     if key_to_remove is None:
         # validate it matches
         epts = EndpointTemplateStore.deserialize(data)
         self.validate_mapping(epts, data, True)
     else:
         # validate that the keys must be present
         del data[key_to_remove]
         with self.assertRaises(InvalidEndpointTemplateMissingKey):
             EndpointTemplateStore.deserialize(data)
 def test_deserialization(self):
     """
     Check deserialization correctly maps the values to the object.
     """
     data = {
         "id": "some-id",
         "region": "some-region",
         "type": "some-type",
         "name": "some-name",
         "enabled": True,
         "publicURL": "http://public.url",
         "internalURL": "http://internal.url",
         "adminURL": "http://admin.internal.url",
         "RAX-AUTH:tenantAlias": "{{tenant_id}}",
         "versionId": "http://some.url/version",
         "versionInfo": "http://some.url/version/info",
         "versionList": "http://some.url/version/list"
     }
     epts = EndpointTemplateStore.deserialize(data)
     self.validate_mapping(epts, data, False)
 def test_serialize_complete(self):
     """
     Serializing the full data will result in the correct dict object.
     """
     data = {
         "id": "some-id",
         "region": "some-region",
         "type": "some-type",
         "name": "some-name",
         "enabled": True,
         "publicURL": "http://public.url",
         "internalURL": "http://internal.url",
         "adminURL": "http://admin.internal.url",
         "RAX-AUTH:tenantAlias": "{{tenant_id}}",
         "versionId": "http://some.url/version",
         "versionInfo": "http://some.url/version/info",
         "versionList": "http://some.url/version/list"
     }
     epts = EndpointTemplateStore.deserialize(data)
     serialized_data = epts.serialize()
     self.assertEqual(data, serialized_data)
    def test_replace_tenant_id(self, tid_template, spec_value):
        """
        Serializing for a tenant with the default tenantid spec will result
        in the correct URLs being generated.
        """
        tenant_id = "some-tenant"
        final_public_url = "http://public.url/" + tenant_id
        final_internal_url = "http://internal.url/" + tenant_id

        data = {
            "id": "some-id",
            "region": "some-region",
            "type": "some-type",
            "name": "some-name",
            "enabled": True,
            "publicURL": "http://public.url/" + tid_template,
            "internalURL": "http://internal.url/" + tid_template,
            "adminURL": None,
            "RAX-AUTH:tenantAlias": spec_value,
            "versionId": "http://some.url/version",
            "versionInfo": "http://some.url/version/info",
            "versionList": "http://some.url/version/list"
        }

        epts = EndpointTemplateStore.deserialize(data)
        self.assertEqual(
            final_public_url,
            epts.get_url(
                epts.public_url,
                tenant_id
            )
        )
        self.assertEqual(
            final_internal_url,
            epts.get_url(
                epts.internal_url,
                tenant_id
            )
        )
 def test_serialize_basic(self):
     """
     Serializing the minimal data will result in the correct dict object.
     """
     data = {
         "id": "some-id",
         "region": "some-region",
         "type": "some-type",
         "name": "some-name"
     }
     epts = EndpointTemplateStore()
     epts.id_key = data['id']
     epts.region_key = data['region']
     epts.type_key = data['type']
     epts.name_key = data['name']
     serialized_data = epts.serialize()
     for k, v in serialized_data.items():
         if k not in data:
             self.assertIsNone(v)
         else:
             self.assertEqual(v, data[k])
Exemple #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""