Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 1 | package stackresources |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | ) |
| 10 | |
| 11 | type Resource struct { |
| 12 | Links []gophercloud.Link `mapstructure:"links"` |
| 13 | LogicalID string `mapstructure:"logical_resource_id"` |
| 14 | Name string `mapstructure:"resource_name"` |
| 15 | PhysicalID string `mapstructure:"physical_resource_id"` |
| 16 | RequiredBy []interface{} `mapstructure:"required_by"` |
| 17 | Status string `mapstructure:"resource_status"` |
| 18 | StatusReason string `mapstructure:"resource_status_reason"` |
| 19 | Type string `mapstructure:"resource_type"` |
| 20 | UpdatedTime time.Time `mapstructure:"-"` |
| 21 | } |
| 22 | |
| 23 | type FindResult struct { |
| 24 | gophercloud.Result |
| 25 | } |
| 26 | |
| 27 | func (r FindResult) Extract() ([]Resource, error) { |
| 28 | if r.Err != nil { |
| 29 | return nil, r.Err |
| 30 | } |
| 31 | |
| 32 | var res struct { |
| 33 | Res []Resource `mapstructure:"resources"` |
| 34 | } |
| 35 | |
| 36 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | |
| 40 | resources := r.Body.(map[string]interface{})["resources"].([]map[string]interface{}) |
| 41 | |
| 42 | for i, resource := range resources { |
| 43 | if date, ok := resource["updated_time"]; ok && date != nil { |
| 44 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | res.Res[i].UpdatedTime = t |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return res.Res, nil |
| 53 | } |
| 54 | |
| 55 | // ResourcePage abstracts the raw results of making a List() request against the API. |
| 56 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 57 | // data provided through the ExtractResources call. |
| 58 | type ResourcePage struct { |
| 59 | pagination.LinkedPageBase |
| 60 | } |
| 61 | |
| 62 | // IsEmpty returns true if a page contains no Server results. |
| 63 | func (page ResourcePage) IsEmpty() (bool, error) { |
| 64 | resources, err := ExtractResources(page) |
| 65 | if err != nil { |
| 66 | return true, err |
| 67 | } |
| 68 | return len(resources) == 0, nil |
| 69 | } |
| 70 | |
| 71 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 72 | func (page ResourcePage) NextPageURL() (string, error) { |
| 73 | type resp struct { |
| 74 | Links []gophercloud.Link `mapstructure:"servers_links"` |
| 75 | } |
| 76 | |
| 77 | var r resp |
| 78 | err := mapstructure.Decode(page.Body, &r) |
| 79 | if err != nil { |
| 80 | return "", err |
| 81 | } |
| 82 | |
| 83 | return gophercloud.ExtractNextURL(r.Links) |
| 84 | } |
| 85 | |
| 86 | // ExtractResources interprets the results of a single page from a List() call, producing a slice of Resource entities. |
| 87 | func ExtractResources(page pagination.Page) ([]Resource, error) { |
| 88 | casted := page.(ResourcePage).Body |
| 89 | |
| 90 | var response struct { |
| 91 | Resources []Resource `mapstructure:"resources"` |
| 92 | } |
| 93 | err := mapstructure.Decode(casted, &response) |
| 94 | return response.Resources, err |
| 95 | } |
| 96 | |
| 97 | type GetResult struct { |
| 98 | gophercloud.Result |
| 99 | } |
| 100 | |
| 101 | func (r GetResult) Extract() (*Resource, error) { |
| 102 | if r.Err != nil { |
| 103 | return nil, r.Err |
| 104 | } |
| 105 | |
| 106 | var res struct { |
| 107 | Res *Resource `mapstructure:"resource"` |
| 108 | } |
| 109 | |
| 110 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | |
| 114 | resource := r.Body.(map[string]interface{})["resource"].(map[string]interface{}) |
| 115 | |
| 116 | if date, ok := resource["updated_time"]; ok && date != nil { |
| 117 | t, err := time.Parse(time.RFC3339, date.(string)) |
| 118 | if err != nil { |
| 119 | return nil, err |
| 120 | } |
| 121 | res.Res.UpdatedTime = t |
| 122 | } |
| 123 | |
| 124 | return res.Res, nil |
| 125 | } |
| 126 | |
| 127 | type MetadataResult struct { |
| 128 | gophercloud.Result |
| 129 | } |
| 130 | |
| 131 | func (r MetadataResult) Extract() (map[string]string, error) { |
| 132 | if r.Err != nil { |
| 133 | return nil, r.Err |
| 134 | } |
| 135 | |
| 136 | var res struct { |
| 137 | Meta map[string]string `mapstructure:"metadata"` |
| 138 | } |
| 139 | |
| 140 | if err := mapstructure.Decode(r.Body, &res); err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | |
| 144 | return res.Meta, nil |
| 145 | } |