blob: 3aefb0cdca19d6299fe4b03f7b3604f32dd6e19c [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)
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.
13func List(c *gophercloud.ServiceClient) pagination.Pager {
Jon Perritt44b1ea22014-10-22 00:13:23 -050014 createPage := func(r pagination.PageResult) pagination.Page {
15 return NetworkPage{pagination.SinglePageBase(r)}
16 }
17
18 return pagination.NewPager(c, listURL(c), createPage)
Jon Perritt24270c42014-10-21 21:11:04 -050019}
20
21// Get retrieves a specific network based on its unique ID.
22func Get(c *gophercloud.ServiceClient, id string) GetResult {
Jon Perritt44b1ea22014-10-22 00:13:23 -050023 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050024 _, res.Err = c.Request("GET", getURL(c, id), gophercloud.RequestOpts{
25 JSONResponse: &res.Body,
26 OkCodes: []int{200},
Jon Perritt44b1ea22014-10-22 00:13:23 -050027 })
28 return res
Jon Perritt24270c42014-10-21 21:11:04 -050029}
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.
35type CreateOptsBuilder interface {
Jon Perritt44b1ea22014-10-22 00:13:23 -050036 ToNetworkCreateMap() (map[string]interface{}, error)
Jon Perritt24270c42014-10-21 21:11:04 -050037}
38
39// CreateOpts is the common options struct used in this package's Create
40// operation.
Jon Perritt44b1ea22014-10-22 00:13:23 -050041type 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 Perritt24270c42014-10-21 21:11:04 -050046}
47
48// ToNetworkCreateMap casts a CreateOpts struct to a map.
49func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
Jon Perritt44b1ea22014-10-22 00:13:23 -050050 n := make(map[string]interface{})
Jon Perritt24270c42014-10-21 21:11:04 -050051
Jon Perritt44b1ea22014-10-22 00:13:23 -050052 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 Perritt24270c42014-10-21 21:11:04 -050058
Jon Perritt44b1ea22014-10-22 00:13:23 -050059 n["label"] = opts.Label
60 n["cidr"] = opts.CIDR
61 return map[string]interface{}{"network": n}, nil
Jon Perritt24270c42014-10-21 21:11:04 -050062}
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.
71func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt44b1ea22014-10-22 00:13:23 -050072 var res CreateResult
Jon Perritt24270c42014-10-21 21:11:04 -050073
Jon Perritt44b1ea22014-10-22 00:13:23 -050074 reqBody, err := opts.ToNetworkCreateMap()
75 if err != nil {
76 res.Err = err
77 return res
78 }
Jon Perritt24270c42014-10-21 21:11:04 -050079
Jon Perritt44b1ea22014-10-22 00:13:23 -050080 // Send request to API
Ash Wilson2199f102015-02-12 16:16:09 -050081 _, res.Err = c.Request("POST", createURL(c), gophercloud.RequestOpts{
82 JSONBody: &reqBody,
83 JSONResponse: &res.Body,
84 OkCodes: []int{200, 201, 202},
Jon Perritt44b1ea22014-10-22 00:13:23 -050085 })
86 return res
87}
88
89// Delete accepts a unique ID and deletes the network associated with it.
90func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
91 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -050092 _, res.Err = c.Request("DELETE", deleteURL(c, networkID), gophercloud.RequestOpts{
93 OkCodes: []int{204},
Jon Perritt44b1ea22014-10-22 00:13:23 -050094 })
95 return res
Jon Perritt24270c42014-10-21 21:11:04 -050096}