blob: cebbffd36a60b9ef72523c4fd0da57e7563595fc [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
Jamie Hannaford5497f942015-03-25 11:55:51 +010024 _, res.Err = c.Get(getURL(c, id), &res.Body, nil)
Jon Perritt44b1ea22014-10-22 00:13:23 -050025 return res
Jon Perritt24270c42014-10-21 21:11:04 -050026}
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.
32type CreateOptsBuilder interface {
Jon Perritt44b1ea22014-10-22 00:13:23 -050033 ToNetworkCreateMap() (map[string]interface{}, error)
Jon Perritt24270c42014-10-21 21:11:04 -050034}
35
36// CreateOpts is the common options struct used in this package's Create
37// operation.
Jon Perritt44b1ea22014-10-22 00:13:23 -050038type 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 Perritt24270c42014-10-21 21:11:04 -050043}
44
45// ToNetworkCreateMap casts a CreateOpts struct to a map.
46func (opts CreateOpts) ToNetworkCreateMap() (map[string]interface{}, error) {
Jon Perritt44b1ea22014-10-22 00:13:23 -050047 n := make(map[string]interface{})
Jon Perritt24270c42014-10-21 21:11:04 -050048
Jon Perritt44b1ea22014-10-22 00:13:23 -050049 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 Perritt24270c42014-10-21 21:11:04 -050055
Jon Perritt44b1ea22014-10-22 00:13:23 -050056 n["label"] = opts.Label
57 n["cidr"] = opts.CIDR
58 return map[string]interface{}{"network": n}, nil
Jon Perritt24270c42014-10-21 21:11:04 -050059}
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.
68func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt44b1ea22014-10-22 00:13:23 -050069 var res CreateResult
Jon Perritt24270c42014-10-21 21:11:04 -050070
Jon Perritt44b1ea22014-10-22 00:13:23 -050071 reqBody, err := opts.ToNetworkCreateMap()
72 if err != nil {
73 res.Err = err
74 return res
75 }
Jon Perritt24270c42014-10-21 21:11:04 -050076
Jon Perritt44b1ea22014-10-22 00:13:23 -050077 // Send request to API
Jamie Hannaford5497f942015-03-25 11:55:51 +010078 _, res.Err = c.Post(createURL(c), reqBody, &res.Body, &gophercloud.RequestOpts{
79 OkCodes: []int{200, 201, 202},
Jon Perritt44b1ea22014-10-22 00:13:23 -050080 })
81 return res
82}
83
84// Delete accepts a unique ID and deletes the network associated with it.
85func Delete(c *gophercloud.ServiceClient, networkID string) DeleteResult {
86 var res DeleteResult
Jamie Hannaford5497f942015-03-25 11:55:51 +010087 _, res.Err = c.Delete(deleteURL(c, networkID), nil)
Jon Perritt44b1ea22014-10-22 00:13:23 -050088 return res
Jon Perritt24270c42014-10-21 21:11:04 -050089}