blob: 212edac51bb0425b15ad6e90239b4aa9e985d2b5 [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
24 Name string
25
26 // Policies are the server group policies
27 Policies []string
28}
29
30// ToServerGroupCreateMap constructs a request body from CreateOpts.
31func (opts CreateOpts) ToServerGroupCreateMap() (map[string]interface{}, error) {
32 if opts.Name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -060033 err := gophercloud.ErrMissingInput{}
34 err.Function = "servergroups.ToServerGroupCreateMap"
35 err.Argument = "servergroups.CreateOpts.Name"
36 return nil, err
Joe Topjianc9fb21b2015-02-22 05:55:48 +000037 }
38
39 if len(opts.Policies) < 1 {
Jon Perrittf094fef2016-03-07 01:41:59 -060040 err := gophercloud.ErrMissingInput{}
41 err.Function = "servergroups.ToServerGroupCreateMap"
42 err.Argument = "servergroups.CreateOpts.Policies"
43 return nil, err
Joe Topjianc9fb21b2015-02-22 05:55:48 +000044 }
45
46 serverGroup := make(map[string]interface{})
47 serverGroup["name"] = opts.Name
48 serverGroup["policies"] = opts.Policies
49
50 return map[string]interface{}{"server_group": serverGroup}, nil
51}
52
53// Create requests the creation of a new Server Group
54func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
55 var res CreateResult
56
57 reqBody, err := opts.ToServerGroupCreateMap()
58 if err != nil {
59 res.Err = err
60 return res
61 }
62
63 _, res.Err = client.Post(createURL(client), reqBody, &res.Body, &gophercloud.RequestOpts{
64 OkCodes: []int{200},
65 })
66 return res
67}
68
69// Get returns data about a previously created ServerGroup.
70func Get(client *gophercloud.ServiceClient, id string) GetResult {
71 var res GetResult
72 _, res.Err = client.Get(getURL(client, id), &res.Body, nil)
73 return res
74}
75
76// Delete requests the deletion of a previously allocated ServerGroup.
77func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
78 var res DeleteResult
79 _, res.Err = client.Delete(deleteURL(client, id), nil)
80 return res
81}