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

     1  package capsules
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // CreateOptsBuilder is the interface options structs have to satisfy in order
    11  // to be used in the main Create operation in this package. Since many
    12  // extensions decorate or modify the common logic, it is useful for them to
    13  // satisfy a basic interface in order for them to be used.
    14  type CreateOptsBuilder interface {
    15  	ToCapsuleCreateMap() (map[string]any, error)
    16  }
    17  
    18  // ListOptsBuilder allows extensions to add additional parameters to the
    19  // List request.
    20  type ListOptsBuilder interface {
    21  	ToCapsuleListQuery() (string, error)
    22  }
    23  
    24  // Get requests details on a single capsule, by ID.
    25  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
    26  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, &gophercloud.RequestOpts{
    27  		OkCodes: []int{200, 203},
    28  	})
    29  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    30  	return
    31  }
    32  
    33  // CreateOpts is the common options struct used in this package's Create
    34  // operation.
    35  type CreateOpts struct {
    36  	// A structure that contains either the template file or url. Call the
    37  	// associated methods to extract the information relevant to send in a create request.
    38  	TemplateOpts *Template `json:"-" required:"true"`
    39  }
    40  
    41  // ToCapsuleCreateMap assembles a request body based on the contents of
    42  // a CreateOpts.
    43  func (opts CreateOpts) ToCapsuleCreateMap() (map[string]any, error) {
    44  	b, err := gophercloud.BuildRequestBody(opts, "")
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	if err := opts.TemplateOpts.Parse(); err != nil {
    50  		return nil, err
    51  	}
    52  	b["template"] = string(opts.TemplateOpts.Bin)
    53  
    54  	return b, nil
    55  }
    56  
    57  // Create implements create capsule request.
    58  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    59  	b, err := opts.ToCapsuleCreateMap()
    60  	if err != nil {
    61  		r.Err = err
    62  		return
    63  	}
    64  	resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{OkCodes: []int{202}})
    65  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    66  	return
    67  }
    68  
    69  // ListOpts allows the filtering and sorting of paginated collections through
    70  // the API. Filtering is achieved by passing in struct field values that map to
    71  // the capsule attributes you want to see returned. Marker and Limit are used
    72  // for pagination.
    73  type ListOpts struct {
    74  	Marker      string `q:"marker"`
    75  	Limit       int    `q:"limit"`
    76  	SortKey     string `q:"sort_key"`
    77  	SortDir     string `q:"sort_dir"`
    78  	AllProjects bool   `q:"all_projects"`
    79  }
    80  
    81  // ToCapsuleListQuery formats a ListOpts into a query string.
    82  func (opts ListOpts) ToCapsuleListQuery() (string, error) {
    83  	q, err := gophercloud.BuildQueryString(opts)
    84  	return q.String(), err
    85  }
    86  
    87  // List makes a request against the API to list capsules accessible to you.
    88  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    89  	url := listURL(client)
    90  	if opts != nil {
    91  		query, err := opts.ToCapsuleListQuery()
    92  		if err != nil {
    93  			return pagination.Pager{Err: err}
    94  		}
    95  		url += query
    96  	}
    97  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    98  		return CapsulePage{pagination.LinkedPageBase{PageResult: r}}
    99  	})
   100  }
   101  
   102  // Delete implements Capsule delete request.
   103  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
   104  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
   105  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   106  	return
   107  }