github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/elb/certificate/requests.go (about)

     1  package certificate
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  // CreateOptsBuilder is the interface options structs have to satisfy in order
    10  // to be used in the main Create operation in this package. Since many
    11  // extensions decorate or modify the common logic, it is useful for them to
    12  // satisfy a basic interface in order for them to be used.
    13  type CreateOptsBuilder interface {
    14  	ToCertificateCreateMap() (map[string]interface{}, error)
    15  }
    16  
    17  // CreateOpts is the common options struct used in this package's Create
    18  // operation.
    19  type CreateOpts struct {
    20  	Name        string `json:"name,omitempty"`
    21  	Description string `json:"description,omitempty"`
    22  	Domain      string `json:"domain,omitempty"`
    23  	Certificate string `json:"certificate" required:"true"`
    24  	PrivateKey  string `json:"private_key" required:"true"`
    25  }
    26  
    27  // ToCertificateCreateMap casts a CreateOpts struct to a map.
    28  func (opts CreateOpts) ToCertificateCreateMap() (map[string]interface{}, error) {
    29  	return golangsdk.BuildRequestBody(opts, "")
    30  }
    31  
    32  // Create is an operation which provisions a new loadbalancer based on the
    33  // configuration defined in the CreateOpts struct. Once the request is
    34  // validated and progress has started on the provisioning process, a
    35  // CreateResult will be returned.
    36  //
    37  // Users with an admin role can create loadbalancers on behalf of other tenants by
    38  // specifying a TenantID attribute different than their own.
    39  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    40  	b, err := opts.ToCertificateCreateMap()
    41  	if err != nil {
    42  		r.Err = err
    43  		return
    44  	}
    45  	log.Printf("[DEBUG] create url:%q, body=%#v", rootURL(c), b)
    46  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    47  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    48  	return
    49  }
    50  
    51  // Get retrieves a particular Loadbalancer based on its unique ID.
    52  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    53  	r.ID = id
    54  	_, r.Err = c.Get(rootURL(c), &r.Body, nil)
    55  	return
    56  }
    57  
    58  // UpdateOptsBuilder is the interface options structs have to satisfy in order
    59  // to be used in the main Update operation in this package. Since many
    60  // extensions decorate or modify the common logic, it is useful for them to
    61  // satisfy a basic interface in order for them to be used.
    62  type UpdateOptsBuilder interface {
    63  	ToCertificateUpdateMap() (map[string]interface{}, error)
    64  }
    65  
    66  // UpdateOpts is the common options struct used in this package's Update
    67  // operation.
    68  type UpdateOpts struct {
    69  	Name        string `json:"name,omitempty"`
    70  	Description string `json:"description,omitempty"`
    71  }
    72  
    73  func (u UpdateOpts) IsNeedUpdate() (bool, error) {
    74  	d, e := u.ToCertificateUpdateMap()
    75  	if e == nil {
    76  		return len(d) != 0, nil
    77  	}
    78  	return false, e
    79  }
    80  
    81  // ToCertificateUpdateMap casts a UpdateOpts struct to a map.
    82  func (opts UpdateOpts) ToCertificateUpdateMap() (map[string]interface{}, error) {
    83  	return golangsdk.BuildRequestBody(opts, "")
    84  }
    85  
    86  // Update is an operation which modifies the attributes of the specified Certificate.
    87  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
    88  	b, err := opts.ToCertificateUpdateMap()
    89  	if err != nil {
    90  		r.Err = err
    91  		return
    92  	}
    93  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
    94  		OkCodes: []int{200},
    95  	})
    96  	return
    97  }
    98  
    99  // Delete will permanently delete a particular Certificate based on its unique ID.
   100  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   101  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
   102  	_, r.Err = c.Delete(resourceURL(c, id), reqOpt)
   103  	return
   104  }