github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/elb/v2/certificates/requests.go (about)

     1  /*
     2   Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
     3  */
     4  
     5  package certificates
     6  
     7  import (
     8  	"github.com/chnsz/golangsdk"
     9  )
    10  
    11  // ListOptsBuilder allows extensions to add additional parameters to the
    12  // List request.
    13  type ListOptsBuilder interface {
    14  	ToCertificateListQuery() (string, error)
    15  }
    16  
    17  // ListOpts parameters used to query the certificate.
    18  type ListOpts struct {
    19  	// Specifies the ID of the certificate from which pagination query starts, that is,
    20  	// the ID of the last certificate on the previous page. This parameter must be used with limit.
    21  	Marker string `q:"marker"`
    22  	// Specifies the number of certificates on each page.
    23  	// If this parameter is not set, all certificates are queried by default.
    24  	Limit int `q:"limit"`
    25  	// Specifies the page direction. The value can be true or false, and the default value is false.
    26  	// This parameter must be used with limit.
    27  	PageReverse *bool `q:"page_reverse"`
    28  	// Specifies the certificate ID.
    29  	Id string `q:"id"`
    30  	// Specifies the certificate name.
    31  	// The value contains a maximum of 255 characters.
    32  	Name string `q:"name"`
    33  	// Provides supplementary information about the certificate.
    34  	// The value contains a maximum of 255 characters.
    35  	Description string `q:"description"`
    36  	// Specifies the certificate type. The default value is server.
    37  	// The value range varies depending on the protocol of the backend server group:
    38  	// server: indicates the server certificate.
    39  	// client: indicates the CA certificate.
    40  	Type string `q:"type"`
    41  	// Specifies the domain name associated with the server certificate. The default value is null.
    42  	Domain string `q:"domain"`
    43  	// Specifies the private key of the server certificate.
    44  	PrivateKey string `q:"private_key"`
    45  	// Specifies the public key of the server certificate or CA certificate used to authenticate the client.
    46  	Certificate string `q:"certificate"`
    47  	// Specifies the time when the certificate was created.
    48  	// The UTC time is in YYYY-MM-DD HH:MM:SS format.
    49  	CreateTime string `q:"create_time"`
    50  	// Specifies the time when the certificate was updated.
    51  	// The UTC time is in YYYY-MM-DD HH:MM:SS format.
    52  	UpdateTime string `q:"update_time"`
    53  }
    54  
    55  // ToCertificateListQuery formats a ListOpts into a query string.
    56  func (opts ListOpts) ToCertificateListQuery() (string, error) {
    57  	q, err := golangsdk.BuildQueryString(opts)
    58  	return q.String(), err
    59  }
    60  
    61  // List query the certificate list
    62  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) (ListResult, error) {
    63  	var r ListResult
    64  	var err error
    65  	url := rootURL(c)
    66  	if opts != nil {
    67  		query, err := opts.ToCertificateListQuery()
    68  		if err != nil {
    69  			return r, err
    70  		}
    71  		url += query
    72  	}
    73  	_, r.Err = c.Get(url, &r.Body, nil)
    74  	return r, err
    75  }
    76  
    77  // CreateOptsBuilder is the interface options structs have to satisfy in order
    78  // to be used in the main Create operation in this package. Since many
    79  // extensions decorate or modify the common logic, it is useful for them to
    80  // satisfy a basic interface in order for them to be used.
    81  type CreateOptsBuilder interface {
    82  	ToCertificateCreateMap() (map[string]interface{}, error)
    83  }
    84  
    85  // CreateOpts is the common options struct used in this package's Create
    86  // operation.
    87  type CreateOpts struct {
    88  	// Specifies the certificate management status
    89  	AdminStateUP bool `json:"admin_state_up,omitempty"`
    90  	// Specifies the certificate name.
    91  	// The value contains a maximum of 255 characters.
    92  	Name string `json:"name,omitempty"`
    93  	// Provides supplementary information about the certificate.
    94  	// The value contains a maximum of 255 characters.
    95  	Description string `json:"description,omitempty"`
    96  	// Specifies the certificate type. The default value is server.
    97  	// The value range varies depending on the protocol of the backend server group:
    98  	// server: indicates the server certificate.
    99  	// client: indicates the CA certificate.
   100  	Type string `json:"type,omitempty"`
   101  	// Specifies the domain name associated with the server certificate. The default value is null.
   102  	Domain string `json:"domain,omitempty"`
   103  	// Specifies the private key of the server certificate.
   104  	PrivateKey string `json:"private_key,omitempty"`
   105  	// Specifies the public key of the server certificate or CA certificate used to authenticate the client.
   106  	Certificate string `json:"certificate" required:"true"`
   107  	// Specifies the enterprise project id.
   108  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
   109  }
   110  
   111  // ToCertificateCreateMap casts a CreateOpts struct to a map.
   112  func (opts CreateOpts) ToCertificateCreateMap() (map[string]interface{}, error) {
   113  	return golangsdk.BuildRequestBody(opts, "")
   114  }
   115  
   116  // Create is an operation which provisions a new loadbalancer based on the
   117  // configuration defined in the CreateOpts struct. Once the request is
   118  // validated and progress has started on the provisioning process, a
   119  // CreateResult will be returned.
   120  //
   121  // Users with an admin role can create loadbalancers on behalf of other tenants by
   122  // specifying a TenantID attribute different than their own.
   123  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   124  	b, err := opts.ToCertificateCreateMap()
   125  	if err != nil {
   126  		r.Err = err
   127  		return
   128  	}
   129  	_, r.Err = c.Post(rootURL(c), b, &r.Body, &golangsdk.RequestOpts{
   130  		OkCodes: []int{200, 201},
   131  	})
   132  	return
   133  }
   134  
   135  // Get retrieves a particular Loadbalancer based on its unique ID.
   136  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   137  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   138  	return
   139  }
   140  
   141  // UpdateOptsBuilder is the interface options structs have to satisfy in order
   142  // to be used in the main Update operation in this package. Since many
   143  // extensions decorate or modify the common logic, it is useful for them to
   144  // satisfy a basic interface in order for them to be used.
   145  type UpdateOptsBuilder interface {
   146  	ToCertificateUpdateMap() (map[string]interface{}, error)
   147  }
   148  
   149  // UpdateOpts is the common options struct used in this package's Update
   150  // operation.
   151  type UpdateOpts struct {
   152  	// Specifies the certificate management status
   153  	AdminStateUP bool `json:"admin_state_up,omitempty"`
   154  	// Specifies the certificate name.
   155  	// The value contains a maximum of 255 characters.
   156  	Name string `json:"name,omitempty"`
   157  	// Provides supplementary information about the certificate.
   158  	// The value contains a maximum of 255 characters.
   159  	Description string `json:"description,omitempty"`
   160  	// Specifies the domain name associated with the server certificate. The default value is null.
   161  	Domain string `json:"domain,omitempty"`
   162  	// Specifies the private key of the server certificate.
   163  	PrivateKey string `json:"private_key,omitempty"`
   164  	// Specifies the public key of the server certificate or CA certificate used to authenticate the client.
   165  	Certificate string `json:"certificate,omitempty"`
   166  }
   167  
   168  // ToCertificateUpdateMap casts a UpdateOpts struct to a map.
   169  func (opts UpdateOpts) ToCertificateUpdateMap() (map[string]interface{}, error) {
   170  	return golangsdk.BuildRequestBody(opts, "")
   171  }
   172  
   173  // Update is an operation which modifies the attributes of the specified Certificate.
   174  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   175  	b, err := opts.ToCertificateUpdateMap()
   176  	if err != nil {
   177  		r.Err = err
   178  		return
   179  	}
   180  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   181  		OkCodes: []int{200},
   182  	})
   183  	return
   184  }
   185  
   186  // Delete will permanently delete a particular Certificate based on its unique ID.
   187  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   188  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   189  	return
   190  }