blob: ee9c3c250cc1372110fc4abc192d6424ee0e4376 [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
Jamie Hannaford1d27afa2015-03-24 16:20:45 +010073 _, res.Err = c.Get(getURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{
74 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070075 })
76 return res
77}
78
79// Metadata retreives the metadata for the given stack resource.
80func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) MetadataResult {
81 var res MetadataResult
82
83 // Send request to API
Jamie Hannaford1d27afa2015-03-24 16:20:45 +010084 _, res.Err = c.Get(metadataURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{
85 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070086 })
87 return res
88}
Jon Perritta065da12015-02-06 10:20:16 -070089
90// ListTypes makes a request against the API to list resource types.
91func ListTypes(client *gophercloud.ServiceClient) pagination.Pager {
92 url := listTypesURL(client)
93
94 createPageFn := func(r pagination.PageResult) pagination.Page {
95 return ResourceTypePage{pagination.SinglePageBase(r)}
96 }
97
98 return pagination.NewPager(client, url, createPageFn)
99}
Jon Perritt1d4aca02015-02-06 12:29:16 -0700100
101// Schema retreives the schema for the given resource type.
102func Schema(c *gophercloud.ServiceClient, resourceType string) SchemaResult {
103 var res SchemaResult
104
105 // Send request to API
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100106 _, res.Err = c.Get(schemaURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{
107 OkCodes: []int{200},
Jon Perritt1d4aca02015-02-06 12:29:16 -0700108 })
109 return res
110}
Jon Perrittb1e303a2015-02-06 22:15:44 -0700111
112// Template retreives the template representation for the given resource type.
113func Template(c *gophercloud.ServiceClient, resourceType string) TemplateResult {
114 var res TemplateResult
115
116 // Send request to API
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100117 _, res.Err = c.Get(templateURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{
118 OkCodes: []int{200},
Jon Perrittb1e303a2015-02-06 22:15:44 -0700119 })
120 return res
121}