-
Notifications
You must be signed in to change notification settings - Fork 2
/
flavor.go
60 lines (50 loc) · 1.49 KB
/
flavor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package dbaas
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// FlavorResponse is the API response for the flavors.
type FlavorResponse struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
FlSize string `json:"fl_size"`
DatastoreTypeIDs []string `json:"datastore_type_ids"`
Vcpus int `json:"vcpus"`
RAM int `json:"ram"`
Disk int `json:"disk"`
}
const FlavorsURI = "/flavors"
// Flavors returns all flavors.
func (api *API) Flavors(ctx context.Context) ([]FlavorResponse, error) {
resp, err := api.makeRequest(ctx, http.MethodGet, FlavorsURI, nil)
if err != nil {
return []FlavorResponse{}, err
}
var result struct {
Flavors []FlavorResponse `json:"flavors"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return []FlavorResponse{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.Flavors, nil
}
// Flavor returns a flavor based on the ID.
func (api *API) Flavor(ctx context.Context, flavorID string) (FlavorResponse, error) {
uri := fmt.Sprintf("%s/%s", FlavorsURI, flavorID)
resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return FlavorResponse{}, err
}
var result struct {
Flavor FlavorResponse `json:"flavor"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return FlavorResponse{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.Flavor, nil
}