blob: eb6a76c008c697405abaf49dcf210764598bd1dc [file] [log] [blame]
Jon Perritt24270c42014-10-21 21:11:04 -05001package networks
2
3import (
Jon Perritt44b1ea22014-10-22 00:13:23 -05004 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
Jon Perritt24270c42014-10-21 21:11:04 -05007)
8
9type commonResult struct {
Jon Perritt44b1ea22014-10-22 00:13:23 -050010 gophercloud.Result
Jon Perritt24270c42014-10-21 21:11:04 -050011}
12
13// Extract is a function that accepts a result and extracts a network resource.
14func (r commonResult) Extract() (*Network, error) {
Jon Perritt44b1ea22014-10-22 00:13:23 -050015 if r.Err != nil {
16 return nil, r.Err
17 }
Jon Perritt24270c42014-10-21 21:11:04 -050018
Jon Perritt44b1ea22014-10-22 00:13:23 -050019 var res struct {
20 Network *Network `json:"network"`
21 }
Jon Perritt24270c42014-10-21 21:11:04 -050022
Jon Perritt44b1ea22014-10-22 00:13:23 -050023 err := mapstructure.Decode(r.Body, &res)
Jon Perritt24270c42014-10-21 21:11:04 -050024
Jon Perritt44b1ea22014-10-22 00:13:23 -050025 return res.Network, err
Jon Perritt24270c42014-10-21 21:11:04 -050026}
27
28// CreateResult represents the result of a create operation.
29type CreateResult struct {
Jon Perritt44b1ea22014-10-22 00:13:23 -050030 commonResult
Jon Perritt24270c42014-10-21 21:11:04 -050031}
32
33// GetResult represents the result of a get operation.
34type GetResult struct {
Jon Perritt44b1ea22014-10-22 00:13:23 -050035 commonResult
Jon Perritt24270c42014-10-21 21:11:04 -050036}
37
38// DeleteResult represents the result of a delete operation.
Jamie Hannafordb65675f2014-10-27 14:03:28 +010039type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050040 gophercloud.ErrResult
Jamie Hannafordb65675f2014-10-27 14:03:28 +010041}
Jon Perritt24270c42014-10-21 21:11:04 -050042
43// Network represents, well, a network.
44type Network struct {
Jon Perritt44b1ea22014-10-22 00:13:23 -050045 // UUID for the network
46 ID string `mapstructure:"id" json:"id"`
Jon Perritt24270c42014-10-21 21:11:04 -050047
Jon Perritt44b1ea22014-10-22 00:13:23 -050048 // Human-readable name for the network. Might not be unique.
49 Label string `mapstructure:"label" json:"label"`
Jon Perritt24270c42014-10-21 21:11:04 -050050
Jon Perritt44b1ea22014-10-22 00:13:23 -050051 // Classless Inter-Domain Routing
52 CIDR string `mapstructure:"cidr" json:"cidr"`
Jon Perritt24270c42014-10-21 21:11:04 -050053}
54
55// NetworkPage is the page returned by a pager when traversing over a
56// collection of networks.
57type NetworkPage struct {
Jon Perritt44b1ea22014-10-22 00:13:23 -050058 pagination.SinglePageBase
59}
60
61// IsEmpty returns true if the NetworkPage contains no Networks.
62func (r NetworkPage) IsEmpty() (bool, error) {
63 networks, err := ExtractNetworks(r)
64 if err != nil {
65 return true, err
66 }
67 return len(networks) == 0, nil
Jon Perritt24270c42014-10-21 21:11:04 -050068}
69
70// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
71// and extracts the elements into a slice of Network structs. In other words,
72// a generic collection is mapped into a relevant slice.
73func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jon Perritt44b1ea22014-10-22 00:13:23 -050074 var resp struct {
75 Networks []Network `mapstructure:"networks" json:"networks"`
76 }
Jon Perritt24270c42014-10-21 21:11:04 -050077
Jon Perritt44b1ea22014-10-22 00:13:23 -050078 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jon Perritt24270c42014-10-21 21:11:04 -050079
Jon Perritt44b1ea22014-10-22 00:13:23 -050080 return resp.Networks, err
Jon Perritt24270c42014-10-21 21:11:04 -050081}