Skip to content

Commit 74bc859

Browse files
committed
Merge pull request #106 from QuentinPerez/account_api
add account api in the config file
2 parents 0d048dc + f573d92 commit 74bc859

File tree

6 files changed

+86
-51
lines changed

6 files changed

+86
-51
lines changed

cmd/scw/main.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func main() {
6363
}
6464

6565
if config != nil {
66-
flAPIEndPoint = flag.String([]string{"-api-endpoint"}, config.APIEndPoint, "Set the API endpoint")
66+
flAPIEndPoint = flag.String([]string{"-api-endpoint"}, config.ComputeAPI, "Set the API endpoint")
6767
}
6868
flag.Parse()
6969

@@ -157,8 +157,13 @@ func getConfig() (*api.Config, error) {
157157
if err != nil {
158158
return nil, err
159159
}
160+
// check if he has an old scwrc version
161+
if config.AccountAPI == "" {
162+
config.AccountAPI = "https://account.scaleway.com"
163+
config.Save()
164+
}
160165
if os.Getenv("scaleway_api_endpoint") == "" {
161-
os.Setenv("scaleway_api_endpoint", config.APIEndPoint)
166+
os.Setenv("scaleway_api_endpoint", config.ComputeAPI)
162167
}
163168
return &config, nil
164169
}
@@ -170,7 +175,7 @@ func getScalewayAPI() (*api.ScalewayAPI, error) {
170175
if err != nil {
171176
return nil, err
172177
}
173-
return api.NewScalewayAPI(os.Getenv("scaleway_api_endpoint"), config.Organization, config.Token)
178+
return api.NewScalewayAPI(config.ComputeAPI, config.AccountAPI, config.Organization, config.Token)
174179
}
175180

176181
func showVersion() {

pkg/api/api.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,14 @@ import (
2525

2626
// ScalewayAPI is the interface used to communicate with the Scaleway API
2727
type ScalewayAPI struct {
28-
// APIEndpoint is the endpoint to the Scaleway API
29-
APIEndPoint string
28+
// ComputeAPI is the endpoint to the Scaleway API
29+
ComputeAPI string
30+
31+
// AccountAPI is the endpoint to the Scaleway Account API
32+
AccountAPI string
33+
34+
// APIEndPoint or ACCOUNTEndPoint
35+
APIUrl string
3036

3137
// Organization is the identifier of the Scaleway organization
3238
Organization string
@@ -500,13 +506,15 @@ var FuncMap = template.FuncMap{
500506
}
501507

502508
// NewScalewayAPI creates a ready-to-use ScalewayAPI client
503-
func NewScalewayAPI(endpoint, organization, token string) (*ScalewayAPI, error) {
509+
func NewScalewayAPI(apiEndPoint, accountEndPoint, organization, token string) (*ScalewayAPI, error) {
504510
cache, err := NewScalewayCache()
505511
if err != nil {
506512
return nil, err
507513
}
508514
s := &ScalewayAPI{
509-
APIEndPoint: endpoint,
515+
ComputeAPI: apiEndPoint,
516+
AccountAPI: accountEndPoint,
517+
APIUrl: apiEndPoint,
510518
Organization: organization,
511519
Token: token,
512520
Cache: cache,
@@ -523,7 +531,7 @@ func (s *ScalewayAPI) Sync() {
523531

524532
// GetResponse returns an http.Response object for the requested resource
525533
func (s *ScalewayAPI) GetResponse(resource string) (*http.Response, error) {
526-
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIEndPoint, "/"), resource)
534+
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
527535
log.Debugf("GET %s", uri)
528536
client := &http.Client{}
529537
req, err := http.NewRequest("GET", uri, nil)
@@ -537,7 +545,7 @@ func (s *ScalewayAPI) GetResponse(resource string) (*http.Response, error) {
537545

538546
// PostResponse returns an http.Response object for the updated resource
539547
func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Response, error) {
540-
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIEndPoint, "/"), resource)
548+
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
541549
client := &http.Client{}
542550
payload := new(bytes.Buffer)
543551
encoder := json.NewEncoder(payload)
@@ -562,7 +570,7 @@ func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Res
562570

563571
// PatchResponse returns an http.Response object for the updated resource
564572
func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Response, error) {
565-
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIEndPoint, "/"), resource)
573+
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
566574
client := &http.Client{}
567575
payload := new(bytes.Buffer)
568576
encoder := json.NewEncoder(payload)
@@ -587,7 +595,7 @@ func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Re
587595

588596
// PutResponse returns an http.Response object for the updated resource
589597
func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Response, error) {
590-
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIEndPoint, "/"), resource)
598+
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
591599
client := &http.Client{}
592600
payload := new(bytes.Buffer)
593601
encoder := json.NewEncoder(payload)
@@ -612,7 +620,7 @@ func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Resp
612620

613621
// DeleteResponse returns an http.Response object for the deleted resource
614622
func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) {
615-
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIEndPoint, "/"), resource)
623+
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
616624
client := &http.Client{}
617625
log.Debugf("DELETE %s", uri)
618626
req, err := http.NewRequest("DELETE", uri, nil)
@@ -1193,6 +1201,8 @@ func (s *ScalewayAPI) GetTasks() (*[]ScalewayTask, error) {
11931201

11941202
// CheckCredentials performs a dummy check to ensure we can contact the API
11951203
func (s *ScalewayAPI) CheckCredentials() error {
1204+
s.enableAccountApi()
1205+
defer s.disableAccountApi()
11961206
query := url.Values{}
11971207
query.Set("token_id", s.Token)
11981208
resp, err := s.GetResponse("tokens?" + query.Encode())
@@ -1311,3 +1321,11 @@ func (s *ScalewayAPI) HideAPICredentials(input string) string {
13111321
output = strings.Replace(output, s.Organization, s.anonuuid.FakeUUID(s.Organization), -1)
13121322
return output
13131323
}
1324+
1325+
func (s *ScalewayAPI) enableAccountApi() {
1326+
s.APIUrl = s.AccountAPI
1327+
}
1328+
1329+
func (s *ScalewayAPI) disableAccountApi() {
1330+
s.APIUrl = s.ComputeAPI
1331+
}

pkg/api/config.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package api
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"github.com/scaleway/scaleway-cli/pkg/utils"
11+
"os"
12+
)
13+
14+
// Config is a Scaleway CLI configuration file
15+
type Config struct {
16+
// ComputeAPI is the endpoint to the Scaleway API
17+
ComputeAPI string `json:"api_endpoint"`
18+
19+
// AccountAPI is the endpoint to the Scaleway Account API
20+
AccountAPI string `json:"account_endpoint"`
21+
22+
// Organization is the identifier of the Scaleway orgnization
23+
Organization string `json:"organization"`
24+
25+
// Token is the authentication token for the Scaleway organization
26+
Token string `json:"token"`
27+
}
28+
29+
// Save write the config file
30+
func (c *Config) Save() error {
31+
scwrcPath, err := utils.GetConfigFilePath()
32+
if err != nil {
33+
return fmt.Errorf("Unable to get scwrc config file path: %s", err)
34+
}
35+
scwrc, err := os.OpenFile(scwrcPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
36+
if err != nil {
37+
return fmt.Errorf("Unable to create scwrc config file: %s", err)
38+
}
39+
defer scwrc.Close()
40+
encoder := json.NewEncoder(scwrc)
41+
err = encoder.Encode(c)
42+
if err != nil {
43+
return fmt.Errorf("Unable to encode scw config file: %s", err)
44+
}
45+
return nil
46+
}

pkg/api/types.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

pkg/commands/command_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func ExampleCommandContext() CommandContext {
17-
apiClient, err := api.NewScalewayAPI("https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
17+
apiClient, err := api.NewScalewayAPI("https://example.org/", "https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")
1818
if err != nil {
1919
panic(err)
2020
}

pkg/commands/login.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package commands
66

77
import (
88
"bufio"
9-
"encoding/json"
109
"fmt"
1110
"os"
1211
"strings"
@@ -15,7 +14,6 @@ import (
1514
"github.com/scaleway/scaleway-cli/vendor/golang.org/x/crypto/ssh/terminal"
1615

1716
"github.com/scaleway/scaleway-cli/pkg/api"
18-
"github.com/scaleway/scaleway-cli/pkg/utils"
1917
)
2018

2119
type LoginArgs struct {
@@ -34,36 +32,21 @@ func RunLogin(ctx CommandContext, args LoginArgs) error {
3432
}
3533

3634
cfg := &api.Config{
37-
APIEndPoint: "https://account.scaleway.com/",
35+
ComputeAPI: "https://api.scaleway.com/",
36+
AccountAPI: "https://account.scaleway.com/",
3837
Organization: strings.Trim(args.Organization, "\n"),
3938
Token: strings.Trim(args.Token, "\n"),
4039
}
4140

42-
api, err := api.NewScalewayAPI(cfg.APIEndPoint, cfg.Organization, cfg.Token)
41+
api, err := api.NewScalewayAPI(cfg.ComputeAPI, cfg.AccountAPI, cfg.Organization, cfg.Token)
4342
if err != nil {
4443
return fmt.Errorf("Unable to create ScalewayAPI: %s", err)
4544
}
4645
err = api.CheckCredentials()
4746
if err != nil {
4847
return fmt.Errorf("Unable to contact ScalewayAPI: %s", err)
4948
}
50-
51-
scwrcPath, err := utils.GetConfigFilePath()
52-
if err != nil {
53-
return fmt.Errorf("Unable to get scwrc config file path: %s", err)
54-
}
55-
scwrc, err := os.OpenFile(scwrcPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
56-
if err != nil {
57-
return fmt.Errorf("Unable to create scwrc config file: %s", err)
58-
}
59-
defer scwrc.Close()
60-
encoder := json.NewEncoder(scwrc)
61-
cfg.APIEndPoint = "https://api.scaleway.com/"
62-
err = encoder.Encode(cfg)
63-
if err != nil {
64-
return fmt.Errorf("Unable to encode scw config file: %s", err)
65-
}
66-
return nil
49+
return cfg.Save()
6750
}
6851

6952
func promptUser(prompt string, output *string, echo bool) {

0 commit comments

Comments
 (0)