blob: f07d6285dba2fa9f77cafc4303ff71fa843197bb [file] [log] [blame]
Jamie Hannafordb0d99122014-09-25 10:49:14 +02001package provider
2
3import (
Jamie Hannafordb0d99122014-09-25 10:49:14 +02004 "github.com/mitchellh/mapstructure"
5 "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
Jamie Hannafordc7c49a72014-10-06 15:49:53 +02009// AdminState gives users a solid type to work with for create and update
10// operations. It is recommended that users use the `Up` and `Down` enums.
Jamie Hannaforda241e312014-10-01 16:54:33 +020011type AdminState *bool
12
13// Convenience vars for AdminStateUp values.
14var (
15 iTrue = true
16 iFalse = false
17
Jamie Hannafordc7c49a72014-10-06 15:49:53 +020018 Up AdminState = &iTrue
19 Down AdminState = &iFalse
Jamie Hannaforda241e312014-10-01 16:54:33 +020020)
21
Jamie Hannafordb0d99122014-09-25 10:49:14 +020022// NetworkExtAttrs represents an extended form of a Network with additional fields.
23type NetworkExtAttrs struct {
24 // UUID for the network
25 ID string `mapstructure:"id" json:"id"`
26
27 // Human-readable name for the network. Might not be unique.
28 Name string `mapstructure:"name" json:"name"`
29
30 // The administrative state of network. If false (down), the network does not forward packets.
31 AdminStateUp bool `mapstructure:"admin_state_up" json:"admin_state_up"`
32
33 // Indicates whether network is currently operational. Possible values include
34 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
35 Status string `mapstructure:"status" json:"status"`
36
37 // Subnets associated with this network.
38 Subnets []string `mapstructure:"subnets" json:"subnets"`
39
40 // Owner of network. Only admin users can specify a tenant_id other than its own.
41 TenantID string `mapstructure:"tenant_id" json:"tenant_id"`
42
43 // Specifies whether the network resource can be accessed by any tenant or not.
44 Shared bool `mapstructure:"shared" json:"shared"`
45
46 // Specifies the nature of the physical network mapped to this network
47 // resource. Examples are flat, vlan, or gre.
48 NetworkType string `json:"provider:network_type" mapstructure:"provider:network_type"`
49
50 // Identifies the physical network on top of which this network object is
51 // being implemented. The OpenStack Networking API does not expose any facility
52 // for retrieving the list of available physical networks. As an example, in
53 // the Open vSwitch plug-in this is a symbolic name which is then mapped to
54 // specific bridges on each compute host through the Open vSwitch plug-in
55 // configuration file.
56 PhysicalNetwork string `json:"provider:physical_network" mapstructure:"provider:physical_network"`
57
58 // Identifies an isolated segment on the physical network; the nature of the
59 // segment depends on the segmentation model defined by network_type. For
60 // instance, if network_type is vlan, then this is a vlan identifier;
61 // otherwise, if network_type is gre, then this will be a gre key.
62 SegmentationID string `json:"provider:segmentation_id" mapstructure:"provider:segmentation_id"`
63}
64
Jamie Hannaford5a531902014-09-26 15:03:31 +020065// ExtractGet decorates a GetResult struct returned from a networks.Get()
66// function with extended attributes.
Jamie Hannafordb0d99122014-09-25 10:49:14 +020067func ExtractGet(r networks.GetResult) (*NetworkExtAttrs, error) {
68 if r.Err != nil {
69 return nil, r.Err
70 }
Jamie Hannafordc1f36492014-10-08 15:48:48 +020071
Jamie Hannafordb0d99122014-09-25 10:49:14 +020072 var res struct {
73 Network *NetworkExtAttrs `json:"network"`
74 }
Jamie Hannafordc1f36492014-10-08 15:48:48 +020075
Jon Perrittb0b9c0d2015-03-10 17:24:08 -060076 err := mapstructure.WeakDecode(r.Body, &res)
Jamie Hannafordc1f36492014-10-08 15:48:48 +020077
78 return res.Network, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +020079}
80
Jamie Hannafordc7c49a72014-10-06 15:49:53 +020081// ExtractCreate decorates a CreateResult struct returned from a networks.Create()
Jamie Hannaford5a531902014-09-26 15:03:31 +020082// function with extended attributes.
Jamie Hannafordb0d99122014-09-25 10:49:14 +020083func ExtractCreate(r networks.CreateResult) (*NetworkExtAttrs, error) {
84 if r.Err != nil {
85 return nil, r.Err
86 }
Jamie Hannafordc1f36492014-10-08 15:48:48 +020087
Jamie Hannafordb0d99122014-09-25 10:49:14 +020088 var res struct {
89 Network *NetworkExtAttrs `json:"network"`
90 }
Jamie Hannafordc1f36492014-10-08 15:48:48 +020091
Jon Perrittb0b9c0d2015-03-10 17:24:08 -060092 err := mapstructure.WeakDecode(r.Body, &res)
Jamie Hannafordc1f36492014-10-08 15:48:48 +020093
94 return res.Network, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +020095}
96
Jamie Hannaford5a531902014-09-26 15:03:31 +020097// ExtractUpdate decorates a UpdateResult struct returned from a
98// networks.Update() function with extended attributes.
Jamie Hannafordb0d99122014-09-25 10:49:14 +020099func ExtractUpdate(r networks.UpdateResult) (*NetworkExtAttrs, error) {
100 if r.Err != nil {
101 return nil, r.Err
102 }
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200103
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200104 var res struct {
105 Network *NetworkExtAttrs `json:"network"`
106 }
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200107
Jon Perrittb0b9c0d2015-03-10 17:24:08 -0600108 err := mapstructure.WeakDecode(r.Body, &res)
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200109
110 return res.Network, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200111}
112
113// ExtractList accepts a Page struct, specifically a NetworkPage struct, and
114// extracts the elements into a slice of NetworkExtAttrs structs. In other
115// words, a generic collection is mapped into a relevant slice.
116func ExtractList(page pagination.Page) ([]NetworkExtAttrs, error) {
117 var resp struct {
118 Networks []NetworkExtAttrs `mapstructure:"networks" json:"networks"`
119 }
120
Jon Perrittb0b9c0d2015-03-10 17:24:08 -0600121 err := mapstructure.WeakDecode(page.(networks.NetworkPage).Body, &resp)
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200122
Jamie Hannafordc1f36492014-10-08 15:48:48 +0200123 return resp.Networks, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200124}