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

     1  package resourceproviders
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // ListOptsBuilder allows extensions to add additional parameters to the
    11  // List request.
    12  type ListOptsBuilder interface {
    13  	ToResourceProviderListQuery() (string, error)
    14  }
    15  
    16  // ListOpts allows the filtering resource providers. Filtering is achieved by
    17  // passing in struct field values that map to the resource provider attributes
    18  // you want to see returned.
    19  type ListOpts struct {
    20  	// Name is the name of the resource provider to filter the list
    21  	Name string `q:"name"`
    22  
    23  	// UUID is the uuid of the resource provider to filter the list
    24  	UUID string `q:"uuid"`
    25  
    26  	// MemberOf is a string representing aggregate uuids to filter or exclude from the list
    27  	MemberOf string `q:"member_of"`
    28  
    29  	// Resources is a comma-separated list of string indicating an amount of resource
    30  	// of a specified class that a provider must have the capacity and availability to serve
    31  	Resources string `q:"resources"`
    32  
    33  	// InTree is a string that represents a resource provider UUID.  The returned resource
    34  	// providers will be in the same provider tree as the specified provider.
    35  	InTree string `q:"in_tree"`
    36  
    37  	// Required is comma-delimited list of string trait names.
    38  	Required string `q:"required"`
    39  }
    40  
    41  // ToResourceProviderListQuery formats a ListOpts into a query string.
    42  func (opts ListOpts) ToResourceProviderListQuery() (string, error) {
    43  	q, err := gophercloud.BuildQueryString(opts)
    44  	return q.String(), err
    45  }
    46  
    47  // List makes a request against the API to list resource providers.
    48  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    49  	url := resourceProvidersListURL(client)
    50  
    51  	if opts != nil {
    52  		query, err := opts.ToResourceProviderListQuery()
    53  		if err != nil {
    54  			return pagination.Pager{Err: err}
    55  		}
    56  		url += query
    57  	}
    58  
    59  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    60  		return ResourceProvidersPage{pagination.SinglePageBase(r)}
    61  	})
    62  }
    63  
    64  // CreateOptsBuilder allows extensions to add additional parameters to the
    65  // Create request.
    66  type CreateOptsBuilder interface {
    67  	ToResourceProviderCreateMap() (map[string]any, error)
    68  }
    69  
    70  // CreateOpts represents options used to create a resource provider.
    71  type CreateOpts struct {
    72  	Name string `json:"name"`
    73  	UUID string `json:"uuid,omitempty"`
    74  	// The UUID of the immediate parent of the resource provider.
    75  	// Available in version >= 1.14
    76  	ParentProviderUUID string `json:"parent_provider_uuid,omitempty"`
    77  }
    78  
    79  // ToResourceProviderCreateMap constructs a request body from CreateOpts.
    80  func (opts CreateOpts) ToResourceProviderCreateMap() (map[string]any, error) {
    81  	b, err := gophercloud.BuildRequestBody(opts, "")
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	return b, nil
    87  }
    88  
    89  // Create makes a request against the API to create a resource provider
    90  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    91  	b, err := opts.ToResourceProviderCreateMap()
    92  	if err != nil {
    93  		r.Err = err
    94  		return
    95  	}
    96  
    97  	resp, err := client.Post(ctx, resourceProvidersListURL(client), b, &r.Body, &gophercloud.RequestOpts{
    98  		OkCodes: []int{200},
    99  	})
   100  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   101  	return
   102  }
   103  
   104  // Delete accepts a unique ID and deletes the resource provider associated with it.
   105  func Delete(ctx context.Context, c *gophercloud.ServiceClient, resourceProviderID string) (r DeleteResult) {
   106  	resp, err := c.Delete(ctx, deleteURL(c, resourceProviderID), nil)
   107  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   108  	return
   109  }
   110  
   111  // Get retrieves a specific resource provider based on its unique ID.
   112  func Get(ctx context.Context, c *gophercloud.ServiceClient, resourceProviderID string) (r GetResult) {
   113  	resp, err := c.Get(ctx, getURL(c, resourceProviderID), &r.Body, nil)
   114  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   115  	return
   116  }
   117  
   118  // UpdateOptsBuilder allows extensions to add additional parameters to the
   119  // Update request.
   120  type UpdateOptsBuilder interface {
   121  	ToResourceProviderUpdateMap() (map[string]any, error)
   122  }
   123  
   124  // UpdateOpts represents options used to update a resource provider.
   125  type UpdateOpts struct {
   126  	Name *string `json:"name,omitempty"`
   127  	// Available in version >= 1.37. It can be set to any existing provider UUID
   128  	// except to providers that would cause a loop. Also it can be set to null
   129  	// to transform the provider to a new root provider. This operation needs to
   130  	// be used carefully. Moving providers can mean that the original rules used
   131  	// to create the existing resource allocations may be invalidated by that move.
   132  	ParentProviderUUID *string `json:"parent_provider_uuid,omitempty"`
   133  }
   134  
   135  // ToResourceProviderUpdateMap constructs a request body from UpdateOpts.
   136  func (opts UpdateOpts) ToResourceProviderUpdateMap() (map[string]any, error) {
   137  	return gophercloud.BuildRequestBody(opts, "")
   138  }
   139  
   140  // Update makes a request against the API to create a resource provider
   141  func Update(ctx context.Context, client *gophercloud.ServiceClient, resourceProviderID string, opts UpdateOptsBuilder) (r UpdateResult) {
   142  	b, err := opts.ToResourceProviderUpdateMap()
   143  	if err != nil {
   144  		r.Err = err
   145  		return
   146  	}
   147  
   148  	resp, err := client.Put(ctx, updateURL(client, resourceProviderID), b, &r.Body, &gophercloud.RequestOpts{
   149  		OkCodes: []int{200},
   150  	})
   151  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   152  	return
   153  }
   154  
   155  func GetUsages(ctx context.Context, client *gophercloud.ServiceClient, resourceProviderID string) (r GetUsagesResult) {
   156  	resp, err := client.Get(ctx, getResourceProviderUsagesURL(client, resourceProviderID), &r.Body, nil)
   157  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   158  	return
   159  }
   160  
   161  func GetInventories(ctx context.Context, client *gophercloud.ServiceClient, resourceProviderID string) (r GetInventoriesResult) {
   162  	resp, err := client.Get(ctx, getResourceProviderInventoriesURL(client, resourceProviderID), &r.Body, nil)
   163  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   164  	return
   165  }
   166  
   167  func GetAllocations(ctx context.Context, client *gophercloud.ServiceClient, resourceProviderID string) (r GetAllocationsResult) {
   168  	resp, err := client.Get(ctx, getResourceProviderAllocationsURL(client, resourceProviderID), &r.Body, nil)
   169  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   170  	return
   171  }
   172  
   173  func GetTraits(ctx context.Context, client *gophercloud.ServiceClient, resourceProviderID string) (r GetTraitsResult) {
   174  	resp, err := client.Get(ctx, getResourceProviderTraitsURL(client, resourceProviderID), &r.Body, nil)
   175  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   176  	return
   177  }