@@ -24,6 +24,12 @@ import (
2424 "github.com/scaleway/scaleway-cli/vendor/github.com/moul/anonuuid"
2525)
2626
27+ // Default values
28+ var (
29+ ComputeAPI string = "https://api.scaleway.com/"
30+ AccountAPI string = "https://account.scaleway.com/"
31+ )
32+
2733// ScalewayAPI is the interface used to communicate with the Scaleway API
2834type ScalewayAPI struct {
2935 // ComputeAPI is the endpoint to the Scaleway API
@@ -499,35 +505,72 @@ type ScalewayImageDefinition struct {
499505 Arch string `json:"arch"`
500506}
501507
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"`
506512}
507513
508514// ScalewayTokenDefinition represents a Scaleway Token
509515type 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"`
516522}
517523
518524// ScalewayTokensDefinition represents a Scaleway Tokens
519525type ScalewayTokensDefinition struct {
520526 Tokens []ScalewayTokenDefinition `json:"tokens"`
521527}
522528
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 {
525568 Key string `json:"key"`
526569}
527570
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"`
531574}
532575
533576// FuncMap used for json inspection
@@ -821,10 +864,10 @@ func (s *ScalewayAPI) PostServer(definition ScalewayServerDefinition) (string, e
821864 return "" , error
822865}
823866
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 ()
828871 resp , err := s .PatchResponse (fmt .Sprintf ("users/%s" , UserID ), definition )
829872 if err != nil {
830873 return err
@@ -1267,8 +1310,8 @@ func (s *ScalewayAPI) GetTasks() (*[]ScalewayTask, error) {
12671310
12681311// CheckCredentials performs a dummy check to ensure we can contact the API
12691312func (s * ScalewayAPI ) CheckCredentials () error {
1270- s .enableAccountAPI ()
1271- defer s .disableAccountAPI ()
1313+ s .EnableAccountAPI ()
1314+ defer s .DisableAccountAPI ()
12721315 query := url.Values {}
12731316 query .Set ("token_id" , s .Token )
12741317 resp , err := s .GetResponse ("tokens?" + query .Encode ())
@@ -1283,8 +1326,8 @@ func (s *ScalewayAPI) CheckCredentials() error {
12831326
12841327// GetUserID returns the UserID
12851328func (s * ScalewayAPI ) GetUserID () (string , error ) {
1286- s .enableAccountAPI ()
1287- defer s .disableAccountAPI ()
1329+ s .EnableAccountAPI ()
1330+ defer s .DisableAccountAPI ()
12881331 resp , err := s .GetResponse ("tokens" )
12891332 if err != nil {
12901333 return "" , err
@@ -1300,7 +1343,7 @@ func (s *ScalewayAPI) GetUserID() (string, error) {
13001343 return "" , err
13011344 }
13021345 if len (tokens .Tokens ) == 0 {
1303- return "" , fmt .Errorf ("Unable to get tokens" )
1346+ return "" , fmt .Errorf ("unable to get tokens" )
13041347 }
13051348 return tokens .Tokens [0 ].UserID , nil
13061349}
@@ -1409,15 +1452,22 @@ func (s *ScalewayAPI) GetBootscriptID(needle string) string {
14091452
14101453// HideAPICredentials removes API credentials from a string
14111454func (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+ }
14141462 return output
14151463}
14161464
1417- func (s * ScalewayAPI ) enableAccountAPI () {
1465+ // EnableAccountAPI enable accountAPI
1466+ func (s * ScalewayAPI ) EnableAccountAPI () {
14181467 s .APIUrl = s .AccountAPI
14191468}
14201469
1421- func (s * ScalewayAPI ) disableAccountAPI () {
1470+ // DisableAccountAPI disable accountAPI
1471+ func (s * ScalewayAPI ) DisableAccountAPI () {
14221472 s .APIUrl = s .ComputeAPI
14231473}
0 commit comments