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

     1  package orders
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     9  )
    10  
    11  // OrderType represents the valid types of orders.
    12  type OrderType string
    13  
    14  const (
    15  	KeyOrder        OrderType = "key"
    16  	AsymmetricOrder OrderType = "asymmetric"
    17  )
    18  
    19  // ListOptsBuilder allows extensions to add additional parameters to
    20  // the List request
    21  type ListOptsBuilder interface {
    22  	ToOrderListQuery() (string, error)
    23  }
    24  
    25  // ListOpts provides options to filter the List results.
    26  type ListOpts struct {
    27  	// Limit is the amount of containers to retrieve.
    28  	Limit int `q:"limit"`
    29  
    30  	// Offset is the index within the list to retrieve.
    31  	Offset int `q:"offset"`
    32  }
    33  
    34  // ToOrderListQuery formats a ListOpts into a query string.
    35  func (opts ListOpts) ToOrderListQuery() (string, error) {
    36  	q, err := gophercloud.BuildQueryString(opts)
    37  	return q.String(), err
    38  }
    39  
    40  // List retrieves a list of orders.
    41  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    42  	url := listURL(client)
    43  	if opts != nil {
    44  		query, err := opts.ToOrderListQuery()
    45  		if err != nil {
    46  			return pagination.Pager{Err: err}
    47  		}
    48  		url += query
    49  	}
    50  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    51  		return OrderPage{pagination.LinkedPageBase{PageResult: r}}
    52  	})
    53  }
    54  
    55  // Get retrieves details of a orders.
    56  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
    57  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil)
    58  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    59  	return
    60  }
    61  
    62  // CreateOptsBuilder allows extensions to add additional parameters to
    63  // the Create request.
    64  type CreateOptsBuilder interface {
    65  	ToOrderCreateMap() (map[string]any, error)
    66  }
    67  
    68  // MetaOpts represents options used for creating an order.
    69  type MetaOpts struct {
    70  	// Algorithm is the algorithm of the secret.
    71  	Algorithm string `json:"algorithm"`
    72  
    73  	// BitLength is the bit length of the secret.
    74  	BitLength int `json:"bit_length"`
    75  
    76  	// Expiration is the expiration date of the order.
    77  	Expiration *time.Time `json:"-"`
    78  
    79  	// Mode is the mode of the secret.
    80  	Mode string `json:"mode"`
    81  
    82  	// Name is the name of the secret.
    83  	Name string `json:"name,omitempty"`
    84  
    85  	// PayloadContentType is the content type of the secret payload.
    86  	PayloadContentType string `json:"payload_content_type,omitempty"`
    87  }
    88  
    89  // CreateOpts provides options used to create a orders.
    90  type CreateOpts struct {
    91  	// Type is the type of order to create.
    92  	Type OrderType `json:"type"`
    93  
    94  	// Meta contains secrets data to create a secret.
    95  	Meta MetaOpts `json:"meta"`
    96  }
    97  
    98  // ToOrderCreateMap formats a CreateOpts into a create request.
    99  func (opts CreateOpts) ToOrderCreateMap() (map[string]any, error) {
   100  	b, err := gophercloud.BuildRequestBody(opts, "")
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	if opts.Meta.Expiration != nil {
   106  		meta := b["meta"].(map[string]any)
   107  		meta["expiration"] = opts.Meta.Expiration.Format(gophercloud.RFC3339NoZ)
   108  		b["meta"] = meta
   109  	}
   110  
   111  	return b, nil
   112  }
   113  
   114  // Create creates a new orders.
   115  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   116  	b, err := opts.ToOrderCreateMap()
   117  	if err != nil {
   118  		r.Err = err
   119  		return
   120  	}
   121  	resp, err := client.Post(ctx, createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
   122  		OkCodes: []int{202},
   123  	})
   124  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   125  	return
   126  }
   127  
   128  // Delete deletes a orders.
   129  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
   130  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
   131  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   132  	return
   133  }