blob: 5128b01d3529aba71ce23ea28c61b4b55dfdc4b8 [file] [log] [blame]
Jamie Hannaforded7f4532015-02-17 14:56:30 +01001package configurations
2
3import (
Jamie Hannaford87704ba2016-01-14 11:49:56 +01004 "fmt"
5 "reflect"
Jamie Hannaforde65ad952015-11-16 14:05:11 +01006 "time"
7
Jamie Hannaforded7f4532015-02-17 14:56:30 +01008 "github.com/mitchellh/mapstructure"
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud"
10 "github.com/gophercloud/gophercloud/pagination"
Jamie Hannaforded7f4532015-02-17 14:56:30 +010011)
12
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010013// Config represents a configuration group API resource.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010014type Config struct {
Jamie Hannaforde65ad952015-11-16 14:05:11 +010015 Created time.Time `mapstructure:"-"`
16 Updated time.Time `mapstructure:"-"`
17 DatastoreName string `mapstructure:"datastore_name"`
18 DatastoreVersionID string `mapstructure:"datastore_version_id"`
19 DatastoreVersionName string `mapstructure:"datastore_version_name"`
Jamie Hannaforded7f4532015-02-17 14:56:30 +010020 Description string
21 ID string
22 Name string
23 Values map[string]interface{}
24}
25
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010026// ConfigPage contains a page of Config resources in a paginated collection.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010027type ConfigPage struct {
28 pagination.SinglePageBase
29}
30
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010031// IsEmpty indicates whether a ConfigPage is empty.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010032func (r ConfigPage) IsEmpty() (bool, error) {
33 is, err := ExtractConfigs(r)
34 if err != nil {
35 return true, err
36 }
37 return len(is) == 0, nil
38}
39
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010040// ExtractConfigs will retrieve a slice of Config structs from a page.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010041func ExtractConfigs(page pagination.Page) ([]Config, error) {
42 casted := page.(ConfigPage).Body
43
44 var resp struct {
45 Configs []Config `mapstructure:"configurations" json:"configurations"`
46 }
47
Jamie Hannaford87704ba2016-01-14 11:49:56 +010048 if err := mapstructure.Decode(casted, &resp); err != nil {
49 return nil, err
50 }
Jamie Hannaforde65ad952015-11-16 14:05:11 +010051
52 var vals []interface{}
Jamie Hannaford87704ba2016-01-14 11:49:56 +010053 switch casted.(type) {
54 case map[string]interface{}:
Jamie Hannaforde65ad952015-11-16 14:05:11 +010055 vals = casted.(map[string]interface{})["configurations"].([]interface{})
Jamie Hannaford87704ba2016-01-14 11:49:56 +010056 case map[string][]interface{}:
57 vals = casted.(map[string][]interface{})["configurations"]
58 default:
59 return resp.Configs, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted))
Jamie Hannaforde65ad952015-11-16 14:05:11 +010060 }
61
62 for i, v := range vals {
63 val := v.(map[string]interface{})
64
65 if t, ok := val["created"].(string); ok && t != "" {
66 creationTime, err := time.Parse(time.RFC3339, t)
67 if err != nil {
68 return resp.Configs, err
69 }
70 resp.Configs[i].Created = creationTime
71 }
72
73 if t, ok := val["updated"].(string); ok && t != "" {
74 updatedTime, err := time.Parse(time.RFC3339, t)
75 if err != nil {
76 return resp.Configs, err
77 }
78 resp.Configs[i].Updated = updatedTime
79 }
80 }
81
Jamie Hannaford87704ba2016-01-14 11:49:56 +010082 return resp.Configs, nil
Jamie Hannaforded7f4532015-02-17 14:56:30 +010083}
84
85type commonResult struct {
86 gophercloud.Result
87}
88
Jamie Hannafordb0d267b2015-02-19 11:59:53 +010089// Extract will retrieve a Config resource from an operation result.
Jamie Hannaforded7f4532015-02-17 14:56:30 +010090func (r commonResult) Extract() (*Config, error) {
91 if r.Err != nil {
92 return nil, r.Err
93 }
94
95 var response struct {
96 Config Config `mapstructure:"configuration"`
97 }
98
99 err := mapstructure.Decode(r.Body, &response)
Jamie Hannaforde65ad952015-11-16 14:05:11 +0100100 val := r.Body.(map[string]interface{})["configuration"].(map[string]interface{})
101
102 if t, ok := val["created"].(string); ok && t != "" {
103 creationTime, err := time.Parse(time.RFC3339, t)
104 if err != nil {
105 return &response.Config, err
106 }
107 response.Config.Created = creationTime
108 }
109
110 if t, ok := val["updated"].(string); ok && t != "" {
111 updatedTime, err := time.Parse(time.RFC3339, t)
112 if err != nil {
113 return &response.Config, err
114 }
115 response.Config.Updated = updatedTime
116 }
117
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100118 return &response.Config, err
119}
120
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100121// GetResult represents the result of a Get operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100122type GetResult struct {
123 commonResult
124}
125
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100126// CreateResult represents the result of a Create operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100127type CreateResult struct {
128 commonResult
129}
130
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100131// UpdateResult represents the result of an Update operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100132type UpdateResult struct {
133 gophercloud.ErrResult
134}
135
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100136// ReplaceResult represents the result of a Replace operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100137type ReplaceResult struct {
138 gophercloud.ErrResult
139}
140
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100141// DeleteResult represents the result of a Delete operation.
Jamie Hannaforded7f4532015-02-17 14:56:30 +0100142type DeleteResult struct {
143 gophercloud.ErrResult
144}
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100145
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100146// Param represents a configuration parameter API resource.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100147type Param struct {
148 Max int
149 Min int
150 Name string
151 RestartRequired bool `mapstructure:"restart_required" json:"restart_required"`
152 Type string
153}
154
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100155// ParamPage contains a page of Param resources in a paginated collection.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100156type ParamPage struct {
157 pagination.SinglePageBase
158}
159
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100160// IsEmpty indicates whether a ParamPage is empty.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100161func (r ParamPage) IsEmpty() (bool, error) {
162 is, err := ExtractParams(r)
163 if err != nil {
164 return true, err
165 }
166 return len(is) == 0, nil
167}
168
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100169// ExtractParams will retrieve a slice of Param structs from a page.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100170func ExtractParams(page pagination.Page) ([]Param, error) {
171 casted := page.(ParamPage).Body
172
173 var resp struct {
174 Params []Param `mapstructure:"configuration-parameters" json:"configuration-parameters"`
175 }
176
177 err := mapstructure.Decode(casted, &resp)
178 return resp.Params, err
179}
180
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100181// ParamResult represents the result of an operation which retrieves details
182// about a particular configuration param.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100183type ParamResult struct {
184 gophercloud.Result
185}
186
Jamie Hannafordb0d267b2015-02-19 11:59:53 +0100187// Extract will retrieve a param from an operation result.
Jamie Hannaford23867bb2015-02-17 15:56:48 +0100188func (r ParamResult) Extract() (*Param, error) {
189 if r.Err != nil {
190 return nil, r.Err
191 }
192
193 var param Param
194
195 err := mapstructure.Decode(r.Body, &param)
196 return &param, err
197}