blob: 823c56e7fc71e8aed0dcdae0a9b24db82627e9eb [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.
39type DeleteResult commonResult
40
41// Network represents, well, a network.
42type Network struct {
Jon Perritt44b1ea22014-10-22 00:13:23 -050043 // UUID for the network
44 ID string `mapstructure:"id" json:"id"`
Jon Perritt24270c42014-10-21 21:11:04 -050045
Jon Perritt44b1ea22014-10-22 00:13:23 -050046 // Human-readable name for the network. Might not be unique.
47 Label string `mapstructure:"label" json:"label"`
Jon Perritt24270c42014-10-21 21:11:04 -050048
Jon Perritt44b1ea22014-10-22 00:13:23 -050049 // Classless Inter-Domain Routing
50 CIDR string `mapstructure:"cidr" json:"cidr"`
Jon Perritt24270c42014-10-21 21:11:04 -050051}
52
53// NetworkPage is the page returned by a pager when traversing over a
54// collection of networks.
55type NetworkPage struct {
Jon Perritt44b1ea22014-10-22 00:13:23 -050056 pagination.SinglePageBase
57}
58
59// IsEmpty returns true if the NetworkPage contains no Networks.
60func (r NetworkPage) IsEmpty() (bool, error) {
61 networks, err := ExtractNetworks(r)
62 if err != nil {
63 return true, err
64 }
65 return len(networks) == 0, nil
Jon Perritt24270c42014-10-21 21:11:04 -050066}
67
68// ExtractNetworks accepts a Page struct, specifically a NetworkPage struct,
69// and extracts the elements into a slice of Network structs. In other words,
70// a generic collection is mapped into a relevant slice.
71func ExtractNetworks(page pagination.Page) ([]Network, error) {
Jon Perritt44b1ea22014-10-22 00:13:23 -050072 var resp struct {
73 Networks []Network `mapstructure:"networks" json:"networks"`
74 }
Jon Perritt24270c42014-10-21 21:11:04 -050075
Jon Perritt44b1ea22014-10-22 00:13:23 -050076 err := mapstructure.Decode(page.(NetworkPage).Body, &resp)
Jon Perritt24270c42014-10-21 21:11:04 -050077
Jon Perritt44b1ea22014-10-22 00:13:23 -050078 return resp.Networks, err
Jon Perritt24270c42014-10-21 21:11:04 -050079}