@@ -24,6 +24,12 @@ import (
24
24
"github.com/scaleway/scaleway-cli/vendor/github.com/moul/anonuuid"
25
25
)
26
26
27
+ // Default values
28
+ var (
29
+ ComputeAPI string = "https://api.scaleway.com/"
30
+ AccountAPI string = "https://account.scaleway.com/"
31
+ )
32
+
27
33
// ScalewayAPI is the interface used to communicate with the Scaleway API
28
34
type ScalewayAPI struct {
29
35
// ComputeAPI is the endpoint to the Scaleway API
@@ -499,35 +505,72 @@ type ScalewayImageDefinition struct {
499
505
Arch string `json:"arch"`
500
506
}
501
507
502
- // ScalewayTokenUserIDRoleDefinition represents a Scaleway Token UserId Role
503
- type ScalewayTokenUserIDRoleDefinition struct {
504
- Organization string `json:"organization,omitempty"`
505
- Role string `json:"role,omitempty"`
508
+ // ScalewayRoleDefinition represents a Scaleway Token UserId Role
509
+ type ScalewayRoleDefinition struct {
510
+ Organization ScalewayOrganizationDefinition `json:"organization,omitempty"`
511
+ Role string `json:"role,omitempty"`
506
512
}
507
513
508
514
// ScalewayTokenDefinition represents a Scaleway Token
509
515
type ScalewayTokenDefinition struct {
510
- UserID string `json:"user_id"`
511
- Description string `json:"description,omitempty"`
512
- Roles ScalewayTokenUserIDRoleDefinition `json:"roles"`
513
- Expires string `json:"expires"`
514
- InheritsUsersPerms bool `json:"inherits_user_perms"`
515
- ID string `json:"id"`
516
+ UserID string `json:"user_id"`
517
+ Description string `json:"description,omitempty"`
518
+ Roles ScalewayRoleDefinition `json:"roles"`
519
+ Expires string `json:"expires"`
520
+ InheritsUsersPerms bool `json:"inherits_user_perms"`
521
+ ID string `json:"id"`
516
522
}
517
523
518
524
// ScalewayTokensDefinition represents a Scaleway Tokens
519
525
type ScalewayTokensDefinition struct {
520
526
Tokens []ScalewayTokenDefinition `json:"tokens"`
521
527
}
522
528
523
- // ScalewayUserPatchKeyDefinition represents a key
524
- type ScalewayUserPatchKeyDefinition struct {
529
+ // ScalewayConnectResponse represents the answer from POST /tokens
530
+ type ScalewayConnectResponse struct {
531
+ Token ScalewayTokenDefinition `json:"token"`
532
+ }
533
+
534
+ // ScalewayConnect represents the data to connect
535
+ type ScalewayConnect struct {
536
+ Email string `json:"email"`
537
+ Password string `json:"password"`
538
+ Description string `json:"description"`
539
+ Expires bool `json:"expires"`
540
+ }
541
+
542
+ // ScalewayOrganizationDefinition represents a Scaleway Organization
543
+ type ScalewayOrganizationDefinition struct {
544
+ ID string `json:"id"`
545
+ Name string `json:"name"`
546
+ Users []ScalewayUserDefinition `json:"users"`
547
+ }
548
+
549
+ // ScalewayOrganizationsDefinition represents a Scaleway Organizations
550
+ type ScalewayOrganizationsDefinition struct {
551
+ Organizations []ScalewayOrganizationDefinition `json:"organizations"`
552
+ }
553
+
554
+ // ScalewayUserDefinition represents a Scaleway User
555
+ type ScalewayUserDefinition struct {
556
+ Email string `json:"email"`
557
+ Firstname string `json:"firstname"`
558
+ Fullname string `json:"fullname"`
559
+ ID string `json:"id"`
560
+ Lastname string `json:"lastname"`
561
+ Organizations []ScalewayOrganizationDefinition `json:"organizations"`
562
+ Roles []ScalewayRoleDefinition `json:"roles"`
563
+ SSHPublicKeys []ScalewayKeyDefinition `json:"ssh_public_keys"`
564
+ }
565
+
566
+ // ScalewayKeyDefinition represents a key
567
+ type ScalewayKeyDefinition struct {
525
568
Key string `json:"key"`
526
569
}
527
570
528
- // ScalewayUserPatchDefinition represents a User Patch
529
- type ScalewayUserPatchDefinition struct {
530
- SSHPublicKeys []ScalewayUserPatchKeyDefinition `json:"ssh_public_keys"`
571
+ // ScalewayUserPatchSSHKeyDefinition represents a User Patch
572
+ type ScalewayUserPatchSSHKeyDefinition struct {
573
+ SSHPublicKeys []ScalewayKeyDefinition `json:"ssh_public_keys"`
531
574
}
532
575
533
576
// FuncMap used for json inspection
@@ -821,10 +864,10 @@ func (s *ScalewayAPI) PostServer(definition ScalewayServerDefinition) (string, e
821
864
return "" , error
822
865
}
823
866
824
- // PatchUser updates a user
825
- func (s * ScalewayAPI ) PatchUser (UserID string , definition ScalewayUserPatchDefinition ) error {
826
- s .enableAccountAPI ()
827
- defer s .disableAccountAPI ()
867
+ // PatchUserSSHKey updates a user
868
+ func (s * ScalewayAPI ) PatchUserSSHKey (UserID string , definition ScalewayUserPatchSSHKeyDefinition ) error {
869
+ s .EnableAccountAPI ()
870
+ defer s .DisableAccountAPI ()
828
871
resp , err := s .PatchResponse (fmt .Sprintf ("users/%s" , UserID ), definition )
829
872
if err != nil {
830
873
return err
@@ -1267,8 +1310,8 @@ func (s *ScalewayAPI) GetTasks() (*[]ScalewayTask, error) {
1267
1310
1268
1311
// CheckCredentials performs a dummy check to ensure we can contact the API
1269
1312
func (s * ScalewayAPI ) CheckCredentials () error {
1270
- s .enableAccountAPI ()
1271
- defer s .disableAccountAPI ()
1313
+ s .EnableAccountAPI ()
1314
+ defer s .DisableAccountAPI ()
1272
1315
query := url.Values {}
1273
1316
query .Set ("token_id" , s .Token )
1274
1317
resp , err := s .GetResponse ("tokens?" + query .Encode ())
@@ -1283,8 +1326,8 @@ func (s *ScalewayAPI) CheckCredentials() error {
1283
1326
1284
1327
// GetUserID returns the UserID
1285
1328
func (s * ScalewayAPI ) GetUserID () (string , error ) {
1286
- s .enableAccountAPI ()
1287
- defer s .disableAccountAPI ()
1329
+ s .EnableAccountAPI ()
1330
+ defer s .DisableAccountAPI ()
1288
1331
resp , err := s .GetResponse ("tokens" )
1289
1332
if err != nil {
1290
1333
return "" , err
@@ -1300,7 +1343,7 @@ func (s *ScalewayAPI) GetUserID() (string, error) {
1300
1343
return "" , err
1301
1344
}
1302
1345
if len (tokens .Tokens ) == 0 {
1303
- return "" , fmt .Errorf ("Unable to get tokens" )
1346
+ return "" , fmt .Errorf ("unable to get tokens" )
1304
1347
}
1305
1348
return tokens .Tokens [0 ].UserID , nil
1306
1349
}
@@ -1409,15 +1452,22 @@ func (s *ScalewayAPI) GetBootscriptID(needle string) string {
1409
1452
1410
1453
// HideAPICredentials removes API credentials from a string
1411
1454
func (s * ScalewayAPI ) HideAPICredentials (input string ) string {
1412
- output := strings .Replace (input , s .Token , s .anonuuid .FakeUUID (s .Token ), - 1 )
1413
- output = strings .Replace (output , s .Organization , s .anonuuid .FakeUUID (s .Organization ), - 1 )
1455
+ output := input
1456
+ if s .Token != "" {
1457
+ output = strings .Replace (output , s .Token , s .anonuuid .FakeUUID (s .Token ), - 1 )
1458
+ }
1459
+ if s .Organization != "" {
1460
+ output = strings .Replace (output , s .Organization , s .anonuuid .FakeUUID (s .Organization ), - 1 )
1461
+ }
1414
1462
return output
1415
1463
}
1416
1464
1417
- func (s * ScalewayAPI ) enableAccountAPI () {
1465
+ // EnableAccountAPI enable accountAPI
1466
+ func (s * ScalewayAPI ) EnableAccountAPI () {
1418
1467
s .APIUrl = s .AccountAPI
1419
1468
}
1420
1469
1421
- func (s * ScalewayAPI ) disableAccountAPI () {
1470
+ // DisableAccountAPI disable accountAPI
1471
+ func (s * ScalewayAPI ) DisableAccountAPI () {
1422
1472
s .APIUrl = s .ComputeAPI
1423
1473
}
0 commit comments