Skip to content

Commit 29e9369

Browse files
committed
Merge pull request #138 from QuentinPerez/fix_upload_ssh_key
fix_upload_ssh_key
2 parents 319f153 + 44891e2 commit 29e9369

File tree

2 files changed

+86
-39
lines changed

2 files changed

+86
-39
lines changed

pkg/api/api.go

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ type ScalewayTokenDefinition struct {
526526

527527
// ScalewayTokensDefinition represents a Scaleway Tokens
528528
type ScalewayTokensDefinition struct {
529-
Tokens []ScalewayTokenDefinition `json:"tokens"`
529+
Token ScalewayTokenDefinition `json:"token"`
530530
}
531531

532532
// ScalewayConnectResponse represents the answer from POST /tokens
@@ -566,6 +566,10 @@ type ScalewayUserDefinition struct {
566566
SSHPublicKeys []ScalewayKeyDefinition `json:"ssh_public_keys"`
567567
}
568568

569+
type ScalewayUsersDefinition struct {
570+
User ScalewayUserDefinition `json:"user"`
571+
}
572+
569573
// ScalewayKeyDefinition represents a key
570574
type ScalewayKeyDefinition struct {
571575
Key string `json:"key"`
@@ -1328,28 +1332,75 @@ func (s *ScalewayAPI) CheckCredentials() error {
13281332
return nil
13291333
}
13301334

1331-
// GetUserID returns the UserID
1335+
// GetUserID returns the userID
13321336
func (s *ScalewayAPI) GetUserID() (string, error) {
13331337
s.EnableAccountAPI()
13341338
defer s.DisableAccountAPI()
1335-
resp, err := s.GetResponse("tokens")
1339+
resp, err := s.GetResponse(fmt.Sprintf("tokens/%s", s.Token))
13361340
if err != nil {
13371341
return "", err
13381342
}
13391343
if resp.StatusCode != 200 {
13401344
return "", fmt.Errorf("[%d] invalid credentials", resp.StatusCode)
13411345
}
13421346
defer resp.Body.Close()
1343-
var tokens ScalewayTokensDefinition
1347+
var token ScalewayTokensDefinition
1348+
13441349
decoder := json.NewDecoder(resp.Body)
1345-
err = decoder.Decode(&tokens)
1350+
err = decoder.Decode(&token)
13461351
if err != nil {
13471352
return "", err
13481353
}
1349-
if len(tokens.Tokens) == 0 {
1350-
return "", fmt.Errorf("unable to get tokens")
1354+
return token.Token.UserID, nil
1355+
}
1356+
1357+
// GetOrganization returns Organization
1358+
func (s *ScalewayAPI) GetOrganization() (*ScalewayOrganizationsDefinition, error) {
1359+
s.EnableAccountAPI()
1360+
defer s.DisableAccountAPI()
1361+
resp, err := s.GetResponse("organizations")
1362+
if err != nil {
1363+
return nil, err
1364+
}
1365+
if resp.StatusCode != 200 {
1366+
return nil, fmt.Errorf("[%d] unable to GET", resp.StatusCode)
1367+
}
1368+
1369+
var data ScalewayOrganizationsDefinition
1370+
1371+
defer resp.Body.Close()
1372+
decoder := json.NewDecoder(resp.Body)
1373+
err = decoder.Decode(&data)
1374+
if err != nil {
1375+
return nil, err
1376+
}
1377+
return &data, nil
1378+
}
1379+
1380+
// GetUser returns the user
1381+
func (s *ScalewayAPI) GetUser() (*ScalewayUserDefinition, error) {
1382+
userID, err := s.GetUserID()
1383+
if err != nil {
1384+
return nil, err
1385+
}
1386+
s.EnableAccountAPI()
1387+
defer s.DisableAccountAPI()
1388+
resp, err := s.GetResponse(fmt.Sprintf("users/%s", userID))
1389+
if err != nil {
1390+
return nil, err
1391+
}
1392+
if resp.StatusCode != 200 {
1393+
return nil, fmt.Errorf("[%d] no such user", resp.StatusCode)
1394+
}
1395+
defer resp.Body.Close()
1396+
var user ScalewayUsersDefinition
1397+
1398+
decoder := json.NewDecoder(resp.Body)
1399+
err = decoder.Decode(&user)
1400+
if err != nil {
1401+
return nil, err
13511402
}
1352-
return tokens.Tokens[0].UserID, nil
1403+
return &user.User, nil
13531404
}
13541405

13551406
//

pkg/commands/login.go

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -107,29 +107,16 @@ func getToken(connect api.ScalewayConnect) (string, error) {
107107
return data.Token.ID, nil
108108
}
109109

110-
func getOrga(token string, email string) (string, error) {
110+
func getOrganization(token string, email string) (string, error) {
111111
FakeConnection, err := api.NewScalewayAPI(api.ComputeAPI, api.AccountAPI, "", token)
112112
if err != nil {
113113
return "", fmt.Errorf("Unable to create a fake ScalewayAPI: %s", err)
114114
}
115-
FakeConnection.EnableAccountAPI()
116-
117-
resp, err := FakeConnection.GetResponse("organizations")
115+
data, err := FakeConnection.GetOrganization()
118116
if err != nil {
119117
return "", err
120118
}
121-
if resp.StatusCode != 200 {
122-
return "", fmt.Errorf("[%d] unable to GET", resp.StatusCode)
123-
}
124-
125-
var data api.ScalewayOrganizationsDefinition
126119

127-
defer resp.Body.Close()
128-
decoder := json.NewDecoder(resp.Body)
129-
err = decoder.Decode(&data)
130-
if err != nil {
131-
return "", err
132-
}
133120
orgaID := ""
134121

135122
for _, orga := range data.Organizations {
@@ -173,13 +160,36 @@ func connectAPI() (string, string, error) {
173160
if err != nil {
174161
return "", "", err
175162
}
176-
orga, err = getOrga(token, connect.Email)
163+
orga, err = getOrganization(token, connect.Email)
177164
if err != nil {
178165
return "", "", err
179166
}
180167
return orga, token, nil
181168
}
182169

170+
// uploadSSHKeys uploads an SSH Key
171+
func uploadSSHKeys(apiConnection *api.ScalewayAPI, newKey string) {
172+
user, err := apiConnection.GetUser()
173+
if err != nil {
174+
logrus.Errorf("Unable to contact ScalewayAPI: %s", err)
175+
} else {
176+
user.SSHPublicKeys = append(user.SSHPublicKeys, api.ScalewayKeyDefinition{Key: strings.Trim(newKey, "\n")})
177+
178+
SSHKeys := api.ScalewayUserPatchSSHKeyDefinition{
179+
SSHPublicKeys: user.SSHPublicKeys,
180+
}
181+
182+
userID, err := apiConnection.GetUserID()
183+
if err != nil {
184+
logrus.Errorf("Unable to get userID: %s", err)
185+
} else {
186+
if err = apiConnection.PatchUserSSHKey(userID, SSHKeys); err != nil {
187+
logrus.Errorf("Unable to patch SSHkey: %v", err)
188+
}
189+
}
190+
}
191+
}
192+
183193
// RunLogin is the handler for 'scw login'
184194
func RunLogin(ctx CommandContext, args LoginArgs) error {
185195
if args.Organization == "" || args.Token == "" {
@@ -211,21 +221,7 @@ func RunLogin(ctx CommandContext, args LoginArgs) error {
211221
logrus.Errorf("Unable to select a key: %v", err)
212222
} else {
213223
if args.SSHKey != "" {
214-
userID, err := apiConnection.GetUserID()
215-
if err != nil {
216-
logrus.Errorf("Unable to contact ScalewayAPI: %s", err)
217-
} else {
218-
219-
SSHKey := api.ScalewayUserPatchSSHKeyDefinition{
220-
SSHPublicKeys: []api.ScalewayKeyDefinition{{
221-
Key: strings.Trim(args.SSHKey, "\n"),
222-
}},
223-
}
224-
225-
if err = apiConnection.PatchUserSSHKey(userID, SSHKey); err != nil {
226-
logrus.Errorf("Unable to patch SSHkey: %v", err)
227-
}
228-
}
224+
uploadSSHKeys(apiConnection, args.SSHKey)
229225
}
230226
}
231227
}

0 commit comments

Comments
 (0)