Merge pull request #12415 from kofj/instance_handler

Instance handler.
This commit is contained in:
Steven Zou 2020-07-09 12:23:25 +08:00 committed by GitHub
commit 82bdce19d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 241 additions and 10 deletions

View File

@ -2,16 +2,16 @@ package handler
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/go-openapi/strfmt"
"github.com/goharbor/harbor/src/pkg/p2p/preheat/models/policy"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
preheatCtl "github.com/goharbor/harbor/src/controller/p2p/preheat"
projectCtl "github.com/goharbor/harbor/src/controller/project"
"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"
"github.com/goharbor/harbor/src/server/v2.0/models"
"github.com/goharbor/harbor/src/server/v2.0/restapi"
@ -38,24 +38,76 @@ func (api *preheatAPI) Prepare(ctx context.Context, operation string, params int
}
func (api *preheatAPI) CreateInstance(ctx context.Context, params operation.CreateInstanceParams) middleware.Responder {
var payload *models.InstanceCreatedResp
return operation.NewCreateInstanceCreated().WithPayload(payload)
instance, err := convertParamInstanceToModelInstance(params.Instance)
if err != nil {
return api.SendError(ctx, err)
}
_, err = api.preheatCtl.CreateInstance(ctx, instance)
if err != nil {
return api.SendError(ctx, err)
}
return operation.NewCreateInstanceCreated()
}
func (api *preheatAPI) DeleteInstance(ctx context.Context, params operation.DeleteInstanceParams) middleware.Responder {
var payload *models.InstanceDeletedResp
return operation.NewDeleteInstanceOK().WithPayload(payload)
instance, err := api.preheatCtl.GetInstanceByName(ctx, params.PreheatInstanceName)
if err != nil {
return api.SendError(ctx, err)
}
err = api.preheatCtl.DeleteInstance(ctx, instance.ID)
if err != nil {
return api.SendError(ctx, err)
}
return operation.NewDeleteInstanceOK()
}
func (api *preheatAPI) GetInstance(ctx context.Context, params operation.GetInstanceParams) middleware.Responder {
var payload *models.Instance
instance, err := api.preheatCtl.GetInstanceByName(ctx, params.PreheatInstanceName)
if err != nil {
return api.SendError(ctx, err)
}
payload, err = convertInstanceToPayload(instance)
if err != nil {
return api.SendError(ctx, err)
}
return operation.NewGetInstanceOK().WithPayload(payload)
}
// ListInstances is List p2p instances
func (api *preheatAPI) ListInstances(ctx context.Context, params operation.ListInstancesParams) middleware.Responder {
var payload []*models.Instance
return operation.NewListInstancesOK().WithPayload(payload)
query, err := api.BuildQuery(ctx, params.Q, params.Page, params.PageSize)
if err != nil {
return api.SendError(ctx, err)
}
total, err := api.preheatCtl.CountInstance(ctx, query)
if err != nil {
return api.SendError(ctx, err)
}
instances, err := api.preheatCtl.ListInstance(ctx, query)
if err != nil {
return api.SendError(ctx, err)
}
for _, instance := range instances {
ins, err := convertInstanceToPayload(instance)
if err != nil {
return api.SendError(ctx, err)
}
payload = append(payload, ins)
}
return operation.NewListInstancesOK().
WithPayload(payload).WithXTotalCount(total).
WithLink(api.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String())
}
func (api *preheatAPI) ListProviders(ctx context.Context, params operation.ListProvidersParams) middleware.Responder {
@ -155,7 +207,7 @@ func (api *preheatAPI) DeletePolicy(ctx context.Context, params operation.Delete
return api.SendError(ctx, err)
}
return operation.NewDeleteInstanceOK()
return operation.NewDeletePolicyOK()
}
// ListPolicies is List preheat policies
@ -235,3 +287,55 @@ func convertParamPolicyToModelPolicy(model *models.PreheatPolicy) (*policy.Schem
UpdatedTime: time.Time(model.UpdateTime),
}, nil
}
func convertInstanceToPayload(model *instanceModel.Instance) (*models.Instance, error) {
if model == nil {
return nil, errors.New("instance can not be nil")
}
var authInfo = map[string]string{}
var err = json.Unmarshal([]byte(model.AuthData), &authInfo)
if err != nil {
return nil, err
}
return &models.Instance{
AuthInfo: authInfo,
AuthMode: model.AuthMode,
Default: model.Default,
Description: model.Description,
Enabled: model.Enabled,
Endpoint: model.Endpoint,
ID: model.ID,
Insecure: model.Insecure,
Name: model.Name,
SetupTimestamp: model.SetupTimestamp,
Status: "Unknown",
Vendor: model.Vendor,
}, nil
}
func convertParamInstanceToModelInstance(model *models.Instance) (*instanceModel.Instance, error) {
if model == nil {
return nil, errors.New("instance can not be nil")
}
var authData, err = json.Marshal(model.AuthInfo)
if err != nil {
return nil, err
}
return &instanceModel.Instance{
AuthData: string(authData),
AuthMode: model.AuthMode,
Default: model.Default,
Description: model.Description,
Enabled: model.Enabled,
Endpoint: model.Endpoint,
ID: model.ID,
Insecure: model.Insecure,
Name: model.Name,
SetupTimestamp: model.SetupTimestamp,
Status: model.Status,
Vendor: model.Vendor,
}, nil
}

View File

@ -7,6 +7,7 @@ import (
"github.com/go-openapi/strfmt"
"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"
"github.com/goharbor/harbor/src/server/v2.0/models"
"github.com/stretchr/testify/assert"
@ -150,3 +151,129 @@ func Test_convertParamPolicyToModelPolicy(t *testing.T) {
})
}
}
func Test_convertInstanceToPayload(t *testing.T) {
tests := []struct {
name string
input *instanceModel.Instance
want *models.Instance
wantErr bool
}{
{
name: "want err",
input: nil,
want: nil,
wantErr: true,
},
{
name: "invalid authData",
input: nil,
want: nil,
wantErr: true,
},
{
name: "want ok",
input: &instanceModel.Instance{
ID: 1,
Name: "abc",
Description: "something",
Vendor: "kraken",
Enabled: true,
Endpoint: "https://example.com",
AuthMode: "none",
AuthData: `{"name":"harbor"}`,
Status: "",
Default: true,
Insecure: true,
SetupTimestamp: 1234,
},
want: &models.Instance{
ID: 1,
Name: "abc",
Description: "something",
Vendor: "kraken",
Enabled: true,
Endpoint: "https://example.com",
AuthMode: "none",
AuthInfo: map[string]string{"name": "harbor"},
Status: "Unknown",
Default: true,
Insecure: true,
SetupTimestamp: 1234,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := convertInstanceToPayload(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("convertInstanceToPayload() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("convertInstanceToPayload() = %v, want %v", got, tt.want)
}
})
}
}
func Test_convertParamInstanceToModelInstance(t *testing.T) {
tests := []struct {
name string
input *models.Instance
want *instanceModel.Instance
wantErr bool
}{
{
name: "want err",
input: nil,
want: nil,
wantErr: true,
},
{
name: "want ok",
input: &models.Instance{
ID: 1,
Name: "abc",
Description: "something",
Vendor: "kraken",
Enabled: true,
Endpoint: "https://example.com",
AuthMode: "none",
AuthInfo: map[string]string{"name": "harbor"},
Status: "Unknown",
Default: true,
Insecure: true,
SetupTimestamp: 1234,
},
want: &instanceModel.Instance{
ID: 1,
Name: "abc",
Description: "something",
Vendor: "kraken",
Enabled: true,
Endpoint: "https://example.com",
AuthMode: "none",
AuthData: `{"name":"harbor"}`,
Status: "Unknown",
Default: true,
Insecure: true,
SetupTimestamp: 1234,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := convertParamInstanceToModelInstance(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("convertParamInstanceToModelInstance() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("convertParamInstanceToModelInstance() = %v, want %v", got, tt.want)
}
})
}
}