44 "errors"
55 "fmt"
66 "log"
7-
8- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
97)
108
119type ApiKeySubject struct {
@@ -38,14 +36,21 @@ type TokenResponse struct {
3836 } `json:"user"`
3937}
4038
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+ }
4246
4347 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" ,
4651 }
4752
48- resp , err := client .RequestAPI (& opts )
53+ resp , err := client .RequestApiXAccessToken (& opts )
4954
5055 if err != nil {
5156 return nil , err
@@ -61,14 +66,21 @@ func (client *Client) GetAPIKey(keyID string) (*ApiKey, error) {
6166 return & apiKey , nil
6267}
6368
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 )
6573
74+ if err != nil {
75+ return err
76+ }
6677 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 ,
6981 }
7082
71- resp , err := client .RequestAPI (& opts )
83+ resp , err := client .RequestApiXAccessToken (& opts )
7284 if err != nil {
7385 fmt .Println (string (resp ))
7486 return err
@@ -77,7 +89,7 @@ func (client *Client) DeleteAPIKey(keyID string) error {
7789 return nil
7890}
7991
80- func (client * Client ) UpdateAPIKey (key * ApiKey ) error {
92+ func (client * Client ) UpdateAPIKey (userID string , accountId string , key * ApiKey ) error {
8193
8294 keyID := key .ID
8395 if keyID == "" {
@@ -89,13 +101,23 @@ func (client *Client) UpdateAPIKey(key *ApiKey) error {
89101 return err
90102 }
91103
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+
92113 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 ,
96118 }
97119
98- resp , err := client .RequestAPI (& opts )
120+ resp , err := client .RequestApiXAccessToken (& opts )
99121
100122 if err != nil {
101123 fmt .Println (string (resp ))
@@ -110,6 +132,7 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
110132
111133 // Check collaborataros
112134 account , err := client .GetAccountByID (accountId )
135+
113136 if err != nil {
114137 return "" , err
115138 }
@@ -118,12 +141,7 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
118141 }
119142
120143 var xAccessToken string
121- if userID == "" {
122- userID , err = client .createRandomUser (accountId )
123- if err != nil {
124- return "" , err
125- }
126- }
144+
127145 // login as user
128146 xAccessToken , err = client .GetXAccessToken (userID , accountId )
129147 if err != nil {
@@ -333,31 +351,3 @@ func (client *Client) CreateApiKeyServiceUser(serviceUserId string, apiKey *ApiK
333351
334352 return string (resp ), nil
335353}
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