refactor: Adjust the common and contrib schemas

1. adjust the common schemas
2. adjust the contrib schemas
3. just use schemas directly because all include in __init__.py
4. update swagger.json file

Change-Id: I4b8c366cbb439fbcb7c118329588eab1b31b9370
This commit is contained in:
zhu.boxiang 2022-07-12 17:23:26 +08:00
parent 1b17da1b2c
commit 7eaa53c9db
10 changed files with 145 additions and 184 deletions

View File

@ -152,7 +152,7 @@
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"$ref": "#/components/schemas/OK" "$ref": "#/components/schemas/Message"
} }
} }
} }
@ -1427,7 +1427,7 @@
"title": "Response 200 List Keystone Endpoints Api V1 Contrib Keystone Endpoints Get", "title": "Response 200 List Keystone Endpoints Api V1 Contrib Keystone Endpoints Get",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/components/schemas/ContribListKeystoneEndpointsResponseModel" "$ref": "#/components/schemas/KeystoneEndpoints"
} }
} }
} }
@ -1842,11 +1842,11 @@
} }
}, },
"422": { "422": {
"description": "Unprocessable Entity", "description": "Validation Error",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"$ref": "#/components/schemas/UnprocessableEntityMessage" "$ref": "#/components/schemas/HTTPValidationError"
} }
} }
} }
@ -1898,25 +1898,8 @@
"properties": { "properties": {
"detail": { "detail": {
"title": "Detail", "title": "Detail",
"type": "string" "type": "string",
} "description": "Detail message"
}
},
"ContribListKeystoneEndpointsResponseModel": {
"title": "ContribListKeystoneEndpointsResponseModel",
"required": [
"region_name",
"url"
],
"type": "object",
"properties": {
"region_name": {
"title": "Region Name",
"type": "string"
},
"url": {
"title": "Url",
"type": "string"
} }
} }
}, },
@ -2873,7 +2856,8 @@
"properties": { "properties": {
"detail": { "detail": {
"title": "Detail", "title": "Detail",
"type": "string" "type": "string",
"description": "Detail message"
} }
} }
}, },
@ -2899,7 +2883,54 @@
"properties": { "properties": {
"detail": { "detail": {
"title": "Detail", "title": "Detail",
"type": "string" "type": "string",
"description": "Detail message"
}
}
},
"KeystoneEndpoints": {
"title": "KeystoneEndpoints",
"required": [
"region_name",
"url"
],
"type": "object",
"properties": {
"region_name": {
"title": "Region Name",
"type": "string",
"description": "Region name"
},
"url": {
"title": "Url",
"type": "string",
"description": "Endpoint URL"
}
}
},
"Message": {
"title": "Message",
"required": [
"message"
],
"type": "object",
"properties": {
"message": {
"title": "Message",
"type": "string",
"description": "Message"
},
"code": {
"title": "Code",
"type": "integer",
"description": "Code",
"default": 200
},
"title": {
"title": "Title",
"type": "string",
"description": "Title",
"default": "OK"
} }
} }
}, },
@ -2912,30 +2943,8 @@
"properties": { "properties": {
"detail": { "detail": {
"title": "Detail", "title": "Detail",
"type": "string"
}
}
},
"OK": {
"title": "OK",
"required": [
"message"
],
"type": "object",
"properties": {
"code": {
"title": "Code",
"type": "integer",
"default": 200
},
"message": {
"title": "Message",
"type": "string"
},
"title": {
"title": "Title",
"type": "string", "type": "string",
"default": "OK" "description": "Detail message"
} }
} }
}, },
@ -3297,20 +3306,8 @@
"properties": { "properties": {
"detail": { "detail": {
"title": "Detail", "title": "Detail",
"type": "string" "type": "string",
} "description": "Detail message"
}
},
"UnprocessableEntityMessage": {
"title": "UnprocessableEntityMessage",
"required": [
"detail"
],
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "string"
} }
} }
}, },

View File

@ -24,7 +24,6 @@ from skyline_apiserver.client.openstack import system
from skyline_apiserver.client.openstack.system import get_endpoints from skyline_apiserver.client.openstack.system import get_endpoints
from skyline_apiserver.config import CONF from skyline_apiserver.config import CONF
from skyline_apiserver.log import LOG from skyline_apiserver.log import LOG
from skyline_apiserver.schemas import common
from skyline_apiserver.types import constants from skyline_apiserver.types import constants
router = APIRouter() router = APIRouter()
@ -34,14 +33,14 @@ router = APIRouter()
"/contrib/keystone_endpoints", "/contrib/keystone_endpoints",
description="List Keystone Endpoints", description="List Keystone Endpoints",
responses={ responses={
200: {"model": List[schemas.ContribListKeystoneEndpointsResponseModel]}, 200: {"model": List[schemas.KeystoneEndpoints]},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=List[schemas.ContribListKeystoneEndpointsResponseModel], response_model=List[schemas.KeystoneEndpoints],
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
response_description="OK", response_description="OK",
) )
async def list_keystone_endpoints() -> List[schemas.ContribListKeystoneEndpointsResponseModel]: async def list_keystone_endpoints() -> List[schemas.KeystoneEndpoints]:
"""Contrib List Keystone Endpoints.""" """Contrib List Keystone Endpoints."""
try: try:
regions = await system.get_regions() regions = await system.get_regions()
@ -64,7 +63,7 @@ async def list_keystone_endpoints() -> List[schemas.ContribListKeystoneEndpoints
description="List Domains", description="List Domains",
responses={ responses={
200: {"model": List[str]}, 200: {"model": List[str]},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=List[str], response_model=List[str],
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -102,7 +101,7 @@ async def list_domains(
description="List Regions", description="List Regions",
responses={ responses={
200: {"model": List[str]}, 200: {"model": List[str]},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=List[str], response_model=List[str],
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,

View File

@ -31,7 +31,6 @@ from skyline_apiserver.client import utils
from skyline_apiserver.client.openstack import cinder, glance, keystone, neutron, nova from skyline_apiserver.client.openstack import cinder, glance, keystone, neutron, nova
from skyline_apiserver.client.utils import generate_session, get_system_session from skyline_apiserver.client.utils import generate_session, get_system_session
from skyline_apiserver.config import CONF from skyline_apiserver.config import CONF
from skyline_apiserver.schemas import common
from skyline_apiserver.types import constants from skyline_apiserver.types import constants
from skyline_apiserver.utils.roles import assert_system_admin_or_reader, is_system_reader_no_admin from skyline_apiserver.utils.roles import assert_system_admin_or_reader, is_system_reader_no_admin
@ -51,10 +50,10 @@ List Servers.
""", """,
responses={ responses={
200: {"model": schemas.ExtListServersResponse}, 200: {"model": schemas.ExtListServersResponse},
400: {"model": common.BadRequestMessage}, 400: {"model": schemas.BadRequestMessage},
401: {"model": common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
403: {"model": common.ForbiddenMessage}, 403: {"model": schemas.ForbiddenMessage},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=schemas.ExtListServersResponse, response_model=schemas.ExtListServersResponse,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -282,10 +281,10 @@ List Recycle Servers.
""", """,
responses={ responses={
200: {"model": schemas.ExtListRecycleServersResponse}, 200: {"model": schemas.ExtListRecycleServersResponse},
400: {"model": common.BadRequestMessage}, 400: {"model": schemas.BadRequestMessage},
401: {"model": common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
403: {"model": common.ForbiddenMessage}, 403: {"model": schemas.ForbiddenMessage},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=schemas.ExtListRecycleServersResponse, response_model=schemas.ExtListRecycleServersResponse,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -505,9 +504,9 @@ async def list_recycle_servers(
description="List Volumes.", description="List Volumes.",
responses={ responses={
200: {"model": schemas.ExtListVolumesResponse}, 200: {"model": schemas.ExtListVolumesResponse},
401: {"model": common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
403: {"model": common.ForbiddenMessage}, 403: {"model": schemas.ForbiddenMessage},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=schemas.ExtListVolumesResponse, response_model=schemas.ExtListVolumesResponse,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -701,9 +700,9 @@ async def list_volumes(
description="List Volume Snapshots.", description="List Volume Snapshots.",
responses={ responses={
200: {"model": schemas.ExtListVolumeSnapshotsResponse}, 200: {"model": schemas.ExtListVolumeSnapshotsResponse},
401: {"model": common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
403: {"model": common.ForbiddenMessage}, 403: {"model": schemas.ForbiddenMessage},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=schemas.ExtListVolumeSnapshotsResponse, response_model=schemas.ExtListVolumeSnapshotsResponse,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -874,9 +873,9 @@ async def list_volume_snapshots(
"/extension/ports", "/extension/ports",
description="List Ports.", description="List Ports.",
responses={ responses={
401: {"model": common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
403: {"model": common.ForbiddenMessage}, 403: {"model": schemas.ForbiddenMessage},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=schemas.ExtListPortsResponse, response_model=schemas.ExtListPortsResponse,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -1064,8 +1063,8 @@ async def list_ports(
description="List compute services.", description="List compute services.",
responses={ responses={
200: {"model": schemas.ExtListComputeServicesResponse}, 200: {"model": schemas.ExtListComputeServicesResponse},
401: {"model": common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=schemas.ExtListComputeServicesResponse, response_model=schemas.ExtListComputeServicesResponse,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,

View File

@ -63,7 +63,7 @@ async def _patch_profile(profile: schemas.Profile, global_request_id: str) -> sc
description="Login & get user profile.", description="Login & get user profile.",
responses={ responses={
200: {"model": schemas.Profile}, 200: {"model": schemas.Profile},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
}, },
response_model=schemas.Profile, response_model=schemas.Profile,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -131,7 +131,7 @@ async def login(
description="Get user profile.", description="Get user profile.",
responses={ responses={
200: {"model": schemas.Profile}, 200: {"model": schemas.Profile},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
}, },
response_model=schemas.Profile, response_model=schemas.Profile,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -152,9 +152,9 @@ async def get_profile(
"/logout", "/logout",
description="Log out.", description="Log out.",
responses={ responses={
200: {"model": schemas.common.OK}, 200: {"model": schemas.Message},
}, },
response_model=schemas.common.OK, response_model=schemas.Message,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
response_description="OK", response_description="OK",
) )
@ -178,7 +178,7 @@ async def logout(
except Exception as e: except Exception as e:
LOG.debug(str(e)) LOG.debug(str(e))
response.delete_cookie(CONF.default.session_name) response.delete_cookie(CONF.default.session_name)
return schemas.common.OK(message="Logout OK") return schemas.Message(message="Logout OK")
@router.post( @router.post(
@ -186,7 +186,7 @@ async def logout(
description="Switch project.", description="Switch project.",
responses={ responses={
200: {"model": schemas.Profile}, 200: {"model": schemas.Profile},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
}, },
response_model=schemas.Profile, response_model=schemas.Profile,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,

View File

@ -22,7 +22,6 @@ from skyline_apiserver import schemas
from skyline_apiserver.api import deps from skyline_apiserver.api import deps
from skyline_apiserver.client.utils import generate_session, get_access from skyline_apiserver.client.utils import generate_session, get_access
from skyline_apiserver.policy import ENFORCER, UserContext from skyline_apiserver.policy import ENFORCER, UserContext
from skyline_apiserver.schemas import Policies, PoliciesRules, common
router = APIRouter() router = APIRouter()
@ -66,11 +65,11 @@ def _generate_target(profile: schemas.Profile) -> Dict[str, str]:
"/policies", "/policies",
description="List policies and permissions", description="List policies and permissions",
responses={ responses={
200: {"model": Policies}, 200: {"model": schemas.Policies},
401: {"model": common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=Policies, response_model=schemas.Policies,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
response_description="OK", response_description="OK",
) )
@ -92,17 +91,17 @@ async def list_policies(
"/policies/check", "/policies/check",
description="Check policies permissions", description="Check policies permissions",
responses={ responses={
200: {"model": Policies}, 200: {"model": schemas.Policies},
401: {"model": common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
403: {"model": common.ForbiddenMessage}, 403: {"model": schemas.ForbiddenMessage},
500: {"model": common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=Policies, response_model=schemas.Policies,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
response_description="OK", response_description="OK",
) )
async def check_policies( async def check_policies(
policy_rules: PoliciesRules, policy_rules: schemas.PoliciesRules,
profile: schemas.Profile = Depends(deps.get_profile_update_jwt), profile: schemas.Profile = Depends(deps.get_profile_update_jwt),
): ):
session = await generate_session(profile) session = await generate_session(profile)

View File

@ -84,8 +84,8 @@ def get_prometheus_query_range_response(
description="Prometheus query API.", description="Prometheus query API.",
responses={ responses={
200: {"model": schemas.PrometheusQueryResponse}, 200: {"model": schemas.PrometheusQueryResponse},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
500: {"model": schemas.common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=schemas.PrometheusQueryResponse, response_model=schemas.PrometheusQueryResponse,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -129,8 +129,8 @@ async def prometheus_query(
description="Prometheus query_range API.", description="Prometheus query_range API.",
responses={ responses={
200: {"model": schemas.PrometheusQueryRangeResponse}, 200: {"model": schemas.PrometheusQueryRangeResponse},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
500: {"model": schemas.common.InternalServerErrorMessage}, 500: {"model": schemas.InternalServerErrorMessage},
}, },
response_model=schemas.PrometheusQueryRangeResponse, response_model=schemas.PrometheusQueryRangeResponse,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,

View File

@ -39,8 +39,8 @@ def assert_setting_key_exist(key: str):
description="Get a setting item.", description="Get a setting item.",
responses={ responses={
200: {"model": schemas.Setting}, 200: {"model": schemas.Setting},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
404: {"model": schemas.common.NotFoundMessage}, 404: {"model": schemas.NotFoundMessage},
}, },
response_model=schemas.Setting, response_model=schemas.Setting,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -63,10 +63,9 @@ async def show_setting(
description="Update a setting item.", description="Update a setting item.",
responses={ responses={
200: {"model": schemas.Setting}, 200: {"model": schemas.Setting},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
403: {"model": schemas.common.ForbiddenMessage}, 403: {"model": schemas.ForbiddenMessage},
404: {"model": schemas.common.NotFoundMessage}, 404: {"model": schemas.NotFoundMessage},
422: {"model": schemas.common.UnprocessableEntityMessage},
}, },
response_model=schemas.Setting, response_model=schemas.Setting,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -94,7 +93,7 @@ async def update_setting(
description="Get all settings.", description="Get all settings.",
responses={ responses={
200: {"model": schemas.Settings}, 200: {"model": schemas.Settings},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
}, },
response_model=schemas.Settings, response_model=schemas.Settings,
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
@ -125,9 +124,9 @@ async def list_settings(
description="Reset a setting item to default", description="Reset a setting item to default",
responses={ responses={
204: {"model": None}, 204: {"model": None},
401: {"model": schemas.common.UnauthorizedMessage}, 401: {"model": schemas.UnauthorizedMessage},
403: {"model": schemas.common.ForbiddenMessage}, 403: {"model": schemas.ForbiddenMessage},
404: {"model": schemas.common.NotFoundMessage}, 404: {"model": schemas.NotFoundMessage},
}, },
status_code=status.HTTP_204_NO_CONTENT, status_code=status.HTTP_204_NO_CONTENT,
response_description="No Content", response_description="No Content",

View File

@ -12,8 +12,17 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from . import common # flake8: noqa: F401
from .contrib import ContribListKeystoneEndpointsResponseModel
from .common import (
BadRequestMessage,
ForbiddenMessage,
InternalServerErrorMessage,
Message,
NotFoundMessage,
UnauthorizedMessage,
)
from .contrib import KeystoneEndpoints
from .extension import ( from .extension import (
ExtListComputeServicesResponse, ExtListComputeServicesResponse,
ExtListPortsResponse, ExtListPortsResponse,
@ -44,44 +53,3 @@ from .prometheus import (
PrometheusQueryResult, PrometheusQueryResult,
) )
from .setting import Setting, Settings, UpdateSetting from .setting import Setting, Settings, UpdateSetting
__all__ = (
"common",
"ContribListKeystoneEndpointsResponseModel",
"Credential",
"Domain",
"ExtListComputeServicesResponse",
"ExtListPortsResponse",
"ExtListRecycleServersResponse",
"ExtListServersResponse",
"ExtListVolumeSnapshotsResponse",
"ExtListVolumesResponse",
"ExtPortDeviceOwner",
"ExtPortSortKey",
"ExtPortStatus",
"ExtRecycleServerSortKey",
"ExtServerSortKey",
"ExtServerStatus",
"ExtSortDir",
"ExtVolumeSnapshotSortKey",
"ExtVolumeSnapshotStatus",
"ExtVolumeSortKey",
"ExtVolumeStatus",
"License",
"Payload",
"Policies",
"PoliciesRules",
"Profile",
"Project",
"Region",
"Role",
"Setting",
"Settings",
"UpdateSetting",
"PrometheusQueryResponse",
"PrometheusQueryData",
"PrometheusQueryResult",
"PrometheusQueryRangeResponse",
"PrometheusQueryRangeData",
"PrometheusQueryRangeResult",
)

View File

@ -12,34 +12,34 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from pydantic import BaseModel from pydantic import BaseModel, Field
class OK(BaseModel): class Message(BaseModel):
code: int = 200 message: str = Field(..., description="Message")
message: str code: int = Field(200, description="Code")
title: str = "OK" title: str = Field("OK", description="Title")
class BadRequestMessage(BaseModel): class ErrorMessageBase(BaseModel):
detail: str detail: str = Field(..., description="Detail message")
class UnprocessableEntityMessage(BaseModel): class BadRequestMessage(ErrorMessageBase):
detail: str """"""
class UnauthorizedMessage(BaseModel): class UnauthorizedMessage(ErrorMessageBase):
detail: str """"""
class ForbiddenMessage(BaseModel): class ForbiddenMessage(ErrorMessageBase):
detail: str """"""
class NotFoundMessage(BaseModel): class NotFoundMessage(ErrorMessageBase):
detail: str """"""
class InternalServerErrorMessage(BaseModel): class InternalServerErrorMessage(ErrorMessageBase):
detail: str """"""

View File

@ -14,9 +14,9 @@
from __future__ import annotations from __future__ import annotations
from pydantic import BaseModel from pydantic import BaseModel, Field
class ContribListKeystoneEndpointsResponseModel(BaseModel): class KeystoneEndpoints(BaseModel):
region_name: str region_name: str = Field(..., description="Region name")
url: str url: str = Field(..., description="Endpoint URL")