blob: fcb8d8a2f82ee56f2a0402abae3747ab2a88a6bd [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 {
Jon Perritt3711cd02014-12-22 22:20:15 -070028 // Include resources from nest stacks up to Depth levels of recursion.
29 Depth int `q:"nested_depth"`
30}
31
32// ToStackResourceListQuery formats a ListOpts into a query string.
33func (opts ListOpts) ToStackResourceListQuery() (string, error) {
34 q, err := gophercloud.BuildQueryString(opts)
35 if err != nil {
36 return "", err
37 }
38 return q.String(), nil
39}
40
41// List makes a request against the API to list resources for the given stack.
42func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
43 url := listURL(client, stackName, stackID)
44
45 if opts != nil {
46 query, err := opts.ToStackResourceListQuery()
47 if err != nil {
48 return pagination.Pager{Err: err}
49 }
50 url += query
51 }
52
53 createPageFn := func(r pagination.PageResult) pagination.Page {
Pratik Mallya5448f582015-08-21 12:21:09 -050054 return ResourcePage{pagination.SinglePageBase(r)}
Jon Perritt3711cd02014-12-22 22:20:15 -070055 }
56
57 return pagination.NewPager(client, url, createPageFn)
58}
59
60// Get retreives data for the given stack resource.
61func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) GetResult {
62 var res GetResult
63
64 // Send request to API
Jamie Hannaford1d27afa2015-03-24 16:20:45 +010065 _, res.Err = c.Get(getURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{
66 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070067 })
68 return res
69}
70
71// Metadata retreives the metadata for the given stack resource.
72func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) MetadataResult {
73 var res MetadataResult
74
75 // Send request to API
Jamie Hannaford1d27afa2015-03-24 16:20:45 +010076 _, res.Err = c.Get(metadataURL(c, stackName, stackID, resourceName), &res.Body, &gophercloud.RequestOpts{
77 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070078 })
79 return res
80}
Jon Perritta065da12015-02-06 10:20:16 -070081
82// ListTypes makes a request against the API to list resource types.
83func ListTypes(client *gophercloud.ServiceClient) pagination.Pager {
84 url := listTypesURL(client)
85
86 createPageFn := func(r pagination.PageResult) pagination.Page {
87 return ResourceTypePage{pagination.SinglePageBase(r)}
88 }
89
90 return pagination.NewPager(client, url, createPageFn)
91}
Jon Perritt1d4aca02015-02-06 12:29:16 -070092
93// Schema retreives the schema for the given resource type.
94func Schema(c *gophercloud.ServiceClient, resourceType string) SchemaResult {
95 var res SchemaResult
96
97 // Send request to API
Jamie Hannaford1d27afa2015-03-24 16:20:45 +010098 _, res.Err = c.Get(schemaURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{
99 OkCodes: []int{200},
Jon Perritt1d4aca02015-02-06 12:29:16 -0700100 })
101 return res
102}
Jon Perrittb1e303a2015-02-06 22:15:44 -0700103
104// Template retreives the template representation for the given resource type.
105func Template(c *gophercloud.ServiceClient, resourceType string) TemplateResult {
106 var res TemplateResult
107
108 // Send request to API
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100109 _, res.Err = c.Get(templateURL(c, resourceType), &res.Body, &gophercloud.RequestOpts{
110 OkCodes: []int{200},
Jon Perrittb1e303a2015-02-06 22:15:44 -0700111 })
112 return res
113}