refactor: Refactor schemas

1. adjust setting schemas
2. adjust prometheus schemas
3. adjust policy schemas
4. adjust policy_manager schemas
5. adjust login schemas
6. remove some invalid model
7. update the swagger.json file

Change-Id: I6b513498999f1c56481107ca78df656e4c8b38cb
This commit is contained in:
zhu.boxiang 2022-07-13 15:21:51 +08:00
parent 7eaa53c9db
commit 4d9b577007
8 changed files with 269 additions and 183 deletions

View File

@ -1915,19 +1915,23 @@
"properties": { "properties": {
"region": { "region": {
"title": "Region", "title": "Region",
"type": "string" "type": "string",
"description": "Credential user region"
}, },
"domain": { "domain": {
"title": "Domain", "title": "Domain",
"type": "string" "type": "string",
"description": "Credential user domain"
}, },
"username": { "username": {
"title": "Username", "title": "Username",
"type": "string" "type": "string",
"description": "Credential username"
}, },
"password": { "password": {
"title": "Password", "title": "Password",
"type": "string" "type": "string",
"description": "Credential password for user"
} }
}, },
"example": { "example": {
@ -1947,11 +1951,13 @@
"properties": { "properties": {
"id": { "id": {
"title": "Id", "title": "Id",
"type": "string" "type": "string",
"description": "Domain ID"
}, },
"name": { "name": {
"title": "Name", "title": "Name",
"type": "string" "type": "string",
"description": "Domain name"
} }
} }
}, },
@ -2960,7 +2966,8 @@
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/components/schemas/Policy" "$ref": "#/components/schemas/Policy"
} },
"description": "Policies list"
} }
} }
}, },
@ -2976,7 +2983,8 @@
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"description": "Policies rules list"
} }
} }
}, },
@ -2990,11 +2998,13 @@
"properties": { "properties": {
"rule": { "rule": {
"title": "Rule", "title": "Rule",
"type": "string" "type": "string",
"description": "Policy rule"
}, },
"allowed": { "allowed": {
"title": "Allowed", "title": "Allowed",
"type": "boolean" "type": "boolean",
"description": "Policy allowed"
} }
} }
}, },
@ -3003,74 +3013,97 @@
"required": [ "required": [
"keystone_token", "keystone_token",
"region", "region",
"exp",
"uuid",
"project", "project",
"user", "user",
"roles", "roles",
"keystone_token_exp", "keystone_token_exp",
"exp",
"uuid",
"version" "version"
], ],
"type": "object", "type": "object",
"properties": { "properties": {
"keystone_token": { "keystone_token": {
"title": "Keystone Token", "title": "Keystone Token",
"type": "string" "type": "string",
"description": "Keystone token"
}, },
"region": { "region": {
"title": "Region", "title": "Region",
"type": "string" "type": "string",
"description": "User region"
},
"exp": {
"title": "Exp",
"type": "integer",
"description": "Token expiration time"
},
"uuid": {
"title": "Uuid",
"type": "string",
"description": "UUID"
}, },
"project": { "project": {
"$ref": "#/components/schemas/Project" "title": "Project",
"allOf": [
{
"$ref": "#/components/schemas/Project"
}
],
"description": "User project"
}, },
"user": { "user": {
"$ref": "#/components/schemas/User" "title": "User",
"allOf": [
{
"$ref": "#/components/schemas/User"
}
],
"description": "User"
}, },
"roles": { "roles": {
"title": "Roles", "title": "Roles",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/components/schemas/Role" "$ref": "#/components/schemas/Role"
} },
"description": "User roles"
}, },
"keystone_token_exp": { "keystone_token_exp": {
"title": "Keystone Token Exp", "title": "Keystone Token Exp",
"type": "string" "type": "string",
"description": "Keystone token expiration time"
}, },
"base_roles": { "base_roles": {
"title": "Base Roles", "title": "Base Roles",
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"description": "User base roles"
}, },
"base_domains": { "base_domains": {
"title": "Base Domains", "title": "Base Domains",
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
} },
"description": "User base domains"
}, },
"endpoints": { "endpoints": {
"title": "Endpoints", "title": "Endpoints",
"type": "object" "type": "object",
"description": "Keystone endpoints"
}, },
"projects": { "projects": {
"title": "Projects", "title": "Projects",
"type": "object" "type": "object",
}, "description": "User projects"
"exp": {
"title": "Exp",
"type": "integer"
},
"uuid": {
"title": "Uuid",
"type": "string"
}, },
"version": { "version": {
"title": "Version", "title": "Version",
"type": "string" "type": "string",
"description": "Version"
} }
} }
}, },
@ -3085,56 +3118,68 @@
"properties": { "properties": {
"id": { "id": {
"title": "Id", "title": "Id",
"type": "string" "type": "string",
"description": "Project ID"
}, },
"name": { "name": {
"title": "Name", "title": "Name",
"type": "string" "type": "string",
"description": "Project name"
}, },
"domain": { "domain": {
"$ref": "#/components/schemas/Domain" "title": "Domain",
"allOf": [
{
"$ref": "#/components/schemas/Domain"
}
],
"description": "Project domain"
} }
} }
}, },
"PrometheusQueryData": { "PrometheusQueryData": {
"title": "PrometheusQueryData", "title": "PrometheusQueryData",
"required": [ "required": [
"result", "resultType",
"resultType" "result"
], ],
"type": "object", "type": "object",
"properties": { "properties": {
"resultType": {
"title": "Resulttype",
"type": "string",
"description": "Prometheus result type"
},
"result": { "result": {
"title": "Result", "title": "Result",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/components/schemas/PrometheusQueryResult" "$ref": "#/components/schemas/PrometheusQueryResult"
} },
}, "description": "Prometheus query result"
"resultType": {
"title": "Resulttype",
"type": "string"
} }
} }
}, },
"PrometheusQueryRangeData": { "PrometheusQueryRangeData": {
"title": "PrometheusQueryRangeData", "title": "PrometheusQueryRangeData",
"required": [ "required": [
"result", "resultType",
"resultType" "result"
], ],
"type": "object", "type": "object",
"properties": { "properties": {
"resultType": {
"title": "Resulttype",
"type": "string",
"description": "Prometheus result type"
},
"result": { "result": {
"title": "Result", "title": "Result",
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/components/schemas/PrometheusQueryRangeResult" "$ref": "#/components/schemas/PrometheusQueryRangeResult"
} },
}, "description": "Prometheus query range result"
"resultType": {
"title": "Resulttype",
"type": "string"
} }
} }
}, },
@ -3147,22 +3192,32 @@
"properties": { "properties": {
"status": { "status": {
"title": "Status", "title": "Status",
"type": "string" "type": "string",
}, "description": "Prometheus status"
"data": {
"$ref": "#/components/schemas/PrometheusQueryRangeData"
}, },
"errorType": { "errorType": {
"title": "Errortype", "title": "Errortype",
"type": "string" "type": "string",
"description": "Prometheus error type"
}, },
"error": { "error": {
"title": "Error", "title": "Error",
"type": "string" "type": "string",
"description": "Prometheus error"
}, },
"warnings": { "warnings": {
"title": "Warnings", "title": "Warnings",
"type": "string" "type": "string",
"description": "Prometheus warnings"
},
"data": {
"title": "Data",
"allOf": [
{
"$ref": "#/components/schemas/PrometheusQueryRangeData"
}
],
"description": "Prometheus query range data"
} }
} }
}, },
@ -3170,7 +3225,7 @@
"title": "PrometheusQueryRangeResult", "title": "PrometheusQueryRangeResult",
"required": [ "required": [
"metric", "metric",
"values" "value"
], ],
"type": "object", "type": "object",
"properties": { "properties": {
@ -3179,12 +3234,14 @@
"type": "object", "type": "object",
"additionalProperties": { "additionalProperties": {
"type": "string" "type": "string"
} },
"description": "Prometheus metric"
}, },
"values": { "value": {
"title": "Values", "title": "Value",
"type": "array", "type": "array",
"items": {} "items": {},
"description": "Prometheus metric value"
} }
} }
}, },
@ -3197,22 +3254,32 @@
"properties": { "properties": {
"status": { "status": {
"title": "Status", "title": "Status",
"type": "string" "type": "string",
}, "description": "Prometheus status"
"data": {
"$ref": "#/components/schemas/PrometheusQueryData"
}, },
"errorType": { "errorType": {
"title": "Errortype", "title": "Errortype",
"type": "string" "type": "string",
"description": "Prometheus error type"
}, },
"error": { "error": {
"title": "Error", "title": "Error",
"type": "string" "type": "string",
"description": "Prometheus error"
}, },
"warnings": { "warnings": {
"title": "Warnings", "title": "Warnings",
"type": "string" "type": "string",
"description": "Prometheus warnings"
},
"data": {
"title": "Data",
"allOf": [
{
"$ref": "#/components/schemas/PrometheusQueryData"
}
],
"description": "Prometheus query data"
} }
} }
}, },
@ -3229,12 +3296,14 @@
"type": "object", "type": "object",
"additionalProperties": { "additionalProperties": {
"type": "string" "type": "string"
} },
"description": "Prometheus metric"
}, },
"value": { "value": {
"title": "Value", "title": "Value",
"type": "array", "type": "array",
"items": {} "items": {},
"description": "Prometheus metric value"
} }
} }
}, },
@ -3248,11 +3317,13 @@
"properties": { "properties": {
"id": { "id": {
"title": "Id", "title": "Id",
"type": "string" "type": "string",
"description": "Role ID"
}, },
"name": { "name": {
"title": "Name", "title": "Name",
"type": "string" "type": "string",
"description": "Role name"
} }
} }
}, },
@ -3260,24 +3331,29 @@
"title": "Setting", "title": "Setting",
"required": [ "required": [
"key", "key",
"value",
"restart_service" "restart_service"
], ],
"type": "object", "type": "object",
"properties": { "properties": {
"key": { "key": {
"title": "Key", "title": "Key",
"type": "string" "type": "string",
"description": "Key of setting"
}, },
"value": { "value": {
"title": "Value" "title": "Value",
"description": "Value of setting"
}, },
"hidden": { "hidden": {
"title": "Hidden", "title": "Hidden",
"type": "boolean" "type": "boolean",
"description": "Hidden setting or not"
}, },
"restart_service": { "restart_service": {
"title": "Restart Service", "title": "Restart Service",
"type": "boolean" "type": "boolean",
"description": "Restart service or not to apply setting"
} }
} }
}, },
@ -3293,7 +3369,8 @@
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/components/schemas/Setting" "$ref": "#/components/schemas/Setting"
} },
"description": "Settings"
} }
} }
}, },
@ -3314,16 +3391,19 @@
"UpdateSetting": { "UpdateSetting": {
"title": "UpdateSetting", "title": "UpdateSetting",
"required": [ "required": [
"key" "key",
"value"
], ],
"type": "object", "type": "object",
"properties": { "properties": {
"key": { "key": {
"title": "Key", "title": "Key",
"type": "string" "type": "string",
"description": "Key of setting"
}, },
"value": { "value": {
"title": "Value" "title": "Value",
"description": "Value of setting"
} }
} }
}, },
@ -3338,14 +3418,22 @@
"properties": { "properties": {
"id": { "id": {
"title": "Id", "title": "Id",
"type": "string" "type": "string",
"description": "User ID"
}, },
"name": { "name": {
"title": "Name", "title": "Name",
"type": "string" "type": "string",
"description": "User name"
}, },
"domain": { "domain": {
"$ref": "#/components/schemas/Domain" "title": "Domain",
"allOf": [
{
"$ref": "#/components/schemas/Domain"
}
],
"description": "User domain"
} }
} }
}, },

View File

@ -19,7 +19,7 @@ from typing import List
from oslo_policy import _parser # type: ignore from oslo_policy import _parser # type: ignore
from oslo_policy.policy import DocumentedRuleDefault, RuleDefault # type: ignore from oslo_policy.policy import DocumentedRuleDefault, RuleDefault # type: ignore
from skyline_apiserver.schemas.policy_manager import Operation, OperationsSchema, ScopeTypesSchema from skyline_apiserver import schemas
class Rule: class Rule:
@ -68,15 +68,15 @@ class APIRule(Rule):
check_str: str, check_str: str,
description: str, description: str,
scope_types: List[str], scope_types: List[str],
operations: List[Operation], operations: List[schemas.Operation],
basic_check_str: str = "", basic_check_str: str = "",
) -> None: ) -> None:
super().__init__(name, check_str, description, basic_check_str) super().__init__(name, check_str, description, basic_check_str)
ScopeTypesSchema.parse_obj(scope_types) schemas.ScopeTypesSchema.parse_obj(scope_types)
self.scope_types = scope_types self.scope_types = scope_types
OperationsSchema.parse_obj(operations) schemas.OperationsSchema.parse_obj(operations)
self.operations = operations self.operations = operations
def format_into_yaml(self) -> str: def format_into_yaml(self) -> str:
@ -105,13 +105,15 @@ class APIRule(Rule):
method = operation.get("method") method = operation.get("method")
if isinstance(method, list): if isinstance(method, list):
for i in method: for i in method:
operations.append(Operation(method=i.upper(), path=operation.get("path", ""))) operations.append(
schemas.Operation(method=i.upper(), path=operation.get("path", ""))
)
elif isinstance(method, str): elif isinstance(method, str):
operations.append( operations.append(
Operation(method=method.upper(), path=operation.get("path", "")), schemas.Operation(method=method.upper(), path=operation.get("path", "")),
) )
else: else:
operations.append(Operation(method="GET", path=operation.get("path", ""))) operations.append(schemas.Operation(method="GET", path=operation.get("path", "")))
return cls( return cls(
name=rule.name, name=rule.name,
check_str=rule.check_str, check_str=rule.check_str,

View File

@ -42,8 +42,9 @@ from .extension import (
ExtVolumeSortKey, ExtVolumeSortKey,
ExtVolumeStatus, ExtVolumeStatus,
) )
from .login import Credential, Domain, License, Payload, Profile, Project, Region, Role from .login import Credential, Payload, Profile
from .policy import Policies, PoliciesRules from .policy import Policies, PoliciesRules
from .policy_manager import Operation, OperationsSchema, ScopeTypesSchema
from .prometheus import ( from .prometheus import (
PrometheusQueryData, PrometheusQueryData,
PrometheusQueryRangeData, PrometheusQueryRangeData,

View File

@ -17,17 +17,17 @@ from __future__ import annotations
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from jose import jwt from jose import jwt
from pydantic import BaseModel from pydantic import BaseModel, Field
from skyline_apiserver import config from skyline_apiserver import config
from skyline_apiserver.types import constants from skyline_apiserver.types import constants
class Credential(BaseModel): class Credential(BaseModel):
region: str region: str = Field(..., description="Credential user region")
domain: str domain: str = Field(..., description="Credential user domain")
username: str username: str = Field(..., description="Credential username")
password: str password: str = Field(..., description="Credential password for user")
class Config: class Config:
schema_extra = { schema_extra = {
@ -41,46 +41,35 @@ class Credential(BaseModel):
class Domain(BaseModel): class Domain(BaseModel):
id: str id: str = Field(..., description="Domain ID")
name: str name: str = Field(..., description="Domain name")
class License(BaseModel):
name: str
summary: str
macs: List[str]
features: List[Dict[str, Any]]
start: str
end: str
class Region(BaseModel):
id: str
class Role(BaseModel): class Role(BaseModel):
id: str id: str = Field(..., description="Role ID")
name: str name: str = Field(..., description="Role name")
class Project(BaseModel): class Project(BaseModel):
id: str id: str = Field(..., description="Project ID")
name: str name: str = Field(..., description="Project name")
domain: Domain domain: Domain = Field(..., description="Project domain")
class User(BaseModel): class User(BaseModel):
id: str id: str = Field(..., description="User ID")
name: str name: str = Field(..., description="User name")
domain: Domain domain: Domain = Field(..., description="User domain")
class Payload(BaseModel): class PayloadBase(BaseModel):
keystone_token: str keystone_token: str = Field(..., description="Keystone token")
region: str region: str = Field(..., description="User region")
exp: int exp: int = Field(..., description="Token expiration time")
uuid: str uuid: str = Field(..., description="UUID")
class Payload(PayloadBase):
def toDict(self) -> Dict[str, Any]: def toDict(self) -> Dict[str, Any]:
return { return {
"keystone_token": self.keystone_token, "keystone_token": self.keystone_token,
@ -97,20 +86,16 @@ class Payload(BaseModel):
) )
class Profile(BaseModel): class Profile(PayloadBase):
keystone_token: str project: Project = Field(..., description="User project")
region: str user: User = Field(..., description="User")
project: Project roles: List[Role] = Field(..., description="User roles")
user: User keystone_token_exp: str = Field(..., description="Keystone token expiration time")
roles: List[Role] base_roles: Optional[List[str]] = Field(None, description="User base roles")
keystone_token_exp: str base_domains: Optional[List[str]] = Field(None, description="User base domains")
base_roles: Optional[List[str]] endpoints: Optional[Dict[str, Any]] = Field(None, description="Keystone endpoints")
base_domains: Optional[List[str]] projects: Optional[Dict[str, Any]] = Field(None, description="User projects")
endpoints: Optional[Dict[str, Any]] version: str = Field(..., description="Version")
projects: Optional[Dict[str, Any]]
exp: int
uuid: str
version: str
def toPayLoad(self) -> Payload: def toPayLoad(self) -> Payload:
return Payload( return Payload(

View File

@ -16,17 +16,17 @@ from __future__ import annotations
from typing import List from typing import List
from pydantic import BaseModel from pydantic import BaseModel, Field
class Policy(BaseModel): class Policy(BaseModel):
rule: str rule: str = Field(..., description="Policy rule")
allowed: bool allowed: bool = Field(..., description="Policy allowed")
class Policies(BaseModel): class Policies(BaseModel):
policies: List[Policy] policies: List[Policy] = Field(..., description="Policies list")
class PoliciesRules(BaseModel): class PoliciesRules(BaseModel):
rules: List[str] rules: List[str] = Field(..., description="Policies rules list")

View File

@ -17,7 +17,7 @@ from __future__ import annotations
from enum import Enum from enum import Enum
from typing import List, TypedDict from typing import List, TypedDict
from pydantic import BaseModel from pydantic import BaseModel, Field
class ScopeType(str, Enum): class ScopeType(str, Enum):
@ -27,7 +27,7 @@ class ScopeType(str, Enum):
class ScopeTypesSchema(BaseModel): class ScopeTypesSchema(BaseModel):
__root__: List[ScopeType] __root__: List[ScopeType] = Field(..., description="Scope types list")
class Method(str, Enum): class Method(str, Enum):
@ -45,12 +45,12 @@ class Operation(TypedDict):
class OperationSchema(BaseModel): class OperationSchema(BaseModel):
method: Method method: Method = Field(..., description="Operation method")
path: str path: str = Field(..., description="Operation path")
class OperationsSchema(BaseModel): class OperationsSchema(BaseModel):
__root__: List[OperationSchema] __root__: List[OperationSchema] = Field(..., description="Operations list")
__all__ = ("ScopeType", "ScopeTypesSchema", "Method", "Operation", "OperationsSchema") __all__ = ("ScopeTypesSchema", "Operation", "OperationsSchema")

View File

@ -2,40 +2,48 @@ from __future__ import annotations
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from pydantic import BaseModel from pydantic import BaseModel, Field
class PrometheusQueryResult(BaseModel): class PrometheusQueryResultBase(BaseModel):
metric: Dict[str, str] metric: Dict[str, str] = Field(..., description="Prometheus metric")
value: List[Any] value: List[Any] = Field(..., description="Prometheus metric value")
class PrometheusQueryData(BaseModel): class PrometheusQueryDataBase(BaseModel):
result: List[PrometheusQueryResult] resultType: str = Field(..., description="Prometheus result type")
resultType: str
class PrometheusQueryResponse(BaseModel): class PrometheusResponseBase(BaseModel):
status: str status: str = Field(..., description="Prometheus status")
data: Optional[PrometheusQueryData] errorType: Optional[str] = Field(None, description="Prometheus error type")
errorType: Optional[str] error: Optional[str] = Field(None, description="Prometheus error")
error: Optional[str] warnings: Optional[str] = Field(None, description="Prometheus warnings")
warnings: Optional[str]
class PrometheusQueryRangeResult(BaseModel): class PrometheusQueryResult(PrometheusQueryResultBase):
metric: Dict[str, str] """"""
values: List[Any]
class PrometheusQueryRangeData(BaseModel): class PrometheusQueryData(PrometheusQueryDataBase):
result: List[PrometheusQueryRangeResult] result: List[PrometheusQueryResult] = Field(..., description="Prometheus query result")
resultType: str
class PrometheusQueryRangeResponse(BaseModel): class PrometheusQueryResponse(PrometheusResponseBase):
status: str data: Optional[PrometheusQueryData] = Field(None, description="Prometheus query data")
data: Optional[PrometheusQueryRangeData]
errorType: Optional[str]
error: Optional[str] class PrometheusQueryRangeResult(PrometheusQueryResultBase):
warnings: Optional[str] """"""
class PrometheusQueryRangeData(PrometheusQueryDataBase):
result: List[PrometheusQueryRangeResult] = Field(
..., description="Prometheus query range result"
)
class PrometheusQueryRangeResponse(PrometheusResponseBase):
data: Optional[PrometheusQueryRangeData] = Field(
None, description="Prometheus query range data"
)

View File

@ -14,20 +14,22 @@
from typing import Any, List, Optional from typing import Any, List, Optional
from pydantic import BaseModel from pydantic import BaseModel, Field
class Setting(BaseModel): class SettingBase(BaseModel):
key: str key: str = Field(..., description="Key of setting")
value: Any value: Any = Field(..., description="Value of setting")
hidden: Optional[bool]
restart_service: bool
class UpdateSetting(BaseModel): class Setting(SettingBase):
key: str hidden: Optional[bool] = Field(None, description="Hidden setting or not")
value: Any restart_service: bool = Field(..., description="Restart service or not to apply setting")
class UpdateSetting(SettingBase):
""""""
class Settings(BaseModel): class Settings(BaseModel):
settings: List[Setting] settings: List[Setting] = Field(..., description="Settings")