Skip to content

Commit

Permalink
Merge pull request #167 from moul/fix-166
Browse files Browse the repository at this point in the history
Added 'scw _userdata local' option (fix #166)
  • Loading branch information
moul committed Sep 7, 2015
2 parents 1b4d37d + cdd441c commit e153bc9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
* `scw info` now prints user/organization info from the API ([#142](https://github.com/scaleway/scaleway-cli/issues/130)
* Added helpers to manipulate new `user_data` API ([#150](https://github.com/scaleway/scaleway-cli/issues/150))
* Support of `scw rm -f/--force` option ([#158](https://github.com/scaleway/scaleway-cli/issues/158))
* Added `scw _userdata local ...` option which interacts with the Metadata API without authentication ([#166](https://github.com/scaleway/scaleway-cli/issues/166))

#### Fixes

Expand Down
57 changes: 49 additions & 8 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ import (

// Default values
var (
ComputeAPI string = "https://api.scaleway.com/"
AccountAPI string = "https://account.scaleway.com/"
ComputeAPI string = "https://api.scaleway.com/"
AccountAPI string = "https://account.scaleway.com/"
MetadataAPI string = "http://169.254.42.42/"
)

// ScalewayAPI is the interface used to communicate with the Scaleway API
Expand All @@ -55,8 +56,9 @@ type ScalewayAPI struct {
// Cache is used to quickly resolve identifiers from names
Cache *ScalewayCache

client *http.Client
anonuuid anonuuid.AnonUUID
client *http.Client
anonuuid anonuuid.AnonUUID
isMetadata bool
}

// ScalewayAPIError represents a Scaleway API Error
Expand Down Expand Up @@ -1355,7 +1357,14 @@ type ScalewayUserdatas struct {

// GetUserdatas gets list of userdata for a server
func (s *ScalewayAPI) GetUserdatas(serverID string) (*ScalewayUserdatas, error) {
resp, err := s.GetResponse("servers/" + serverID + "/user_data")
var url string
if s.isMetadata {
url = "/user_data"
} else {
url = fmt.Sprintf("servers/%s/user_data", serverID)
}

resp, err := s.GetResponse(url)
if err != nil {
return nil, err
}
Expand All @@ -1381,7 +1390,14 @@ func (s *ScalewayAPI) GetUserdata(serverID string, key string) (*ScalewayUserdat
var data ScalewayUserdata
var err error

resp, err := s.GetResponse("servers/" + serverID + "/user_data/" + key)
var url string
if s.isMetadata {
url = fmt.Sprintf("/user_data/%s", key)
} else {
url = fmt.Sprintf("servers/%s/user_data/%s", serverID, key)
}

resp, err := s.GetResponse(url)
if err != nil {
return nil, err
}
Expand All @@ -1397,7 +1413,13 @@ func (s *ScalewayAPI) GetUserdata(serverID string, key string) (*ScalewayUserdat

// PatchUserdata sets a user data
func (s *ScalewayAPI) PatchUserdata(serverID string, key string, value []byte) error {
resource := fmt.Sprintf("servers/%s/user_data/%s", serverID, key)
var resource string
if s.isMetadata {
resource = fmt.Sprintf("/user_data/%s", key)
} else {
resource = fmt.Sprintf("servers/%s/user_data/%s", serverID, key)
}

uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource)
payload := new(bytes.Buffer)
payload.Write(value)
Expand Down Expand Up @@ -1432,7 +1454,14 @@ func (s *ScalewayAPI) PatchUserdata(serverID string, key string, value []byte) e

// DeleteUserdata deletes a server user_data
func (s *ScalewayAPI) DeleteUserdata(serverID string, key string) error {
resp, err := s.DeleteResponse(fmt.Sprintf("servers/%s/user_data/%s", serverID, key))
var url string
if s.isMetadata {
url = fmt.Sprintf("/user_data/%s", key)
} else {
url = fmt.Sprintf("servers/%s/user_data/%s", serverID, key)
}

resp, err := s.DeleteResponse(url)
if err != nil {
return err
}
Expand Down Expand Up @@ -1687,6 +1716,18 @@ func (s *ScalewayAPI) DisableAccountAPI() {
s.APIUrl = s.ComputeAPI
}

// EnableMetadataAPI enable metadataAPI
func (s *ScalewayAPI) EnableMetadataAPI() {
s.APIUrl = MetadataAPI
s.isMetadata = true
}

// DisableMetadataAPI disable metadataAPI
func (s *ScalewayAPI) DisableMetadataAPI() {
s.APIUrl = s.ComputeAPI
s.isMetadata = false
}

// SetPassword register the password
func (s *ScalewayAPI) SetPassword(password string) {
s.password = password
Expand Down
10 changes: 9 additions & 1 deletion pkg/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ func Start(rawArgs []string, streams *commands.Streams) (int, error) {
if err != nil {
return 1, fmt.Errorf("usage: scw %s", cmd.UsageLine)
}
if cmd.Name() != "login" && cmd.Name() != "help" && cmd.Name() != "version" {
switch cmd.Name() {
case "login", "help", "version":
// commands that don't need API
case "_userdata":
// commands that may need API
api, _ := getScalewayAPI()
cmd.API = api
default:
// commands that do need API
if cfgErr != nil {
if name != "login" && config == nil {
logrus.Debugf("cfgErr: %v", cfgErr)
Expand Down
27 changes: 22 additions & 5 deletions pkg/cli/x_userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package cli
import (
"fmt"
"strings"

"github.com/scaleway/scaleway-cli/pkg/api"
)

var cmdUserdata = &Command{
Expand Down Expand Up @@ -39,12 +41,27 @@ func runUserdata(cmd *Command, args []string) error {
}

ctx := cmd.GetContext(args)
serverID := ctx.API.GetServerID(args[0])
var Api *api.ScalewayAPI
var err error
var serverID string
if args[0] == "local" {
Api, err = api.NewScalewayAPI("", "", "", "")
if err != nil {
return err
}
Api.EnableMetadataAPI()
} else {
if ctx.API == nil {
return fmt.Errorf("You need to login first: 'scw login'")
}
serverID = ctx.API.GetServerID(args[0])
Api = ctx.API
}

switch len(args) {
case 1:
// List userdata
res, err := ctx.API.GetUserdatas(serverID)
res, err := Api.GetUserdatas(serverID)
if err != nil {
return err
}
Expand All @@ -57,7 +74,7 @@ func runUserdata(cmd *Command, args []string) error {
switch len(parts) {
case 1:
// Get userdatas
res, err := ctx.API.GetUserdata(serverID, key)
res, err := Api.GetUserdata(serverID, key)
if err != nil {
return err
}
Expand All @@ -66,14 +83,14 @@ func runUserdata(cmd *Command, args []string) error {
value := parts[1]
if value != "" {
// Set userdata
err := ctx.API.PatchUserdata(serverID, key, []byte(value))
err := Api.PatchUserdata(serverID, key, []byte(value))
if err != nil {
return err
}
fmt.Fprintln(ctx.Stdout, key)
} else {
// Delete userdata
err := ctx.API.DeleteUserdata(serverID, key)
err := Api.DeleteUserdata(serverID, key)
if err != nil {
return err
}
Expand Down

0 comments on commit e153bc9

Please sign in to comment.