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