From 7cb7e4f1d6dc74ee62b792e3856b6521392372b0 Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:03:03 +0100 Subject: [PATCH 01/11] chore: move federation schemas into dedicated mod --- codegenerator/openapi/keystone.py | 239 ++---------------- .../openapi/keystone_schemas/federation.py | 224 ++++++++++++++++ 2 files changed, 251 insertions(+), 212 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index 98727ca..40f32d8 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -46,6 +46,21 @@ class KeystoneGenerator(OpenStackServerSourceBase): "/versions": "version", } + RESOURCE_MODULES = [ + application_credential, + auth, + common, + domain, + endpoint, + federation, + group, + project, + region, + role, + service, + user, + ] + def __init__(self): self.api_version = "3.0" self.min_api_version = "3.14" @@ -494,18 +509,6 @@ def _post_process_operation_hook( if ref not in [x.ref for x in operation_spec.parameters]: operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId == "OS-FEDERATION/identity_providers:get": - for ( - key, - val, - ) in federation.IDENTITY_PROVIDERS_LIST_PARAMETERS.items(): - openapi_spec.components.parameters.setdefault( - key, ParameterSchema(**val) - ) - ref = f"#/components/parameters/{key}" - if ref not in [x.ref for x in operation_spec.parameters]: - operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId == "services:get": for ( key, @@ -542,19 +545,10 @@ def _post_process_operation_hook( if ref not in [x.ref for x in operation_spec.parameters]: operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId in [ - "OS-FEDERATION.FEDERATION/projects:get", - "OS-FEDERATION.FEDERATION/projects:head", - "OS-FEDERATION.FEDERATION/domains:get", - "OS-FEDERATION.FEDERATION/domains:head", - "endpoints/endpoint_id/OS-ENDPOINT-POLICY/policy:get", - ]: - operation_spec.deprecated = True - - # Let Role schemas process operation - role._post_process_operation_hook( - openapi_spec, operation_spec, path=path - ) + for resource_mod in self.RESOURCE_MODULES: + hook = getattr(resource_mod, "_post_process_operation_hook", None) + if hook: + hook(openapi_spec, operation_spec, path=path) def _get_schema_ref( self, @@ -565,12 +559,14 @@ def _get_schema_ref( action_name=None, ): # Invoke modularized schema _get_schema_ref - # Roles - (ref, mime_type, matched) = role._get_schema_ref( - openapi_spec, name, description, schema_def, action_name - ) - if matched: - return (ref, mime_type) + for resource_mod in self.RESOURCE_MODULES: + hook = getattr(resource_mod, "_get_schema_ref", None) + if hook: + (ref, mime_type, matched) = hook( + openapi_spec, name, description, schema_def, action_name + ) + if matched: + return (ref, mime_type) mime_type = "application/json" @@ -855,7 +851,6 @@ def _get_schema_ref( ref = f"#/components/schemas/{name}" elif name in [ "AuthProjectsGetResponse", - "Os_FederationProjectsGetResponse", ]: openapi_spec.components.schemas.setdefault( name, TypeSchema(**auth.AUTH_PROJECTS_SCHEMA) @@ -863,7 +858,6 @@ def _get_schema_ref( ref = f"#/components/schemas/{name}" elif name in [ "AuthDomainsGetResponse", - "Os_FederationDomainsGetResponse", ]: openapi_spec.components.schemas.setdefault( name, TypeSchema(**auth.AUTH_DOMAINS_SCHEMA) @@ -879,48 +873,6 @@ def _get_schema_ref( name, TypeSchema(**auth.AUTH_CATALOG_SCHEMA) ) ref = f"#/components/schemas/{name}" - elif name in [ - "AuthOs_FederationSaml2PostRequest", - "AuthOs_FederationSaml2EcpPostRequest", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**auth.AUTH_TOKEN_ISSUE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "AuthOs_FederationSaml2PostResponse", - "AuthOs_FederationSaml2EcpPostResponse", - ]: - mime_type = "text/xml" - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - type="string", - format="xml", - descripion="SAML assertion in XML format", - ), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "AuthOs_FederationWebssoGetResponse", - "AuthOs_FederationWebssoPostResponse", - "AuthOs_FederationIdentity_ProvidersProtocolsWebssoGetResponse", - "AuthOs_FederationIdentity_ProvidersProtocolsWebssoPostResponse", - "Os_FederationIdentity_ProvidersProtocolsAuthGetResponse", - "Os_FederationIdentity_ProvidersProtocolsAuthPostResponse", - ]: - # Federation based auth returns unscoped token even it is not - # described explicitly in apiref - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**auth.AUTH_TOKEN_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "AuthOs_FederationWebssoPostRequest", - "AuthOs_FederationIdentity_ProvidersProtocolsWebssoPostRequest", - ]: - ref = None # ### Application Credentials elif name == "UsersAccess_RuleGetResponse": openapi_spec.components.schemas.setdefault( @@ -972,143 +924,6 @@ def _get_schema_ref( ), ) ref = f"#/components/schemas/{name}" - # ### Identity provider - elif name == "Os_FederationIdentity_ProvidersGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.IDENTITY_PROVIDERS_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "Os_FederationIdentity_ProviderGetResponse", - "Os_FederationIdentity_ProviderPutResponse", - "Os_FederationIdentity_ProviderPatchResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.IDENTITY_PROVIDER_CONTAINER_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "Os_FederationIdentity_ProviderPutRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.IDENTITY_PROVIDER_CREATE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "Os_FederationIdentity_ProviderPatchRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.IDENTITY_PROVIDER_UPDATE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - # ### Identity provider protocols - elif name == "Os_FederationIdentity_ProvidersProtocolsGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.IDENTITY_PROVIDER_PROTOCOLS_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "Os_FederationIdentity_ProvidersProtocolGetResponse", - "Os_FederationIdentity_ProvidersProtocolPutResponse", - "Os_FederationIdentity_ProvidersProtocolPatchResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **federation.IDENTITY_PROVIDER_PROTOCOL_CONTAINER_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name == "Os_FederationIdentity_ProvidersProtocolPutRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **federation.IDENTITY_PROVIDER_PROTOCOL_CREATE_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name == "Os_FederationIdentity_ProvidersProtocolPatchRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **federation.IDENTITY_PROVIDER_PROTOCOL_UPDATE_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - # ### Identity provider mapping - elif name == "Os_FederationMappingsGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.MAPPINGS_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "Os_FederationMappingGetResponse", - "Os_FederationMappingPutResponse", - "Os_FederationMappingPatchResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.MAPPING_CONTAINER_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "Os_FederationMappingPutRequest", - "Os_FederationMappingPatchRequest", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.MAPPING_CREATE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - # ### Identity provider service provider - elif name == "Os_FederationService_ProvidersGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**federation.FEDERATION_SERVICE_PROVIDERS_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "Os_FederationService_ProviderGetResponse", - "Os_FederationService_ProviderPutResponse", - "Os_FederationService_ProviderPatchResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **federation.FEDERATION_SERVICE_PROVIDER_CONTAINER_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name == "Os_FederationService_ProviderPutRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **federation.FEDERATION_SERVICE_PROVIDER_CREATE_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name == "Os_FederationService_ProviderPatchRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **federation.FEDERATION_SERVICE_PROVIDER_UPDATE_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - # SAML2 Metadata - elif name == "Os_FederationSaml2MetadataGetResponse": - mime_type = "text/xml" - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - type="string", - format="xml", - descripion="Identity Provider metadata information in XML format", - ), - ) - ref = f"#/components/schemas/{name}" # ### Services elif name == "ServicesGetResponse": openapi_spec.components.schemas.setdefault( diff --git a/codegenerator/openapi/keystone_schemas/federation.py b/codegenerator/openapi/keystone_schemas/federation.py index 8895e17..463a337 100644 --- a/codegenerator/openapi/keystone_schemas/federation.py +++ b/codegenerator/openapi/keystone_schemas/federation.py @@ -17,6 +17,10 @@ from keystone.federation import schema as federation_schema from keystone.federation import utils as federation_mapping_schema +from codegenerator.common.schema import ParameterSchema +from codegenerator.common.schema import TypeSchema +from codegenerator.openapi.keystone_schemas import auth + IDENTITY_PROVIDER_SCHEMA: dict[str, Any] = { "type": "object", @@ -221,3 +225,223 @@ "service_provider": federation_schema.service_provider_update }, } + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + operationId = operation_spec.operationId + if operationId == "OS-FEDERATION/identity_providers:get": + for ( + key, + val, + ) in IDENTITY_PROVIDERS_LIST_PARAMETERS.items(): + openapi_spec.components.parameters.setdefault( + key, ParameterSchema(**val) + ) + ref = f"#/components/parameters/{key}" + if ref not in [x.ref for x in operation_spec.parameters]: + operation_spec.parameters.append(ParameterSchema(ref=ref)) + elif operationId in [ + "OS-FEDERATION/projects:get", + "OS-FEDERATION/projects:head", + "OS-FEDERATION/domains:get", + "OS-FEDERATION/domains:head", + "endpoints/endpoint_id/OS-ENDPOINT-POLICY/policy:get", + ]: + operation_spec.deprecated = True + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str | None + if name == "Os_FederationProjectsGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**auth.AUTH_PROJECTS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "Os_FederationDomainsGetResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**auth.AUTH_DOMAINS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "AuthOs_FederationSaml2PostRequest", + "AuthOs_FederationSaml2EcpPostRequest", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**auth.AUTH_TOKEN_ISSUE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "AuthOs_FederationSaml2PostResponse", + "AuthOs_FederationSaml2EcpPostResponse", + ]: + mime_type = "text/xml" + openapi_spec.components.schemas.setdefault( + name, + TypeSchema( + type="string", + format="xml", + descripion="SAML assertion in XML format", + ), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "AuthOs_FederationWebssoGetResponse", + "AuthOs_FederationWebssoPostResponse", + "AuthOs_FederationIdentity_ProvidersProtocolsWebssoGetResponse", + "AuthOs_FederationIdentity_ProvidersProtocolsWebssoPostResponse", + "Os_FederationIdentity_ProvidersProtocolsAuthGetResponse", + "Os_FederationIdentity_ProvidersProtocolsAuthPostResponse", + ]: + # Federation based auth returns unscoped token even it is not + # described explicitly in apiref + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**auth.AUTH_TOKEN_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "AuthOs_FederationWebssoPostRequest", + "AuthOs_FederationIdentity_ProvidersProtocolsWebssoPostRequest", + ]: + ref = None + # ### Identity provider + elif name == "Os_FederationIdentity_ProvidersGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**IDENTITY_PROVIDERS_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "Os_FederationIdentity_ProviderGetResponse", + "Os_FederationIdentity_ProviderPutResponse", + "Os_FederationIdentity_ProviderPatchResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**IDENTITY_PROVIDER_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "Os_FederationIdentity_ProviderPutRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**IDENTITY_PROVIDER_CREATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "Os_FederationIdentity_ProviderPatchRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**IDENTITY_PROVIDER_UPDATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + # ### Identity provider protocols + elif name == "Os_FederationIdentity_ProvidersProtocolsGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**IDENTITY_PROVIDER_PROTOCOLS_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "Os_FederationIdentity_ProvidersProtocolGetResponse", + "Os_FederationIdentity_ProvidersProtocolPutResponse", + "Os_FederationIdentity_ProvidersProtocolPatchResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**IDENTITY_PROVIDER_PROTOCOL_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "Os_FederationIdentity_ProvidersProtocolPutRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**IDENTITY_PROVIDER_PROTOCOL_CREATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "Os_FederationIdentity_ProvidersProtocolPatchRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**IDENTITY_PROVIDER_PROTOCOL_UPDATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + # ### Identity provider mapping + elif name == "Os_FederationMappingsGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**MAPPINGS_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "Os_FederationMappingGetResponse", + "Os_FederationMappingPutResponse", + "Os_FederationMappingPatchResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**MAPPING_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "Os_FederationMappingPutRequest", + "Os_FederationMappingPatchRequest", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**MAPPING_CREATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + # ### Identity provider service provider + elif name == "Os_FederationService_ProvidersGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**FEDERATION_SERVICE_PROVIDERS_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "Os_FederationService_ProviderGetResponse", + "Os_FederationService_ProviderPutResponse", + "Os_FederationService_ProviderPatchResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**FEDERATION_SERVICE_PROVIDER_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "Os_FederationService_ProviderPutRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**FEDERATION_SERVICE_PROVIDER_CREATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "Os_FederationService_ProviderPatchRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**FEDERATION_SERVICE_PROVIDER_UPDATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + # SAML2 Metadata + elif name == "Os_FederationSaml2MetadataGetResponse": + mime_type = "text/xml" + openapi_spec.components.schemas.setdefault( + name, + TypeSchema( + type="string", + format="xml", + descripion="Identity Provider metadata information in XML format", + ), + ) + ref = f"#/components/schemas/{name}" + else: + return (None, None, False) + + return (ref, mime_type, True) From d3e7d1a04563915973fef6dce2492270f35be3f5 Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:07:57 +0100 Subject: [PATCH 02/11] chore: move rest or role schemas to separate mod --- codegenerator/openapi/keystone.py | 73 ------------------- .../openapi/keystone_schemas/role.py | 37 ++++++++-- 2 files changed, 30 insertions(+), 80 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index 40f32d8..1687eeb 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -17,7 +17,6 @@ from ruamel.yaml.scalarstring import LiteralScalarString -from keystone.assignment import schema as assignment_schema from keystone.identity import schema as identity_schema from keystone.resource import schema as ks_schema @@ -629,28 +628,6 @@ def _get_schema_ref( ) ref = f"#/components/schemas/{name}" - # Project/Domain Roles - elif name == "ProjectsUsersRolesGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLES_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectsGroupsRolesGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLES_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "DomainsUsersRolesGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLES_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "DomainsGroupsRolesGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLES_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - # Domains elif name == "DomainsPostRequest": openapi_spec.components.schemas.setdefault( @@ -753,56 +730,6 @@ def _get_schema_ref( ) ref = f"#/components/schemas/{name}" - # Roles - elif name == "RolesGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLES_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "RolesPostRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**assignment_schema.role_create) - ) - ref = f"#/components/schemas/{name}" - elif name == "RolesPostResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLE_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "RoleGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLE_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "RolePatchRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**assignment_schema.role_update) - ) - ref = f"#/components/schemas/{name}" - elif name == "RolePatchResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLE_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - - # Role Implies - elif name == "RolesImpliesGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**role.ROLES_INFERENCE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "RolesImplyGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLE_INFERENCE_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "RolesImplyPutResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**role.ROLE_INFERENCE_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - # Domain Config elif name in [ "DomainsConfigDefaultGetResponse", diff --git a/codegenerator/openapi/keystone_schemas/role.py b/codegenerator/openapi/keystone_schemas/role.py index 51c06aa..613931d 100644 --- a/codegenerator/openapi/keystone_schemas/role.py +++ b/codegenerator/openapi/keystone_schemas/role.py @@ -16,8 +16,7 @@ from codegenerator.common.schema import ParameterSchema from codegenerator.common.schema import TypeSchema - -from . import auth +from codegenerator.openapi.keystone_schemas import auth ROLE_SCHEMA: dict[str, Any] = { "type": "object", @@ -270,17 +269,17 @@ def _get_schema_ref( name, TypeSchema(**ROLES_SCHEMA) ) ref = f"#/components/schemas/{name}" - elif name == "RolesPostRequest": + elif name == "RoleGetResponse": openapi_spec.components.schemas.setdefault( - name, TypeSchema(**assignment_schema.role_create) + name, TypeSchema(**ROLE_SCHEMA) ) ref = f"#/components/schemas/{name}" - elif name == "RolesPostResponse": + elif name == "RolesPostRequest": openapi_spec.components.schemas.setdefault( - name, TypeSchema(**ROLE_SCHEMA) + name, TypeSchema(**assignment_schema.role_create) ) ref = f"#/components/schemas/{name}" - elif name == "RoleGetResponse": + elif name == "RolesPostResponse": openapi_spec.components.schemas.setdefault( name, TypeSchema(**ROLE_SCHEMA) ) @@ -313,11 +312,35 @@ def _get_schema_ref( name, TypeSchema(**ROLE_INFERENCE_SCHEMA) ) ref = f"#/components/schemas/{name}" + elif name == "Role_AssignmentsGetResponse": openapi_spec.components.schemas.setdefault( name, TypeSchema(**ROLE_ASSIGNMENTS_SCHEMA) ) ref = f"#/components/schemas/{name}" + + # Project/Domain Roles + elif name == "ProjectsUsersRolesGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**ROLES_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectsGroupsRolesGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**ROLES_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "DomainsUsersRolesGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**ROLES_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "DomainsGroupsRolesGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**ROLES_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + else: return (None, None, False) From 82de7aec5947513b4265586801b151be1048c9f4 Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:12:01 +0100 Subject: [PATCH 03/11] chore: move appcreds schemas to separate mod --- codegenerator/openapi/keystone.py | 51 --------------- .../application_credential.py | 65 ++++++++++++++++++- 2 files changed, 64 insertions(+), 52 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index 1687eeb..0362708 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -800,57 +800,6 @@ def _get_schema_ref( name, TypeSchema(**auth.AUTH_CATALOG_SCHEMA) ) ref = f"#/components/schemas/{name}" - # ### Application Credentials - elif name == "UsersAccess_RuleGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **application_credential.APPLICATION_CREDENTIAL_ACCESS_RULE_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name == "UsersAccess_RulesGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **application_credential.APPLICATION_CREDENTIAL_ACCESS_RULES_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name == "UsersApplication_CredentialsGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **application_credential.APPLICATION_CREDENTIALS_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "UsersApplication_CredentialGetResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **application_credential.APPLICATION_CREDENTIAL_CONTAINER_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name == "UsersApplication_CredentialsPostRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **application_credential.APPLICATION_CREDENTIAL_CREATE_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" - elif name in "UsersApplication_CredentialsPostResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema( - **application_credential.APPLICATION_CREDENTIAL_CREATE_RESPONSE_SCHEMA - ), - ) - ref = f"#/components/schemas/{name}" # ### Services elif name == "ServicesGetResponse": openapi_spec.components.schemas.setdefault( diff --git a/codegenerator/openapi/keystone_schemas/application_credential.py b/codegenerator/openapi/keystone_schemas/application_credential.py index 98d744a..6f21d95 100644 --- a/codegenerator/openapi/keystone_schemas/application_credential.py +++ b/codegenerator/openapi/keystone_schemas/application_credential.py @@ -18,7 +18,8 @@ schema as application_credential_schema, ) -from . import common +from codegenerator.common.schema import TypeSchema +from codegenerator.openapi.keystone_schemas import common # Application Credentials APPLICATION_CREDENTIAL_ACCESS_RULES_SCHEMA: dict[str, Any] = { @@ -106,3 +107,65 @@ "schema": {"type": "string"}, }, } + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + pass + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + # ### Application Credentials + if name == "UsersAccess_RuleGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**APPLICATION_CREDENTIAL_ACCESS_RULE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "UsersAccess_RulesGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**APPLICATION_CREDENTIAL_ACCESS_RULES_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "UsersApplication_CredentialsGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**APPLICATION_CREDENTIALS_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "UsersApplication_CredentialGetResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**APPLICATION_CREDENTIAL_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "UsersApplication_CredentialsPostRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**APPLICATION_CREDENTIAL_CREATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in "UsersApplication_CredentialsPostResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**APPLICATION_CREDENTIAL_CREATE_RESPONSE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True) From fed4effb46b6769d6068e3aab72a2f8e6d9dfdc1 Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:18:33 +0100 Subject: [PATCH 04/11] chore: move auth schemas to separate module --- codegenerator/openapi/keystone.py | 60 +------------ .../openapi/keystone_schemas/auth.py | 85 ++++++++++++++++++- 2 files changed, 85 insertions(+), 60 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index 0362708..a06a1b5 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -457,23 +457,7 @@ def _post_process_operation_hook( """Hook to allow service specific generator to modify details""" operationId = operation_spec.operationId - if operationId == "auth/tokens:post": - (receipt_schema_ref, receipt_mime_type) = self._get_schema_ref( - openapi_spec, "AuthReceiptSchema" - ) - operation_spec.responses["401"] = { - "description": "Unauthorized", - "headers": { - "Openstack-Auth-Receipt": { - "$ref": "#/components/headers/Openstack-Auth-Receipt" - } - }, - "content": { - receipt_mime_type: {"schema": {"$ref": receipt_schema_ref}} - }, - } - - elif operationId == "projects:get": + if operationId == "projects:get": for ( key, val, @@ -758,48 +742,6 @@ def _get_schema_ref( ) ref = f"#/components/schemas/{name}" - # Auth - elif name == "AuthTokensPostRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**auth.AUTH_TOKEN_ISSUE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in ["AuthTokensGetResponse", "AuthTokensPostResponse"]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**auth.AUTH_SCOPED_TOKEN_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "AuthReceiptSchema": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**auth.AUTH_RECEIPT_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "AuthProjectsGetResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**auth.AUTH_PROJECTS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "AuthDomainsGetResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**auth.AUTH_DOMAINS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "AuthSystemGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**auth.AUTH_SYSTEMS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "AuthCatalogGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**auth.AUTH_CATALOG_SCHEMA) - ) - ref = f"#/components/schemas/{name}" # ### Services elif name == "ServicesGetResponse": openapi_spec.components.schemas.setdefault( diff --git a/codegenerator/openapi/keystone_schemas/auth.py b/codegenerator/openapi/keystone_schemas/auth.py index cead6ee..9ea8124 100644 --- a/codegenerator/openapi/keystone_schemas/auth.py +++ b/codegenerator/openapi/keystone_schemas/auth.py @@ -14,7 +14,8 @@ from typing import Any -from . import common +from codegenerator.common.schema import TypeSchema +from codegenerator.openapi.keystone_schemas import common SCOPE_SCHEMA: dict[str, Any] = { @@ -549,3 +550,85 @@ }, }, } + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + operationId = operation_spec.operationId + + if operationId == "auth/tokens:post": + (receipt_schema_ref, receipt_mime_type, matched) = _get_schema_ref( + openapi_spec, "AuthReceiptSchema" + ) + operation_spec.responses["401"] = { + "description": "Unauthorized", + "headers": { + "Openstack-Auth-Receipt": { + "$ref": "#/components/headers/Openstack-Auth-Receipt" + } + }, + "content": { + receipt_mime_type: {"schema": {"$ref": receipt_schema_ref}} + }, + } + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + + # Auth + if name == "AuthTokensPostRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**AUTH_TOKEN_ISSUE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in ["AuthTokensGetResponse", "AuthTokensPostResponse"]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**AUTH_SCOPED_TOKEN_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "AuthReceiptSchema": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**AUTH_RECEIPT_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "AuthProjectsGetResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**AUTH_PROJECTS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "AuthDomainsGetResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**AUTH_DOMAINS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "AuthSystemGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**AUTH_SYSTEMS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "AuthCatalogGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**AUTH_CATALOG_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True) From ebd04010fcc6be8a9a329c7476fac8d0f5564a92 Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:22:08 +0100 Subject: [PATCH 05/11] chore: move domain schemas to separate module --- codegenerator/openapi/keystone.py | 60 ------------- .../openapi/keystone_schemas/domain.py | 84 +++++++++++++++++++ 2 files changed, 84 insertions(+), 60 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index a06a1b5..c0e775f 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -612,38 +612,6 @@ def _get_schema_ref( ) ref = f"#/components/schemas/{name}" - # Domains - elif name == "DomainsPostRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**ks_schema.domain_create) - ) - ref = f"#/components/schemas/{name}" - elif name == "DomainsPostResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**domain.DOMAIN_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "DomainPatchRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**ks_schema.domain_update) - ) - ref = f"#/components/schemas/{name}" - elif name == "DomainPatchResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**domain.DOMAIN_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "DomainsGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**domain.DOMAINS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "DomainGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**domain.DOMAIN_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - # Users elif name == "UserPatchRequest": openapi_spec.components.schemas.setdefault( @@ -714,34 +682,6 @@ def _get_schema_ref( ) ref = f"#/components/schemas/{name}" - # Domain Config - elif name in [ - "DomainsConfigDefaultGetResponse", - "DomainsConfigPutRequest", - "DomainsConfigPutResponse", - "DomainsConfigPatchResponse", - "DomainsConfigPatchResponse", - "DomainsConfigDefaultGetResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**domain.DOMAIN_CONFIGS_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "DomainsConfigGroupGetResponse", - "DomainsConfigGroupPatchRequest", - "DomainsConfigGroupPatchResponse", - "DomainsConfigGroupPatchResponse", - "DomainsConfigGroupPatchResponse", - "DomainsConfigDefaultGroupGetResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**domain.DOMAIN_CONFIG_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - # ### Services elif name == "ServicesGetResponse": openapi_spec.components.schemas.setdefault( diff --git a/codegenerator/openapi/keystone_schemas/domain.py b/codegenerator/openapi/keystone_schemas/domain.py index 5a9cdfc..4e99b79 100644 --- a/codegenerator/openapi/keystone_schemas/domain.py +++ b/codegenerator/openapi/keystone_schemas/domain.py @@ -15,6 +15,9 @@ from keystone.resource import schema as ks_schema +from codegenerator.common.schema import TypeSchema + + DOMAIN_SCHEMA: dict[str, Any] = { "type": "object", "properties": { @@ -79,3 +82,84 @@ DOMAIN_CONFIG_GROUP_LDAP, ] } + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + pass + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + # Domains + if name == "DomainsPostRequest": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**ks_schema.domain_create) + ) + ref = f"#/components/schemas/{name}" + elif name == "DomainsPostResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**DOMAIN_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "DomainPatchRequest": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**ks_schema.domain_update) + ) + ref = f"#/components/schemas/{name}" + elif name == "DomainPatchResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**DOMAIN_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "DomainsGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**DOMAINS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "DomainGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**DOMAIN_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + # Domain Config + elif name in [ + "DomainsConfigDefaultGetResponse", + "DomainsConfigPutRequest", + "DomainsConfigPutResponse", + "DomainsConfigPatchResponse", + "DomainsConfigPatchResponse", + "DomainsConfigDefaultGetResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**DOMAIN_CONFIGS_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "DomainsConfigGroupGetResponse", + "DomainsConfigGroupPatchRequest", + "DomainsConfigGroupPatchResponse", + "DomainsConfigGroupPatchResponse", + "DomainsConfigGroupPatchResponse", + "DomainsConfigDefaultGroupGetResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**DOMAIN_CONFIG_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True) From 4e0581bd2e7dad3e5d2c71b81f4d072ce3d8704f Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:27:12 +0100 Subject: [PATCH 06/11] chore: move user schemas to separate module --- codegenerator/openapi/keystone.py | 66 --------------- .../application_credential.py | 14 +++- .../openapi/keystone_schemas/user.py | 80 ++++++++++++++++++- 3 files changed, 92 insertions(+), 68 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index c0e775f..2da02eb 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -469,29 +469,6 @@ def _post_process_operation_hook( if ref not in [x.ref for x in operation_spec.parameters]: operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId == "users:get": - for key, val in user.USER_LIST_PARAMETERS.items(): - openapi_spec.components.parameters.setdefault( - key, ParameterSchema(**val) - ) - ref = f"#/components/parameters/{key}" - if ref not in [x.ref for x in operation_spec.parameters]: - operation_spec.parameters.append(ParameterSchema(ref=ref)) - - elif operationId == "users/user_id/application_credentials:get": - for ( - key, - val, - ) in ( - application_credential.APPLICATION_CREDENTIALS_LIST_PARAMETERS.items() - ): - openapi_spec.components.parameters.setdefault( - key, ParameterSchema(**val) - ) - ref = f"#/components/parameters/{key}" - if ref not in [x.ref for x in operation_spec.parameters]: - operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId == "services:get": for ( key, @@ -612,49 +589,6 @@ def _get_schema_ref( ) ref = f"#/components/schemas/{name}" - # Users - elif name == "UserPatchRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**user.USER_PATCH_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "UsersPostRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**user.USER_CREATE_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "UserPatchResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**user.USER_CONTAINER_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "UsersGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**user.USERS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "UserGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**user.USER_CONTAINER_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "UsersPasswordPostRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**user.USER_PWD_CHANGE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "UsersGroupsGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**user.USER_GROUPS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "UsersProjectsGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**user.USER_PROJECTS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - # Groups elif name == "GroupPatchRequest": openapi_spec.components.schemas.setdefault( diff --git a/codegenerator/openapi/keystone_schemas/application_credential.py b/codegenerator/openapi/keystone_schemas/application_credential.py index 6f21d95..2383ce3 100644 --- a/codegenerator/openapi/keystone_schemas/application_credential.py +++ b/codegenerator/openapi/keystone_schemas/application_credential.py @@ -19,6 +19,7 @@ ) from codegenerator.common.schema import TypeSchema +from codegenerator.common.schema import ParameterSchema from codegenerator.openapi.keystone_schemas import common # Application Credentials @@ -113,7 +114,18 @@ def _post_process_operation_hook( openapi_spec, operation_spec, path: str | None = None ): """Hook to allow service specific generator to modify details""" - pass + operationId = operation_spec.operationId + if operationId == "users/user_id/application_credentials:get": + for ( + key, + val, + ) in APPLICATION_CREDENTIALS_LIST_PARAMETERS.items(): + openapi_spec.components.parameters.setdefault( + key, ParameterSchema(**val) + ) + ref = f"#/components/parameters/{key}" + if ref not in [x.ref for x in operation_spec.parameters]: + operation_spec.parameters.append(ParameterSchema(ref=ref)) def _get_schema_ref( diff --git a/codegenerator/openapi/keystone_schemas/user.py b/codegenerator/openapi/keystone_schemas/user.py index f47c735..4d1bae0 100644 --- a/codegenerator/openapi/keystone_schemas/user.py +++ b/codegenerator/openapi/keystone_schemas/user.py @@ -14,7 +14,11 @@ from keystone.identity import schema as identity_schema -USER_LIST_PARAMETERS = { +from codegenerator.common.schema import ParameterSchema +from codegenerator.common.schema import TypeSchema + + +USER_LIST_PARAMETERS: dict[str, Any] = { "domain_id": { "in": "query", "name": "domain_id", @@ -182,3 +186,77 @@ } }, } + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + operationId = operation_spec.operationId + + if operationId == "users:get": + for key, val in USER_LIST_PARAMETERS.items(): + openapi_spec.components.parameters.setdefault( + key, ParameterSchema(**val) + ) + ref = f"#/components/parameters/{key}" + if ref not in [x.ref for x in operation_spec.parameters]: + operation_spec.parameters.append(ParameterSchema(ref=ref)) + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + # Users + if name == "UserPatchRequest": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**USER_PATCH_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "UsersPostRequest": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**USER_CREATE_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "UserPatchResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**USER_CONTAINER_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "UsersGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**USERS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "UserGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**USER_CONTAINER_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "UsersPasswordPostRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**USER_PWD_CHANGE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "UsersGroupsGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**USER_GROUPS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "UsersProjectsGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**USER_PROJECTS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True) From 622e4320e82a610d0678af57bc2d5b4b321b0a9d Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:30:05 +0100 Subject: [PATCH 07/11] chore: move endpoint schemas to separate module --- codegenerator/openapi/keystone.py | 41 ----------- .../openapi/keystone_schemas/endpoint.py | 72 ++++++++++++++++++- 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index 2da02eb..b630560 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -481,18 +481,6 @@ def _post_process_operation_hook( if ref not in [x.ref for x in operation_spec.parameters]: operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId == "endpoints:get": - for ( - key, - val, - ) in endpoint.ENDPOINTS_LIST_PARAMETERS.items(): - openapi_spec.components.parameters.setdefault( - key, ParameterSchema(**val) - ) - ref = f"#/components/parameters/{key}" - if ref not in [x.ref for x in operation_spec.parameters]: - operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId == "regions:get": for ( key, @@ -645,35 +633,6 @@ def _get_schema_ref( TypeSchema(**service.SERVICE_UPDATE_SCHEMA), ) ref = f"#/components/schemas/{name}" - # ### Endpoints - elif name == "EndpointsGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**endpoint.ENDPOINTS_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "EndpointGetResponse", - "EndpointsPostResponse", - "EndpointPatchResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**endpoint.ENDPOINT_CONTAINER_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "EndpointsPostRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**endpoint.ENDPOINT_CREATE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "EndpointsPostRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**endpoint.ENDPOINT_UPDATE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" # ### Regions elif name == "RegionsGetResponse": openapi_spec.components.schemas.setdefault( diff --git a/codegenerator/openapi/keystone_schemas/endpoint.py b/codegenerator/openapi/keystone_schemas/endpoint.py index a453a8d..d8dcb34 100644 --- a/codegenerator/openapi/keystone_schemas/endpoint.py +++ b/codegenerator/openapi/keystone_schemas/endpoint.py @@ -14,6 +14,9 @@ from typing import Any +from codegenerator.common.schema import TypeSchema +from codegenerator.common.schema import ParameterSchema + ENDPOINT_SCHEMA: dict[str, Any] = { "type": "object", "properties": { @@ -29,17 +32,17 @@ "interface": { "type": "string", "enum": ["internal", "admin", "public"], - "description": "The interface type, which describes the visibility of the endpoint. Value is: - public. Visible by end users on a publicly available network interface. - internal. Visible by end users on an unmetered internal network interface. - admin. Visible by administrative users on a secure network interface.", + "description": "The interface type, which describes the visibility of the Value is: - public. Visible by end users on a publicly available network interface. - internal. Visible by end users on an unmetered internal network interface. - admin. Visible by administrative users on a secure network interface.", }, "region": { "type": "string", - "description": "The geographic location of the service endpoint.", + "description": "The geographic location of the service ", "x-openstack": {"max-ver": "3.2"}, }, "region_id": { "type": "string", "format": "uuid", - "description": "The geographic location of the service endpoint.", + "description": "The geographic location of the service ", "x-openstack": {"min-ver": "3.2"}, }, "service_id": { @@ -99,3 +102,66 @@ ENDPOINT_CONTAINER_SCHEMA ) ENDPOINT_UPDATE_SCHEMA["properties"]["endpoint"]["properties"].pop("id") + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + operationId = operation_spec.operationId + if operationId == "endpoints:get": + for ( + key, + val, + ) in ENDPOINTS_LIST_PARAMETERS.items(): + openapi_spec.components.parameters.setdefault( + key, ParameterSchema(**val) + ) + ref = f"#/components/parameters/{key}" + if ref not in [x.ref for x in operation_spec.parameters]: + operation_spec.parameters.append(ParameterSchema(ref=ref)) + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + # ### Endpoints + if name == "EndpointsGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**ENDPOINTS_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "EndpointGetResponse", + "EndpointsPostResponse", + "EndpointPatchResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**ENDPOINT_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "EndpointsPostRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**ENDPOINT_CREATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "EndpointsPostRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**ENDPOINT_UPDATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True) From d00121f3c70e6c59fbf978a06d787cbbc4985717 Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:32:34 +0100 Subject: [PATCH 08/11] chore: move service schemas to separate module --- codegenerator/openapi/keystone.py | 41 ----------- .../openapi/keystone_schemas/service.py | 69 ++++++++++++++++++- 2 files changed, 68 insertions(+), 42 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index b630560..f8db63d 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -469,18 +469,6 @@ def _post_process_operation_hook( if ref not in [x.ref for x in operation_spec.parameters]: operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId == "services:get": - for ( - key, - val, - ) in service.SERVICES_LIST_PARAMETERS.items(): - openapi_spec.components.parameters.setdefault( - key, ParameterSchema(**val) - ) - ref = f"#/components/parameters/{key}" - if ref not in [x.ref for x in operation_spec.parameters]: - operation_spec.parameters.append(ParameterSchema(ref=ref)) - elif operationId == "regions:get": for ( key, @@ -604,35 +592,6 @@ def _get_schema_ref( ) ref = f"#/components/schemas/{name}" - # ### Services - elif name == "ServicesGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**service.SERVICES_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "ServiceGetResponse", - "ServicesPostResponse", - "ServicePatchResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**service.SERVICE_CONTAINER_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "ServicesPostRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**service.SERVICE_CREATE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "ServicePatchRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**service.SERVICE_UPDATE_SCHEMA), - ) - ref = f"#/components/schemas/{name}" # ### Regions elif name == "RegionsGetResponse": openapi_spec.components.schemas.setdefault( diff --git a/codegenerator/openapi/keystone_schemas/service.py b/codegenerator/openapi/keystone_schemas/service.py index 07dd9bd..782ad2d 100644 --- a/codegenerator/openapi/keystone_schemas/service.py +++ b/codegenerator/openapi/keystone_schemas/service.py @@ -14,6 +14,10 @@ from typing import Any +from codegenerator.common.schema import TypeSchema +from codegenerator.common.schema import ParameterSchema + + SERVICE_SCHEMA: dict[str, Any] = { "type": "object", "properties": { @@ -36,7 +40,7 @@ }, "type": { "type": "string", - "description": "The service type, which describes the API implemented by the service.", + "description": "The service type, which describes the API implemented by the ", }, }, } @@ -66,3 +70,66 @@ "schema": {"type": "string"}, }, } + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + operationId = operation_spec.operationId + if operationId == "services:get": + for ( + key, + val, + ) in SERVICES_LIST_PARAMETERS.items(): + openapi_spec.components.parameters.setdefault( + key, ParameterSchema(**val) + ) + ref = f"#/components/parameters/{key}" + if ref not in [x.ref for x in operation_spec.parameters]: + operation_spec.parameters.append(ParameterSchema(ref=ref)) + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + # ### Services + if name == "ServicesGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**SERVICES_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "ServiceGetResponse", + "ServicesPostResponse", + "ServicePatchResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**SERVICE_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "ServicesPostRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**SERVICE_CREATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "ServicePatchRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**SERVICE_UPDATE_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True) From 456920d202af6283d96f409f376dcbad6404e8bc Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:34:56 +0100 Subject: [PATCH 09/11] chore: move region schemas to separate module --- .../openapi/keystone_schemas/region.py | 59 ++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/codegenerator/openapi/keystone_schemas/region.py b/codegenerator/openapi/keystone_schemas/region.py index 745caba..5214043 100644 --- a/codegenerator/openapi/keystone_schemas/region.py +++ b/codegenerator/openapi/keystone_schemas/region.py @@ -12,6 +12,8 @@ # from typing import Any +from codegenerator.common.schema import TypeSchema +from codegenerator.common.schema import ParameterSchema REGION_SCHEMA: dict[str, Any] = { "type": "object", @@ -23,12 +25,12 @@ "id": { "type": "string", "format": "uuid", - "description": "The ID for the region.", + "description": "The ID for the ", }, "parent_id": { "type": "string", "format": "uuid", - "description": "To make this region a child of another region, set this parameter to the ID of the parent region.", + "description": "To make this region a child of another region, set this parameter to the ID of the parent ", }, }, } @@ -51,3 +53,56 @@ "schema": {"type": "string", "format": "uuid"}, }, } + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + operationId = operation_spec.operationId + if operationId == "regions:get": + for ( + key, + val, + ) in REGIONS_LIST_PARAMETERS.items(): + openapi_spec.components.parameters.setdefault( + key, ParameterSchema(**val) + ) + ref = f"#/components/parameters/{key}" + if ref not in [x.ref for x in operation_spec.parameters]: + operation_spec.parameters.append(ParameterSchema(ref=ref)) + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + # ### Regions + if name == "RegionsGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**REGIONS_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name in [ + "RegionGetResponse", + "RegionsPostRequest", + "RegionsPostResponse", + "RegionPatchRequest", + "RegionPatchResponse", + ]: + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**REGION_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True) From 56cffe44bcb187e735d5e9bd5b732cf5d29cee7b Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:38:48 +0100 Subject: [PATCH 10/11] chore: move project schemas to separate module --- codegenerator/openapi/keystone.py | 107 +----------------- .../openapi/keystone_schemas/project.py | 96 ++++++++++++++++ 2 files changed, 97 insertions(+), 106 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index f8db63d..6ccad6a 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -18,7 +18,6 @@ from ruamel.yaml.scalarstring import LiteralScalarString from keystone.identity import schema as identity_schema -from keystone.resource import schema as ks_schema from codegenerator.common.schema import ParameterSchema from codegenerator.common.schema import PathSchema @@ -455,32 +454,6 @@ def _post_process_operation_hook( self, openapi_spec, operation_spec, path: str | None = None ): """Hook to allow service specific generator to modify details""" - operationId = operation_spec.operationId - - if operationId == "projects:get": - for ( - key, - val, - ) in project.PROJECT_LIST_PARAMETERS.items(): - openapi_spec.components.parameters.setdefault( - key, ParameterSchema(**val) - ) - ref = f"#/components/parameters/{key}" - if ref not in [x.ref for x in operation_spec.parameters]: - operation_spec.parameters.append(ParameterSchema(ref=ref)) - - elif operationId == "regions:get": - for ( - key, - val, - ) in region.REGIONS_LIST_PARAMETERS.items(): - openapi_spec.components.parameters.setdefault( - key, ParameterSchema(**val) - ) - ref = f"#/components/parameters/{key}" - if ref not in [x.ref for x in operation_spec.parameters]: - operation_spec.parameters.append(ParameterSchema(ref=ref)) - for resource_mod in self.RESOURCE_MODULES: hook = getattr(resource_mod, "_post_process_operation_hook", None) if hook: @@ -506,67 +479,8 @@ def _get_schema_ref( mime_type = "application/json" - # Projects - if name == "ProjectsPostRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**project.PROJECT_CREATE_REQUEST_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectsPostResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**project.PROJECT_CONTAINER_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectPatchRequest": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**project.PROJECT_UPDATE_REQUEST_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectPatchResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**project.PROJECT_CONTAINER_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectsGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**project.PROJECTS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**project.PROJECT_CONTAINER_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - - # Project Tags - elif name == "ProjectsTagPutRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**ks_schema.project_tag_create) - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectsTagsPutRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**ks_schema.project_tags_update) - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectsTagsGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**common.TAGS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "ProjectsTagsPutResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**common.TAGS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - # Groups - elif name == "GroupPatchRequest": + if name == "GroupPatchRequest": openapi_spec.components.schemas.setdefault( name, TypeSchema(**identity_schema.user_update) ) @@ -592,25 +506,6 @@ def _get_schema_ref( ) ref = f"#/components/schemas/{name}" - # ### Regions - elif name == "RegionsGetResponse": - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**region.REGIONS_SCHEMA), - ) - ref = f"#/components/schemas/{name}" - elif name in [ - "RegionGetResponse", - "RegionsPostRequest", - "RegionsPostResponse", - "RegionPatchRequest", - "RegionPatchResponse", - ]: - openapi_spec.components.schemas.setdefault( - name, - TypeSchema(**region.REGION_CONTAINER_SCHEMA), - ) - ref = f"#/components/schemas/{name}" # Default else: (ref, mime_type) = super()._get_schema_ref( diff --git a/codegenerator/openapi/keystone_schemas/project.py b/codegenerator/openapi/keystone_schemas/project.py index 4b3d564..2a32c12 100644 --- a/codegenerator/openapi/keystone_schemas/project.py +++ b/codegenerator/openapi/keystone_schemas/project.py @@ -16,6 +16,10 @@ from keystone.resource import schema as ks_schema +from codegenerator.common.schema import TypeSchema +from codegenerator.common.schema import ParameterSchema +from codegenerator.openapi.keystone_schemas import common + PROJECT_SCHEMA: dict[str, Any] = { "type": "object", @@ -90,3 +94,95 @@ "x-openstack": {"min-ver": "3.4"}, }, } + + +def _post_process_operation_hook( + openapi_spec, operation_spec, path: str | None = None +): + """Hook to allow service specific generator to modify details""" + operationId = operation_spec.operationId + if operationId == "projects:get": + for ( + key, + val, + ) in PROJECT_LIST_PARAMETERS.items(): + openapi_spec.components.parameters.setdefault( + key, ParameterSchema(**val) + ) + ref = f"#/components/parameters/{key}" + if ref not in [x.ref for x in operation_spec.parameters]: + operation_spec.parameters.append(ParameterSchema(ref=ref)) + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + # Projects + if name == "ProjectsPostRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**PROJECT_CREATE_REQUEST_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectsPostResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**PROJECT_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectPatchRequest": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**PROJECT_UPDATE_REQUEST_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectPatchResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**PROJECT_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectsGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**PROJECTS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectGetResponse": + openapi_spec.components.schemas.setdefault( + name, + TypeSchema(**PROJECT_CONTAINER_SCHEMA), + ) + ref = f"#/components/schemas/{name}" + + # Project Tags + elif name == "ProjectsTagPutRequest": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**ks_schema.project_tag_create) + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectsTagsPutRequest": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**ks_schema.project_tags_update) + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectsTagsGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**common.TAGS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "ProjectsTagsPutResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**common.TAGS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True) From 8be809664167cca5319c76990dfa12ca86559363 Mon Sep 17 00:00:00 2001 From: gtema Date: Tue, 27 Feb 2024 15:41:41 +0100 Subject: [PATCH 11/11] chore: move group schemas to separate module --- codegenerator/openapi/keystone.py | 38 ++-------------- .../openapi/keystone_schemas/group.py | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+), 35 deletions(-) diff --git a/codegenerator/openapi/keystone.py b/codegenerator/openapi/keystone.py index 6ccad6a..16cf28a 100644 --- a/codegenerator/openapi/keystone.py +++ b/codegenerator/openapi/keystone.py @@ -17,8 +17,6 @@ from ruamel.yaml.scalarstring import LiteralScalarString -from keystone.identity import schema as identity_schema - from codegenerator.common.schema import ParameterSchema from codegenerator.common.schema import PathSchema from codegenerator.common.schema import SpecSchema @@ -477,39 +475,9 @@ def _get_schema_ref( if matched: return (ref, mime_type) - mime_type = "application/json" - - # Groups - if name == "GroupPatchRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**identity_schema.user_update) - ) - ref = f"#/components/schemas/{name}" - elif name == "GroupsPostRequest": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**identity_schema.user_create) - ) - ref = f"#/components/schemas/{name}" - elif name == "GroupPatchResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**group.GROUP_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "GroupsGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**group.GROUPS_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - elif name == "GroupGetResponse": - openapi_spec.components.schemas.setdefault( - name, TypeSchema(**group.GROUP_SCHEMA) - ) - ref = f"#/components/schemas/{name}" - # Default - else: - (ref, mime_type) = super()._get_schema_ref( - openapi_spec, name, description, action_name=action_name - ) + (ref, mime_type) = super()._get_schema_ref( + openapi_spec, name, description, action_name=action_name + ) return (ref, mime_type) diff --git a/codegenerator/openapi/keystone_schemas/group.py b/codegenerator/openapi/keystone_schemas/group.py index cebea60..d81b377 100644 --- a/codegenerator/openapi/keystone_schemas/group.py +++ b/codegenerator/openapi/keystone_schemas/group.py @@ -14,6 +14,9 @@ from keystone.identity import schema as identity_schema +from codegenerator.common.schema import TypeSchema + + GROUP_SCHEMA: dict[str, Any] = { "type": "object", "properties": { @@ -26,3 +29,45 @@ "type": "object", "properties": {"groups": {"type": "array", "items": GROUP_SCHEMA}}, } + + +def _get_schema_ref( + openapi_spec, + name, + description=None, + schema_def=None, + action_name=None, +) -> tuple[str | None, str | None, bool]: + mime_type: str = "application/json" + ref: str + # Groups + if name == "GroupPatchRequest": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**identity_schema.user_update) + ) + ref = f"#/components/schemas/{name}" + elif name == "GroupsPostRequest": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**identity_schema.user_create) + ) + ref = f"#/components/schemas/{name}" + elif name == "GroupPatchResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**GROUP_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "GroupsGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**GROUPS_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + elif name == "GroupGetResponse": + openapi_spec.components.schemas.setdefault( + name, TypeSchema(**GROUP_SCHEMA) + ) + ref = f"#/components/schemas/{name}" + + else: + return (None, None, False) + + return (ref, mime_type, True)