blob: d3c973ecb2125b0db0eabcc21d1710af86ad13db [file] [log] [blame]
Jon Perritt24270c42014-10-21 21:11:04 -05001package networks
2
3import (
Jon Perritt44b1ea22014-10-22 00:13:23 -05004 "errors"
Jon Perritt24270c42014-10-21 21:11:04 -05005
Jon Perritt44b1ea22014-10-22 00:13:23 -05006 "github.com/rackspace/gophercloud"
7 "github.com/rackspace/gophercloud/pagination"
Jon Perritt24270c42014-10-21 21:11:04 -05008
Jon Perritt44b1ea22014-10-22 00:13:23 -05009 "github.com/racker/perigee"
Jon Perritt24270c42014-10-21 21:11:04 -050010)
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.
15func List(c *gophercloud.ServiceClient) pagination.Pager {
Jon Perritt44b1ea22014-10-22 00:13:23 -050016 createPage := func(r pagination.PageResult) pagination.Page {
17 return NetworkPage{pagination.SinglePageBase(r)}
18 }
19
20 return pagination.NewPager(c, listURL(c), createPage)
Jon Perritt24270c42014-10-21 21:11:04 -050021}
22
23// Get retrieves a specific network based on its unique ID.
24func Get(c *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt44b1ea22014-10-22 00:13:23 -050025 var res GetResult
26 _, res.Err = perigee.Request("GET", getURL(c, id), perigee.Options{
Jon Perrittb8edf082014-10-22 16:04:31 -050027 MoreHeaders: c.AuthenticatedHeaders(),
Jon Perritt44b1ea22014-10-22 00:13:23 -050028 Results: &res.Body,
29 OkCodes: []int{200},
30 })
31 return res
Jon Perritt24270c42014-10-21 21:11:04 -050032}
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.
38type CreateOptsBuilder interface {
Jon Perritt44b1ea22014-10-22 00:13:23 -050039 ToNetworkCreateMap() (map[string]interface{}, error)
Jon Perritt24270c42014-10-21 21:11:04 -050040}
41
42// CreateOpts is the common options struct used in this package's Create
43// operation.
Jon Perritt44b1ea22014-10-22 00:13:23 -050044type 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 Perritt24270c42014-10-21 21:11:04 -050049}
50
51// ToNetworkCreateMap casts a CreateOpts struct to a map.
52func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
Jon Perritt44b1ea22014-10-22 00:13:23 -050053 n := make(map[string]interface{})
Jon Perritt24270c42014-10-21 21:11:04 -050054
Jon Perritt44b1ea22014-10-22 00:13:23 -050055 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 Perritt24270c42014-10-21 21:11:04 -050061
Jon Perritt44b1ea22014-10-22 00:13:23 -050062 n["label"] = opts.Label
63 n["cidr"] = opts.CIDR
64 return map[string]interface{}{"network": n}, nil
Jon Perritt24270c42014-10-21 21:11:04 -050065}
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.
74func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt44b1ea22014-10-22 00:13:23 -050075 var res CreateResult
Jon Perritt24270c42014-10-21 21:11:04 -050076
Jon Perritt44b1ea22014-10-22 00:13:23 -050077 reqBody, err := opts.ToNetworkCreateMap()
78 if err != nil {
79 res.Err = err
80 return res
81 }
Jon Perritt24270c42014-10-21 21:11:04 -050082
Jon Perritt44b1ea22014-10-22 00:13:23 -050083 // Send request to API
84 _, res.Err = perigee.Request("POST", createURL(c), perigee.Options{
Jon Perrittb8edf082014-10-22 16:04:31 -050085 MoreHeaders: c.AuthenticatedHeaders(),
Jon Perritt44b1ea22014-10-22 00:13:23 -050086 ReqBody: &reqBody,
87 Results: &res.Body,
Jon Perritt857b3122014-10-22 18:17:44 -050088 OkCodes: []int{200, 201, 202},
Jon Perritt44b1ea22014-10-22 00:13:23 -050089 })
90 return res
91}
92
93// Delete accepts a unique ID and deletes the network associated with it.
94func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
95 var res DeleteResult
96 _, res.Err = perigee.Request("DELETE", deleteURL(c, networkID), perigee.Options{
Jon Perrittb8edf082014-10-22 16:04:31 -050097 MoreHeaders: c.AuthenticatedHeaders(),
Jon Perritt44b1ea22014-10-22 00:13:23 -050098 OkCodes: []int{204},
99 })
100 return res
Jon Perritt24270c42014-10-21 21:11:04 -0500101}