github.com/gophercloud/gophercloud@v1.11.0/openstack/keymanager/v1/orders/requests.go (about)

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