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

     1  package loadbalancers
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/elb"
     8  	"github.com/huaweicloud/golangsdk/openstack/utils"
     9  )
    10  
    11  // CreateOptsBuilder is the interface options structs have to satisfy in order
    12  // to be used in the main Create operation in this package. Since many
    13  // extensions decorate or modify the common logic, it is useful for them to
    14  // satisfy a basic interface in order for them to be used.
    15  type CreateOptsBuilder interface {
    16  	ToLoadBalancerCreateMap() (map[string]interface{}, error)
    17  }
    18  
    19  // CreateOpts is the common options struct used in this package's Create
    20  // operation.
    21  type CreateOpts struct {
    22  	Name            string `json:"name" required:"true"`
    23  	Description     string `json:"description,omitempty"`
    24  	VpcID           string `json:"vpc_id" required:"true"`
    25  	BandWidth       int    `json:"bandwidth,omitempty"`
    26  	Type            string `json:"type" required:"true"`
    27  	AdminStateUp    int    `json:"admin_state_up" required:"true"`
    28  	VipSubnetID     string `json:"vip_subnet_id,omitempty"`
    29  	AZ              string `json:"az,omitempty"`
    30  	ChargeMode      string `json:"charge_mode,omitempty"`
    31  	EipType         string `json:"eip_type,omitempty"`
    32  	SecurityGroupID string `json:"security_group_id,omitempty"`
    33  	VipAddress      string `json:"vip_address,omitempty"`
    34  	TenantID        string `json:"tenantId,omitempty"`
    35  }
    36  
    37  // ToLoadBalancerCreateMap casts a CreateOpts struct to a map.
    38  func (opts CreateOpts) ToLoadBalancerCreateMap() (map[string]interface{}, error) {
    39  	return golangsdk.BuildRequestBody(opts, "")
    40  }
    41  
    42  // Create is an operation which provisions a new loadbalancer based on the
    43  // configuration defined in the CreateOpts struct. Once the request is
    44  // validated and progress has started on the provisioning process, a
    45  // CreateResult will be returned.
    46  //
    47  // Users with an admin role can create loadbalancers on behalf of other tenants by
    48  // specifying a TenantID attribute different than their own.
    49  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r elb.JobResult) {
    50  	b, err := opts.ToLoadBalancerCreateMap()
    51  	if err != nil {
    52  		r.Err = err
    53  		return
    54  	}
    55  	log.Printf("[DEBUG] create ELB-LoadBalancer url:%q, body=%#v", rootURL(c), b)
    56  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    57  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    58  	return
    59  }
    60  
    61  // Get retrieves a particular Loadbalancer based on its unique ID.
    62  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    63  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    64  	return
    65  }
    66  
    67  // UpdateOptsBuilder is the interface options structs have to satisfy in order
    68  // to be used in the main Update operation in this package. Since many
    69  // extensions decorate or modify the common logic, it is useful for them to
    70  // satisfy a basic interface in order for them to be used.
    71  type UpdateOptsBuilder interface {
    72  	ToLoadBalancerUpdateMap() (map[string]interface{}, error)
    73  }
    74  
    75  // UpdateOpts is the common options struct used in this package's Update
    76  // operation.
    77  type UpdateOpts struct {
    78  	// Optional. Human-readable name for the Loadbalancer. Does not have to be unique.
    79  	Name string `json:"name,omitempty"`
    80  	// Optional. Human-readable description for the Loadbalancer.
    81  	Description string `json:"description"`
    82  
    83  	BandWidth int `json:"bandwidth,omitempty"`
    84  	// Optional. The administrative state of the Loadbalancer. A valid value is true (UP)
    85  	// or false (DOWN).
    86  	AdminStateUp int `json:"admin_state_up"`
    87  }
    88  
    89  // ToLoadBalancerUpdateMap casts a UpdateOpts struct to a map.
    90  func (opts UpdateOpts) ToLoadBalancerUpdateMap() (map[string]interface{}, error) {
    91  	return golangsdk.BuildRequestBody(opts, "")
    92  }
    93  
    94  // Update is an operation which modifies the attributes of the specified LoadBalancer.
    95  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts, not_pass_param []string) (r elb.JobResult) {
    96  	b, err := opts.ToLoadBalancerUpdateMap()
    97  	if err != nil {
    98  		r.Err = err
    99  		return
   100  	}
   101  	utils.DeleteNotPassParams(&b, not_pass_param)
   102  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   103  		OkCodes: []int{200},
   104  	})
   105  	return
   106  }
   107  
   108  // Delete will permanently delete a particular LoadBalancer based on its unique ID.
   109  func Delete(c *golangsdk.ServiceClient, id string) (r elb.JobResult) {
   110  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   111  	_, r.Err = c.DeleteWithResponse(resourceURL(c, id), &r.Body, reqOpt)
   112  	return
   113  }