blob: 5679fb209abbc5bfd876478e952cca8c8ae689ed [file] [log] [blame]
Jon Perritt3711cd02014-12-22 22:20:15 -07001package stackresources
2
3import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
6 "github.com/rackspace/gophercloud/pagination"
7)
8
9// Find retreives stack resources for the given stack name.
10func Find(c *gophercloud.ServiceClient, stackName string) FindResult {
11 var res FindResult
12
13 // Send request to API
14 _, res.Err = perigee.Request("GET", findURL(c, stackName), perigee.Options{
15 MoreHeaders: c.AuthenticatedHeaders(),
16 Results: &res.Body,
Jon Perritta065da12015-02-06 10:20:16 -070017 OkCodes: []int{200},
Jon Perritt3711cd02014-12-22 22:20:15 -070018 })
19 return res
20}
21
22// ListOptsBuilder allows extensions to add additional parameters to the
23// List request.
24type ListOptsBuilder interface {
25 ToStackResourceListQuery() (string, error)
26}
27
28// ListOpts allows the filtering and sorting of paginated collections through
29// the API. Marker and Limit are used for pagination.
30type ListOpts struct {
31 // The stack resource ID with which to start the listing.
32 Marker string `q:"marker"`
33
34 // Integer value for the limit of values to return.
35 Limit int `q:"limit"`
36
37 // Include resources from nest stacks up to Depth levels of recursion.
38 Depth int `q:"nested_depth"`
39}
40
41// ToStackResourceListQuery formats a ListOpts into a query string.
42func (opts ListOpts) ToStackResourceListQuery() (string, error) {
43 q, err := gophercloud.BuildQueryString(opts)
44 if err != nil {
45 return "", err
46 }
47 return q.String(), nil
48}
49
50// List makes a request against the API to list resources for the given stack.
51func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
52 url := listURL(client, stackName, stackID)
53
54 if opts != nil {
55 query, err := opts.ToStackResourceListQuery()
56 if err != nil {
57 return pagination.Pager{Err: err}
58 }
59 url += query
60 }
61
62 createPageFn := func(r pagination.PageResult) pagination.Page {
Jon Perritt64f594d2015-02-08 21:24:33 -070063 p := ResourcePage{pagination.MarkerPageBase{PageResult: r}}
64 p.MarkerPageBase.Owner = p
65 return p
Jon Perritt3711cd02014-12-22 22:20:15 -070066 }
67
68 return pagination.NewPager(client, url, createPageFn)
69}
70
71// Get retreives data for the given stack resource.
72func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) GetResult {
73 var res GetResult
74
75 // Send request to API
76 _, res.Err = perigee.Request("GET", getURL(c, stackName, stackID, resourceName), perigee.Options{
77 MoreHeaders: c.AuthenticatedHeaders(),
78 Results: &res.Body,
79 OkCodes: []int{200},
80 })
81 return res
82}
83
84// Metadata retreives the metadata for the given stack resource.
85func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) MetadataResult {
86 var res MetadataResult
87
88 // Send request to API
89 _, res.Err = perigee.Request("GET", metadataURL(c, stackName, stackID, resourceName), perigee.Options{
90 MoreHeaders: c.AuthenticatedHeaders(),
91 Results: &res.Body,
92 OkCodes: []int{200},
93 })
94 return res
95}
Jon Perritta065da12015-02-06 10:20:16 -070096
97// ListTypes makes a request against the API to list resource types.
98func ListTypes(client *gophercloud.ServiceClient) pagination.Pager {
99 url := listTypesURL(client)
100
101 createPageFn := func(r pagination.PageResult) pagination.Page {
102 return ResourceTypePage{pagination.SinglePageBase(r)}
103 }
104
105 return pagination.NewPager(client, url, createPageFn)
106}
Jon Perritt1d4aca02015-02-06 12:29:16 -0700107
108// Schema retreives the schema for the given resource type.
109func Schema(c *gophercloud.ServiceClient, resourceType string) SchemaResult {
110 var res SchemaResult
111
112 // Send request to API
113 _, res.Err = perigee.Request("GET", schemaURL(c, resourceType), perigee.Options{
114 MoreHeaders: c.AuthenticatedHeaders(),
115 Results: &res.Body,
116 OkCodes: []int{200},
117 })
118 return res
119}
Jon Perrittb1e303a2015-02-06 22:15:44 -0700120
121// Template retreives the template representation for the given resource type.
122func Template(c *gophercloud.ServiceClient, resourceType string) TemplateResult {
123 var res TemplateResult
124
125 // Send request to API
126 _, res.Err = perigee.Request("GET", templateURL(c, resourceType), perigee.Options{
127 MoreHeaders: c.AuthenticatedHeaders(),
128 Results: &res.Body,
129 OkCodes: []int{200},
130 })
131 return res
132}