Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 1 | package stackresources |
| 2 | |
| 3 | import ( |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 4 | "encoding/json" |
Jon Perritt | a11b5df | 2015-03-05 13:51:59 -0700 | [diff] [blame] | 5 | "fmt" |
| 6 | "reflect" |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 7 | "time" |
| 8 | |
| 9 | "github.com/mitchellh/mapstructure" |
| 10 | "github.com/rackspace/gophercloud" |
| 11 | "github.com/rackspace/gophercloud/pagination" |
| 12 | ) |
| 13 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 14 | // Resource represents a stack resource. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 15 | type Resource struct { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 16 | Attributes map[string]interface{} `mapstructure:"attributes"` |
| 17 | CreationTime time.Time `mapstructure:"-"` |
| 18 | Description string `mapstructure:"description"` |
| 19 | Links []gophercloud.Link `mapstructure:"links"` |
| 20 | LogicalID string `mapstructure:"logical_resource_id"` |
| 21 | Name string `mapstructure:"resource_name"` |
| 22 | PhysicalID string `mapstructure:"physical_resource_id"` |
| 23 | RequiredBy []interface{} `mapstructure:"required_by"` |
| 24 | Status string `mapstructure:"resource_status"` |
| 25 | StatusReason string `mapstructure:"resource_status_reason"` |
| 26 | Type string `mapstructure:"resource_type"` |
| 27 | UpdatedTime time.Time `mapstructure:"-"` |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 30 | // FindResult represents the result of a Find operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 31 | type FindResult struct { |
| 32 | gophercloud.Result |
| 33 | } |
| 34 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 35 | // Extract returns a slice of Resource objects and is called after a |
| 36 | // Find operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 37 | func (r FindResult) Extract() ([]Resource, error) { |
| 38 | if r.Err != nil { |
| 39 | return nil, r.Err |
| 40 | } |
| 41 | |
| 42 | var res struct { |
| 43 | Res []Resource `mapstructure:"resources"` |
| 44 | } |
| 45 | |
| 46 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 50 | resources := r.Body.(map[string]interface{})["resources"].([]interface{}) |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 51 | |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 52 | for i, resourceRaw := range resources { |
| 53 | resource := resourceRaw.(map[string]interface{}) |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 54 | if date, ok := resource["updated_time"]; ok && date != nil { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 55 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | res.Res[i].UpdatedTime = t |
| 60 | } |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 61 | if date, ok := resource["creation_time"]; ok && date != nil { |
| 62 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
| 63 | if err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | res.Res[i].CreationTime = t |
| 67 | } |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | return res.Res, nil |
| 71 | } |
| 72 | |
| 73 | // ResourcePage abstracts the raw results of making a List() request against the API. |
| 74 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 75 | // data provided through the ExtractResources call. |
| 76 | type ResourcePage struct { |
Pratik Mallya | 5448f58 | 2015-08-21 12:21:09 -0500 | [diff] [blame] | 77 | pagination.SinglePageBase |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // IsEmpty returns true if a page contains no Server results. |
Jon Perritt | c871733 | 2015-02-08 20:14:29 -0700 | [diff] [blame] | 81 | func (r ResourcePage) IsEmpty() (bool, error) { |
| 82 | resources, err := ExtractResources(r) |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 83 | if err != nil { |
| 84 | return true, err |
| 85 | } |
| 86 | return len(resources) == 0, nil |
| 87 | } |
| 88 | |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 89 | // ExtractResources interprets the results of a single page from a List() call, producing a slice of Resource entities. |
| 90 | func ExtractResources(page pagination.Page) ([]Resource, error) { |
| 91 | casted := page.(ResourcePage).Body |
| 92 | |
| 93 | var response struct { |
| 94 | Resources []Resource `mapstructure:"resources"` |
| 95 | } |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 96 | if err := mapstructure.Decode(casted, &response); err != nil { |
| 97 | return nil, err |
| 98 | } |
Jon Perritt | a11b5df | 2015-03-05 13:51:59 -0700 | [diff] [blame] | 99 | var resources []interface{} |
| 100 | switch casted.(type) { |
| 101 | case map[string]interface{}: |
| 102 | resources = casted.(map[string]interface{})["resources"].([]interface{}) |
| 103 | case map[string][]interface{}: |
| 104 | resources = casted.(map[string][]interface{})["resources"] |
| 105 | default: |
| 106 | return response.Resources, fmt.Errorf("Unknown type: %v", reflect.TypeOf(casted)) |
| 107 | } |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 108 | |
| 109 | for i, resourceRaw := range resources { |
| 110 | resource := resourceRaw.(map[string]interface{}) |
| 111 | if date, ok := resource["updated_time"]; ok && date != nil { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 112 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | response.Resources[i].UpdatedTime = t |
| 117 | } |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 118 | if date, ok := resource["creation_time"]; ok && date != nil { |
| 119 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
| 120 | if err != nil { |
| 121 | return nil, err |
| 122 | } |
| 123 | response.Resources[i].CreationTime = t |
| 124 | } |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 127 | return response.Resources, nil |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 130 | // GetResult represents the result of a Get operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 131 | type GetResult struct { |
| 132 | gophercloud.Result |
| 133 | } |
| 134 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 135 | // Extract returns a pointer to a Resource object and is called after a |
| 136 | // Get operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 137 | func (r GetResult) Extract() (*Resource, error) { |
| 138 | if r.Err != nil { |
| 139 | return nil, r.Err |
| 140 | } |
| 141 | |
| 142 | var res struct { |
| 143 | Res *Resource `mapstructure:"resource"` |
| 144 | } |
| 145 | |
| 146 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | |
| 150 | resource := r.Body.(map[string]interface{})["resource"].(map[string]interface{}) |
| 151 | |
| 152 | if date, ok := resource["updated_time"]; ok && date != nil { |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 153 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 154 | if err != nil { |
| 155 | return nil, err |
| 156 | } |
| 157 | res.Res.UpdatedTime = t |
| 158 | } |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 159 | if date, ok := resource["creation_time"]; ok && date != nil { |
| 160 | t, err := time.Parse(gophercloud.STACK_TIME_FMT, date.(string)) |
| 161 | if err != nil { |
| 162 | return nil, err |
| 163 | } |
| 164 | res.Res.CreationTime = t |
| 165 | } |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 166 | |
| 167 | return res.Res, nil |
| 168 | } |
| 169 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 170 | // MetadataResult represents the result of a Metadata operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 171 | type MetadataResult struct { |
| 172 | gophercloud.Result |
| 173 | } |
| 174 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 175 | // Extract returns a map object and is called after a |
| 176 | // Metadata operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 177 | func (r MetadataResult) Extract() (map[string]string, error) { |
| 178 | if r.Err != nil { |
| 179 | return nil, r.Err |
| 180 | } |
| 181 | |
| 182 | var res struct { |
| 183 | Meta map[string]string `mapstructure:"metadata"` |
| 184 | } |
| 185 | |
| 186 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 187 | return nil, err |
| 188 | } |
| 189 | |
| 190 | return res.Meta, nil |
| 191 | } |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 192 | |
| 193 | // ResourceTypePage abstracts the raw results of making a ListTypes() request against the API. |
| 194 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 195 | // data provided through the ExtractResourceTypes call. |
| 196 | type ResourceTypePage struct { |
| 197 | pagination.SinglePageBase |
| 198 | } |
| 199 | |
| 200 | // IsEmpty returns true if a ResourceTypePage contains no resource types. |
| 201 | func (r ResourceTypePage) IsEmpty() (bool, error) { |
| 202 | rts, err := ExtractResourceTypes(r) |
| 203 | if err != nil { |
| 204 | return true, err |
| 205 | } |
| 206 | return len(rts) == 0, nil |
| 207 | } |
| 208 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 209 | // ResourceTypes represents the type that holds the result of ExtractResourceTypes. |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 210 | // We define methods on this type to sort it before output |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 211 | type ResourceTypes []string |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 212 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 213 | func (r ResourceTypes) Len() int { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 214 | return len(r) |
| 215 | } |
| 216 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 217 | func (r ResourceTypes) Swap(i, j int) { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 218 | r[i], r[j] = r[j], r[i] |
| 219 | } |
| 220 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 221 | func (r ResourceTypes) Less(i, j int) bool { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 222 | return r[i] < r[j] |
| 223 | } |
| 224 | |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 225 | // ExtractResourceTypes extracts and returns resource types. |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 226 | func ExtractResourceTypes(page pagination.Page) (ResourceTypes, error) { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 227 | casted := page.(ResourceTypePage).Body |
| 228 | |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 229 | var response struct { |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 230 | ResourceTypes ResourceTypes `mapstructure:"resource_types"` |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 233 | if err := mapstructure.Decode(casted, &response); err != nil { |
| 234 | return nil, err |
| 235 | } |
| 236 | return response.ResourceTypes, nil |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 237 | } |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 238 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 239 | // TypeSchema represents a stack resource schema. |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 240 | type TypeSchema struct { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 241 | Attributes map[string]interface{} `mapstructure:"attributes"` |
| 242 | Properties map[string]interface{} `mapstrucutre:"properties"` |
| 243 | ResourceType string `mapstructure:"resource_type"` |
| 244 | SupportStatus map[string]interface{} `mapstructure:"support_status"` |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 247 | // SchemaResult represents the result of a Schema operation. |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 248 | type SchemaResult struct { |
| 249 | gophercloud.Result |
| 250 | } |
| 251 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 252 | // Extract returns a pointer to a TypeSchema object and is called after a |
| 253 | // Schema operation. |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 254 | func (r SchemaResult) Extract() (*TypeSchema, error) { |
| 255 | if r.Err != nil { |
| 256 | return nil, r.Err |
| 257 | } |
| 258 | |
| 259 | var res TypeSchema |
| 260 | |
| 261 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 262 | return nil, err |
| 263 | } |
| 264 | |
| 265 | return &res, nil |
| 266 | } |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 267 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 268 | // TemplateResult represents the result of a Template operation. |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 269 | type TemplateResult struct { |
| 270 | gophercloud.Result |
| 271 | } |
| 272 | |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 273 | // Extract returns the template and is called after a |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 274 | // Template operation. |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 275 | func (r TemplateResult) Extract() ([]byte, error) { |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 276 | if r.Err != nil { |
| 277 | return nil, r.Err |
| 278 | } |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 279 | template, err := json.MarshalIndent(r.Body, "", " ") |
| 280 | if err != nil { |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 281 | return nil, err |
| 282 | } |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 283 | return template, nil |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 284 | } |