blob: 09fdff6089cccd5cc349c78597ba438bf0d68e24 [file] [log] [blame]
Jamie Hannafordb0d99122014-09-25 10:49:14 +02001package provider
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// NetworkExtAttrs represents an extended form of a Network with additional fields.
12type NetworkExtAttrs struct {
13 // UUID for the network
14 ID string `mapstructure:"id" json:"id"`
15
16 // Human-readable name for the network. Might not be unique.
17 Name string `mapstructure:"name" json:"name"`
18
19 // The administrative state of network. If false (down), the network does not forward packets.
20 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
21
22 // Indicates whether network is currently operational. Possible values include
23 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
24 Status string `mapstructure:"status" json:"status"`
25
26 // Subnets associated with this network.
27 Subnets []string `mapstructure:"subnets" json:"subnets"`
28
29 // Owner of network. Only admin users can specify a tenant_id other than its own.
30 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
31
32 // Specifies whether the network resource can be accessed by any tenant or not.
33 Shared bool `mapstructure:"shared" json:"shared"`
34
35 // Specifies the nature of the physical network mapped to this network
36 // resource. Examples are flat, vlan, or gre.
37 NetworkType string `json:"provider:network_type" mapstructure:"provider:network_type"`
38
39 // Identifies the physical network on top of which this network object is
40 // being implemented. The OpenStack Networking API does not expose any facility
41 // for retrieving the list of available physical networks. As an example, in
42 // the Open vSwitch plug-in this is a symbolic name which is then mapped to
43 // specific bridges on each compute host through the Open vSwitch plug-in
44 // configuration file.
45 PhysicalNetwork string `json:"provider:physical_network" mapstructure:"provider:physical_network"`
46
47 // Identifies an isolated segment on the physical network; the nature of the
48 // segment depends on the segmentation model defined by network_type. For
49 // instance, if network_type is vlan, then this is a vlan identifier;
50 // otherwise, if network_type is gre, then this will be a gre key.
51 SegmentationID string `json:"provider:segmentation_id" mapstructure:"provider:segmentation_id"`
52}
53
54func ExtractGet(r networks.GetResult) (*NetworkExtAttrs, error) {
55 if r.Err != nil {
56 return nil, r.Err
57 }
58 var res struct {
59 Network *NetworkExtAttrs `json:"network"`
60 }
61 err := mapstructure.Decode(r.Resp, &res)
62 if err != nil {
63 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
64 }
65 return res.Network, nil
66}
67
68func ExtractCreate(r networks.CreateResult) (*NetworkExtAttrs, error) {
69 if r.Err != nil {
70 return nil, r.Err
71 }
72 var res struct {
73 Network *NetworkExtAttrs `json:"network"`
74 }
75 err := mapstructure.Decode(r.Resp, &res)
76 if err != nil {
77 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
78 }
79 return res.Network, nil
80}
81
82func ExtractUpdate(r networks.UpdateResult) (*NetworkExtAttrs, error) {
83 if r.Err != nil {
84 return nil, r.Err
85 }
86 var res struct {
87 Network *NetworkExtAttrs `json:"network"`
88 }
89 err := mapstructure.Decode(r.Resp, &res)
90 if err != nil {
91 return nil, fmt.Errorf("Error decoding Neutron network: %v", err)
92 }
93 return res.Network, nil
94}
95
96// ExtractList accepts a Page struct, specifically a NetworkPage struct, and
97// extracts the elements into a slice of NetworkExtAttrs structs. In other
98// words, a generic collection is mapped into a relevant slice.
99func ExtractList(page pagination.Page) ([]NetworkExtAttrs, error) {
100 var resp struct {
101 Networks []NetworkExtAttrs `mapstructure:"networks" json:"networks"`
102 }
103
104 err := mapstructure.Decode(page.(networks.NetworkPage).Body, &resp)
105 if err != nil {
106 return nil, err
107 }
108
109 return resp.Networks, nil
110}