@@ -400,7 +400,7 @@ type ScalewayServer struct {
400
400
// StateDetail is the detailed status of the server
401
401
StateDetail string `json:"state_detail,omitempty"`
402
402
403
- // PrivateIP reprensents the private IPV4 attached to the server (changes on each boot)
403
+ // PrivateIP represents the private IPV4 attached to the server (changes on each boot)
404
404
PrivateIP string `json:"private_ip,omitempty"`
405
405
406
406
// Bootscript is the unique identifier of the selected bootscript
@@ -497,6 +497,37 @@ type ScalewayImageDefinition struct {
497
497
Arch string `json:"arch"`
498
498
}
499
499
500
+ // ScalewayTokenUserIDRoleDefinition represents a Scaleway Token UserId Role
501
+ type ScalewayTokenUserIDRoleDefinition struct {
502
+ Organization string `json:"organization,omitempty"`
503
+ Role string `json:"role,omitempty"`
504
+ }
505
+
506
+ // ScalewayTokenDefinition represents a Scaleway Token
507
+ type ScalewayTokenDefinition struct {
508
+ UserID string `json:"user_id"`
509
+ Description string `json:"description,omitempty"`
510
+ Roles ScalewayTokenUserIDRoleDefinition `json:"roles"`
511
+ Expires string `json:"expires"`
512
+ InheritsUsersPerms bool `json:"inherits_user_perms"`
513
+ ID string `json:"id"`
514
+ }
515
+
516
+ // ScalewayTokensDefinition represents a Scaleway Tokens
517
+ type ScalewayTokensDefinition struct {
518
+ Tokens []ScalewayTokenDefinition `json:"tokens"`
519
+ }
520
+
521
+ // ScalewayUserPatchKeyDefinition represents a key
522
+ type ScalewayUserPatchKeyDefinition struct {
523
+ Key string `json:"key"`
524
+ }
525
+
526
+ // ScalewayUserPatchDefinition represents a User Patch
527
+ type ScalewayUserPatchDefinition struct {
528
+ SSHPublicKeys []ScalewayUserPatchKeyDefinition `json:"ssh_public_keys"`
529
+ }
530
+
500
531
// FuncMap used for json inspection
501
532
var FuncMap = template.FuncMap {
502
533
"json" : func (v interface {}) string {
@@ -752,7 +783,7 @@ func (s *ScalewayAPI) DeleteServer(serverID string) error {
752
783
func (s * ScalewayAPI ) PostServer (definition ScalewayServerDefinition ) (string , error ) {
753
784
definition .Organization = s .Organization
754
785
755
- resp , err := s .PostResponse (fmt . Sprintf ( "servers" ) , definition )
786
+ resp , err := s .PostResponse ("servers" , definition )
756
787
if err != nil {
757
788
return "" , err
758
789
}
@@ -783,6 +814,34 @@ func (s *ScalewayAPI) PostServer(definition ScalewayServerDefinition) (string, e
783
814
return "" , error
784
815
}
785
816
817
+ // PatchUser updates a user
818
+ func (s * ScalewayAPI ) PatchUser (UserID string , definition ScalewayUserPatchDefinition ) error {
819
+ s .enableAccountAPI ()
820
+ defer s .disableAccountAPI ()
821
+ resp , err := s .PatchResponse (fmt .Sprintf ("users/%s" , UserID ), definition )
822
+ if err != nil {
823
+ return err
824
+ }
825
+
826
+ defer resp .Body .Close ()
827
+ decoder := json .NewDecoder (resp .Body )
828
+
829
+ // Succeed PATCH code
830
+ if resp .StatusCode == 200 {
831
+ return nil
832
+ }
833
+
834
+ var error ScalewayAPIError
835
+ err = decoder .Decode (& error )
836
+ if err != nil {
837
+ return err
838
+ }
839
+
840
+ error .StatusCode = resp .StatusCode
841
+ error .Debug ()
842
+ return error
843
+ }
844
+
786
845
// PatchServer updates a server
787
846
func (s * ScalewayAPI ) PatchServer (serverID string , definition ScalewayServerPatchDefinition ) error {
788
847
resp , err := s .PatchResponse (fmt .Sprintf ("servers/%s" , serverID ), definition )
@@ -894,7 +953,7 @@ func (s *ScalewayAPI) PostVolume(definition ScalewayVolumeDefinition) (string, e
894
953
if definition .Type == "" {
895
954
definition .Type = "l_ssd"
896
955
}
897
- resp , err := s .PostResponse (fmt . Sprintf ( "volumes" ) , definition )
956
+ resp , err := s .PostResponse ("volumes" , definition )
898
957
if err != nil {
899
958
return "" , err
900
959
}
@@ -1201,20 +1260,46 @@ func (s *ScalewayAPI) GetTasks() (*[]ScalewayTask, error) {
1201
1260
1202
1261
// CheckCredentials performs a dummy check to ensure we can contact the API
1203
1262
func (s * ScalewayAPI ) CheckCredentials () error {
1204
- s .enableAccountApi ()
1205
- defer s .disableAccountApi ()
1263
+ s .enableAccountAPI ()
1264
+ defer s .disableAccountAPI ()
1206
1265
query := url.Values {}
1207
1266
query .Set ("token_id" , s .Token )
1208
1267
resp , err := s .GetResponse ("tokens?" + query .Encode ())
1209
1268
if err != nil {
1210
1269
return err
1211
1270
}
1212
1271
if resp .StatusCode != 200 {
1213
- return fmt .Errorf ("invalid credentials" )
1272
+ return fmt .Errorf ("[%d] invalid credentials" , resp . StatusCode )
1214
1273
}
1215
1274
return nil
1216
1275
}
1217
1276
1277
+ // GetUserID returns the UserID
1278
+ func (s * ScalewayAPI ) GetUserID () (string , error ) {
1279
+ s .enableAccountAPI ()
1280
+ defer s .disableAccountAPI ()
1281
+ resp , err := s .GetResponse ("tokens" )
1282
+ if err != nil {
1283
+ return "" , err
1284
+ }
1285
+ if resp .StatusCode != 200 {
1286
+ return "" , fmt .Errorf ("[%d] invalid credentials" , resp .StatusCode )
1287
+ }
1288
+ defer resp .Body .Close ()
1289
+ var tokens ScalewayTokensDefinition
1290
+ decoder := json .NewDecoder (resp .Body )
1291
+ err = decoder .Decode (& tokens )
1292
+ if err != nil {
1293
+ return "" , err
1294
+ }
1295
+ if len (tokens .Tokens ) == 0 {
1296
+ return "" , fmt .Errorf ("Unable to get tokens" )
1297
+ }
1298
+ return tokens .Tokens [0 ].UserID , nil
1299
+ }
1300
+
1301
+ //
1302
+
1218
1303
// GetServerID returns exactly one server matching or dies
1219
1304
func (s * ScalewayAPI ) GetServerID (needle string ) string {
1220
1305
// Parses optional type prefix, i.e: "server:name" -> "name"
@@ -1322,10 +1407,10 @@ func (s *ScalewayAPI) HideAPICredentials(input string) string {
1322
1407
return output
1323
1408
}
1324
1409
1325
- func (s * ScalewayAPI ) enableAccountApi () {
1410
+ func (s * ScalewayAPI ) enableAccountAPI () {
1326
1411
s .APIUrl = s .AccountAPI
1327
1412
}
1328
1413
1329
- func (s * ScalewayAPI ) disableAccountApi () {
1414
+ func (s * ScalewayAPI ) disableAccountAPI () {
1330
1415
s .APIUrl = s .ComputeAPI
1331
1416
}
0 commit comments