Skip to content

Commit d1646b3

Browse files
authored
add an optional robot_prefix to the provider configuration (#494)
add an optional robot_prefix to the provider configuration `robot_prefix` (String) Without this option, the provider will try to automatically determine the robot prefix with a call to the admin api. If you don't have admin access and want to create system robot account, you'll have to set this value. and fix, permission access effect, to be able to use the provider with robot account Signed-off-by: flbla <flbla@users.noreply.github.com>
1 parent 2a5198b commit d1646b3

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

client/client.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ type Client struct {
1919
bearerToken string
2020
insecure bool
2121
httpClient *http.Client
22+
robotPrefix string
2223
}
2324

2425
// NewClient creates common settings
25-
func NewClient(url string, username string, password string, bearerToken string, insecure bool) *Client {
26+
func NewClient(url string, username string, password string, bearerToken string, insecure bool, robotPrefix string) *Client {
2627

2728
return &Client{
2829
url: url,
@@ -31,6 +32,7 @@ func NewClient(url string, username string, password string, bearerToken string,
3132
bearerToken: bearerToken,
3233
insecure: insecure,
3334
httpClient: &http.Client{},
35+
robotPrefix: robotPrefix,
3436
}
3537
}
3638

@@ -115,3 +117,8 @@ func GetID(body string) (id string, err error) {
115117

116118
return location, nil
117119
}
120+
121+
// get robotPrefix
122+
func (c *Client) GetRobotPrefix() string {
123+
return c.robotPrefix
124+
}

client/robot_account.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ func RobotBody(d *schema.ResourceData) models.RobotBody {
2626
access := models.RobotBodyAccess{
2727
Action: a.(map[string]interface{})["action"].(string),
2828
Resource: a.(map[string]interface{})["resource"].(string),
29-
Effect: a.(map[string]interface{})["effect"].(string),
3029
}
30+
if a.(map[string]interface{})["effect"] != "" {
31+
access.Effect = a.(map[string]interface{})["effect"].(string)
32+
}
33+
3134
permission.Access = append(permission.Access, access)
3235
}
3336

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ description: |-
2323
- `api_version` (Number) Choose which version of the api you would like to use 1 or 2 (default is 2)
2424
- `bearer_token` (String) The bearer token to be used to access harbor. Will take precedence over username and password if set
2525
- `insecure` (Boolean) Choose to ignore certificate errors
26+
- `robot_prefix` (String) Without this option, the provider will try to automatically determine the robot prefix with a call to the admin api. If you don't have admin access and want to create system robot account, you'll have to set this value.
2627

2728
### Environment variables
2829

provider/provider.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func Provider() *schema.Provider {
4444
Optional: true,
4545
Default: 2,
4646
},
47+
"robot_prefix": {
48+
Type: schema.TypeString,
49+
Optional: true,
50+
},
4751
},
4852

4953
ResourcesMap: map[string]*schema.Resource{
@@ -100,6 +104,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
100104
bearerToken := d.Get("bearer_token").(string)
101105
insecure := d.Get("insecure").(bool)
102106
apiVersion := d.Get("api_version").(int)
107+
robotPrefix := d.Get("robot_prefix").(string)
103108

104109
if strings.HasSuffix(url, "/") {
105110
url = strings.Trim(url, "/")
@@ -111,5 +116,5 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
111116
apiPath = "/api/v2.0"
112117
}
113118

114-
return client.NewClient(url+apiPath, username, password, bearerToken, insecure), nil
119+
return client.NewClient(url+apiPath, username, password, bearerToken, insecure, robotPrefix), nil
115120
}

provider/resource_robot_account.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func resourceRobotAccount() *schema.Resource {
7272
"effect": {
7373
Type: schema.TypeString,
7474
Optional: true,
75-
Default: "allow",
75+
//Default: "allow",
7676
},
7777
},
7878
},
@@ -152,28 +152,33 @@ func resourceRobotAccountRead(d *schema.ResourceData, m interface{}) error {
152152
apiClient := m.(*client.Client)
153153

154154
robot, err := getRobot(d, apiClient)
155+
155156
if err != nil {
156157
d.SetId("")
157158
return nil
158159
}
159160

160161
var shortName string
161-
if robot.Level == "project" {
162-
shortName = strings.Split(robot.Name, robot.Permissions[0].Namespace+"+")[1]
162+
if m.(*client.Client).GetRobotPrefix() != "" {
163+
// if robot_prefix is set, we use it to get the short name
164+
shortName = strings.TrimPrefix(robot.Name, m.(*client.Client).GetRobotPrefix())
163165
} else {
164-
resp, _, respCode, err := apiClient.SendRequest("GET", models.PathConfig, nil, 200)
165-
if respCode == 404 && err != nil {
166-
d.SetId("")
167-
return fmt.Errorf("error getting system configuration %s", err)
168-
}
169-
var systemConfig models.ConfigBodyResponse
170-
err = json.Unmarshal([]byte(resp), &systemConfig)
171-
if err != nil {
172-
return fmt.Errorf("error getting system configuration %s", err)
166+
if robot.Level == "project" {
167+
shortName = strings.Split(robot.Name, robot.Permissions[0].Namespace+"+")[1]
168+
} else {
169+
resp, _, respCode, err := apiClient.SendRequest("GET", models.PathConfig, nil, 200)
170+
if respCode == 404 && err != nil {
171+
d.SetId("")
172+
return fmt.Errorf("error getting system configuration (probably missing admin rights) %s, you can use robot_prefix to force the prefix", err)
173+
}
174+
var systemConfig models.ConfigBodyResponse
175+
err = json.Unmarshal([]byte(resp), &systemConfig)
176+
if err != nil {
177+
return fmt.Errorf("error getting system configuration (probably missing admin rights) %s, you can use robot_prefix to force the prefix", err)
178+
}
179+
shortName = strings.TrimPrefix(robot.Name, systemConfig.RobotNamePrefix.Value)
173180
}
174-
shortName = strings.TrimPrefix(robot.Name, systemConfig.RobotNamePrefix.Value)
175181
}
176-
177182
d.Set("name", shortName)
178183
d.Set("robot_id", strconv.Itoa(robot.ID))
179184
d.Set("full_name", robot.Name)

templates/index.md.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ For example, the {{ .SchemaMarkdown }} template can be used to replace manual sc
2727
- `api_version` (Number) Choose which version of the api you would like to use 1 or 2 (default is 2)
2828
- `bearer_token` (String) The bearer token to be used to access harbor. Will take precedence over username and password if set
2929
- `insecure` (Boolean) Choose to ignore certificate errors
30+
- `robot_prefix` (String) Without this option, the provider will try to automatically determine the robot prefix with a call to the admin api. If you don't have admin access and want to create system robot account, you'll have to set this value.
3031

3132
### Environment variables
3233

0 commit comments

Comments
 (0)