blob: 2a66edc8c258173045fef600412f8ad664351fa9 [file] [log] [blame]
Jon Perritt3711cd02014-12-22 22:20:15 -07001package stackresources
2
3import (
Jon Perritt3711cd02014-12-22 22:20:15 -07004 "github.com/rackspace/gophercloud"
5 "github.com/rackspace/gophercloud/pagination"
6)
7
Jon Perritt8bdc1a62015-02-12 09:14:48 -07008// Find retrieves stack resources for the given stack name.
Jon Perritt3711cd02014-12-22 22:20:15 -07009func Find(c *gophercloud.ServiceClient, stackName string) FindResult {
10 var res FindResult
11
12 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -050013 _, res.Err = c.Request("GET", findURL(c, stackName), gophercloud.RequestOpts{
14 JSONResponse: &res.Body,
15 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070016 })
17 return res
18}
19
20// ListOptsBuilder allows extensions to add additional parameters to the
21// List request.
22type ListOptsBuilder interface {
23 ToStackResourceListQuery() (string, error)
24}
25
26// ListOpts allows the filtering and sorting of paginated collections through
27// the API. Marker and Limit are used for pagination.
28type ListOpts struct {
29 // The stack resource ID with which to start the listing.
30 Marker string `q:"marker"`
31
32 // Integer value for the limit of values to return.
33 Limit int `q:"limit"`
34
35 // Include resources from nest stacks up to Depth levels of recursion.
36 Depth int `q:"nested_depth"`
37}
38
39// ToStackResourceListQuery formats a ListOpts into a query string.
40func (opts ListOpts) ToStackResourceListQuery() (string, error) {
41 q, err := gophercloud.BuildQueryString(opts)
42 if err != nil {
43 return "", err
44 }
45 return q.String(), nil
46}
47
48// List makes a request against the API to list resources for the given stack.
49func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
50 url := listURL(client, stackName, stackID)
51
52 if opts != nil {
53 query, err := opts.ToStackResourceListQuery()
54 if err != nil {
55 return pagination.Pager{Err: err}
56 }
57 url += query
58 }
59
60 createPageFn := func(r pagination.PageResult) pagination.Page {
Jon Perritt64f594d2015-02-08 21:24:33 -070061 p := ResourcePage{pagination.MarkerPageBase{PageResult: r}}
62 p.MarkerPageBase.Owner = p
63 return p
Jon Perritt3711cd02014-12-22 22:20:15 -070064 }
65
66 return pagination.NewPager(client, url, createPageFn)
67}
68
69// Get retreives data for the given stack resource.
70func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) GetResult {
71 var res GetResult
72
73 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -050074 _, res.Err = c.Request("GET", getURL(c, stackName, stackID, resourceName), gophercloud.RequestOpts{
75 JSONResponse: &res.Body,
76 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070077 })
78 return res
79}
80
81// Metadata retreives the metadata for the given stack resource.
82func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) MetadataResult {
83 var res MetadataResult
84
85 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -050086 _, res.Err = c.Request("GET", metadataURL(c, stackName, stackID, resourceName), gophercloud.RequestOpts{
87 JSONResponse: &res.Body,
88 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070089 })
90 return res
91}
Jon Perritta065da12015-02-06 10:20:16 -070092
93// ListTypes makes a request against the API to list resource types.
94func ListTypes(client *gophercloud.ServiceClient) pagination.Pager {
95 url := listTypesURL(client)
96
97 createPageFn := func(r pagination.PageResult) pagination.Page {
98 return ResourceTypePage{pagination.SinglePageBase(r)}
99 }
100
101 return pagination.NewPager(client, url, createPageFn)
102}
Jon Perritt1d4aca02015-02-06 12:29:16 -0700103
104// Schema retreives the schema for the given resource type.
105func Schema(c *gophercloud.ServiceClient, resourceType string) SchemaResult {
106 var res SchemaResult
107
108 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -0500109 _, res.Err = c.Request("GET", schemaURL(c, resourceType), gophercloud.RequestOpts{
110 JSONResponse: &res.Body,
111 OkCodes: []int{200},
Jon Perritt1d4aca02015-02-06 12:29:16 -0700112 })
113 return res
114}
Jon Perrittb1e303a2015-02-06 22:15:44 -0700115
116// Template retreives the template representation for the given resource type.
117func Template(c *gophercloud.ServiceClient, resourceType string) TemplateResult {
118 var res TemplateResult
119
120 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -0500121 _, res.Err = c.Request("GET", templateURL(c, resourceType), gophercloud.RequestOpts{
122 JSONResponse: &res.Body,
123 OkCodes: []int{200},
Jon Perrittb1e303a2015-02-06 22:15:44 -0700124 })
125 return res
126}