Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 1 | package networks |
| 2 | |
| 3 | import ( |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 4 | "errors" |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 5 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 8 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 9 | "github.com/racker/perigee" |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 10 | ) |
| 11 | |
| 12 | // List returns a Pager which allows you to iterate over a collection of |
| 13 | // networks. It accepts a ListOpts struct, which allows you to filter and sort |
| 14 | // the returned collection for greater efficiency. |
| 15 | func List(c *gophercloud.ServiceClient) pagination.Pager { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 16 | createPage := func(r pagination.PageResult) pagination.Page { |
| 17 | return NetworkPage{pagination.SinglePageBase(r)} |
| 18 | } |
| 19 | |
| 20 | return pagination.NewPager(c, listURL(c), createPage) |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // Get retrieves a specific network based on its unique ID. |
| 24 | func Get(c *gophercloud.ServiceClient, id string) GetResult { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 25 | var res GetResult |
| 26 | _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{ |
Jon Perritt | b8edf08 | 2014-10-22 16:04:31 -0500 | [diff] [blame] | 27 | MoreHeaders: c.AuthenticatedHeaders(), |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 28 | Results: &res.Body, |
| 29 | OkCodes: []int{200}, |
| 30 | }) |
| 31 | return res |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 35 | // to be used in the main Create operation in this package. Since many |
| 36 | // extensions decorate or modify the common logic, it is useful for them to |
| 37 | // satisfy a basic interface in order for them to be used. |
| 38 | type CreateOptsBuilder interface { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 39 | ToNetworkCreateMap() (map[string]interface{}, error) |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // CreateOpts is the common options struct used in this package's Create |
| 43 | // operation. |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 44 | type CreateOpts struct { |
| 45 | // REQUIRED. See Network object for more info. |
| 46 | CIDR string |
| 47 | // REQUIRED. See Network object for more info. |
| 48 | Label string |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | // ToNetworkCreateMap casts a CreateOpts struct to a map. |
| 52 | func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 53 | n := make(map[string]interface{}) |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 54 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 55 | if opts.CIDR == "" { |
| 56 | return nil, errors.New("Required field CIDR not set.") |
| 57 | } |
| 58 | if opts.Label == "" { |
| 59 | return nil, errors.New("Required field Label not set.") |
| 60 | } |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 61 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 62 | n["label"] = opts.Label |
| 63 | n["cidr"] = opts.CIDR |
| 64 | return map[string]interface{}{"network": n}, nil |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 68 | // provided. This operation does not actually require a request body, i.e. the |
| 69 | // CreateOpts struct argument can be empty. |
| 70 | // |
| 71 | // The tenant ID that is contained in the URI is the tenant that creates the |
| 72 | // network. An admin user, however, has the option of specifying another tenant |
| 73 | // ID in the CreateOpts struct. |
| 74 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 75 | var res CreateResult |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 76 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 77 | reqBody, err := opts.ToNetworkCreateMap() |
| 78 | if err != nil { |
| 79 | res.Err = err |
| 80 | return res |
| 81 | } |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 82 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 83 | // Send request to API |
| 84 | _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{ |
Jon Perritt | b8edf08 | 2014-10-22 16:04:31 -0500 | [diff] [blame] | 85 | MoreHeaders: c.AuthenticatedHeaders(), |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 86 | ReqBody: &reqBody, |
| 87 | Results: &res.Body, |
Jon Perritt | 857b312 | 2014-10-22 18:17:44 -0500 | [diff] [blame] | 88 | OkCodes: []int{200, 201, 202}, |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 89 | }) |
| 90 | return res |
| 91 | } |
| 92 | |
| 93 | // Delete accepts a unique ID and deletes the network associated with it. |
| 94 | func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult { |
| 95 | var res DeleteResult |
| 96 | _, res.Err = perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{ |
Jon Perritt | b8edf08 | 2014-10-22 16:04:31 -0500 | [diff] [blame] | 97 | MoreHeaders: c.AuthenticatedHeaders(), |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 98 | OkCodes: []int{204}, |
| 99 | }) |
| 100 | return res |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 101 | } |