Skip to content

Added _ips #196

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

Merged
merged 1 commit into from
Sep 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'

### master (unreleased)

* Support of 'scw _ips' command ... ([#196](https://github.com/scaleway/scaleway-cli/issues/196))
* Report **permissions** in `scw info` ([#191](https://github.com/scaleway/scaleway-cli/issues/191))
* Report **dashboard** statistics in `scw info` ([#177](https://github.com/scaleway/scaleway-cli/issues/177))
* Support of `scw _userdata name VAR=@/path/to/file` ([#183](https://github.com/scaleway/scaleway-cli/issues/183))
Expand Down
146 changes: 146 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,25 @@ type ScalewayGetSecurityGroup struct {
SecurityGroups ScalewaySecurityGroups `json:"security_group"`
}

type ScalewayIPDefinition struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayIPDefinition should have comment or be unexported

Organization string `json:"organization"`
Reverse string `json:"reverse"`
ID string `json:"id"`
Server struct {
Identifier string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
} `json:"server,omitempty"`
Address string `json:"address"`
}

type ScalewayGetIPS struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayGetIPS should have comment or be unexported

IPS []ScalewayIPDefinition `json:"ips"`
}

type ScalewayGetIP struct {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported type ScalewayGetIP should have comment or be unexported

IP ScalewayIPDefinition `json:"ip"`
}

// ScalewaySecurityGroup represents a Scaleway security group
type ScalewaySecurityGroup struct {
// Identifier is a unique identifier for the security group
Expand Down Expand Up @@ -1872,6 +1891,133 @@ func (s *ScalewayAPI) GetASecurityGroup(groupsID string) (*ScalewayGetSecurityGr
return &securityGroups, nil
}

// GetIPS returns a ScalewayGetIPS
func (s *ScalewayAPI) GetIPS() (*ScalewayGetIPS, error) {
resp, err := s.GetResponse("ips")
if err != nil {
return nil, err
}
defer resp.Body.Close()

var ips ScalewayGetIPS
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&ips)
if err != nil {
return nil, err
}
return &ips, nil
}

// NewIP returns a new IP
func (s *ScalewayAPI) NewIP() (*ScalewayGetIP, error) {
var orga struct {
Organization string `json:"organization"`
}
orga.Organization = s.Organization
resp, err := s.PostResponse("ips", orga)
if err != nil {
return nil, err
}
defer resp.Body.Close()

// Succeed POST code
if resp.StatusCode == 201 {
var ip ScalewayGetIP
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&ip)
if err != nil {
return nil, err
}
return &ip, nil
}
var error ScalewayAPIError
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&error)
if err != nil {
return nil, err
}
error.StatusCode = resp.StatusCode
error.Debug()
return nil, error
}

// AttachIP attachs an IP to a server
func (s *ScalewayAPI) AttachIP(ipID, serverID string) error {
var update struct {
Address string `json:"address"`
ID string `json:"id"`
Reverse *string `json:"reverse"`
Organization string `json:"organization"`
Server string `json:"server"`
}

ip, err := s.GetIP(idIP)
if err != nil {
return err
}
update.Address = ip.IP.Address
update.ID = ip.IP.ID
update.Organization = ip.IP.Organization
update.Server = serverID
resp, err := s.PutResponse(fmt.Sprintf("ips/%s", ipID), update)
if err != nil {
return err
}
if resp.StatusCode == 200 {
return nil
}
var error ScalewayAPIError
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&error)
if err != nil {
return err
}
error.StatusCode = resp.StatusCode
error.Debug()
return error
}

// DeleteIP deletes an IP
func (s *ScalewayAPI) DeleteIP(ipID string) error {
resp, err := s.DeleteResponse(fmt.Sprintf("ips/%s", ipID))
if err != nil {
return err
}
defer resp.Body.Close()

// Succeed PUT code
if resp.StatusCode == 204 {
return nil
}

var error ScalewayAPIError
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&error)
if err != nil {
return err
}
error.StatusCode = resp.StatusCode
error.Debug()
return error
}

// GetIP returns a ScalewayGetIP
func (s *ScalewayAPI) GetIP(ipID string) (*ScalewayGetIP, error) {
resp, err := s.GetResponse(fmt.Sprintf("ips/%s", ipID))
if err != nil {
return nil, err
}
defer resp.Body.Close()

var ip ScalewayGetIP
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&ip)
if err != nil {
return nil, err
}
return &ip, nil
}

// GetBootscriptID returns exactly one bootscript matching or dies
func (s *ScalewayAPI) GetBootscriptID(needle string) string {
// Parses optional type prefix, i.e: "bootscript:name" -> "name"
Expand Down
1 change: 1 addition & 0 deletions pkg/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ var Commands = []*Command{
cmdFlushCache,
cmdPatch,
cmdSecurityGroups,
cmdIPS,
}