github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/waf/v1/certificates/requests.go (about)

     1  package certificates
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
     9  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    10  }
    11  
    12  // CreateOptsBuilder allows extensions to add additional parameters to the
    13  // Create request.
    14  type CreateOptsBuilder interface {
    15  	ToCertCreateMap() (map[string]interface{}, error)
    16  }
    17  
    18  // CreateOpts contains all the values needed to create a new certificate.
    19  type CreateOpts struct {
    20  	// Certificate name
    21  	Name string `json:"name" required:"true"`
    22  	// Certificate content
    23  	Content string `json:"content" required:"true"`
    24  	// Private Key
    25  	Key string `json:"key" required:"true"`
    26  }
    27  
    28  // ToCertCreateMap builds a create request body from CreateOpts.
    29  func (opts CreateOpts) ToCertCreateMap() (map[string]interface{}, error) {
    30  	return golangsdk.BuildRequestBody(opts, "")
    31  }
    32  
    33  // Create will create a new certificate based on the values in CreateOpts.
    34  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    35  	b, err := opts.ToCertCreateMap()
    36  	if err != nil {
    37  		r.Err = err
    38  		return
    39  	}
    40  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    41  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    42  	return
    43  }
    44  
    45  // UpdateOptsBuilder allows extensions to add additional parameters to the
    46  // Update request.
    47  type UpdateOptsBuilder interface {
    48  	ToCertUpdateMap() (map[string]interface{}, error)
    49  }
    50  
    51  // UpdateOpts contains all the values needed to update a certificate.
    52  type UpdateOpts struct {
    53  	// Certificate name
    54  	Name string `json:"name,omitempty"`
    55  }
    56  
    57  // ToCertUpdateMap builds a update request body from UpdateOpts.
    58  func (opts UpdateOpts) ToCertUpdateMap() (map[string]interface{}, error) {
    59  	return golangsdk.BuildRequestBody(opts, "")
    60  }
    61  
    62  // Update accepts a UpdateOpts struct and uses the values to update a certificate.The response code from api is 200
    63  func Update(c *golangsdk.ServiceClient, certID string, opts UpdateOptsBuilder) (r UpdateResult) {
    64  	b, err := opts.ToCertUpdateMap()
    65  	if err != nil {
    66  		r.Err = err
    67  		return
    68  	}
    69  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    70  	_, r.Err = c.Put(resourceURL(c, certID), b, nil, reqOpt)
    71  	return
    72  }
    73  
    74  type ListOptsBuilder interface {
    75  	ToCertificateListQuery() (string, error)
    76  }
    77  
    78  // ListOpts the struct is used to query certificate list
    79  type ListOpts struct {
    80  	Page      int    `q:"page"`
    81  	Pagesize  int    `q:"pagesize"`
    82  	Name      string `q:"name"`
    83  	Host      *bool  `q:"host"`
    84  	ExpStatus *int   `q:"exp_status"`
    85  }
    86  
    87  // ToCertificateListQuery formats a ListOpts into a query string.
    88  func (opts ListOpts) ToCertificateListQuery() (string, error) {
    89  	q, err := golangsdk.BuildQueryString(opts)
    90  	return q.String(), err
    91  }
    92  
    93  // List sends a request to obtain a certificate list
    94  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    95  	url := rootURL(c)
    96  	if opts != nil {
    97  		query, err := opts.ToCertificateListQuery()
    98  		if err != nil {
    99  			return pagination.Pager{Err: err}
   100  		}
   101  		url += query
   102  	}
   103  
   104  	pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   105  		return CertificatePage{pagination.SinglePageBase(r)}
   106  	})
   107  	pager.Headers = RequestOpts.MoreHeaders
   108  	return pager
   109  }
   110  
   111  // Get retrieves a particular certificate based on its unique ID.
   112  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   113  	reqOpt := &golangsdk.RequestOpts{
   114  		OkCodes:     []int{200},
   115  		MoreHeaders: RequestOpts.MoreHeaders,
   116  	}
   117  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, reqOpt)
   118  	return
   119  }
   120  
   121  // Delete will permanently delete a particular certificate based on its unique ID.
   122  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   123  	reqOpt := &golangsdk.RequestOpts{
   124  		OkCodes:     []int{200, 204},
   125  		MoreHeaders: RequestOpts.MoreHeaders,
   126  	}
   127  	_, r.Err = c.Delete(resourceURL(c, id), reqOpt)
   128  	return
   129  }