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" |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 5 | "time" |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 6 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 7 | "github.com/gophercloud/gophercloud" |
| 8 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 9 | ) |
| 10 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 11 | // Resource represents a stack resource. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 12 | type Resource struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 13 | Attributes map[string]interface{} `json:"attributes"` |
| 14 | CreationTime time.Time `json:"-"` |
| 15 | Description string `json:"description"` |
| 16 | Links []gophercloud.Link `json:"links"` |
| 17 | LogicalID string `json:"logical_resource_id"` |
| 18 | Name string `json:"resource_name"` |
| 19 | PhysicalID string `json:"physical_resource_id"` |
| 20 | RequiredBy []interface{} `json:"required_by"` |
| 21 | Status string `json:"resource_status"` |
| 22 | StatusReason string `json:"resource_status_reason"` |
| 23 | Type string `json:"resource_type"` |
| 24 | UpdatedTime time.Time `json:"-"` |
| 25 | } |
| 26 | |
| 27 | func (r *Resource) UnmarshalJSON(b []byte) error { |
| 28 | type tmp Resource |
| 29 | var s struct { |
| 30 | tmp |
| 31 | CreationTime gophercloud.JSONRFC3339NoZ `json:"creation_time"` |
| 32 | UpdatedTime gophercloud.JSONRFC3339NoZ `json:"updated_time"` |
| 33 | } |
| 34 | err := json.Unmarshal(b, &s) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | *r = Resource(s.tmp) |
| 39 | |
| 40 | r.CreationTime = time.Time(s.CreationTime) |
| 41 | r.UpdatedTime = time.Time(s.UpdatedTime) |
| 42 | |
| 43 | return nil |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 46 | // FindResult represents the result of a Find operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 47 | type FindResult struct { |
| 48 | gophercloud.Result |
| 49 | } |
| 50 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 51 | // Extract returns a slice of Resource objects and is called after a |
| 52 | // Find operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 53 | func (r FindResult) Extract() ([]Resource, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 54 | var s struct { |
| 55 | Resources []Resource `json:"resources"` |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 56 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 57 | err := r.ExtractInto(&s) |
| 58 | return s.Resources, err |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // ResourcePage abstracts the raw results of making a List() request against the API. |
| 62 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 63 | // data provided through the ExtractResources call. |
| 64 | type ResourcePage struct { |
Pratik Mallya | 5448f58 | 2015-08-21 12:21:09 -0500 | [diff] [blame] | 65 | pagination.SinglePageBase |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // IsEmpty returns true if a page contains no Server results. |
Jon Perritt | c871733 | 2015-02-08 20:14:29 -0700 | [diff] [blame] | 69 | func (r ResourcePage) IsEmpty() (bool, error) { |
| 70 | resources, err := ExtractResources(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 71 | return len(resources) == 0, err |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 74 | // ExtractResources interprets the results of a single page from a List() call, producing a slice of Resource entities. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 75 | func ExtractResources(r pagination.Page) ([]Resource, error) { |
| 76 | var s struct { |
| 77 | Resources []Resource `json:"resources"` |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 78 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 79 | err := (r.(ResourcePage)).ExtractInto(&s) |
| 80 | return s.Resources, err |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 83 | // GetResult represents the result of a Get operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 84 | type GetResult struct { |
| 85 | gophercloud.Result |
| 86 | } |
| 87 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 88 | // Extract returns a pointer to a Resource object and is called after a |
| 89 | // Get operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 90 | func (r GetResult) Extract() (*Resource, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 91 | var s struct { |
| 92 | Resource *Resource `json:"resource"` |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 93 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 94 | err := r.ExtractInto(&s) |
| 95 | return s.Resource, err |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 98 | // MetadataResult represents the result of a Metadata operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 99 | type MetadataResult struct { |
| 100 | gophercloud.Result |
| 101 | } |
| 102 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 103 | // Extract returns a map object and is called after a |
| 104 | // Metadata operation. |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 105 | func (r MetadataResult) Extract() (map[string]string, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 106 | var s struct { |
| 107 | Meta map[string]string `json:"metadata"` |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 108 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 109 | err := r.ExtractInto(&s) |
| 110 | return s.Meta, err |
Jon Perritt | 3711cd0 | 2014-12-22 22:20:15 -0700 | [diff] [blame] | 111 | } |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 112 | |
| 113 | // ResourceTypePage abstracts the raw results of making a ListTypes() request against the API. |
| 114 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 115 | // data provided through the ExtractResourceTypes call. |
| 116 | type ResourceTypePage struct { |
| 117 | pagination.SinglePageBase |
| 118 | } |
| 119 | |
| 120 | // IsEmpty returns true if a ResourceTypePage contains no resource types. |
| 121 | func (r ResourceTypePage) IsEmpty() (bool, error) { |
| 122 | rts, err := ExtractResourceTypes(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 123 | return len(rts) == 0, err |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 126 | // ResourceTypes represents the type that holds the result of ExtractResourceTypes. |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 127 | // We define methods on this type to sort it before output |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 128 | type ResourceTypes []string |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 129 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 130 | func (r ResourceTypes) Len() int { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 131 | return len(r) |
| 132 | } |
| 133 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 134 | func (r ResourceTypes) Swap(i, j int) { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 135 | r[i], r[j] = r[j], r[i] |
| 136 | } |
| 137 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 138 | func (r ResourceTypes) Less(i, j int) bool { |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 139 | return r[i] < r[j] |
| 140 | } |
| 141 | |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 142 | // ExtractResourceTypes extracts and returns resource types. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 143 | func ExtractResourceTypes(r pagination.Page) (ResourceTypes, error) { |
| 144 | var s struct { |
| 145 | ResourceTypes ResourceTypes `json:"resource_types"` |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 146 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 147 | err := (r.(ResourceTypePage)).ExtractInto(&s) |
| 148 | return s.ResourceTypes, err |
Jon Perritt | a065da1 | 2015-02-06 10:20:16 -0700 | [diff] [blame] | 149 | } |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 150 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 151 | // TypeSchema represents a stack resource schema. |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 152 | type TypeSchema struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 153 | Attributes map[string]interface{} `json:"attributes"` |
| 154 | Properties map[string]interface{} `json:"properties"` |
| 155 | ResourceType string `json:"resource_type"` |
| 156 | SupportStatus map[string]interface{} `json:"support_status"` |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 159 | // SchemaResult represents the result of a Schema operation. |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 160 | type SchemaResult struct { |
| 161 | gophercloud.Result |
| 162 | } |
| 163 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 164 | // Extract returns a pointer to a TypeSchema object and is called after a |
| 165 | // Schema operation. |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 166 | func (r SchemaResult) Extract() (*TypeSchema, error) { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 167 | var s *TypeSchema |
| 168 | err := r.ExtractInto(&s) |
| 169 | return s, err |
Jon Perritt | 1d4aca0 | 2015-02-06 12:29:16 -0700 | [diff] [blame] | 170 | } |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 171 | |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 172 | // TemplateResult represents the result of a Template operation. |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 173 | type TemplateResult struct { |
| 174 | gophercloud.Result |
| 175 | } |
| 176 | |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 177 | // Extract returns the template and is called after a |
Jon Perritt | bba201b | 2015-02-08 21:12:38 -0700 | [diff] [blame] | 178 | // Template operation. |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 179 | func (r TemplateResult) Extract() ([]byte, error) { |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 180 | if r.Err != nil { |
| 181 | return nil, r.Err |
| 182 | } |
Pratik Mallya | 827c03e | 2015-09-17 00:10:47 -0500 | [diff] [blame] | 183 | template, err := json.MarshalIndent(r.Body, "", " ") |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 184 | return template, err |
Jon Perritt | b1e303a | 2015-02-06 22:15:44 -0700 | [diff] [blame] | 185 | } |