Skip to content

Commit

Permalink
Merge pull request #82 from digitalocean/feature/tagging
Browse files Browse the repository at this point in the history
Add support for tagging api
  • Loading branch information
bryanl committed Mar 23, 2016
2 parents 719e6da + 960a9c9 commit 859ab8f
Show file tree
Hide file tree
Showing 7 changed files with 1,145 additions and 13 deletions.
96 changes: 96 additions & 0 deletions droplet_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,31 @@ type ActionRequest map[string]interface{}
// See: https://developers.digitalocean.com/documentation/v2#droplet-actions
type DropletActionsService interface {
Shutdown(int) (*Action, *Response, error)
ShutdownByTag(string) (*Action, *Response, error)
PowerOff(int) (*Action, *Response, error)
PowerOffByTag(string) (*Action, *Response, error)
PowerOn(int) (*Action, *Response, error)
PowerOnByTag(string) (*Action, *Response, error)
PowerCycle(int) (*Action, *Response, error)
PowerCycleByTag(string) (*Action, *Response, error)
Reboot(int) (*Action, *Response, error)
Restore(int, int) (*Action, *Response, error)
Resize(int, string, bool) (*Action, *Response, error)
Rename(int, string) (*Action, *Response, error)
Snapshot(int, string) (*Action, *Response, error)
SnapshotByTag(string, string) (*Action, *Response, error)
EnableBackups(int) (*Action, *Response, error)
EnableBackupsByTag(string) (*Action, *Response, error)
DisableBackups(int) (*Action, *Response, error)
DisableBackupsByTag(string) (*Action, *Response, error)
PasswordReset(int) (*Action, *Response, error)
RebuildByImageID(int, int) (*Action, *Response, error)
RebuildByImageSlug(int, string) (*Action, *Response, error)
ChangeKernel(int, int) (*Action, *Response, error)
EnableIPv6(int) (*Action, *Response, error)
EnableIPv6ByTag(string) (*Action, *Response, error)
EnablePrivateNetworking(int) (*Action, *Response, error)
EnablePrivateNetworkingByTag(string) (*Action, *Response, error)
Upgrade(int) (*Action, *Response, error)
Get(int, int) (*Action, *Response, error)
GetByURI(string) (*Action, *Response, error)
Expand All @@ -48,24 +57,48 @@ func (s *DropletActionsServiceOp) Shutdown(id int) (*Action, *Response, error) {
return s.doAction(id, request)
}

// Shutdown Droplets by Tag
func (s *DropletActionsServiceOp) ShutdownByTag(tag string) (*Action, *Response, error) {
request := &ActionRequest{"type": "shutdown"}
return s.doActionByTag(tag, request)
}

// PowerOff a Droplet
func (s *DropletActionsServiceOp) PowerOff(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_off"}
return s.doAction(id, request)
}

// PowerOff a Droplet by Tag
func (s *DropletActionsServiceOp) PowerOffByTag(tag string) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_off"}
return s.doActionByTag(tag, request)
}

// PowerOn a Droplet
func (s *DropletActionsServiceOp) PowerOn(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_on"}
return s.doAction(id, request)
}

// PowerOn a Droplet by Tag
func (s *DropletActionsServiceOp) PowerOnByTag(tag string) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_on"}
return s.doActionByTag(tag, request)
}

// PowerCycle a Droplet
func (s *DropletActionsServiceOp) PowerCycle(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_cycle"}
return s.doAction(id, request)
}

// PowerCycle a Droplet by Tag
func (s *DropletActionsServiceOp) PowerCycleByTag(tag string) (*Action, *Response, error) {
request := &ActionRequest{"type": "power_cycle"}
return s.doActionByTag(tag, request)
}

// Reboot a Droplet
func (s *DropletActionsServiceOp) Reboot(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "reboot"}
Expand Down Expand Up @@ -113,18 +146,40 @@ func (s *DropletActionsServiceOp) Snapshot(id int, name string) (*Action, *Respo
return s.doAction(id, request)
}

// Snapshot a Droplet by Tag
func (s *DropletActionsServiceOp) SnapshotByTag(tag string, name string) (*Action, *Response, error) {
requestType := "snapshot"
request := &ActionRequest{
"type": requestType,
"name": name,
}
return s.doActionByTag(tag, request)
}

// EnableBackups enables backups for a droplet.
func (s *DropletActionsServiceOp) EnableBackups(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_backups"}
return s.doAction(id, request)
}

// EnableBackups enables backups for a droplet by Tag
func (s *DropletActionsServiceOp) EnableBackupsByTag(tag string) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_backups"}
return s.doActionByTag(tag, request)
}

// DisableBackups disables backups for a droplet.
func (s *DropletActionsServiceOp) DisableBackups(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "disable_backups"}
return s.doAction(id, request)
}

// DisableBackups disables backups for a droplet by tag
func (s *DropletActionsServiceOp) DisableBackupsByTag(tag string) (*Action, *Response, error) {
request := &ActionRequest{"type": "disable_backups"}
return s.doActionByTag(tag, request)
}

// PasswordReset resets the password for a droplet.
func (s *DropletActionsServiceOp) PasswordReset(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "password_reset"}
Expand Down Expand Up @@ -155,12 +210,24 @@ func (s *DropletActionsServiceOp) EnableIPv6(id int) (*Action, *Response, error)
return s.doAction(id, request)
}

// EnableIPv6 enables IPv6 for a droplet by Tag
func (s *DropletActionsServiceOp) EnableIPv6ByTag(tag string) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_ipv6"}
return s.doActionByTag(tag, request)
}

// EnablePrivateNetworking enables private networking for a droplet.
func (s *DropletActionsServiceOp) EnablePrivateNetworking(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_private_networking"}
return s.doAction(id, request)
}

// EnablePrivateNetworking enables private networking for a droplet by Tag
func (s *DropletActionsServiceOp) EnablePrivateNetworkingByTag(tag string) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_private_networking"}
return s.doActionByTag(tag, request)
}

// Upgrade a droplet.
func (s *DropletActionsServiceOp) Upgrade(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "upgrade"}
Expand Down Expand Up @@ -192,6 +259,31 @@ func (s *DropletActionsServiceOp) doAction(id int, request *ActionRequest) (*Act
return &root.Event, resp, err
}

func (s *DropletActionsServiceOp) doActionByTag(tag string, request *ActionRequest) (*Action, *Response, error) {
if tag == "" {
return nil, nil, NewArgError("tag", "cannot be empty")
}

if request == nil {
return nil, nil, NewArgError("request", "request can't be nil")
}

path := dropletActionPathByTag(tag)

req, err := s.client.NewRequest("POST", path, request)
if err != nil {
return nil, nil, err
}

root := new(actionRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}

return &root.Event, resp, err
}

// Get an action for a particular droplet by id.
func (s *DropletActionsServiceOp) Get(dropletID, actionID int) (*Action, *Response, error) {
if dropletID < 1 {
Expand Down Expand Up @@ -236,3 +328,7 @@ func (s *DropletActionsServiceOp) get(path string) (*Action, *Response, error) {
func dropletActionPath(dropletID int) string {
return fmt.Sprintf("v2/droplets/%d/actions", dropletID)
}

func dropletActionPathByTag(tag string) string {
return fmt.Sprintf("v2/droplets/actions?tag_name=%s", tag)
}
Loading

0 comments on commit 859ab8f

Please sign in to comment.