Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame^] | 1 | package servergroups |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | "github.com/rackspace/gophercloud/pagination" |
| 7 | ) |
| 8 | |
| 9 | // A ServerGroup creates a policy for instance placement in the cloud |
| 10 | type ServerGroup struct { |
| 11 | // ID is the unique ID of the Server Group. |
| 12 | ID string `mapstructure:"id"` |
| 13 | |
| 14 | // Name is the common name of the server group. |
| 15 | Name string `mapstructure:"name"` |
| 16 | |
| 17 | // Polices are the group policies. |
| 18 | Policies []string `mapstructure:"policies"` |
| 19 | |
| 20 | // Members are the members of the server group. |
| 21 | Members []string `mapstructure:"members"` |
| 22 | |
| 23 | // Metadata includes a list of all user-specified key-value pairs attached to the Server Group. |
| 24 | Metadata map[string]interface{} |
| 25 | } |
| 26 | |
| 27 | // ServerGroupsPage stores a single, only page of ServerGroups |
| 28 | // results from a List call. |
| 29 | type ServerGroupsPage struct { |
| 30 | pagination.SinglePageBase |
| 31 | } |
| 32 | |
| 33 | // IsEmpty determines whether or not a ServerGroupsPage is empty. |
| 34 | func (page ServerGroupsPage) IsEmpty() (bool, error) { |
| 35 | va, err := ExtractServerGroups(page) |
| 36 | return len(va) == 0, err |
| 37 | } |
| 38 | |
| 39 | // ExtractServerGroups interprets a page of results as a slice of |
| 40 | // ServerGroups. |
| 41 | func ExtractServerGroups(page pagination.Page) ([]ServerGroup, error) { |
| 42 | casted := page.(ServerGroupsPage).Body |
| 43 | var response struct { |
| 44 | ServerGroups []ServerGroup `mapstructure:"server_groups"` |
| 45 | } |
| 46 | |
| 47 | err := mapstructure.WeakDecode(casted, &response) |
| 48 | |
| 49 | return response.ServerGroups, err |
| 50 | } |
| 51 | |
| 52 | type ServerGroupResult struct { |
| 53 | gophercloud.Result |
| 54 | } |
| 55 | |
| 56 | // Extract is a method that attempts to interpret any Server Group resource |
| 57 | // response as a ServerGroup struct. |
| 58 | func (r ServerGroupResult) Extract() (*ServerGroup, error) { |
| 59 | if r.Err != nil { |
| 60 | return nil, r.Err |
| 61 | } |
| 62 | |
| 63 | var res struct { |
| 64 | ServerGroup *ServerGroup `json:"server_group" mapstructure:"server_group"` |
| 65 | } |
| 66 | |
| 67 | err := mapstructure.WeakDecode(r.Body, &res) |
| 68 | return res.ServerGroup, err |
| 69 | } |
| 70 | |
| 71 | // CreateResult is the response from a Create operation. Call its Extract method to interpret it |
| 72 | // as a ServerGroup. |
| 73 | type CreateResult struct { |
| 74 | ServerGroupResult |
| 75 | } |
| 76 | |
| 77 | // GetResult is the response from a Get operation. Call its Extract method to interpret it |
| 78 | // as a ServerGroup. |
| 79 | type GetResult struct { |
| 80 | ServerGroupResult |
| 81 | } |
| 82 | |
| 83 | // DeleteResult is the response from a Delete operation. Call its Extract method to determine if |
| 84 | // the call succeeded or failed. |
| 85 | type DeleteResult struct { |
| 86 | gophercloud.ErrResult |
| 87 | } |