blob: 2da020acfe6b29763c6a9edb98134628af86e55f [file] [log] [blame]
Jamie Hannafordb0d99122014-09-25 10:49:14 +02001package provider
2
3import (
Joe Topjiand5be3fe2016-08-29 09:41:13 -06004 "encoding/json"
5 "strconv"
6
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/networking/v2/networks"
8 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
Jamie Hannafordb0d99122014-09-25 10:49:14 +02009)
10
11// NetworkExtAttrs represents an extended form of a Network with additional fields.
12type NetworkExtAttrs struct {
13 // UUID for the network
Jon Perritt3c166472016-02-25 03:07:41 -060014 ID string `json:"id"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020015
16 // Human-readable name for the network. Might not be unique.
Jon Perritt3c166472016-02-25 03:07:41 -060017 Name string `json:"name"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020018
19 // The administrative state of network. If false (down), the network does not forward packets.
Jon Perritt3c166472016-02-25 03:07:41 -060020 AdminStateUp bool `json:"admin_state_up"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020021
22 // Indicates whether network is currently operational. Possible values include
23 // `ACTIVE', `DOWN', `BUILD', or `ERROR'. Plug-ins might define additional values.
Jon Perritt3c166472016-02-25 03:07:41 -060024 Status string `json:"status"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020025
26 // Subnets associated with this network.
Jon Perritt3c166472016-02-25 03:07:41 -060027 Subnets []string `json:"subnets"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020028
29 // Owner of network. Only admin users can specify a tenant_id other than its own.
Jon Perritt3c166472016-02-25 03:07:41 -060030 TenantID string `json:"tenant_id"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020031
32 // Specifies whether the network resource can be accessed by any tenant or not.
Jon Perritt3c166472016-02-25 03:07:41 -060033 Shared bool `json:"shared"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020034
35 // Specifies the nature of the physical network mapped to this network
36 // resource. Examples are flat, vlan, or gre.
Jon Perritt3c166472016-02-25 03:07:41 -060037 NetworkType string `json:"provider:network_type"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020038
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.
Jon Perritt3c166472016-02-25 03:07:41 -060045 PhysicalNetwork string `json:"provider:physical_network"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020046
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.
Jon Perritt3c166472016-02-25 03:07:41 -060051 SegmentationID string `json:"provider:segmentation_id"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +020052}
53
Joe Topjiand5be3fe2016-08-29 09:41:13 -060054func (n *NetworkExtAttrs) UnmarshalJSON(b []byte) error {
55 type tmp NetworkExtAttrs
56 var networkExtAttrs *struct {
57 tmp
58 SegmentationID interface{} `json:"provider:segmentation_id"`
59 }
60
61 if err := json.Unmarshal(b, &networkExtAttrs); err != nil {
62 return err
63 }
64
65 *n = NetworkExtAttrs(networkExtAttrs.tmp)
66
67 switch t := networkExtAttrs.SegmentationID.(type) {
68 case float64:
69 n.SegmentationID = strconv.FormatFloat(t, 'f', -1, 64)
70 case string:
71 n.SegmentationID = string(t)
72 }
73
74 return nil
75}
76
Jamie Hannaford5a531902014-09-26 15:03:31 +020077// ExtractGet decorates a GetResult struct returned from a networks.Get()
78// function with extended attributes.
Jamie Hannafordb0d99122014-09-25 10:49:14 +020079func ExtractGet(r networks.GetResult) (*NetworkExtAttrs, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060080 var s struct {
Jamie Hannafordb0d99122014-09-25 10:49:14 +020081 Network *NetworkExtAttrs `json:"network"`
82 }
Jon Perritt3c166472016-02-25 03:07:41 -060083 err := r.ExtractInto(&s)
84 return s.Network, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +020085}
86
Jamie Hannafordc7c49a72014-10-06 15:49:53 +020087// ExtractCreate decorates a CreateResult struct returned from a networks.Create()
Jamie Hannaford5a531902014-09-26 15:03:31 +020088// function with extended attributes.
Jamie Hannafordb0d99122014-09-25 10:49:14 +020089func ExtractCreate(r networks.CreateResult) (*NetworkExtAttrs, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060090 var s struct {
Jamie Hannafordb0d99122014-09-25 10:49:14 +020091 Network *NetworkExtAttrs `json:"network"`
92 }
Jon Perritt3c166472016-02-25 03:07:41 -060093 err := r.ExtractInto(&s)
94 return s.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) {
Jon Perritt3c166472016-02-25 03:07:41 -0600100 var s struct {
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200101 Network *NetworkExtAttrs `json:"network"`
102 }
Jon Perritt3c166472016-02-25 03:07:41 -0600103 err := r.ExtractInto(&s)
104 return s.Network, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200105}
106
107// ExtractList accepts a Page struct, specifically a NetworkPage struct, and
108// extracts the elements into a slice of NetworkExtAttrs structs. In other
109// words, a generic collection is mapped into a relevant slice.
Jon Perritt31b66462016-02-25 22:25:30 -0600110func ExtractList(r pagination.Page) ([]NetworkExtAttrs, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600111 var s struct {
112 Networks []NetworkExtAttrs `json:"networks" json:"networks"`
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200113 }
Jon Perritt31b66462016-02-25 22:25:30 -0600114 err := (r.(networks.NetworkPage)).ExtractInto(&s)
Jon Perritt3c166472016-02-25 03:07:41 -0600115 return s.Networks, err
Jamie Hannafordb0d99122014-09-25 10:49:14 +0200116}