Skip to content

Commit

Permalink
add: update project robot account cmd
Browse files Browse the repository at this point in the history
Signed-off-by: bupd <[email protected]>
  • Loading branch information
bupd committed Jun 4, 2024
1 parent 200582b commit 53bbbb5
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 25 deletions.
7 changes: 4 additions & 3 deletions cmd/harbor/root/project/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ func Robot() *cobra.Command {
}
cmd.AddCommand(
robot.ListRobotCommand(),
robot.DeleteCommand(),
robot.ViewCommand(),
robot.CreateRobot(),
robot.DeleteRobotCommand(),
robot.ViewRobotCommand(),
robot.CreateRobotCommand(),
robot.UpdateRobotCommand(),
)

return cmd
Expand Down
4 changes: 2 additions & 2 deletions cmd/harbor/root/project/robot/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/spf13/viper"
)

// CreateProjectCommand creates a new `harbor create project` command
func CreateRobot() *cobra.Command {
// to-do add json file as input and getting json file as output from input.
func CreateRobotCommand() *cobra.Command {
var (
opts create.CreateView
projectName string
Expand Down
6 changes: 3 additions & 3 deletions cmd/harbor/root/project/robot/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/spf13/cobra"
)

// NewGetRegistryCommand creates a new `harbor get registry` command
func DeleteCommand() *cobra.Command {
// to-do improve DeleteRobotCommand and multi delete
func DeleteRobotCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [robotID]",
Short: "delete robot by id",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
robotID, err := strconv.ParseInt(args[0], 10, 64)
Expand Down
8 changes: 4 additions & 4 deletions cmd/harbor/root/project/robot/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"github.com/spf13/viper"
)

// ListRobotCommand creates a new `harbor robot list` command
// ListRobotCommand creates a new `harbor project robot list` command
func ListRobotCommand() *cobra.Command {
var (
query string
opts api.ListFlags
query string
opts api.ListFlags
)

projectQString := constants.ProjectQString
Expand All @@ -29,7 +29,7 @@ func ListRobotCommand() *cobra.Command {
if len(args) > 0 {
opts.Q = projectQString + args[0]
} else {
projectID := prompt.GetProjectIDFromUser()
projectID := prompt.GetProjectIDFromUser()
opts.Q = projectQString + strconv.FormatInt(projectID, 10)
}

Expand Down
117 changes: 117 additions & 0 deletions cmd/harbor/root/project/robot/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package robot

import (
"strconv"

"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/prompt"
"github.com/goharbor/harbor-cli/pkg/views/robot/update"
log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
)

// to-do complete UpdateRobotCommand
func UpdateRobotCommand() *cobra.Command {
var (
robotID int64
opts update.UpdateView
all bool
)

cmd := &cobra.Command{
Use: "update [robotID]",
Short: "update robot by id",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) == 1 {
robotID, err = strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.Errorf("failed to parse robot ID: %v", err)
}

} else {
projectID := prompt.GetProjectIDFromUser()
robotID = prompt.GetRobotIDFromUser(projectID)
}

robot, err := api.GetRobot(robotID)
bot := robot.Payload

opts = update.UpdateView{
CreationTime: bot.CreationTime,
Description: bot.Description,
Disable: bot.Disable,
Duration: bot.Duration,
Editable: bot.Editable,
ID: bot.ID,
Level: bot.Level,
Name: bot.Name,
Secret: bot.Secret,
}

// declare empty permissions to hold permissions
permissions := []models.Permission{}

if all {
perms, _ := api.GetPermissions()
permission := perms.Payload.Project

choices := []models.Permission{}
for _, perm := range permission {
choices = append(choices, *perm)
}
permissions = choices
} else {
permissions = prompt.GetRobotPermissionsFromUser()
}

// []Permission to []*Access
var accesses []*models.Access
for _, perm := range permissions {
access := &models.Access{
Action: perm.Action,
Resource: perm.Resource,
}
accesses = append(accesses, access)
}
// convert []models.permission to []*model.Access
perm := &update.RobotPermission{
Kind: bot.Permissions[0].Kind,
Namespace: bot.Permissions[0].Namespace,
Access: accesses,
}
opts.Permissions = []*update.RobotPermission{perm}

err = updateRobotView(&opts)
if err != nil {
log.Errorf("failed to Update robot")
}
},
}

flags := cmd.Flags()
flags.BoolVarP(
&all,
"all-permission",
"a",
false,
"Select all permissions for the robot account",
)
flags.StringVarP(&opts.Name, "name", "", "", "name of the robot account")
flags.StringVarP(&opts.Description, "description", "", "", "description of the robot account")
flags.Int64VarP(&opts.Duration, "duration", "", 0, "set expiration of robot account in days")

return cmd
}

func updateRobotView(updateView *update.UpdateView) error {
if updateView == nil {
updateView = &update.UpdateView{}
}

update.UpdateRobotView(updateView)
return api.UpdateRobot(updateView)
}
6 changes: 4 additions & 2 deletions cmd/harbor/root/project/robot/view.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package robot

import (
"os"
"strconv"

"github.com/goharbor/go-client/pkg/sdk/v2.0/client/robot"
Expand All @@ -12,8 +13,8 @@ import (
"github.com/spf13/cobra"
)

// ViewCommand creates a new `harbor project robot view` command
func ViewCommand() *cobra.Command {
// handle robot view with interactive like in list command.
func ViewRobotCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "view [robotID]",
Short: "get robot by id",
Expand All @@ -29,6 +30,7 @@ func ViewCommand() *cobra.Command {
robot, err = api.GetRobot(robotID)
if err != nil {
log.Errorf("failed to List robots")
os.Exit(1)
}
}
robots := []*models.Robot{robot.Payload}
Expand Down
56 changes: 53 additions & 3 deletions pkg/api/robot_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/robot/create"
"github.com/goharbor/harbor-cli/pkg/views/robot/update"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func DeleteRobot(robotID int64) error {
return nil
}

func CreateRobot(opts create.CreateView) (*robot.CreateRobotCreated ,error) {
func CreateRobot(opts create.CreateView) (*robot.CreateRobotCreated, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
Expand All @@ -79,7 +80,7 @@ func CreateRobot(opts create.CreateView) (*robot.CreateRobotCreated ,error) {
}
convertedPerms = append(convertedPerms, convertedPerm)
}
response, err := client.Robot.CreateRobot(
response, err := client.Robot.CreateRobot(
ctx,
&robot.CreateRobotParams{
Robot: &models.RobotCreate{
Expand All @@ -93,13 +94,62 @@ func CreateRobot(opts create.CreateView) (*robot.CreateRobotCreated ,error) {
},
)
if err != nil {
return nil,err
return nil, err
}

log.Info("robot created successfully.")
return response, nil
}

// update robot with robotID
func UpdateRobot(opts *update.UpdateView) error {
ctx, client, err := utils.ContextWithClient()
if err != nil {
log.Errorf("Error: %v", err)
return err
}

log.Println(opts)

// Create a slice to store converted permissions
permissions := opts.Permissions
convertedPerms := make([]*models.RobotPermission, 0, len(permissions))

kind := "project"
// Loop through original permissions and convert them
for _, perm := range permissions {
convertedPerm := &models.RobotPermission{
Access: perm.Access,
Kind: kind,
Namespace: opts.Permissions[0].Namespace,
}
convertedPerms = append(convertedPerms, convertedPerm)
}
_, err = client.Robot.UpdateRobot(
ctx,
&robot.UpdateRobotParams{
Robot: &models.Robot{
Description: opts.Description,
Duration: opts.Duration,
Editable: opts.Editable,
Disable: opts.Disable,
ID: opts.ID,
Level: opts.Level,
Name: opts.Name,
Permissions: convertedPerms,
},
RobotID: opts.ID,
},
)
if err != nil {
log.Errorf("Error in updating Robot: %v", err)
return err
}

log.Info("robot updated successfully.")
return nil
}

func GetPermissions() (*permissions.GetPermissionsOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/prompt/prompt.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package prompt

import (
"strconv"

"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/constants"
aview "github.com/goharbor/harbor-cli/pkg/views/artifact/select"
tview "github.com/goharbor/harbor-cli/pkg/views/artifact/tags/select"
pview "github.com/goharbor/harbor-cli/pkg/views/project/select"
Expand Down Expand Up @@ -104,3 +107,15 @@ func GetRobotPermissionsFromUser() []models.Permission {
}()
return <-permissions
}

func GetRobotIDFromUser(projectID int64) int64 {
robotID := make(chan int64)
var opts api.ListFlags
opts.Q = constants.ProjectQString + strconv.FormatInt(projectID, 10)

go func() {
response, _ := api.ListRobot(opts)
robotView.ListRobot(response.Payload, robotID)
}()
return <-robotID
}
9 changes: 5 additions & 4 deletions pkg/views/base/multiselect/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package multiselect

import (
"fmt"
"log"
"os"
"strings"

"github.com/charmbracelet/bubbles/viewport"
Expand All @@ -19,9 +21,9 @@ var (
return lipgloss.NewStyle().BorderStyle(b).Padding(0, 1)
}()

selectedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("43"))
itemStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("46"))
blockStyle = lipgloss.NewStyle().
selectedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("43"))
itemStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("46"))
blockStyle = lipgloss.NewStyle().
Background(lipgloss.Color("81")).
Foreground(lipgloss.Color("#000000")).
Bold(true).
Expand Down Expand Up @@ -170,7 +172,6 @@ func (m Model) listView() string {
return s
}


func (m Model) GetSelectedPermissions() *[]models.Permission {
selectedPermissions := make([]models.Permission, 0, len(m.selected))
for index := range m.selected {
Expand Down
8 changes: 5 additions & 3 deletions pkg/views/robot/list/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

var columns = []table.Column{
{Title: "ID", Width: 4},
{Title: "Name", Width: 30},
{Title: "Status", Width: 18},
{Title: "Permissions", Width: 12},
Expand Down Expand Up @@ -47,10 +48,11 @@ func ListRobots(robots []*models.Robot) {

createdTime, _ := utils.FormatCreatedTime(robot.CreationTime.String())
rows = append(rows, table.Row{
robot.Name, // Project Name
enabledStatus, // Access Level
strconv.FormatInt(robot.ID, 10),
robot.Name,
enabledStatus,
TotalPermissions,
createdTime, // Creation Time
createdTime,
expires,
robot.Description,
})
Expand Down
Loading

0 comments on commit 53bbbb5

Please sign in to comment.