github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/orchestration/v1/stackresources/requests.go (about)

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