blob: b63ef68d806f0c3cc019ced7da504cc435b5da0c [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,
Jon Perritt3711cd02014-12-22 22:20:15 -070015 })
16 return res
17}
18
19// ListOptsBuilder allows extensions to add additional parameters to the
20// List request.
21type ListOptsBuilder interface {
22 ToStackResourceListQuery() (string, error)
23}
24
25// ListOpts allows the filtering and sorting of paginated collections through
26// the API. Marker and Limit are used for pagination.
27type ListOpts struct {
28 // The stack resource ID with which to start the listing.
29 Marker string `q:"marker"`
30
31 // Integer value for the limit of values to return.
32 Limit int `q:"limit"`
33
34 // Include resources from nest stacks up to Depth levels of recursion.
35 Depth int `q:"nested_depth"`
36}
37
38// ToStackResourceListQuery formats a ListOpts into a query string.
39func (opts ListOpts) ToStackResourceListQuery() (string, error) {
40 q, err := gophercloud.BuildQueryString(opts)
41 if err != nil {
42 return "", err
43 }
44 return q.String(), nil
45}
46
47// List makes a request against the API to list resources for the given stack.
48func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
49 url := listURL(client, stackName, stackID)
50
51 if opts != nil {
52 query, err := opts.ToStackResourceListQuery()
53 if err != nil {
54 return pagination.Pager{Err: err}
55 }
56 url += query
57 }
58
59 createPageFn := func(r pagination.PageResult) pagination.Page {
Jon Perritt64f594d2015-02-08 21:24:33 -070060 p := ResourcePage{pagination.MarkerPageBase{PageResult: r}}
61 p.MarkerPageBase.Owner = p
62 return p
Jon Perritt3711cd02014-12-22 22:20:15 -070063 }
64
65 return pagination.NewPager(client, url, createPageFn)
66}
67
68// Get retreives data for the given stack resource.
69func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) GetResult {
70 var res GetResult
71
72 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -050073 _, res.Err = c.Request("GET", getURL(c, stackName, stackID, resourceName), gophercloud.RequestOpts{
74 JSONResponse: &res.Body,
75 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070076 })
77 return res
78}
79
80// Metadata retreives the metadata for the given stack resource.
81func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) MetadataResult {
82 var res MetadataResult
83
84 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -050085 _, res.Err = c.Request("GET", metadataURL(c, stackName, stackID, resourceName), gophercloud.RequestOpts{
86 JSONResponse: &res.Body,
87 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070088 })
89 return res
90}
Jon Perritta065da12015-02-06 10:20:16 -070091
92// ListTypes makes a request against the API to list resource types.
93func ListTypes(client *gophercloud.ServiceClient) pagination.Pager {
94 url := listTypesURL(client)
95
96 createPageFn := func(r pagination.PageResult) pagination.Page {
97 return ResourceTypePage{pagination.SinglePageBase(r)}
98 }
99
100 return pagination.NewPager(client, url, createPageFn)
101}
Jon Perritt1d4aca02015-02-06 12:29:16 -0700102
103// Schema retreives the schema for the given resource type.
104func Schema(c *gophercloud.ServiceClient, resourceType string) SchemaResult {
105 var res SchemaResult
106
107 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -0500108 _, res.Err = c.Request("GET", schemaURL(c, resourceType), gophercloud.RequestOpts{
109 JSONResponse: &res.Body,
110 OkCodes: []int{200},
Jon Perritt1d4aca02015-02-06 12:29:16 -0700111 })
112 return res
113}
Jon Perrittb1e303a2015-02-06 22:15:44 -0700114
115// Template retreives the template representation for the given resource type.
116func Template(c *gophercloud.ServiceClient, resourceType string) TemplateResult {
117 var res TemplateResult
118
119 // Send request to API
Ash Wilsondecfed72015-02-13 09:14:55 -0500120 _, res.Err = c.Request("GET", templateURL(c, resourceType), gophercloud.RequestOpts{
121 JSONResponse: &res.Body,
122 OkCodes: []int{200},
Jon Perrittb1e303a2015-02-06 22:15:44 -0700123 })
124 return res
125}