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