Skip to content

Commit 451d0e4

Browse files
committed
Fix get API key for user org
1 parent 639a36e commit 451d0e4

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

scalewayclient.go

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,13 @@ const (
2424

2525
type ScalewayClient struct {
2626
httpClient http.Client
27-
organizationToProfile map[string]OrganizationProfile
27+
organizationToJwt map[string]string
28+
organizationToUserId map[string]string
2829
defaultOrganizationId string
2930
jti string
3031
issuer string
3132
}
3233

33-
type OrganizationProfile struct {
34-
jwt string
35-
userId string
36-
}
37-
3834
type ApiKey struct {
3935
AccessKey string `json:"access_key"`
4036
SecretKey string `json:"secret_key"`
@@ -48,8 +44,9 @@ type PostBody struct {
4844

4945
func NewScalewayClient(email, password, token string) (*ScalewayClient, error) {
5046
client := ScalewayClient{
51-
httpClient: http.Client{},
52-
organizationToProfile: make(map[string]OrganizationProfile),
47+
httpClient: http.Client{},
48+
organizationToJwt: make(map[string]string),
49+
organizationToUserId: make(map[string]string),
5350
}
5451

5552
postBody := PostBody{
@@ -70,21 +67,27 @@ func NewScalewayClient(email, password, token string) (*ScalewayClient, error) {
7067
client.jti = jsonResponse["jwt"].(map[string]interface{})["jti"].(string)
7168
client.issuer = jsonResponse["jwt"].(map[string]interface{})["issuer_id"].(string)
7269

73-
client.organizationToProfile[client.defaultOrganizationId] = OrganizationProfile{
74-
jwt: jsonResponse["token"].(string),
70+
jwt := jsonResponse["token"].(string)
71+
72+
jsonResponse, err = client.sendRequest(IAM_USERS_API+client.issuer, nil, jwt, "GET")
73+
if err != nil {
74+
return nil, err
7575
}
76+
client.defaultOrganizationId = jsonResponse["organization_id"].(string)
77+
client.organizationToJwt[client.defaultOrganizationId] = jwt
78+
client.organizationToUserId[client.defaultOrganizationId] = client.issuer
7679

7780
return &client, nil
7881
}
7982

8083
func (client *ScalewayClient) ListOrganizations() (map[string]string, error) {
81-
jsonResponse, err := client.sendRequest(IAM_USERS_API+client.issuer, nil, client.getOrCreateOrganizationProfile(client.defaultOrganizationId).jwt, "GET")
84+
jsonResponse, err := client.sendRequest(IAM_USERS_API+client.issuer, nil, client.getOrCreateOrganizationJwt(client.defaultOrganizationId), "GET")
8285
if err != nil {
8386
return nil, err
8487
}
8588
accountRootUserId := jsonResponse["account_root_user_id"].(string)
8689

87-
jsonResponse, err = client.sendRequest(ACCOUNT_USERS_API+accountRootUserId, nil, client.getOrCreateOrganizationProfile(client.defaultOrganizationId).jwt, "GET")
90+
jsonResponse, err = client.sendRequest(ACCOUNT_USERS_API+accountRootUserId, nil, client.getOrCreateOrganizationJwt(client.defaultOrganizationId), "GET")
8891
if err != nil {
8992
return nil, err
9093
}
@@ -102,11 +105,11 @@ func (sc *ScalewayClient) CreateAPIKey(organizationId string, duration time.Dura
102105
jsonBody, _ := json.Marshal(map[string]string{
103106
"default_project_id": organizationId,
104107
"description": "generated by scw-2fa-init",
105-
"user_id": sc.getOrCreateOrganizationProfile(organizationId).userId,
108+
"user_id": sc.getOrCreateOrganizationUserId(organizationId),
106109
"expiresAt": time.Now().Add(duration).UTC().Format(time.RFC3339),
107110
})
108111

109-
jsonResponse, err := sc.sendRequest(API_KEYS_API, jsonBody, sc.getOrCreateOrganizationProfile(organizationId).jwt, http.MethodPost)
112+
jsonResponse, err := sc.sendRequest(API_KEYS_API, jsonBody, sc.getOrCreateOrganizationJwt(organizationId), http.MethodPost)
110113
if err != nil {
111114
return nil, err
112115
}
@@ -117,21 +120,27 @@ func (sc *ScalewayClient) CreateAPIKey(organizationId string, duration time.Dura
117120
return &ApiKey{AccessKey: accessKey, SecretKey: secretKey}, nil
118121
}
119122

120-
func (client *ScalewayClient) getOrCreateOrganizationProfile(organizationId string) OrganizationProfile {
121-
organizationProfile, ok := client.organizationToProfile[organizationId]
123+
func (client *ScalewayClient) getOrCreateOrganizationUserId(organizationId string) string {
124+
userId, ok := client.organizationToUserId[organizationId]
122125
if !ok {
123-
jsonBody, _ := json.Marshal(map[string]string{
124-
"organization_id": organizationId,
125-
})
126-
127-
jsonResponse, _ := client.sendRequest(fmt.Sprintf(SWITCH_ORGANIZATION_API, client.jti), jsonBody, client.getOrCreateOrganizationProfile(client.defaultOrganizationId).jwt, "POST")
128-
organizationProfile = OrganizationProfile{
129-
jwt: jsonResponse["token"].(string),
130-
userId: jsonResponse["user_id"].(string),
131-
}
132-
client.organizationToProfile[organizationId] = organizationProfile
133-
}
134-
return organizationProfile
126+
client.completeOrganizationData(organizationId)
127+
}
128+
userId, _ = client.organizationToUserId[organizationId]
129+
return userId
130+
}
131+
132+
func (client *ScalewayClient) completeOrganizationData(organizationId string) {
133+
jsonBody, _ := json.Marshal(map[string]string{
134+
"organization_id": organizationId,
135+
})
136+
137+
jsonResponse, err := client.sendRequest(fmt.Sprintf(SWITCH_ORGANIZATION_API, client.jti), jsonBody, client.getOrCreateOrganizationJwt(client.defaultOrganizationId), "POST")
138+
if err != nil {
139+
fmt.Println(err)
140+
return
141+
}
142+
client.organizationToUserId[organizationId] = jsonResponse["user_id"].(string)
143+
client.organizationToJwt[organizationId] = jsonResponse["token"].(string)
135144
}
136145

137146
func (client *ScalewayClient) sendRequest(url string, body []byte, jwt string, method string) (map[string]interface{}, error) {
@@ -150,13 +159,23 @@ func (client *ScalewayClient) sendRequest(url string, body []byte, jwt string, m
150159
return nil, err
151160
}
152161

162+
responseBody, _ := ioutil.ReadAll(response.Body)
163+
153164
if response.StatusCode >= 400 {
154-
return nil, errors.New("Request failed with status code: " + strconv.Itoa(response.StatusCode))
165+
return nil, errors.New("Request failed with status code: " + strconv.Itoa(response.StatusCode) + "\n" + string(responseBody))
155166
}
156167

157-
responseBody, _ := ioutil.ReadAll(response.Body)
158168
var jsonResponse map[string]interface{}
159169
json.Unmarshal(responseBody, &jsonResponse)
160170

161171
return jsonResponse, nil
162172
}
173+
174+
func (client *ScalewayClient) getOrCreateOrganizationJwt(organizationId string) string {
175+
jwt, ok := client.organizationToJwt[organizationId]
176+
if !ok {
177+
client.completeOrganizationData(organizationId)
178+
}
179+
jwt, _ = client.organizationToJwt[organizationId]
180+
return jwt
181+
}

0 commit comments

Comments
 (0)