5
5
"fmt"
6
6
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
7
7
"log"
8
- "net/http"
9
8
)
10
9
11
10
type ApiKeySubject struct {
@@ -30,6 +29,14 @@ type ApiKey struct {
30
29
Created string `json:"created,omitempty"`
31
30
}
32
31
32
+ type TokenResponse struct {
33
+ AccessToken string `json:"accessToken"`
34
+ User struct {
35
+ UserName string `json:"userName,omitempty"`
36
+ Email string `json:"email,omitempty"`
37
+ } `json:"user"`
38
+ }
39
+
33
40
func (client * Client ) GetAPIKey (keyID string ) (* ApiKey , error ) {
34
41
35
42
opts := RequestOptions {
@@ -134,55 +141,56 @@ func (client *Client) CreateApiKey(userID string, accountId string, apiKey *ApiK
134
141
// GetXAccessToken
135
142
func (client * Client ) GetXAccessToken (userID string , accountId string ) (string , error ) {
136
143
137
- url := fmt .Sprintf ("%s/admin/user/loginAsUser?userId=%s" , client .Host , userID )
138
- request , err := http .NewRequest ("GET" , url , nil )
144
+ fullPath := fmt .Sprintf ("/admin/user/loginAsUser?userId=%s" , userID )
145
+ opts := RequestOptions {
146
+ Path : fullPath ,
147
+ Method : "GET" ,
148
+ }
149
+
150
+ resp , err := client .RequestAPI (& opts )
151
+
139
152
if err != nil {
140
153
return "" , err
141
154
}
142
155
143
- request . Header . Set ( "Authorization" , client . Token )
144
- request . Header . Set ( "Content-Type" , "application/json; charset=utf-8" )
156
+ var userCfAccessToken string
157
+ var asUserTokenResponse TokenResponse
145
158
146
- resp , err := client . Client . Do ( request )
159
+ err = DecodeResponseInto ( resp , & asUserTokenResponse )
147
160
if err != nil {
148
161
return "" , err
149
162
}
150
163
151
- defer resp . Body . Close ()
164
+ userCfAccessToken = asUserTokenResponse . AccessToken
152
165
153
- var userCfAccessToken string
154
- for _ , cookie := range resp .Cookies () {
155
- if cookie .Name == "cf-access-token" {
156
- userCfAccessToken = cookie .Value
157
- break
158
- }
159
- }
160
166
if userCfAccessToken == "" {
161
167
return "" , fmt .Errorf ("Failed to GetXAccessToken for userId = %s" , userID )
162
168
}
163
169
164
170
// change account
165
- changeAccURL := fmt .Sprintf ("%s/user/changeaccount/%s" , client .Host , accountId )
166
- changeAccRequest , err := http .NewRequest ("POST" , changeAccURL , nil )
167
- if err != nil {
168
- return "" , err
171
+ fullPath = fmt .Sprintf ("/user/changeaccount/%s" , accountId )
172
+ opts = RequestOptions {
173
+ Path : fullPath ,
174
+ Method : "POST" ,
175
+ XAccessToken : userCfAccessToken ,
169
176
}
170
177
171
- changeAccRequest .Header .Set ("x-access-token" , userCfAccessToken )
172
- changeAccRequest .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
178
+ resp , err = client .RequestApiXAccessToken (& opts )
173
179
174
- changeAccResp , err := client .Client .Do (changeAccRequest )
175
180
if err != nil {
176
181
return "" , err
177
182
}
178
183
179
184
var accCfAccessToken string
180
- for _ , cookie := range changeAccResp . Cookies () {
181
- if cookie . Name == "cf-access-token" {
182
- accCfAccessToken = cookie . Value
183
- break
184
- }
185
+ var changeAccountTokenResponse TokenResponse
186
+
187
+ err = DecodeResponseInto ( resp , & changeAccountTokenResponse )
188
+ if err != nil {
189
+ return "" , err
185
190
}
191
+
192
+ accCfAccessToken = changeAccountTokenResponse .AccessToken
193
+
186
194
if accCfAccessToken == "" {
187
195
return "" , fmt .Errorf ("Failed to GetXAccessToken for userId = %s after ChangeAcocunt to %s" , userID , accountId )
188
196
}
0 commit comments