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