| 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 | 
|  | 24 | Name string | 
|  | 25 |  | 
|  | 26 | // Policies are the server group policies | 
|  | 27 | Policies []string | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | // ToServerGroupCreateMap constructs a request body from CreateOpts. | 
|  | 31 | func (opts CreateOpts) ToServerGroupCreateMap() (map[string]interface{}, error) { | 
|  | 32 | if opts.Name == "" { | 
| Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 33 | err := gophercloud.ErrMissingInput{} | 
|  | 34 | err.Function = "servergroups.ToServerGroupCreateMap" | 
|  | 35 | err.Argument = "servergroups.CreateOpts.Name" | 
|  | 36 | return nil, err | 
| Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
|  | 39 | if len(opts.Policies) < 1 { | 
| Jon Perritt | f094fef | 2016-03-07 01:41:59 -0600 | [diff] [blame] | 40 | err := gophercloud.ErrMissingInput{} | 
|  | 41 | err.Function = "servergroups.ToServerGroupCreateMap" | 
|  | 42 | err.Argument = "servergroups.CreateOpts.Policies" | 
|  | 43 | return nil, err | 
| Joe Topjian | c9fb21b | 2015-02-22 05:55:48 +0000 | [diff] [blame] | 44 | } | 
|  | 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 | 
|  | 54 | func 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. | 
|  | 70 | func 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. | 
|  | 77 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { | 
|  | 78 | var res DeleteResult | 
|  | 79 | _, res.Err = client.Delete(deleteURL(client, id), nil) | 
|  | 80 | return res | 
|  | 81 | } |