4
4
"errors"
5
5
"fmt"
6
6
"log"
7
-
8
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9
7
)
10
8
11
9
type ApiKeySubject struct {
@@ -38,14 +36,21 @@ type TokenResponse struct {
38
36
} `json:"user"`
39
37
}
40
38
41
- func (client * Client ) GetAPIKey (keyID string ) (* ApiKey , error ) {
39
+ func (client * Client ) GetAPIKey (userID string , accountId string , keyID string ) (* ApiKey , error ) {
40
+
41
+ xAccessToken , err := client .GetXAccessToken (userID , accountId )
42
+
43
+ if err != nil {
44
+ return nil , err
45
+ }
42
46
43
47
opts := RequestOptions {
44
- Path : fmt .Sprintf ("/auth/key/%s" , keyID ),
45
- Method : "GET" ,
48
+ Path : fmt .Sprintf ("/auth/key/%s" , keyID ),
49
+ XAccessToken : xAccessToken ,
50
+ Method : "GET" ,
46
51
}
47
52
48
- resp , err := client .RequestAPI (& opts )
53
+ resp , err := client .RequestApiXAccessToken (& opts )
49
54
50
55
if err != nil {
51
56
return nil , err
@@ -61,14 +66,21 @@ func (client *Client) GetAPIKey(keyID string) (*ApiKey, error) {
61
66
return & apiKey , nil
62
67
}
63
68
64
- func (client * Client ) DeleteAPIKey (keyID string ) error {
69
+ func (client * Client ) DeleteAPIKey (userID string , accountId string , keyID string ) error {
70
+ // login as user
71
+
72
+ xAccessToken , err := client .GetXAccessToken (userID , accountId )
65
73
74
+ if err != nil {
75
+ return err
76
+ }
66
77
opts := RequestOptions {
67
- Path : fmt .Sprintf ("/auth/key/%s" , keyID ),
68
- Method : "DELETE" ,
78
+ Path : fmt .Sprintf ("/auth/key/%s" , keyID ),
79
+ Method : "DELETE" ,
80
+ XAccessToken : xAccessToken ,
69
81
}
70
82
71
- resp , err := client .RequestAPI (& opts )
83
+ resp , err := client .RequestApiXAccessToken (& opts )
72
84
if err != nil {
73
85
fmt .Println (string (resp ))
74
86
return err
@@ -77,7 +89,7 @@ func (client *Client) DeleteAPIKey(keyID string) error {
77
89
return nil
78
90
}
79
91
80
- func (client * Client ) UpdateAPIKey (key * ApiKey ) error {
92
+ func (client * Client ) UpdateAPIKey (userID string , accountId string , key * ApiKey ) error {
81
93
82
94
keyID := key .ID
83
95
if keyID == "" {
@@ -89,13 +101,23 @@ func (client *Client) UpdateAPIKey(key *ApiKey) error {
89
101
return err
90
102
}
91
103
104
+ var xAccessToken string
105
+
106
+ // login as user
107
+ xAccessToken , err = client .GetXAccessToken (userID , accountId )
108
+
109
+ if err != nil {
110
+ return err
111
+ }
112
+
92
113
opts := RequestOptions {
93
- Path : fmt .Sprintf ("/auth/key/%s" , keyID ),
94
- Method : "PATCH" ,
95
- Body : body ,
114
+ Path : fmt .Sprintf ("/auth/key/%s" , keyID ),
115
+ Method : "PATCH" ,
116
+ XAccessToken : xAccessToken ,
117
+ Body : body ,
96
118
}
97
119
98
- resp , err := client .RequestAPI (& opts )
120
+ resp , err := client .RequestApiXAccessToken (& opts )
99
121
100
122
if err != nil {
101
123
fmt .Println (string (resp ))
@@ -110,6 +132,7 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
110
132
111
133
// Check collaborataros
112
134
account , err := client .GetAccountByID (accountId )
135
+
113
136
if err != nil {
114
137
return "" , err
115
138
}
@@ -118,12 +141,7 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
118
141
}
119
142
120
143
var xAccessToken string
121
- if userID == "" {
122
- userID , err = client .createRandomUser (accountId )
123
- if err != nil {
124
- return "" , err
125
- }
126
- }
144
+
127
145
// login as user
128
146
xAccessToken , err = client .GetXAccessToken (userID , accountId )
129
147
if err != nil {
@@ -333,31 +351,3 @@ func (client *Client) CreateApiKeyServiceUser(serviceUserId string, apiKey *ApiK
333
351
334
352
return string (resp ), nil
335
353
}
336
-
337
- func (client * Client ) createRandomUser (accountId string ) (string , error ) {
338
- // add user
339
- userPrefix := acctest .RandString (10 )
340
- userName := "tfuser" + userPrefix
341
- userEmail := userName + "@codefresh.io"
342
-
343
- user , err := client .AddNewUserToAccount (accountId , userName , userEmail )
344
- if err != nil {
345
- return "" , err
346
- }
347
- userID := user .ID
348
-
349
- // activate
350
- err = client .ActivateUser (userID )
351
-
352
- if err != nil {
353
- return "" , err
354
- }
355
-
356
- // set user as account admin
357
- err = client .SetUserAsAccountAdmin (accountId , userID )
358
- if err != nil {
359
- return "" , nil
360
- }
361
- return userID , nil
362
-
363
- }
0 commit comments