@@ -3,7 +3,6 @@ package utils
33import (
44 "context"
55 "fmt"
6- "strconv"
76 "strings"
87
98 "github.com/gophercloud/gophercloud/v2"
@@ -29,6 +28,7 @@ var goodStatus = map[string]bool{
2928// It returns the highest-Priority Version, OR exact match with client endpoint,
3029// among the alternatives that are provided, as well as its corresponding endpoint.
3130func ChooseVersion (ctx context.Context , client * gophercloud.ProviderClient , recognized []* Version ) (* Version , string , error ) {
31+ // TODO(stephenfin): This could be removed since we can accomplish this with GetServiceVersions now.
3232 type linkResp struct {
3333 Href string `json:"href"`
3434 Rel string `json:"rel"`
@@ -114,129 +114,3 @@ func ChooseVersion(ctx context.Context, client *gophercloud.ProviderClient, reco
114114
115115 return highest , endpoint , nil
116116}
117-
118- type SupportedMicroversions struct {
119- MaxMajor int
120- MaxMinor int
121- MinMajor int
122- MinMinor int
123- }
124-
125- // GetServiceVersions returns the minimum and maximum microversion that is supported by the ServiceClient Endpoint.
126- func GetServiceVersions (ctx context.Context , client * gophercloud.ProviderClient , endpointURL string ) (SupportedMicroversions , error ) {
127- type valueResp struct {
128- ID string `json:"id"`
129- Status string `json:"status"`
130- Version string `json:"version"`
131- MinVersion string `json:"min_version"`
132- }
133-
134- type response struct {
135- Version valueResp `json:"version"`
136- Versions []valueResp `json:"versions"`
137- }
138- var minVersion , maxVersion string
139- var supportedMicroversions SupportedMicroversions
140- var resp response
141- _ , err := client .Request (ctx , "GET" , endpointURL , & gophercloud.RequestOpts {
142- JSONResponse : & resp ,
143- OkCodes : []int {200 , 300 },
144- })
145-
146- if err != nil {
147- return supportedMicroversions , err
148- }
149-
150- if len (resp .Versions ) > 0 {
151- // We are dealing with an unversioned endpoint
152- // We only handle the case when there is exactly one, and assume it is the correct one
153- if len (resp .Versions ) > 1 {
154- return supportedMicroversions , fmt .Errorf ("unversioned endpoint with multiple alternatives not supported" )
155- }
156- minVersion = resp .Versions [0 ].MinVersion
157- maxVersion = resp .Versions [0 ].Version
158- } else {
159- minVersion = resp .Version .MinVersion
160- maxVersion = resp .Version .Version
161- }
162-
163- // Return early if the endpoint does not support microversions
164- if minVersion == "" && maxVersion == "" {
165- return supportedMicroversions , fmt .Errorf ("microversions not supported by endpoint" )
166- }
167-
168- supportedMicroversions .MinMajor , supportedMicroversions .MinMinor , err = ParseMicroversion (minVersion )
169- if err != nil {
170- return supportedMicroversions , err
171- }
172-
173- supportedMicroversions .MaxMajor , supportedMicroversions .MaxMinor , err = ParseMicroversion (maxVersion )
174- if err != nil {
175- return supportedMicroversions , err
176- }
177-
178- return supportedMicroversions , nil
179- }
180-
181- // GetSupportedMicroversions returns the minimum and maximum microversion that is supported by the ServiceClient Endpoint.
182- func GetSupportedMicroversions (ctx context.Context , client * gophercloud.ServiceClient ) (SupportedMicroversions , error ) {
183- return GetServiceVersions (ctx , client .ProviderClient , client .Endpoint )
184- }
185-
186- // RequireMicroversion checks that the required microversion is supported and
187- // returns a ServiceClient with the microversion set.
188- func RequireMicroversion (ctx context.Context , client gophercloud.ServiceClient , required string ) (gophercloud.ServiceClient , error ) {
189- supportedMicroversions , err := GetSupportedMicroversions (ctx , & client )
190- if err != nil {
191- return client , fmt .Errorf ("unable to determine supported microversions: %w" , err )
192- }
193- supported , err := supportedMicroversions .IsSupported (required )
194- if err != nil {
195- return client , err
196- }
197- if ! supported {
198- return client , fmt .Errorf ("microversion %s not supported. Supported versions: %v" , required , supportedMicroversions )
199- }
200- client .Microversion = required
201- return client , nil
202- }
203-
204- // IsSupported checks if a microversion falls in the supported interval.
205- // It returns true if the version is within the interval and false otherwise.
206- func (supported SupportedMicroversions ) IsSupported (version string ) (bool , error ) {
207- // Parse the version X.Y into X and Y integers that are easier to compare.
208- vMajor , vMinor , err := ParseMicroversion (version )
209- if err != nil {
210- return false , err
211- }
212-
213- // Check that the major version number is supported.
214- if (vMajor < supported .MinMajor ) || (vMajor > supported .MaxMajor ) {
215- return false , nil
216- }
217-
218- // Check that the minor version number is supported
219- if (vMinor <= supported .MaxMinor ) && (vMinor >= supported .MinMinor ) {
220- return true , nil
221- }
222-
223- return false , nil
224- }
225-
226- // ParseMicroversion parses the version major.minor into separate integers major and minor.
227- // For example, "2.53" becomes 2 and 53.
228- func ParseMicroversion (version string ) (major int , minor int , err error ) {
229- parts := strings .Split (version , "." )
230- if len (parts ) != 2 {
231- return 0 , 0 , fmt .Errorf ("invalid microversion format: %q" , version )
232- }
233- major , err = strconv .Atoi (parts [0 ])
234- if err != nil {
235- return 0 , 0 , err
236- }
237- minor , err = strconv .Atoi (parts [1 ])
238- if err != nil {
239- return 0 , 0 , err
240- }
241- return major , minor , nil
242- }
0 commit comments