blob: 1597b43eb224cce9babfecd4de6e2a202206d38e [file] [log] [blame]
Joe Topjianc9fb21b2015-02-22 05:55:48 +00001package servergroups
2
3import (
4 "errors"
5
6 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
8)
9
10// List returns a Pager that allows you to iterate over a collection of ServerGroups.
11func List(client *gophercloud.ServiceClient) pagination.Pager {
12 return pagination.NewPager(client, listURL(client), func(r pagination.PageResult) pagination.Page {
13 return ServerGroupsPage{pagination.SinglePageBase(r)}
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.
19type CreateOptsBuilder interface {
20 ToServerGroupCreateMap() (map[string]interface{}, error)
21}
22
23// CreateOpts specifies a Server Group allocation request
24type 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.
33func (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
50func 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.
66func 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.
73func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
74 var res DeleteResult
75 _, res.Err = client.Delete(deleteURL(client, id), nil)
76 return res
77}