From b2268dbf8e2fc023ce8f7a76fbad97e1915b7373 Mon Sep 17 00:00:00 2001 From: He Weiwei Date: Thu, 4 Nov 2021 23:39:36 +0800 Subject: [PATCH] fix: remove x-go-type in swagger.yaml (#15923) Closes #15912 Signed-off-by: He Weiwei --- api/v2.0/swagger.yaml | 136 +++++++++--------- src/lib/config/config.go | 4 - src/server/v2.0/handler/config.go | 9 +- src/server/v2.0/handler/model/quota.go | 4 +- .../v2.0/handler/model/resource_list.go | 41 ++++++ src/server/v2.0/handler/project.go | 4 +- src/server/v2.0/handler/quota.go | 10 +- src/server/v2.0/handler/quota_test.go | 13 +- 8 files changed, 137 insertions(+), 84 deletions(-) create mode 100644 src/server/v2.0/handler/model/resource_list.go diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index b8537db2e..b6f3a2604 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -2742,7 +2742,7 @@ paths: type: integer Link: description: Link to previous page and next page - type: string + type: string '401': $ref: '#/responses/401' '403': @@ -6773,10 +6773,6 @@ definitions: additionalProperties: type: integer format: int64 - x-go-type: - type: ResourceList - import: - package: "github.com/goharbor/harbor/src/pkg/quota/types" ReplicationExecution: type: object description: The replication execution @@ -7794,7 +7790,7 @@ definitions: description: The name of the user group group_type: type: integer - description: 'The group type, 1 for LDAP group, 2 for HTTP group.' + description: 'The group type, 1 for LDAP group, 2 for HTTP group.' SupportedWebhookEventTypes: type: object description: Supportted webhook event types and notify types. @@ -7927,10 +7923,18 @@ definitions: format: date-time InternalConfigurationsResponse: type: object - x-go-type: - type: InternalCfg - import: - package: "github.com/goharbor/harbor/src/lib/config" + additionalProperties: + $ref: '#/definitions/InternalConfigurationValue' + InternalConfigurationValue: + type: object + properties: + value: + type: object + description: The value of current config item + editable: + type: boolean + x-omitempty: false + description: The configure item can be updated or not ConfigurationsResponse: type: object properties: @@ -8105,280 +8109,280 @@ definitions: description: 'The parameters of the policy, the values are dependent on the type of the policy.' Configurations: type: object - properties: + properties: auth_mode: type: string - description: The auth mode of current system, such as "db_auth", "ldap_auth", "oidc_auth" + description: The auth mode of current system, such as "db_auth", "ldap_auth", "oidc_auth" x-omitempty: true x-isnullable: true email_from: type: string - description: The sender name for Email notification. + description: The sender name for Email notification. x-omitempty: true x-isnullable: true email_host: type: string - description: The hostname of SMTP server that sends Email notification. + description: The hostname of SMTP server that sends Email notification. x-omitempty: true x-isnullable: true email_identity: type: string - description: By default it's empty so the email_username is picked + description: By default it's empty so the email_username is picked x-omitempty: true x-isnullable: true email_insecure: type: boolean - description: Whether or not the certificate will be verified when Harbor tries to access the email server. + description: Whether or not the certificate will be verified when Harbor tries to access the email server. x-omitempty: true x-isnullable: true email_password: type: string - description: Email password + description: Email password x-omitempty: true x-isnullable: true email_port: type: integer - description: The port of SMTP server + description: The port of SMTP server x-omitempty: true x-isnullable: true email_ssl: type: boolean - description: When it''s set to true the system will access Email server via TLS by default. If it''s set to false, it still will handle "STARTTLS" from server side. + description: When it''s set to true the system will access Email server via TLS by default. If it''s set to false, it still will handle "STARTTLS" from server side. x-omitempty: true x-isnullable: true email_username: type: string - description: The username for authenticate against SMTP server + description: The username for authenticate against SMTP server x-omitempty: true x-isnullable: true ldap_base_dn: type: string - description: The Base DN for LDAP binding. + description: The Base DN for LDAP binding. x-omitempty: true x-isnullable: true ldap_filter: type: string - description: The filter for LDAP search + description: The filter for LDAP search x-omitempty: true x-isnullable: true ldap_group_base_dn: type: string - description: The base DN to search LDAP group. + description: The base DN to search LDAP group. x-omitempty: true x-isnullable: true ldap_group_admin_dn: type: string - description: Specify the ldap group which have the same privilege with Harbor admin + description: Specify the ldap group which have the same privilege with Harbor admin x-omitempty: true x-isnullable: true ldap_group_attribute_name: type: string - description: The attribute which is used as identity of the LDAP group, default is cn.' + description: The attribute which is used as identity of the LDAP group, default is cn.' x-omitempty: true x-isnullable: true ldap_group_search_filter: type: string - description: The filter to search the ldap group + description: The filter to search the ldap group x-omitempty: true x-isnullable: true ldap_group_search_scope: type: integer - description: The scope to search ldap group. ''0-LDAP_SCOPE_BASE, 1-LDAP_SCOPE_ONELEVEL, 2-LDAP_SCOPE_SUBTREE'' + description: The scope to search ldap group. ''0-LDAP_SCOPE_BASE, 1-LDAP_SCOPE_ONELEVEL, 2-LDAP_SCOPE_SUBTREE'' x-omitempty: true x-isnullable: true ldap_scope: type: integer - description: The scope to search ldap users,'0-LDAP_SCOPE_BASE, 1-LDAP_SCOPE_ONELEVEL, 2-LDAP_SCOPE_SUBTREE' + description: The scope to search ldap users,'0-LDAP_SCOPE_BASE, 1-LDAP_SCOPE_ONELEVEL, 2-LDAP_SCOPE_SUBTREE' x-omitempty: true x-isnullable: true ldap_search_dn: type: string - description: The DN of the user to do the search. + description: The DN of the user to do the search. x-omitempty: true x-isnullable: true ldap_search_password: type: string - description: The password of the ldap search dn + description: The password of the ldap search dn x-omitempty: true x-isnullable: true ldap_timeout: type: integer - description: Timeout in seconds for connection to LDAP server + description: Timeout in seconds for connection to LDAP server x-omitempty: true x-isnullable: true ldap_uid: type: string - description: The attribute which is used as identity for the LDAP binding, such as "CN" or "SAMAccountname" + description: The attribute which is used as identity for the LDAP binding, such as "CN" or "SAMAccountname" x-omitempty: true x-isnullable: true ldap_url: type: string - description: The URL of LDAP server + description: The URL of LDAP server x-omitempty: true x-isnullable: true ldap_verify_cert: type: boolean - description: Whether verify your OIDC server certificate, disable it if your OIDC server is hosted via self-hosted certificate. + description: Whether verify your OIDC server certificate, disable it if your OIDC server is hosted via self-hosted certificate. x-omitempty: true x-isnullable: true ldap_group_membership_attribute: type: string - description: The user attribute to identify the group membership + description: The user attribute to identify the group membership x-omitempty: true x-isnullable: true project_creation_restriction: type: string - description: Indicate who can create projects, it could be ''adminonly'' or ''everyone''. + description: Indicate who can create projects, it could be ''adminonly'' or ''everyone''. x-omitempty: true x-isnullable: true read_only: type: boolean - description: The flag to indicate whether Harbor is in readonly mode. + description: The flag to indicate whether Harbor is in readonly mode. x-omitempty: true x-isnullable: true self_registration: type: boolean - description: Whether the Harbor instance supports self-registration. If it''s set to false, admin need to add user to the instance. + description: Whether the Harbor instance supports self-registration. If it''s set to false, admin need to add user to the instance. x-omitempty: true x-isnullable: true token_expiration: type: integer - description: The expiration time of the token for internal Registry, in minutes. + description: The expiration time of the token for internal Registry, in minutes. x-omitempty: true x-isnullable: true uaa_client_id: type: string - description: The client id of UAA + description: The client id of UAA x-omitempty: true x-isnullable: true uaa_client_secret: type: string - description: The client secret of the UAA + description: The client secret of the UAA x-omitempty: true x-isnullable: true uaa_endpoint: type: string - description: The endpoint of the UAA + description: The endpoint of the UAA x-omitempty: true x-isnullable: true uaa_verify_cert: type: boolean - description: Verify the certificate in UAA server + description: Verify the certificate in UAA server x-omitempty: true x-isnullable: true http_authproxy_endpoint: type: string - description: The endpoint of the HTTP auth + description: The endpoint of the HTTP auth x-omitempty: true x-isnullable: true http_authproxy_tokenreview_endpoint: type: string - description: The token review endpoint + description: The token review endpoint x-omitempty: true x-isnullable: true http_authproxy_admin_groups: type: string - description: The group which has the harbor admin privileges + description: The group which has the harbor admin privileges x-omitempty: true x-isnullable: true http_authproxy_admin_usernames: type: string - description: The username which has the harbor admin privileges + description: The username which has the harbor admin privileges x-omitempty: true x-isnullable: true http_authproxy_verify_cert: type: boolean - description: Verify the HTTP auth provider's certificate + description: Verify the HTTP auth provider's certificate x-omitempty: true x-isnullable: true http_authproxy_skip_search: type: boolean - description: Search user before onboard + description: Search user before onboard x-omitempty: true x-isnullable: true http_authproxy_server_certificate: type: string - description: The certificate of the HTTP auth provider + description: The certificate of the HTTP auth provider x-omitempty: true x-isnullable: true oidc_name: type: string - description: The OIDC provider name + description: The OIDC provider name x-omitempty: true x-isnullable: true oidc_endpoint: type: string - description: The endpoint of the OIDC provider + description: The endpoint of the OIDC provider x-omitempty: true x-isnullable: true oidc_client_id: type: string - description: The client ID of the OIDC provider + description: The client ID of the OIDC provider x-omitempty: true x-isnullable: true oidc_client_secret: type: string - description: The OIDC provider secret + description: The OIDC provider secret x-omitempty: true x-isnullable: true oidc_groups_claim: type: string - description: The attribute claims the group name + description: The attribute claims the group name x-omitempty: true x-isnullable: true oidc_admin_group: type: string - description: The OIDC group which has the harbor admin privileges + description: The OIDC group which has the harbor admin privileges x-omitempty: true x-isnullable: true oidc_scope: type: string - description: The scope of the OIDC provider + description: The scope of the OIDC provider x-omitempty: true x-isnullable: true oidc_user_claim: type: string - description: The attribute claims the username + description: The attribute claims the username x-omitempty: true x-isnullable: true oidc_verify_cert: type: boolean - description: Verify the OIDC provider's certificate' + description: Verify the OIDC provider's certificate' x-omitempty: true x-isnullable: true oidc_auto_onboard: type: boolean - description: Auto onboard the OIDC user + description: Auto onboard the OIDC user x-omitempty: true x-isnullable: true oidc_extra_redirect_parms: type: string - description: Extra parameters to add when redirect request to OIDC provider + description: Extra parameters to add when redirect request to OIDC provider x-omitempty: true x-isnullable: true robot_token_duration: type: integer - description: The robot account token duration in days + description: The robot account token duration in days x-omitempty: true x-isnullable: true robot_name_prefix: type: string - description: The rebot account name prefix + description: The rebot account name prefix x-omitempty: true x-isnullable: true notification_enable: type: boolean - description: Enable notification + description: Enable notification x-omitempty: true x-isnullable: true quota_per_project_enable: type: boolean - description: Enable quota per project + description: Enable quota per project x-omitempty: true x-isnullable: true storage_per_project: type: integer - description: The storage quota per project + description: The storage quota per project x-omitempty: true x-isnullable: true StringConfigItem: diff --git a/src/lib/config/config.go b/src/lib/config/config.go index 2b4203914..cb4a51997 100644 --- a/src/lib/config/config.go +++ b/src/lib/config/config.go @@ -22,7 +22,6 @@ import ( "github.com/goharbor/harbor/src/common" comModels "github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/lib/config/metadata" - "github.com/goharbor/harbor/src/lib/config/models" "github.com/goharbor/harbor/src/lib/encrypt" "github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/lib/orm" @@ -43,9 +42,6 @@ var ( managers = make(map[string]Manager) ) -// InternalCfg internal configure response model -type InternalCfg map[string]*models.Value - // Manager defines the operation for config type Manager interface { Load(ctx context.Context) error diff --git a/src/server/v2.0/handler/config.go b/src/server/v2.0/handler/config.go index cda25fc3b..09c7f04c7 100644 --- a/src/server/v2.0/handler/config.go +++ b/src/server/v2.0/handler/config.go @@ -17,6 +17,7 @@ package handler import ( "context" "encoding/json" + "github.com/go-openapi/runtime/middleware" "github.com/goharbor/harbor/src/common/rbac" "github.com/goharbor/harbor/src/common/security" @@ -111,7 +112,13 @@ func (c *configAPI) GetInternalconfig(ctx context.Context, params configure.GetI if err != nil { return c.SendError(ctx, err) } - return configure.NewGetInternalconfigOK().WithPayload(resultCfg) + + payload := make(models.InternalConfigurationsResponse, len(resultCfg)) + for key, cfg := range resultCfg { + payload[key] = models.InternalConfigurationValue{Value: cfg.Val, Editable: cfg.Editable} + } + + return configure.NewGetInternalconfigOK().WithPayload(payload) } func toResponseModel(cfg map[string]*cfgModels.Value) (*models.ConfigurationsResponse, error) { diff --git a/src/server/v2.0/handler/model/quota.go b/src/server/v2.0/handler/model/quota.go index db1ebcc59..01d955632 100644 --- a/src/server/v2.0/handler/model/quota.go +++ b/src/server/v2.0/handler/model/quota.go @@ -54,8 +54,8 @@ func (q *Quota) ToSwagger(ctx context.Context) *models.Quota { return &models.Quota{ ID: q.ID, Ref: q.Ref, - Hard: hard, - Used: used, + Hard: NewResourceList(hard).ToSwagger(), + Used: NewResourceList(used).ToSwagger(), CreationTime: strfmt.DateTime(q.CreationTime), UpdateTime: strfmt.DateTime(q.UpdateTime), } diff --git a/src/server/v2.0/handler/model/resource_list.go b/src/server/v2.0/handler/model/resource_list.go new file mode 100644 index 000000000..09a9c63e6 --- /dev/null +++ b/src/server/v2.0/handler/model/resource_list.go @@ -0,0 +1,41 @@ +// Copyright Project Harbor Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package model + +import ( + "github.com/goharbor/harbor/src/pkg/quota/types" + "github.com/goharbor/harbor/src/server/v2.0/models" +) + +// ResourceList model +type ResourceList struct { + types.ResourceList +} + +// ToSwagger converts the resource list to the swagger model +func (rl *ResourceList) ToSwagger() models.ResourceList { + result := make(map[string]int64, len(rl.ResourceList)) + + for name, value := range rl.ResourceList { + result[string(name)] = value + } + + return result +} + +// NewResourceList new resource list instance +func NewResourceList(rl types.ResourceList) *ResourceList { + return &ResourceList{ResourceList: rl} +} diff --git a/src/server/v2.0/handler/project.go b/src/server/v2.0/handler/project.go index 43b29afb7..86ba1a037 100644 --- a/src/server/v2.0/handler/project.go +++ b/src/server/v2.0/handler/project.go @@ -748,10 +748,10 @@ func getProjectQuotaSummary(ctx context.Context, p *project.Project, summary *mo summary.Quota = &models.ProjectSummaryQuota{} if hard, err := q.GetHard(); err == nil { - summary.Quota.Hard = hard + summary.Quota.Hard = model.NewResourceList(hard).ToSwagger() } if used, err := q.GetUsed(); err == nil { - summary.Quota.Used = used + summary.Quota.Used = model.NewResourceList(used).ToSwagger() } } diff --git a/src/server/v2.0/handler/quota.go b/src/server/v2.0/handler/quota.go index f6bd14fcc..a26524fae 100644 --- a/src/server/v2.0/handler/quota.go +++ b/src/server/v2.0/handler/quota.go @@ -23,6 +23,7 @@ import ( "github.com/goharbor/harbor/src/lib" "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/q" + "github.com/goharbor/harbor/src/pkg/quota/types" "github.com/goharbor/harbor/src/server/v2.0/handler/model" "github.com/goharbor/harbor/src/server/v2.0/models" operation "github.com/goharbor/harbor/src/server/v2.0/restapi/operations/quota" @@ -102,11 +103,16 @@ func (qa *quotaAPI) UpdateQuota(ctx context.Context, params operation.UpdateQuot return qa.SendError(ctx, err) } - if err := quota.Validate(ctx, q.Reference, params.Hard.Hard); err != nil { + hard := make(types.ResourceList, len(params.Hard.Hard)) + for name, value := range params.Hard.Hard { + hard[types.ResourceName(name)] = value + } + + if err := quota.Validate(ctx, q.Reference, hard); err != nil { return qa.SendError(ctx, errors.BadRequestError(nil).WithMessage(err.Error())) } - q.SetHard(params.Hard.Hard) + q.SetHard(hard) if err := qa.quotaCtl.Update(ctx, q); err != nil { return qa.SendError(ctx, err) diff --git a/src/server/v2.0/handler/quota_test.go b/src/server/v2.0/handler/quota_test.go index 13b1d4bf9..6bba508ad 100644 --- a/src/server/v2.0/handler/quota_test.go +++ b/src/server/v2.0/handler/quota_test.go @@ -25,7 +25,6 @@ import ( "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/pkg/quota" - "github.com/goharbor/harbor/src/pkg/quota/types" "github.com/goharbor/harbor/src/server/v2.0/models" "github.com/goharbor/harbor/src/server/v2.0/restapi" quotatesting "github.com/goharbor/harbor/src/testing/controller/quota" @@ -75,7 +74,7 @@ func (suite *QuotaTestSuite) TestAuthorization() { } quota := models.QuotaUpdateReq{ - Hard: types.ResourceList{"storage": 1000}, + Hard: models.ResourceList{"storage": 1000}, } reqs := []struct { @@ -214,7 +213,7 @@ func (suite *QuotaTestSuite) TestUpdateQuota() { { // update quota with empty hard quota := models.QuotaUpdateReq{ - Hard: types.ResourceList{}, + Hard: models.ResourceList{}, } res, err := suite.PutJSON("/quotas/1", quota) @@ -227,7 +226,7 @@ func (suite *QuotaTestSuite) TestUpdateQuota() { mock.OnAnything(suite.quotaCtl, "Get").Return(nil, errors.NotFoundError(nil)).Once() quota := models.QuotaUpdateReq{ - Hard: types.ResourceList{"storage": 1000}, + Hard: models.ResourceList{"storage": 1000}, } res, err := suite.PutJSON("/quotas/1", quota) @@ -241,7 +240,7 @@ func (suite *QuotaTestSuite) TestUpdateQuota() { mock.OnAnything(suite.quotaCtl, "Update").Return(nil).Once() quota := models.QuotaUpdateReq{ - Hard: types.ResourceList{"storage": 1000}, + Hard: models.ResourceList{"storage": 1000}, } res, err := suite.PutJSON("/quotas/1", quota) @@ -255,7 +254,7 @@ func (suite *QuotaTestSuite) TestUpdateQuota() { mock.OnAnything(suite.quotaCtl, "Update").Return(fmt.Errorf("failed to update the quota")).Once() quota := models.QuotaUpdateReq{ - Hard: types.ResourceList{"storage": 1000}, + Hard: models.ResourceList{"storage": 1000}, } res, err := suite.PutJSON("/quotas/1", quota) @@ -269,7 +268,7 @@ func (suite *QuotaTestSuite) TestUpdateQuota() { mock.OnAnything(suite.quotaCtl, "Update").Return(nil).Once() quota := models.QuotaUpdateReq{ - Hard: types.ResourceList{"size": 1000}, + Hard: models.ResourceList{"size": 1000}, } res, err := suite.PutJSON("/quotas/1", quota)