Skip to content

Commit 78d8f1b

Browse files
committed
feat(robot): Added robot delete command for system level robots and added documentation
- add delete command after user prompt or robor id - documented usage of list, view and delete comman - made changes to satisfy linter those changes are a futher step for completing system level robot command functionality. Signed-off-by: Patrick Eschenbach <patrickeschenbach96@gmail.com>
1 parent fb86937 commit 78d8f1b

21 files changed

+1084
-337
lines changed

cmd/harbor/root/robot/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ Examples:
201201
Kind: "system",
202202
})
203203
opts.Permissions = mergedPermissions
204-
205204
}
205+
206206
response, err := api.CreateRobot(opts, "system")
207207
if err != nil {
208208
return fmt.Errorf("failed to create robot: %v", utils.ParseHarborErrorMsg(err))

cmd/harbor/root/robot/delete.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,31 @@ import (
2727

2828
// to-do improve DeleteRobotCommand and multi select & delete
2929
func DeleteRobotCommand() *cobra.Command {
30-
var ProjectName string
3130
cmd := &cobra.Command{
3231
Use: "delete [robotID]",
3332
Short: "delete robot by id",
34-
Long: `Delete a robot account from a Harbor project.
33+
Long: `Delete a robot account from Harbor.
3534
3635
This command permanently removes a robot account from Harbor. Once deleted,
3736
the robot's credentials will no longer be valid, and any automated processes
3837
using those credentials will fail.
3938
4039
The command supports multiple ways to identify the robot account to delete:
4140
- By providing the robot ID directly as an argument
42-
- By specifying a project with the --project flag and selecting the robot interactively
43-
- Without any arguments, which will prompt for both project and robot selection
41+
- Without any arguments, which will prompt for robot selection
4442
4543
Important considerations:
4644
- Deletion is permanent and cannot be undone
4745
- All access tokens for the robot will be invalidated immediately
4846
- Any systems using the robot's credentials will need to be updated
47+
- For system robots, access across all projects will be revoked
4948
5049
Examples:
5150
# Delete robot by ID
52-
harbor-cli project robot delete 123
51+
harbor-cli robot delete 123
5352
54-
# Delete robot by selecting from a specific project
55-
harbor-cli project robot delete --project myproject
56-
57-
# Interactive deletion (will prompt for project and robot selection)
58-
harbor-cli project robot delete`,
53+
# Interactive deletion (will prompt for robot selection)
54+
harbor-cli robot delete`,
5955
Args: cobra.MaximumNArgs(1),
6056
Run: func(cmd *cobra.Command, args []string) {
6157
var (
@@ -65,17 +61,10 @@ Examples:
6561
if len(args) == 1 {
6662
robotID, err = strconv.ParseInt(args[0], 10, 64)
6763
if err != nil {
68-
log.Fatalf("failed to parse robot ID: %v", utils.ParseHarborErrorMsg(err))
69-
}
70-
} else if ProjectName != "" {
71-
project, err := api.GetProject(ProjectName, false)
72-
if err != nil {
73-
log.Fatalf("failed to get project by name %s: %v", ProjectName, utils.ParseHarborErrorMsg(err))
64+
log.Fatalf("failed to parse robot ID: %v", err)
7465
}
75-
robotID = prompt.GetRobotIDFromUser(int64(project.Payload.ProjectID))
7666
} else {
77-
projectID := prompt.GetProjectIDFromUser()
78-
robotID = prompt.GetRobotIDFromUser(projectID)
67+
robotID = prompt.GetRobotIDFromUser(-1)
7968
}
8069
err = api.DeleteRobot(robotID)
8170
if err != nil {
@@ -87,7 +76,5 @@ Examples:
8776
},
8877
}
8978

90-
flags := cmd.Flags()
91-
flags.StringVarP(&ProjectName, "project", "", "", "set project name")
9279
return cmd
9380
}

cmd/harbor/root/robot/list.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,39 +29,37 @@ func ListRobotCommand() *cobra.Command {
2929
cmd := &cobra.Command{
3030
Use: "list [projectName]",
3131
Short: "list robot",
32-
Long: `List robot accounts in a Harbor project.
32+
Long: `List robot accounts in Harbor.
3333
34-
This command displays a list of robot accounts, either from a specific project
35-
or by prompting you to select a project interactively. The list includes basic
34+
This command displays a list of system-level robot accounts. The list includes basic
3635
information about each robot account, such as ID, name, creation time, and
3736
expiration status.
3837
39-
The command supports multiple ways to specify the project:
40-
- By providing a project name as an argument
41-
- By using the --project-id flag
42-
- By using the -q/--query flag with a project filter
43-
- Without any arguments, which will prompt for project selection
38+
System-level robots have permissions that can span across multiple projects, making
39+
them suitable for CI/CD pipelines and automation tasks that require access to
40+
multiple projects in Harbor.
4441
4542
You can control the output using pagination flags and format options:
4643
- Use --page and --page-size to navigate through results
47-
- Use --sort to order the results
44+
- Use --sort to order the results by name, creation time, etc.
45+
- Use -q/--query to filter robots by specific criteria
4846
- Set output-format in your configuration for JSON, YAML, or other formats
4947
5048
Examples:
51-
# List robots in a specific project by name
52-
harbor-cli project robot list myproject
49+
# List all system robots
50+
harbor-cli robot list
5351
54-
# List robots in a project by ID
55-
harbor-cli project robot list --project-id 123
52+
# List system robots with pagination
53+
harbor-cli robot list --page 2 --page-size 20
5654
57-
# List robots with pagination
58-
harbor-cli project robot list --page 2 --page-size 20
55+
# List system robots with custom sorting
56+
harbor-cli robot list --sort name
5957
60-
# List robots with custom sorting
61-
harbor-cli project robot list --sort name
58+
# Filter system robots by name
59+
harbor-cli robot list -q name=ci-robot
6260
63-
# Interactive listing (will prompt for project selection)
64-
harbor-cli project robot list`,
61+
# Get robot details in JSON format
62+
harbor-cli robot list --output-format json`,
6563
Args: cobra.MaximumNArgs(0),
6664
Run: func(cmd *cobra.Command, args []string) {
6765
robots, err := api.ListRobot(opts)
@@ -84,7 +82,6 @@ Examples:
8482
flags := cmd.Flags()
8583
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number")
8684
flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page")
87-
flags.Int64VarP(&opts.ProjectID, "project-id", "", 0, "Project ID")
8885
flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources")
8986
flags.StringVarP(
9087
&opts.Sort,

cmd/harbor/root/robot/view.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,35 @@ import (
2626
)
2727

2828
func ViewRobotCommand() *cobra.Command {
29-
var (
30-
ProjectName string
31-
)
3229
cmd := &cobra.Command{
3330
Use: "view [robotID]",
3431
Short: "get robot by id",
3532
Long: `View detailed information about a robot account in Harbor.
3633
3734
This command displays comprehensive information about a robot account including
3835
its ID, name, description, creation time, expiration, and the permissions
39-
it has been granted within its project.
36+
it has been granted. Supports both system-level and project-level robot accounts.
4037
4138
The command supports multiple ways to identify the robot account:
4239
- By providing the robot ID directly as an argument
43-
- By specifying a project with the --project flag and selecting the robot interactively
44-
- Without any arguments, which will prompt for both project and robot selection
40+
- Without any arguments, which will prompt for robot selection
4541
4642
The displayed information includes:
4743
- Basic details (ID, name, description)
4844
- Temporal information (creation date, expiration date, remaining time)
4945
- Security details (disabled status)
5046
- Detailed permissions breakdown by resource and action
47+
- For system robots: permissions across multiple projects are shown separately
48+
49+
System-level robots can have permissions spanning multiple projects, while
50+
project-level robots are scoped to a single project.
5151
5252
Examples:
5353
# View robot by ID
54-
harbor-cli project robot view 123
55-
56-
# View robot by selecting from a specific project
57-
harbor-cli project robot view --project myproject
54+
harbor-cli robot view 123
5855
59-
# Interactive selection (will prompt for project and robot)
60-
harbor-cli project robot view`,
56+
# Interactive selection (will prompt for robot)
57+
harbor-cli robot view`,
6158
Args: cobra.MaximumNArgs(1),
6259
Run: func(cmd *cobra.Command, args []string) {
6360
var (
@@ -86,7 +83,5 @@ Examples:
8683
},
8784
}
8885

89-
flags := cmd.Flags()
90-
flags.StringVarP(&ProjectName, "project", "", "", "set project name")
9186
return cmd
9287
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
---
2+
title: harbor robot create
3+
weight: 70
4+
---
5+
## harbor robot create
6+
7+
### Description
8+
9+
##### create robot
10+
11+
### Synopsis
12+
13+
Create a new robot account within a Harbor project.
14+
15+
Robot accounts are non-human users that can be used for automation purposes
16+
such as CI/CD pipelines, scripts, or other automated processes that need
17+
to interact with Harbor. They have specific permissions and a defined lifetime.
18+
19+
This command supports both interactive and non-interactive modes:
20+
- Without flags: opens an interactive form for configuring the robot
21+
- With flags: creates a robot with the specified parameters
22+
- With config file: loads robot configuration from YAML or JSON
23+
24+
A robot account requires:
25+
- A unique name
26+
- A project where it will be created
27+
- A set of permissions
28+
- A duration (lifetime in days)
29+
30+
The generated robot credentials can be:
31+
- Displayed on screen
32+
- Copied to clipboard (default)
33+
- Exported to a JSON file with the -e flag
34+
35+
Configuration File Format (YAML or JSON):
36+
name: "robot-name" # Required: Name of the robot account
37+
description: "..." # Optional: Description of the robot account
38+
duration: 90 # Required: Lifetime in days
39+
project: "project-name" # Required: Project where the robot will be created
40+
permissions: # Required: At least one permission must be specified
41+
- resource: "repository" # Either specify a single resource
42+
actions: ["pull", "push"]
43+
- resources: ["artifact", "scan"] # Or specify multiple resources
44+
actions: ["read"]
45+
- resource: "project" # Use "*" as an action to grant all available actions
46+
actions: ["*"]
47+
48+
Examples:
49+
# Interactive mode
50+
harbor-cli project robot create
51+
52+
# Non-interactive mode with all flags
53+
harbor-cli project robot create --project myproject --name ci-robot --description "CI pipeline" --duration 90
54+
55+
# Create with all permissions
56+
harbor-cli project robot create --project myproject --name ci-robot --all-permission
57+
58+
# Load from configuration file
59+
harbor-cli project robot create --robot-config-file ./robot-config.yaml
60+
61+
# Export secret to file
62+
harbor-cli project robot create --project myproject --name ci-robot --export-to-file
63+
64+
```sh
65+
harbor robot create [flags]
66+
```
67+
68+
### Options
69+
70+
```sh
71+
-a, --all-permission Select all permissions for the robot account
72+
--description string description of the robot account
73+
--duration int set expiration of robot account in days
74+
-e, --export-to-file Choose to export robot account to file
75+
-h, --help help for create
76+
--name string name of the robot account
77+
--project string set project name
78+
-r, --robot-config-file string YAML/JSON file with robot configuration
79+
```
80+
81+
### Options inherited from parent commands
82+
83+
```sh
84+
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
85+
-o, --output-format string Output format. One of: json|yaml
86+
-v, --verbose verbose output
87+
```
88+
89+
### SEE ALSO
90+
91+
* [harbor robot](harbor-robot.md) - Manage robot accounts
92+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
title: harbor robot delete
3+
weight: 60
4+
---
5+
## harbor robot delete
6+
7+
### Description
8+
9+
##### delete robot by id
10+
11+
### Synopsis
12+
13+
Delete a robot account from Harbor.
14+
15+
This command permanently removes a robot account from Harbor. Once deleted,
16+
the robot's credentials will no longer be valid, and any automated processes
17+
using those credentials will fail.
18+
19+
The command supports multiple ways to identify the robot account to delete:
20+
- By providing the robot ID directly as an argument
21+
- Without any arguments, which will prompt for robot selection
22+
23+
Important considerations:
24+
- Deletion is permanent and cannot be undone
25+
- All access tokens for the robot will be invalidated immediately
26+
- Any systems using the robot's credentials will need to be updated
27+
- For system robots, access across all projects will be revoked
28+
29+
Examples:
30+
# Delete robot by ID
31+
harbor-cli robot delete 123
32+
33+
# Interactive deletion (will prompt for robot selection)
34+
harbor-cli robot delete
35+
36+
```sh
37+
harbor robot delete [robotID] [flags]
38+
```
39+
40+
### Options
41+
42+
```sh
43+
-h, --help help for delete
44+
```
45+
46+
### Options inherited from parent commands
47+
48+
```sh
49+
-c, --config string config file (default is $HOME/.config/harbor-cli/config.yaml)
50+
-o, --output-format string Output format. One of: json|yaml
51+
-v, --verbose verbose output
52+
```
53+
54+
### SEE ALSO
55+
56+
* [harbor robot](harbor-robot.md) - Manage robot accounts
57+

0 commit comments

Comments
 (0)