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