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 |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 24 | _, res.Err = c.Get(getURL(c, id), &res.Body, nil) |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 25 | return res |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 29 | // to be used in the main Create operation in this package. Since many |
| 30 | // extensions decorate or modify the common logic, it is useful for them to |
| 31 | // satisfy a basic interface in order for them to be used. |
| 32 | type CreateOptsBuilder interface { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 33 | ToNetworkCreateMap() (map[string]interface{}, error) |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // CreateOpts is the common options struct used in this package's Create |
| 37 | // operation. |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 38 | type CreateOpts struct { |
| 39 | // REQUIRED. See Network object for more info. |
| 40 | CIDR string |
| 41 | // REQUIRED. See Network object for more info. |
| 42 | Label string |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | // ToNetworkCreateMap casts a CreateOpts struct to a map. |
| 46 | func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 47 | n := make(map[string]interface{}) |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 48 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 49 | if opts.CIDR == "" { |
| 50 | return nil, errors.New("Required field CIDR not set.") |
| 51 | } |
| 52 | if opts.Label == "" { |
| 53 | return nil, errors.New("Required field Label not set.") |
| 54 | } |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 55 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 56 | n["label"] = opts.Label |
| 57 | n["cidr"] = opts.CIDR |
| 58 | return map[string]interface{}{"network": n}, nil |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // Create accepts a CreateOpts struct and creates a new network using the values |
| 62 | // provided. This operation does not actually require a request body, i.e. the |
| 63 | // CreateOpts struct argument can be empty. |
| 64 | // |
| 65 | // The tenant ID that is contained in the URI is the tenant that creates the |
| 66 | // network. An admin user, however, has the option of specifying another tenant |
| 67 | // ID in the CreateOpts struct. |
| 68 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 69 | var res CreateResult |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 70 | |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 71 | reqBody, err := opts.ToNetworkCreateMap() |
| 72 | if err != nil { |
| 73 | res.Err = err |
| 74 | return res |
| 75 | } |
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 | // Send request to API |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 78 | _, res.Err = c.Post(createURL(c), reqBody, &res.Body, &gophercloud.RequestOpts{ |
| 79 | OkCodes: []int{200, 201, 202}, |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 80 | }) |
| 81 | return res |
| 82 | } |
| 83 | |
| 84 | // Delete accepts a unique ID and deletes the network associated with it. |
| 85 | func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult { |
| 86 | var res DeleteResult |
Jamie Hannaford | 5497f94 | 2015-03-25 11:55:51 +0100 | [diff] [blame] | 87 | _, res.Err = c.Delete(deleteURL(c, networkID), nil) |
Jon Perritt | 44b1ea2 | 2014-10-22 00:13:23 -0500 | [diff] [blame] | 88 | return res |
Jon Perritt | 24270c4 | 2014-10-21 21:11:04 -0500 | [diff] [blame] | 89 | } |