blob: 59c02a38c14cae8e9c13e666da8bc5ea8b682bb5 [file] [log] [blame]
Jon Perritt3711cd02014-12-22 22:20:15 -07001package stackresources
2
3import (
Pratik Mallya827c03e2015-09-17 00:10:47 -05004 "encoding/json"
jrperritt98d01622017-01-12 14:24:42 -06005 "time"
Jon Perritt3711cd02014-12-22 22:20:15 -07006
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/pagination"
Jon Perritt3711cd02014-12-22 22:20:15 -07009)
10
Jon Perrittbba201b2015-02-08 21:12:38 -070011// Resource represents a stack resource.
Jon Perritt3711cd02014-12-22 22:20:15 -070012type Resource struct {
jrperritt98d01622017-01-12 14:24:42 -060013 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
27func (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 Perritt3711cd02014-12-22 22:20:15 -070044}
45
Jon Perrittbba201b2015-02-08 21:12:38 -070046// FindResult represents the result of a Find operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070047type FindResult struct {
48 gophercloud.Result
49}
50
Jon Perrittbba201b2015-02-08 21:12:38 -070051// Extract returns a slice of Resource objects and is called after a
52// Find operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070053func (r FindResult) Extract() ([]Resource, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060054 var s struct {
55 Resources []Resource `json:"resources"`
Jon Perritt3711cd02014-12-22 22:20:15 -070056 }
Jon Perritt3c166472016-02-25 03:07:41 -060057 err := r.ExtractInto(&s)
58 return s.Resources, err
Jon Perritt3711cd02014-12-22 22:20:15 -070059}
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.
64type ResourcePage struct {
Pratik Mallya5448f582015-08-21 12:21:09 -050065 pagination.SinglePageBase
Jon Perritt3711cd02014-12-22 22:20:15 -070066}
67
68// IsEmpty returns true if a page contains no Server results.
Jon Perrittc8717332015-02-08 20:14:29 -070069func (r ResourcePage) IsEmpty() (bool, error) {
70 resources, err := ExtractResources(r)
Jon Perritt3c166472016-02-25 03:07:41 -060071 return len(resources) == 0, err
Jon Perritt3711cd02014-12-22 22:20:15 -070072}
73
Jon Perritt3711cd02014-12-22 22:20:15 -070074// ExtractResources interprets the results of a single page from a List() call, producing a slice of Resource entities.
Jon Perritt3c166472016-02-25 03:07:41 -060075func ExtractResources(r pagination.Page) ([]Resource, error) {
76 var s struct {
77 Resources []Resource `json:"resources"`
Jon Perritt3711cd02014-12-22 22:20:15 -070078 }
Jon Perritt3c166472016-02-25 03:07:41 -060079 err := (r.(ResourcePage)).ExtractInto(&s)
80 return s.Resources, err
Jon Perritt3711cd02014-12-22 22:20:15 -070081}
82
Jon Perrittbba201b2015-02-08 21:12:38 -070083// GetResult represents the result of a Get operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070084type GetResult struct {
85 gophercloud.Result
86}
87
Jon Perrittbba201b2015-02-08 21:12:38 -070088// Extract returns a pointer to a Resource object and is called after a
89// Get operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070090func (r GetResult) Extract() (*Resource, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060091 var s struct {
92 Resource *Resource `json:"resource"`
Jon Perritt3711cd02014-12-22 22:20:15 -070093 }
Jon Perritt3c166472016-02-25 03:07:41 -060094 err := r.ExtractInto(&s)
95 return s.Resource, err
Jon Perritt3711cd02014-12-22 22:20:15 -070096}
97
Jon Perrittbba201b2015-02-08 21:12:38 -070098// MetadataResult represents the result of a Metadata operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070099type MetadataResult struct {
100 gophercloud.Result
101}
102
Jon Perrittbba201b2015-02-08 21:12:38 -0700103// Extract returns a map object and is called after a
104// Metadata operation.
Jon Perritt3711cd02014-12-22 22:20:15 -0700105func (r MetadataResult) Extract() (map[string]string, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600106 var s struct {
107 Meta map[string]string `json:"metadata"`
Jon Perritt3711cd02014-12-22 22:20:15 -0700108 }
Jon Perritt3c166472016-02-25 03:07:41 -0600109 err := r.ExtractInto(&s)
110 return s.Meta, err
Jon Perritt3711cd02014-12-22 22:20:15 -0700111}
Jon Perritta065da12015-02-06 10:20:16 -0700112
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.
116type ResourceTypePage struct {
117 pagination.SinglePageBase
118}
119
120// IsEmpty returns true if a ResourceTypePage contains no resource types.
121func (r ResourceTypePage) IsEmpty() (bool, error) {
122 rts, err := ExtractResourceTypes(r)
Jon Perritt3c166472016-02-25 03:07:41 -0600123 return len(rts) == 0, err
Jon Perritta065da12015-02-06 10:20:16 -0700124}
125
Pratik Mallya3de347f2015-09-22 12:25:59 -0500126// ResourceTypes represents the type that holds the result of ExtractResourceTypes.
Pratik Mallya827c03e2015-09-17 00:10:47 -0500127// We define methods on this type to sort it before output
Pratik Mallya3de347f2015-09-22 12:25:59 -0500128type ResourceTypes []string
Pratik Mallya827c03e2015-09-17 00:10:47 -0500129
Pratik Mallya3de347f2015-09-22 12:25:59 -0500130func (r ResourceTypes) Len() int {
Pratik Mallya827c03e2015-09-17 00:10:47 -0500131 return len(r)
132}
133
Pratik Mallya3de347f2015-09-22 12:25:59 -0500134func (r ResourceTypes) Swap(i, j int) {
Pratik Mallya827c03e2015-09-17 00:10:47 -0500135 r[i], r[j] = r[j], r[i]
136}
137
Pratik Mallya3de347f2015-09-22 12:25:59 -0500138func (r ResourceTypes) Less(i, j int) bool {
Pratik Mallya827c03e2015-09-17 00:10:47 -0500139 return r[i] < r[j]
140}
141
Jon Perritta065da12015-02-06 10:20:16 -0700142// ExtractResourceTypes extracts and returns resource types.
Jon Perritt3c166472016-02-25 03:07:41 -0600143func ExtractResourceTypes(r pagination.Page) (ResourceTypes, error) {
144 var s struct {
145 ResourceTypes ResourceTypes `json:"resource_types"`
Jon Perritta065da12015-02-06 10:20:16 -0700146 }
Jon Perritt3c166472016-02-25 03:07:41 -0600147 err := (r.(ResourceTypePage)).ExtractInto(&s)
148 return s.ResourceTypes, err
Jon Perritta065da12015-02-06 10:20:16 -0700149}
Jon Perritt1d4aca02015-02-06 12:29:16 -0700150
Jon Perrittbba201b2015-02-08 21:12:38 -0700151// TypeSchema represents a stack resource schema.
Jon Perritt1d4aca02015-02-06 12:29:16 -0700152type TypeSchema struct {
Jon Perritt3c166472016-02-25 03:07:41 -0600153 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 Perritt1d4aca02015-02-06 12:29:16 -0700157}
158
Jon Perrittbba201b2015-02-08 21:12:38 -0700159// SchemaResult represents the result of a Schema operation.
Jon Perritt1d4aca02015-02-06 12:29:16 -0700160type SchemaResult struct {
161 gophercloud.Result
162}
163
Jon Perrittbba201b2015-02-08 21:12:38 -0700164// Extract returns a pointer to a TypeSchema object and is called after a
165// Schema operation.
Jon Perritt1d4aca02015-02-06 12:29:16 -0700166func (r SchemaResult) Extract() (*TypeSchema, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600167 var s *TypeSchema
168 err := r.ExtractInto(&s)
169 return s, err
Jon Perritt1d4aca02015-02-06 12:29:16 -0700170}
Jon Perrittb1e303a2015-02-06 22:15:44 -0700171
Jon Perrittbba201b2015-02-08 21:12:38 -0700172// TemplateResult represents the result of a Template operation.
Jon Perrittb1e303a2015-02-06 22:15:44 -0700173type TemplateResult struct {
174 gophercloud.Result
175}
176
Pratik Mallya827c03e2015-09-17 00:10:47 -0500177// Extract returns the template and is called after a
Jon Perrittbba201b2015-02-08 21:12:38 -0700178// Template operation.
Pratik Mallya827c03e2015-09-17 00:10:47 -0500179func (r TemplateResult) Extract() ([]byte, error) {
Jon Perrittb1e303a2015-02-06 22:15:44 -0700180 if r.Err != nil {
181 return nil, r.Err
182 }
Pratik Mallya827c03e2015-09-17 00:10:47 -0500183 template, err := json.MarshalIndent(r.Body, "", " ")
Jon Perritt31b66462016-02-25 22:25:30 -0600184 return template, err
Jon Perrittb1e303a2015-02-06 22:15:44 -0700185}