blob: bd3e29f7c25c0c843f957d56de71efeda653ddd5 [file] [log] [blame]
Jon Perritt3711cd02014-12-22 22:20:15 -07001package stackresources
2
3import (
Pratik Mallya827c03e2015-09-17 00:10:47 -05004 "encoding/json"
Jon Perritt3711cd02014-12-22 22:20:15 -07005
Jon Perritt27249f42016-02-18 10:35:59 -06006 "github.com/gophercloud/gophercloud"
7 "github.com/gophercloud/gophercloud/pagination"
Jon Perritt3711cd02014-12-22 22:20:15 -07008)
9
Jon Perrittbba201b2015-02-08 21:12:38 -070010// Resource represents a stack resource.
Jon Perritt3711cd02014-12-22 22:20:15 -070011type Resource struct {
Jon Perritt3c166472016-02-25 03:07:41 -060012 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 Perritt3711cd02014-12-22 22:20:15 -070024}
25
Jon Perrittbba201b2015-02-08 21:12:38 -070026// FindResult represents the result of a Find operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070027type FindResult struct {
28 gophercloud.Result
29}
30
Jon Perrittbba201b2015-02-08 21:12:38 -070031// Extract returns a slice of Resource objects and is called after a
32// Find operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070033func (r FindResult) Extract() ([]Resource, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060034 var s struct {
35 Resources []Resource `json:"resources"`
Jon Perritt3711cd02014-12-22 22:20:15 -070036 }
Jon Perritt3c166472016-02-25 03:07:41 -060037 err := r.ExtractInto(&s)
38 return s.Resources, err
Jon Perritt3711cd02014-12-22 22:20:15 -070039}
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.
44type ResourcePage struct {
Pratik Mallya5448f582015-08-21 12:21:09 -050045 pagination.SinglePageBase
Jon Perritt3711cd02014-12-22 22:20:15 -070046}
47
48// IsEmpty returns true if a page contains no Server results.
Jon Perrittc8717332015-02-08 20:14:29 -070049func (r ResourcePage) IsEmpty() (bool, error) {
50 resources, err := ExtractResources(r)
Jon Perritt3c166472016-02-25 03:07:41 -060051 return len(resources) == 0, err
Jon Perritt3711cd02014-12-22 22:20:15 -070052}
53
Jon Perritt3711cd02014-12-22 22:20:15 -070054// 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 -060055func ExtractResources(r pagination.Page) ([]Resource, error) {
56 var s struct {
57 Resources []Resource `json:"resources"`
Jon Perritt3711cd02014-12-22 22:20:15 -070058 }
Jon Perritt3c166472016-02-25 03:07:41 -060059 err := (r.(ResourcePage)).ExtractInto(&s)
60 return s.Resources, err
Jon Perritt3711cd02014-12-22 22:20:15 -070061}
62
Jon Perrittbba201b2015-02-08 21:12:38 -070063// GetResult represents the result of a Get operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070064type GetResult struct {
65 gophercloud.Result
66}
67
Jon Perrittbba201b2015-02-08 21:12:38 -070068// Extract returns a pointer to a Resource object and is called after a
69// Get operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070070func (r GetResult) Extract() (*Resource, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060071 var s struct {
72 Resource *Resource `json:"resource"`
Jon Perritt3711cd02014-12-22 22:20:15 -070073 }
Jon Perritt3c166472016-02-25 03:07:41 -060074 err := r.ExtractInto(&s)
75 return s.Resource, err
Jon Perritt3711cd02014-12-22 22:20:15 -070076}
77
Jon Perrittbba201b2015-02-08 21:12:38 -070078// MetadataResult represents the result of a Metadata operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070079type MetadataResult struct {
80 gophercloud.Result
81}
82
Jon Perrittbba201b2015-02-08 21:12:38 -070083// Extract returns a map object and is called after a
84// Metadata operation.
Jon Perritt3711cd02014-12-22 22:20:15 -070085func (r MetadataResult) Extract() (map[string]string, error) {
Jon Perritt3c166472016-02-25 03:07:41 -060086 var s struct {
87 Meta map[string]string `json:"metadata"`
Jon Perritt3711cd02014-12-22 22:20:15 -070088 }
Jon Perritt3c166472016-02-25 03:07:41 -060089 err := r.ExtractInto(&s)
90 return s.Meta, err
Jon Perritt3711cd02014-12-22 22:20:15 -070091}
Jon Perritta065da12015-02-06 10:20:16 -070092
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.
96type ResourceTypePage struct {
97 pagination.SinglePageBase
98}
99
100// IsEmpty returns true if a ResourceTypePage contains no resource types.
101func (r ResourceTypePage) IsEmpty() (bool, error) {
102 rts, err := ExtractResourceTypes(r)
Jon Perritt3c166472016-02-25 03:07:41 -0600103 return len(rts) == 0, err
Jon Perritta065da12015-02-06 10:20:16 -0700104}
105
Pratik Mallya3de347f2015-09-22 12:25:59 -0500106// ResourceTypes represents the type that holds the result of ExtractResourceTypes.
Pratik Mallya827c03e2015-09-17 00:10:47 -0500107// We define methods on this type to sort it before output
Pratik Mallya3de347f2015-09-22 12:25:59 -0500108type ResourceTypes []string
Pratik Mallya827c03e2015-09-17 00:10:47 -0500109
Pratik Mallya3de347f2015-09-22 12:25:59 -0500110func (r ResourceTypes) Len() int {
Pratik Mallya827c03e2015-09-17 00:10:47 -0500111 return len(r)
112}
113
Pratik Mallya3de347f2015-09-22 12:25:59 -0500114func (r ResourceTypes) Swap(i, j int) {
Pratik Mallya827c03e2015-09-17 00:10:47 -0500115 r[i], r[j] = r[j], r[i]
116}
117
Pratik Mallya3de347f2015-09-22 12:25:59 -0500118func (r ResourceTypes) Less(i, j int) bool {
Pratik Mallya827c03e2015-09-17 00:10:47 -0500119 return r[i] < r[j]
120}
121
Jon Perritta065da12015-02-06 10:20:16 -0700122// ExtractResourceTypes extracts and returns resource types.
Jon Perritt3c166472016-02-25 03:07:41 -0600123func ExtractResourceTypes(r pagination.Page) (ResourceTypes, error) {
124 var s struct {
125 ResourceTypes ResourceTypes `json:"resource_types"`
Jon Perritta065da12015-02-06 10:20:16 -0700126 }
Jon Perritt3c166472016-02-25 03:07:41 -0600127 err := (r.(ResourceTypePage)).ExtractInto(&s)
128 return s.ResourceTypes, err
Jon Perritta065da12015-02-06 10:20:16 -0700129}
Jon Perritt1d4aca02015-02-06 12:29:16 -0700130
Jon Perrittbba201b2015-02-08 21:12:38 -0700131// TypeSchema represents a stack resource schema.
Jon Perritt1d4aca02015-02-06 12:29:16 -0700132type TypeSchema struct {
Jon Perritt3c166472016-02-25 03:07:41 -0600133 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 Perritt1d4aca02015-02-06 12:29:16 -0700137}
138
Jon Perrittbba201b2015-02-08 21:12:38 -0700139// SchemaResult represents the result of a Schema operation.
Jon Perritt1d4aca02015-02-06 12:29:16 -0700140type SchemaResult struct {
141 gophercloud.Result
142}
143
Jon Perrittbba201b2015-02-08 21:12:38 -0700144// Extract returns a pointer to a TypeSchema object and is called after a
145// Schema operation.
Jon Perritt1d4aca02015-02-06 12:29:16 -0700146func (r SchemaResult) Extract() (*TypeSchema, error) {
Jon Perritt3c166472016-02-25 03:07:41 -0600147 var s *TypeSchema
148 err := r.ExtractInto(&s)
149 return s, err
Jon Perritt1d4aca02015-02-06 12:29:16 -0700150}
Jon Perrittb1e303a2015-02-06 22:15:44 -0700151
Jon Perrittbba201b2015-02-08 21:12:38 -0700152// TemplateResult represents the result of a Template operation.
Jon Perrittb1e303a2015-02-06 22:15:44 -0700153type TemplateResult struct {
154 gophercloud.Result
155}
156
Pratik Mallya827c03e2015-09-17 00:10:47 -0500157// Extract returns the template and is called after a
Jon Perrittbba201b2015-02-08 21:12:38 -0700158// Template operation.
Pratik Mallya827c03e2015-09-17 00:10:47 -0500159func (r TemplateResult) Extract() ([]byte, error) {
Jon Perrittb1e303a2015-02-06 22:15:44 -0700160 if r.Err != nil {
161 return nil, r.Err
162 }
Pratik Mallya827c03e2015-09-17 00:10:47 -0500163 template, err := json.MarshalIndent(r.Body, "", " ")
Jon Perritt31b66462016-02-25 22:25:30 -0600164 return template, err
Jon Perrittb1e303a2015-02-06 22:15:44 -0700165}