github.com/gophercloud/gophercloud@v1.11.0/openstack/orchestration/v1/stackresources/requests.go (about)

     1  package stackresources
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // Find retrieves stack resources for the given stack name.
     9  func Find(c *gophercloud.ServiceClient, stackName string) (r FindResult) {
    10  	resp, err := c.Get(findURL(c, stackName), &r.Body, nil)
    11  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    12  	return
    13  }
    14  
    15  // ListOptsBuilder allows extensions to add additional parameters to the
    16  // List request.
    17  type ListOptsBuilder interface {
    18  	ToStackResourceListQuery() (string, error)
    19  }
    20  
    21  // ListOpts allows the filtering and sorting of paginated collections through
    22  // the API. Marker and Limit are used for pagination.
    23  type ListOpts struct {
    24  	// Include resources from nest stacks up to Depth levels of recursion.
    25  	Depth int `q:"nested_depth"`
    26  }
    27  
    28  // ToStackResourceListQuery formats a ListOpts into a query string.
    29  func (opts ListOpts) ToStackResourceListQuery() (string, error) {
    30  	q, err := gophercloud.BuildQueryString(opts)
    31  	return q.String(), err
    32  }
    33  
    34  // List makes a request against the API to list resources for the given stack.
    35  func List(client *gophercloud.ServiceClient, stackName, stackID string, opts ListOptsBuilder) pagination.Pager {
    36  	url := listURL(client, stackName, stackID)
    37  	if opts != nil {
    38  		query, err := opts.ToStackResourceListQuery()
    39  		if err != nil {
    40  			return pagination.Pager{Err: err}
    41  		}
    42  		url += query
    43  	}
    44  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    45  		return ResourcePage{pagination.SinglePageBase(r)}
    46  	})
    47  }
    48  
    49  // Get retreives data for the given stack resource.
    50  func Get(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) (r GetResult) {
    51  	resp, err := c.Get(getURL(c, stackName, stackID, resourceName), &r.Body, nil)
    52  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    53  	return
    54  }
    55  
    56  // Metadata retreives the metadata for the given stack resource.
    57  func Metadata(c *gophercloud.ServiceClient, stackName, stackID, resourceName string) (r MetadataResult) {
    58  	resp, err := c.Get(metadataURL(c, stackName, stackID, resourceName), &r.Body, nil)
    59  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    60  	return
    61  }
    62  
    63  // ListTypes makes a request against the API to list resource types.
    64  func ListTypes(client *gophercloud.ServiceClient) pagination.Pager {
    65  	return pagination.NewPager(client, listTypesURL(client), func(r pagination.PageResult) pagination.Page {
    66  		return ResourceTypePage{pagination.SinglePageBase(r)}
    67  	})
    68  }
    69  
    70  // Schema retreives the schema for the given resource type.
    71  func Schema(c *gophercloud.ServiceClient, resourceType string) (r SchemaResult) {
    72  	resp, err := c.Get(schemaURL(c, resourceType), &r.Body, nil)
    73  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    74  	return
    75  }
    76  
    77  // Template retreives the template representation for the given resource type.
    78  func Template(c *gophercloud.ServiceClient, resourceType string) (r TemplateResult) {
    79  	resp, err := c.Get(templateURL(c, resourceType), &r.Body, nil)
    80  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    81  	return
    82  }
    83  
    84  // MarkUnhealthyOpts contains the common options struct used in this package's
    85  // MarkUnhealthy operations.
    86  type MarkUnhealthyOpts struct {
    87  	// A boolean indicating whether the target resource should be marked as unhealthy.
    88  	MarkUnhealthy bool `json:"mark_unhealthy"`
    89  	// The reason for the current stack resource state.
    90  	ResourceStatusReason string `json:"resource_status_reason,omitempty"`
    91  }
    92  
    93  // MarkUnhealthyOptsBuilder is the interface options structs have to satisfy in order
    94  // to be used in the MarkUnhealthy operation in this package
    95  type MarkUnhealthyOptsBuilder interface {
    96  	ToMarkUnhealthyMap() (map[string]interface{}, error)
    97  }
    98  
    99  // ToMarkUnhealthyMap validates that a template was supplied and calls
   100  // the ToMarkUnhealthyMap private function.
   101  func (opts MarkUnhealthyOpts) ToMarkUnhealthyMap() (map[string]interface{}, error) {
   102  	b, err := gophercloud.BuildRequestBody(opts, "")
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  	return b, nil
   107  }
   108  
   109  // MarkUnhealthy marks the specified resource in the stack as unhealthy.
   110  func MarkUnhealthy(c *gophercloud.ServiceClient, stackName, stackID, resourceName string, opts MarkUnhealthyOptsBuilder) (r MarkUnhealthyResult) {
   111  	b, err := opts.ToMarkUnhealthyMap()
   112  	if err != nil {
   113  		r.Err = err
   114  		return
   115  	}
   116  	resp, err := c.Patch(markUnhealthyURL(c, stackName, stackID, resourceName), b, nil, nil)
   117  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   118  	return
   119  }