-
Notifications
You must be signed in to change notification settings - Fork 152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new user_data API helpers (#150) #161
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ import ( | |
"crypto/tls" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
@@ -1322,6 +1323,98 @@ func (s *ScalewayAPI) GetBootscript(bootscriptID string) (*ScalewayBootscript, e | |
return &oneBootscript.Bootscript, nil | ||
} | ||
|
||
type ScalewayUserdatas struct { | ||
UserData []string `json:"user_data"` | ||
} | ||
|
||
// GetUserdatas gets list of userdata for a server | ||
func (s *ScalewayAPI) GetUserdatas(serverID string) (*ScalewayUserdatas, error) { | ||
resp, err := s.GetResponse("servers/" + serverID + "/user_data") | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
var userdatas ScalewayUserdatas | ||
decoder := json.NewDecoder(resp.Body) | ||
err = decoder.Decode(&userdatas) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &userdatas, nil | ||
} | ||
|
||
type ScalewayUserdata []byte | ||
|
||
func (s *ScalewayUserdata) String() string { | ||
return string(*s) | ||
} | ||
|
||
// GetUserdata gets a specific userdata for a server | ||
func (s *ScalewayAPI) GetUserdata(serverID string, key string) (*ScalewayUserdata, error) { | ||
var data ScalewayUserdata | ||
var err error | ||
resp, err := s.GetResponse("servers/" + serverID + "/user_data/" + key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("no such user_data %q (%d)", key, resp.StatusCode) | ||
} | ||
|
||
defer resp.Body.Close() | ||
data, err = ioutil.ReadAll(resp.Body) | ||
return &data, err | ||
} | ||
|
||
// 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) | ||
uri := fmt.Sprintf("%s/%s", strings.TrimRight(s.APIUrl, "/"), resource) | ||
payload := new(bytes.Buffer) | ||
payload.Write(value) | ||
|
||
req, err := http.NewRequest("PATCH", uri, payload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
req.Header.Set("X-Auth-Token", s.Token) | ||
req.Header.Set("Content-Type", "text/plain") | ||
|
||
curl, err := http2curl.GetCurlCommand(req) | ||
if os.Getenv("SCW_SENSITIVE") != "1" { | ||
log.Debug(s.HideAPICredentials(curl.String())) | ||
} else { | ||
log.Debug(curl.String()) | ||
} | ||
|
||
resp, err := s.client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode == 204 { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("cannot set user_data (%d)", resp.StatusCode) | ||
} | ||
|
||
// 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)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defer resp.Body.Close() |
||
// Succeed POST code | ||
if resp.StatusCode == 204 { | ||
return nil | ||
} | ||
return fmt.Errorf("cannot delete user_data (%d)", resp.StatusCode) | ||
} | ||
|
||
// GetTasks get the list of tasks from the ScalewayAPI | ||
func (s *ScalewayAPI) GetTasks() (*[]ScalewayTask, error) { | ||
query := url.Values{} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ var Commands = []*Command{ | |
cmdStop, | ||
cmdTag, | ||
cmdTop, | ||
cmdUserdata, | ||
cmdVersion, | ||
cmdWait, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (C) 2015 Scaleway. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.md file. | ||
|
||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var cmdUserdata = &Command{ | ||
Exec: runUserdata, | ||
UsageLine: "_userdata [OPTIONS] SERVER [FIELD[=VALUE]]", | ||
Description: "", | ||
Hidden: true, | ||
Help: "List, read and write and delete server's userdata", | ||
Examples: ` | ||
$ scw _userdata myserver | ||
$ scw _userdata myserver key | ||
$ scw _userdata myserver key=value | ||
$ scw _userdata myserver key="" | ||
`, | ||
} | ||
|
||
func init() { | ||
cmdUserdata.Flag.BoolVar(&userdataHelp, []string{"h", "-help"}, false, "Print usage") | ||
} | ||
|
||
// Flags | ||
var userdataHelp bool // -h, --help flag | ||
|
||
func runUserdata(cmd *Command, args []string) error { | ||
if userdataHelp { | ||
return cmd.PrintUsage() | ||
} | ||
if len(args) < 1 { | ||
return cmd.PrintShortUsage() | ||
} | ||
|
||
ctx := cmd.GetContext(args) | ||
serverID := ctx.API.GetServerID(args[0]) | ||
|
||
switch len(args) { | ||
case 1: | ||
// List userdata | ||
res, err := ctx.API.GetUserdatas(serverID) | ||
if err != nil { | ||
return err | ||
} | ||
for _, key := range res.UserData { | ||
fmt.Fprintln(ctx.Stdout, key) | ||
} | ||
default: | ||
parts := strings.Split(args[1], "=") | ||
key := parts[0] | ||
switch len(parts) { | ||
case 1: | ||
// Get userdatas | ||
res, err := ctx.API.GetUserdata(serverID, key) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(ctx.Stdout, "%s\n", res.String()) | ||
default: | ||
value := parts[1] | ||
if value != "" { | ||
// Set userdata | ||
err := ctx.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) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should not be after the line 1359 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep :)