blob: 6762ef2c3e993fdf649ad780323da088deafa874 [file] [log] [blame]
ehdou10f1f852016-10-14 20:58:23 +03001package sharenetworks
2
ehdou07943332016-10-24 21:21:58 +03003import (
jrperritt98d01622017-01-12 14:24:42 -06004 "encoding/json"
ehdou07943332016-10-24 21:21:58 +03005 "net/url"
6 "strconv"
jrperritt98d01622017-01-12 14:24:42 -06007 "time"
ehdou07943332016-10-24 21:21:58 +03008
9 "github.com/gophercloud/gophercloud"
10 "github.com/gophercloud/gophercloud/pagination"
11)
ehdou10f1f852016-10-14 20:58:23 +030012
13// ShareNetwork contains all the information associated with an OpenStack
14// ShareNetwork.
15type ShareNetwork struct {
16 // The Share Network ID
17 ID string `json:"id"`
18 // The UUID of the project where the share network was created
19 ProjectID string `json:"project_id"`
20 // The neutron network ID
21 NeutronNetID string `json:"neutron_net_id"`
22 // The neutron subnet ID
23 NeutronSubnetID string `json:"neutron_subnet_id"`
24 // The nova network ID
25 NovaNetID string `json:"nova_net_id"`
26 // The network type. A valid value is VLAN, VXLAN, GRE or flat
27 NetworkType string `json:"network_type"`
28 // The segmentation ID
29 SegmentationID int `json:"segmentation_id"`
30 // The IP block from which to allocate the network, in CIDR notation
31 CIDR string `json:"cidr"`
32 // The IP version of the network. A valid value is 4 or 6
33 IPVersion int `json:"ip_version"`
34 // The Share Network name
35 Name string `json:"name"`
36 // The Share Network description
37 Description string `json:"description"`
38 // The date and time stamp when the Share Network was created
jrperritt98d01622017-01-12 14:24:42 -060039 CreatedAt time.Time `json:"-"`
ehdou10f1f852016-10-14 20:58:23 +030040 // The date and time stamp when the Share Network was updated
jrperritt98d01622017-01-12 14:24:42 -060041 UpdatedAt time.Time `json:"-"`
42}
43
44func (r *ShareNetwork) UnmarshalJSON(b []byte) error {
45 type tmp ShareNetwork
46 var s struct {
47 tmp
48 CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
49 UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
50 }
51 err := json.Unmarshal(b, &s)
52 if err != nil {
53 return err
54 }
55 *r = ShareNetwork(s.tmp)
56
57 r.CreatedAt = time.Time(s.CreatedAt)
58 r.UpdatedAt = time.Time(s.UpdatedAt)
59
60 return nil
ehdou10f1f852016-10-14 20:58:23 +030061}
62
63type commonResult struct {
64 gophercloud.Result
65}
66
ehdou07943332016-10-24 21:21:58 +030067// ShareNetworkPage is a pagination.pager that is returned from a call to the List function.
68type ShareNetworkPage struct {
69 pagination.MarkerPageBase
70}
71
72// NextPageURL generates the URL for the page of results after this one.
73func (r ShareNetworkPage) NextPageURL() (string, error) {
74 currentURL := r.URL
75 mark, err := r.Owner.LastMarker()
76 if err != nil {
77 return "", err
78 }
79
80 q := currentURL.Query()
81 q.Set("offset", mark)
82 currentURL.RawQuery = q.Encode()
83 return currentURL.String(), nil
84}
85
86// LastMarker returns the last offset in a ListResult.
87func (r ShareNetworkPage) LastMarker() (string, error) {
88 maxInt := strconv.Itoa(int(^uint(0) >> 1))
89 shareNetworks, err := ExtractShareNetworks(r)
90 if err != nil {
91 return maxInt, err
92 }
93 if len(shareNetworks) == 0 {
94 return maxInt, nil
95 }
96
97 u, err := url.Parse(r.URL.String())
98 if err != nil {
99 return maxInt, err
100 }
101 queryParams := u.Query()
102 offset := queryParams.Get("offset")
103 limit := queryParams.Get("limit")
104
105 // Limit is not present, only one page required
106 if limit == "" {
107 return maxInt, nil
108 }
109
110 iOffset := 0
111 if offset != "" {
112 iOffset, err = strconv.Atoi(offset)
113 if err != nil {
114 return maxInt, err
115 }
116 }
117 iLimit, err := strconv.Atoi(limit)
118 if err != nil {
119 return maxInt, err
120 }
121 iOffset = iOffset + iLimit
122 offset = strconv.Itoa(iOffset)
123
124 return offset, nil
125}
126
127// IsEmpty satisifies the IsEmpty method of the Page interface
128func (r ShareNetworkPage) IsEmpty() (bool, error) {
129 shareNetworks, err := ExtractShareNetworks(r)
130 return len(shareNetworks) == 0, err
131}
132
133// ExtractShareNetworks extracts and returns ShareNetworks. It is used while
134// iterating over a sharenetworks.List call.
135func ExtractShareNetworks(r pagination.Page) ([]ShareNetwork, error) {
136 var s struct {
137 ShareNetworks []ShareNetwork `json:"share_networks"`
138 }
139 err := (r.(ShareNetworkPage)).ExtractInto(&s)
140 return s.ShareNetworks, err
141}
142
ehdou10f1f852016-10-14 20:58:23 +0300143// Extract will get the ShareNetwork object out of the commonResult object.
144func (r commonResult) Extract() (*ShareNetwork, error) {
145 var s struct {
146 ShareNetwork *ShareNetwork `json:"share_network"`
147 }
148 err := r.ExtractInto(&s)
149 return s.ShareNetwork, err
150}
151
152// CreateResult contains the response body and error from a Create request.
153type CreateResult struct {
154 commonResult
155}
ehdou53687252016-10-14 22:10:13 +0300156
157// DeleteResult contains the response body and error from a Delete request.
158type DeleteResult struct {
159 gophercloud.ErrResult
160}
ehdoua088cec2016-10-25 21:02:05 +0300161
162// GetResult contains the response body and error from a Get request.
163type GetResult struct {
164 commonResult
165}
ehdoufd6d86c2016-11-03 21:35:45 +0200166
167// UpdateResult contains the response body and error from an Update request.
168type UpdateResult struct {
169 commonResult
170}