Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 1 | package servergroups |
| 2 | |
| 3 | import ( |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 4 | "github.com/gophercloud/gophercloud" |
| 5 | "github.com/gophercloud/gophercloud/pagination" |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // List returns a Pager that allows you to iterate over a collection of ServerGroups. |
| 9 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 10 | return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 11 | return ServerGroupPage{pagination.SinglePageBase(r)} |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 12 | }) |
| 13 | } |
| 14 | |
| 15 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. Notably, the |
| 16 | // CreateOpts struct in this package does. |
| 17 | type CreateOptsBuilder interface { |
| 18 | ToServerGroupCreateMap() (map[string]interface{}, error) |
| 19 | } |
| 20 | |
| 21 | // CreateOpts specifies a Server Group allocation request |
| 22 | type CreateOpts struct { |
| 23 | // Name is the name of the server group |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 24 | Name string `json:"name" required:"true"` |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 25 | // Policies are the server group policies |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 26 | Policies []string `json:"policies" required:"true"` |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // ToServerGroupCreateMap constructs a request body from CreateOpts. |
| 30 | func (opts CreateOpts) ToServerGroupCreateMap() (map[string]interface{}, error) { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 31 | return gophercloud.BuildRequestBody(opts, "server_group") |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // Create requests the creation of a new Server Group |
| 35 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 36 | var r CreateResult |
| 37 | b, err := opts.ToServerGroupCreateMap() |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 38 | if err != nil { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 39 | r.Err = err |
| 40 | return r |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 41 | } |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 42 | _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{ |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 43 | OkCodes: []int{200}, |
| 44 | }) |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 45 | return r |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | // Get returns data about a previously created ServerGroup. |
| 49 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 50 | var r GetResult |
| 51 | _, r.Err = client.Get(getURL(client, id), &r.Body, nil) |
| 52 | return r |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // Delete requests the deletion of a previously allocated ServerGroup. |
| 56 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
Jon Perritt | db0ae14 | 2016-03-13 00:33:41 -0600 | [diff] [blame^] | 57 | var r DeleteResult |
| 58 | _, r.Err = client.Delete(deleteURL(client, id), nil) |
| 59 | return r |
Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 60 | } |