blob: 1348cfaf19bf6b5da79870ab0ae4ac0770426257 [file] [log] [blame]
Joe Topjianc9fb21b2015-02-22 05:55:48 +00001package servergroups
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/gophercloud/pagination"
Joe Topjianc9fb21b2015-02-22 05:55:48 +00006)
7
8// List returns a Pager that allows you to iterate over a collection of ServerGroups.
9func List(client *gophercloud.ServiceClient) pagination.Pager {
10 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
Jon Perritt12395212016-02-24 10:41:17 -060011 return ServerGroupPage{pagination.SinglePageBase(r)}
Joe Topjianc9fb21b2015-02-22 05:55:48 +000012 })
13}
14
15// CreateOptsBuilder describes struct types that can be accepted by the Create call. Notably, the
16// CreateOpts struct in this package does.
17type CreateOptsBuilder interface {
18 ToServerGroupCreateMap() (map[string]interface{}, error)
19}
20
21// CreateOpts specifies a Server Group allocation request
22type CreateOpts struct {
23 // Name is the name of the server group
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 Name string `json:"name" required:"true"`
Joe Topjianc9fb21b2015-02-22 05:55:48 +000025 // Policies are the server group policies
Jon Perrittdb0ae142016-03-13 00:33:41 -060026 Policies []string `json:"policies" required:"true"`
Joe Topjianc9fb21b2015-02-22 05:55:48 +000027}
28
29// ToServerGroupCreateMap constructs a request body from CreateOpts.
30func (opts CreateOpts) ToServerGroupCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -060031 return gophercloud.BuildRequestBody(opts, "server_group")
Joe Topjianc9fb21b2015-02-22 05:55:48 +000032}
33
34// Create requests the creation of a new Server Group
35func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060036 var r CreateResult
37 b, err := opts.ToServerGroupCreateMap()
Joe Topjianc9fb21b2015-02-22 05:55:48 +000038 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -060039 r.Err = err
40 return r
Joe Topjianc9fb21b2015-02-22 05:55:48 +000041 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060042 _, r.Err = client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
Joe Topjianc9fb21b2015-02-22 05:55:48 +000043 OkCodes: []int{200},
44 })
Jon Perrittdb0ae142016-03-13 00:33:41 -060045 return r
Joe Topjianc9fb21b2015-02-22 05:55:48 +000046}
47
48// Get returns data about a previously created ServerGroup.
49func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060050 var r GetResult
51 _, r.Err = client.Get(getURL(client, id), &r.Body, nil)
52 return r
Joe Topjianc9fb21b2015-02-22 05:55:48 +000053}
54
55// Delete requests the deletion of a previously allocated ServerGroup.
56func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittdb0ae142016-03-13 00:33:41 -060057 var r DeleteResult
58 _, r.Err = client.Delete(deleteURL(client, id), nil)
59 return r
Joe Topjianc9fb21b2015-02-22 05:55:48 +000060}