1 Star 0 Fork 0

nanjo_fan / goharbor-client

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
robot.go 7.62 KB
一键复制 编辑 原始数据 按行查看 历史
王帆 提交于 2023-02-13 09:15 . 修改依赖
package robot
import (
"context"
"github.com/go-openapi/runtime"
v2client "gitee.com/nanjo-fan/goharbor-client/internal/api/client"
"gitee.com/nanjo-fan/goharbor-client/internal/api/client/robot"
"gitee.com/nanjo-fan/goharbor-client/model"
"gitee.com/nanjo-fan/goharbor-client/pkg/config"
"gitee.com/nanjo-fan/goharbor-client/pkg/errors"
)
const (
// Definitions in this block may be used to interact with the package methods.
// LevelProject defines a project-wide access level for a robot account.
LevelProject Level = "project"
// LevelSystem defines a system-wide access level for a robot account.
LevelSystem Level = "system"
ResourceRepository AccessResource = "repository"
ResourceArtifact AccessResource = "artifact"
ResourceHelmChart AccessResource = "helm-chart"
ResourceHelmChartVersion AccessResource = "helm-chart-version"
ResourceTag AccessResource = "tag"
ResourceArtifactLabel AccessResource = "artifact-label"
ResourceScan AccessResource = "scan"
ActionPush AccessAction = "push"
ActionPull AccessAction = "pull"
ActionCreate AccessAction = "create"
ActionDelete AccessAction = "delete"
ActionRead AccessAction = "read"
)
// RESTClient is a subclient for handling project related actions.
type RESTClient struct {
// Options contains optional configuration when making API calls.
Options *config.Options
// The new client of the harbor v2 API
V2Client *v2client.Harbor
// AuthInfo contains the auth information that is provided on API calls.
AuthInfo runtime.ClientAuthInfoWriter
}
func NewClient(v2Client *v2client.Harbor, opts *config.Options, authInfo runtime.ClientAuthInfoWriter) *RESTClient {
return &RESTClient{
Options: opts,
V2Client: v2Client,
AuthInfo: authInfo,
}
}
type Client interface {
ListRobotAccounts(ctx context.Context) ([]*model.Robot, error)
GetRobotAccountByName(ctx context.Context, name string) (*model.Robot, error)
GetRobotAccountByID(ctx context.Context, id int64) (*model.Robot, error)
NewRobotAccount(ctx context.Context, r *model.RobotCreate) (*model.RobotCreated, error)
DeleteRobotAccountByName(ctx context.Context, name string) error
DeleteRobotAccountByID(ctx context.Context, id int64) error
UpdateRobotAccount(ctx context.Context, r *model.Robot) error
RefreshRobotAccountSecretByID(ctx context.Context, id int64, sec string) (*model.RobotSec, error)
RefreshRobotAccountSecretByName(ctx context.Context, name string, sec string) (*model.RobotSec, error)
}
type Level string
func (in Level) String() string {
return string(in)
}
type AccessResource string
func (in AccessResource) String() string {
return string(in)
}
type AccessAction string
func (in AccessAction) String() string {
return string(in)
}
// ListRobotAccounts ListProjectRobots returns a list of all robot accounts.
func (c *RESTClient) ListRobotAccounts(ctx context.Context) ([]*model.Robot, error) {
var robotAccounts []*model.Robot
page := c.Options.Page
params := &robot.ListRobotParams{
Page: &page,
PageSize: &c.Options.PageSize,
Q: &c.Options.Query,
Sort: &c.Options.Sort,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
for {
resp, err := c.V2Client.Robot.ListRobot(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRobotErrors(err)
}
if len(resp.Payload) == 0 {
break
}
totalCount := resp.XTotalCount
robotAccounts = append(robotAccounts, resp.Payload...)
if int64(len(robotAccounts)) >= totalCount {
break
}
page++
}
return robotAccounts, nil
}
// GetRobotAccountByName GetRobotByName lists all existing robot accounts and returns the one matching the provided name.
// Note that the generic 'robot$'-prefix of the robot name is implicitly used for getting the resource.
func (c *RESTClient) GetRobotAccountByName(ctx context.Context, name string) (*model.Robot, error) {
robots, err := c.ListRobotAccounts(ctx)
if err != nil {
return nil, err
}
for _, r := range robots {
if r.Name == "robot$"+name {
return r, nil
}
}
return nil, &errors.ErrRobotAccountUnknownResource{}
}
// GetRobotAccountByID GetRobotByID returns a robot account identified by its 'id'.
func (c *RESTClient) GetRobotAccountByID(ctx context.Context, id int64) (*model.Robot, error) {
params := &robot.GetRobotByIDParams{
RobotID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Robot.GetRobotByID(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRobotErrors(err)
}
return resp.Payload, nil
}
// NewRobotAccount creates a new robot account from the specification of 'r' and returns a 'RobotCreated' response.
func (c *RESTClient) NewRobotAccount(ctx context.Context, r *model.RobotCreate) (*model.RobotCreated, error) {
params := &robot.CreateRobotParams{
Robot: r,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Robot.CreateRobot(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRobotErrors(err)
}
return resp.Payload, nil
}
// DeleteRobotAccountByName deletes a robot account identified by its 'name'.
// Note that the generic 'robot$'-prefix of the robot name is implicitly used for deletion.
func (c *RESTClient) DeleteRobotAccountByName(ctx context.Context, name string) error {
robots, err := c.ListRobotAccounts(ctx)
if err != nil {
return err
}
for _, r := range robots {
if r.Name == "robot$"+name {
return c.DeleteRobotAccountByID(ctx, r.ID)
}
}
return &errors.ErrRobotAccountUnknownResource{}
}
// DeleteRobotAccountByID DeleteProjectRobotByID deletes a robot account identified by its id.
func (c *RESTClient) DeleteRobotAccountByID(ctx context.Context, id int64) error {
params := &robot.DeleteRobotParams{
RobotID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Robot.DeleteRobot(params, c.AuthInfo)
if err != nil {
return handleSwaggerRobotErrors(err)
}
return nil
}
// UpdateRobotAccount updates the robot account 'r' with the provided specification.
// Note that model.Robot.Name & model.Robot.Level are immutable by API definitions.
// NOTE: Updating existing system-level robot accounts with wildcard access to all projects
// will fail until https://github.com/goharbor/harbor/pull/17117 is merged.
func (c *RESTClient) UpdateRobotAccount(ctx context.Context, r *model.Robot) error {
params := &robot.UpdateRobotParams{
Robot: r,
RobotID: r.ID,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
_, err := c.V2Client.Robot.UpdateRobot(params, c.AuthInfo)
if err != nil {
return handleSwaggerRobotErrors(err)
}
return nil
}
// RefreshRobotAccountSecretByID updates the robot account secret with the provided string "sec", by its id and return a 'RobotSec' response.
func (c *RESTClient) RefreshRobotAccountSecretByID(ctx context.Context, id int64, sec string) (*model.RobotSec, error) {
r := &model.RobotSec{Secret: sec}
params := &robot.RefreshSecParams{
RobotSec: r,
RobotID: id,
Context: ctx,
}
params.WithTimeout(c.Options.Timeout)
resp, err := c.V2Client.Robot.RefreshSec(params, c.AuthInfo)
if err != nil {
return nil, handleSwaggerRobotErrors(err)
}
return resp.Payload, nil
}
// RefreshRobotAccountSecretByName updates the robot account secret with the provided string "sec", by its name and return a 'RobotSec' response.
func (c *RESTClient) RefreshRobotAccountSecretByName(ctx context.Context, name string, sec string) (*model.RobotSec, error) {
robots, err := c.ListRobotAccounts(ctx)
if err != nil {
return nil, err
}
for _, r := range robots {
if r.Name == "robot$"+name {
return c.RefreshRobotAccountSecretByID(ctx, r.ID, sec)
}
}
return nil, &errors.ErrRobotAccountUnknownResource{}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/nanjo-fan/goharbor-client.git
git@gitee.com:nanjo-fan/goharbor-client.git
nanjo-fan
goharbor-client
goharbor-client
v1.0.3

搜索帮助

344bd9b3 5694891 D2dac590 5694891