From 080afbfe1bad287c17fb7fc4e6cb1ef6d2cc5625 Mon Sep 17 00:00:00 2001 From: fanjiankong Date: Wed, 8 Jul 2020 23:58:20 +0800 Subject: [PATCH] Add preheat APIs, handlers. 1. Manual preheat. 2. Instance health check. Signed-off-by: fanjiankong --- api/v2.0/swagger.yaml | 56 ++++++++++++++++++++++++++++ src/server/v2.0/handler/preheat.go | 59 ++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index 5c5078df0..aedfaec7d 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -667,6 +667,33 @@ paths: $ref: '#/responses/404' '500': $ref: '#/responses/500' + /p2p/preheat/instances/ping: + post: + summary: Ping status of a instance. + description: | + This endpoint checks status of a instance, the instance can be given by ID or Endpoint URL (together with credential) + operationId: PingInstances + parameters: + - $ref: '#/parameters/requestId' + - name: instance + in: body + description: The JSON object of instance. + required: true + schema: + $ref: '#/definitions/Instance' + tags: + - preheat + responses: + '200': + $ref: '#/responses/200' + '400': + $ref: '#/responses/400' + '401': + $ref: '#/responses/401' + '404': + description: Instance not found (when instance is provided by ID). + '500': + $ref: '#/responses/500' /p2p/preheat/instances: get: summary: List P2P provider instances @@ -934,6 +961,35 @@ paths: $ref: '#/responses/409' '500': $ref: '#/responses/500' + post: + summary: Manual preheat + description: Manual preheat + tags: + - preheat + operationId: ManualPreheat + parameters: + - $ref: '#/parameters/requestId' + - $ref: '#/parameters/projectName' + - $ref: '#/parameters/preheatPolicyName' + - name: policy + in: body + description: The policy schema info + required: true + schema: + $ref: '#/definitions/PreheatPolicy' + responses: + '201': + $ref: '#/responses/201' + '400': + $ref: '#/responses/400' + '401': + $ref: '#/responses/401' + '404': + $ref: '#/responses/404' + '403': + $ref: '#/responses/403' + '500': + $ref: '#/responses/500' delete: summary: Delete a preheat policy description: Delete a preheat policy diff --git a/src/server/v2.0/handler/preheat.go b/src/server/v2.0/handler/preheat.go index 8dd2c785a..592bfa537 100644 --- a/src/server/v2.0/handler/preheat.go +++ b/src/server/v2.0/handler/preheat.go @@ -10,6 +10,7 @@ import ( "github.com/go-openapi/strfmt" preheatCtl "github.com/goharbor/harbor/src/controller/p2p/preheat" projectCtl "github.com/goharbor/harbor/src/controller/project" + liberrors "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/policy" instanceModel "github.com/goharbor/harbor/src/pkg/p2p/preheat/models/provider" "github.com/goharbor/harbor/src/pkg/p2p/preheat/provider" @@ -22,6 +23,7 @@ func newPreheatAPI() *preheatAPI { return &preheatAPI{ preheatCtl: preheatCtl.Ctl, projectCtl: projectCtl.Ctl, + enforcer: preheatCtl.Enf, } } @@ -31,6 +33,7 @@ type preheatAPI struct { BaseAPI preheatCtl preheatCtl.Controller projectCtl projectCtl.Controller + enforcer preheatCtl.Enforcer } func (api *preheatAPI) Prepare(ctx context.Context, operation string, params interface{}) middleware.Responder { @@ -248,6 +251,62 @@ func (api *preheatAPI) ListPolicies(ctx context.Context, params operation.ListPo WithLink(api.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String()) } +// ManualPreheat is manual preheat +func (api *preheatAPI) ManualPreheat(ctx context.Context, params operation.ManualPreheatParams) middleware.Responder { + project, err := api.projectCtl.GetByName(ctx, params.ProjectName) + if err != nil { + return api.SendError(ctx, err) + } + + policy, err := api.preheatCtl.GetPolicyByName(ctx, project.ProjectID, params.PreheatPolicyName) + if err != nil { + return api.SendError(ctx, err) + } + + _, err = api.enforcer.EnforcePolicy(ctx, policy.ID) + if err != nil { + return api.SendError(ctx, err) + } + + // TODO: build execution URL + var location = "" + + return operation.NewManualPreheatCreated().WithLocation(location) +} + +func (api *preheatAPI) PingInstances(ctx context.Context, params operation.PingInstancesParams) middleware.Responder { + var instance *instanceModel.Instance + var err error + + if params.Instance.ID > 0 { + // by ID + instance, err = api.preheatCtl.GetInstance(ctx, params.Instance.ID) + if liberrors.IsNotFoundErr(err) { + return operation.NewPingInstancesNotFound() + } + if err != nil { + api.SendError(ctx, err) + } + } else { + // by endpoint URL + if params.Instance.Endpoint == "" { + return operation.NewPingInstancesBadRequest() + } + + instance, err = convertParamInstanceToModelInstance(params.Instance) + if err != nil { + api.SendError(ctx, err) + } + } + + err = api.preheatCtl.CheckHealth(ctx, instance) + if err != nil { + api.SendError(ctx, err) + } + + return operation.NewPingInstancesOK() +} + // convertPolicyToPayload converts model policy to swagger model func convertPolicyToPayload(policy *policy.Schema) (*models.PreheatPolicy, error) { if policy == nil {