blob: 4cd2133c5f37a8cbd9d0cdd1639e8f074a7cfc33 [file] [log] [blame]
Jamie Hannaford9823bb62014-09-26 17:06:36 +02001package external
2
3import (
4 "fmt"
5
6 "github.com/mitchellh/mapstructure"
7 "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
8 "github.com/rackspace/gophercloud/pagination"
9)
10
11// NetworkExternal represents a decorated form of a Network with based on the
12// "external-net" extension.
13type NetworkExternal struct {
14 // UUID for the network
15 ID string `mapstructure:"id" json:"id"`
16
17 // Human-readable name for the network. Might not be unique.
18 Name string `mapstructure:"name" json:"name"`
19
20 // The administrative state of network. If false (down), the network does not forward packets.
21 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
22
23 // Indicates whether network is currently operational. Possible values include
24 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
25 Status string `mapstructure:"status" json:"status"`
26
27 // Subnets associated with this network.
28 Subnets []string `mapstructure:"subnets" json:"subnets"`
29
30 // Owner of network. Only admin users can specify a tenant_id other than its own.
31 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
32
33 // Specifies whether the network resource can be accessed by any tenant or not.
34 Shared bool `mapstructure:"shared" json:"shared"`
35
36 // Specifies whether the network is an external network or not.
37 External bool `mapstructure:"router:external" json:"router:external"`
38}
39
40// ExtractGet decorates a GetResult struct returned from a networks.Get()
41// function with extended attributes.
42func ExtractGet(r networks.GetResult) (*NetworkExternal, error) {
43 if r.Err != nil {
44 return nil, r.Err
45 }
46 var res struct {
47 Network *NetworkExternal `json:"network"`
48 }
49 err := mapstructure.Decode(r.Resp, &res)
50 if err != nil {
51 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
52 }
53 return res.Network, nil
54}
55
56// ExtractCreate decorates a CreateResult struct returned from a networks.Create()
57// function with extended attributes.
58func ExtractCreate(r networks.CreateResult) (*NetworkExternal, error) {
59 if r.Err != nil {
60 return nil, r.Err
61 }
62 var res struct {
63 Network *NetworkExternal `json:"network"`
64 }
65 err := mapstructure.Decode(r.Resp, &res)
66 if err != nil {
67 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
68 }
69 return res.Network, nil
70}
71
72// ExtractUpdate decorates a UpdateResult struct returned from a
73// networks.Update() function with extended attributes.
74func ExtractUpdate(r networks.UpdateResult) (*NetworkExternal, error) {
75 if r.Err != nil {
76 return nil, r.Err
77 }
78 var res struct {
79 Network *NetworkExternal `json:"network"`
80 }
81 err := mapstructure.Decode(r.Resp, &res)
82 if err != nil {
83 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
84 }
85 return res.Network, nil
86}
87
88// ExtractList accepts a Page struct, specifically a NetworkPage struct, and
89// extracts the elements into a slice of NetworkExtAttrs structs. In other
90// words, a generic collection is mapped into a relevant slice.
91func ExtractList(page pagination.Page) ([]NetworkExternal, error) {
92 var resp struct {
93 Networks []NetworkExternal `mapstructure:"networks" json:"networks"`
94 }
95
96 err := mapstructure.Decode(page.(networks.NetworkPage).Body, &resp)
97 if err != nil {
98 return nil, err
99 }
100
101 return resp.Networks, nil
102}