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