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

     1  package loadbalancer_elbs
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  	//"fmt"
     7  )
     8  
     9  // ListOptsBuilder allows extensions to add additional parameters to the
    10  // List request.
    11  type ListOptsBuilder interface {
    12  	ToLoadBalancerListQuery() (string, error)
    13  }
    14  
    15  // ListOpts allows the filtering and sorting of paginated collections through
    16  // the API. Filtering is achieved by passing in struct field values that map to
    17  // the Loadbalancer attributes you want to see returned. SortKey allows you to
    18  // sort by a particular attribute. SortDir sets the direction, and is
    19  // either `asc' or `desc'. Marker and Limit are used for pagination.
    20  type ListOpts struct {
    21  	Name            string `q:"name"`
    22  	Description     string `q:"description"`
    23  	VpcID           string `q:"vpc_id"`
    24  	Bandwidth       int    `q:"bandwidth"`
    25  	Type            string `q:"type"`
    26  	AdminStateUp    *bool  `q:"admin_state_up"`
    27  	VipSubnetID     string `q:"vip_subnet_id"`
    28  	AZ              string `q:"az"`
    29  	ChargeMode      string `q:"charge_mode"`
    30  	EipType         string `q:"eip_type"`
    31  	SecurityGroupID string `q:"security_group_id"`
    32  	VipAddress      string `q:"vip_address"`
    33  	TenantID        string `q:"tenantId"`
    34  	ID              string `q:"id"`
    35  	Limit           int    `q:"limit"`
    36  	Marker          string `q:"marker"`
    37  	SortKey         string `q:"sort_key"`
    38  	SortDir         string `q:"sort_dir"`
    39  }
    40  
    41  // ToLoadbalancerListQuery formats a ListOpts into a query string.
    42  func (opts ListOpts) ToLoadBalancerListQuery() (string, error) {
    43  	q, err := golangsdk.BuildQueryString(opts)
    44  	return q.String(), err
    45  }
    46  
    47  // List returns a Pager which allows you to iterate over a collection of
    48  // routers. It accepts a ListOpts struct, which allows you to filter and sort
    49  // the returned collection for greater efficiency.
    50  //
    51  // Default policy settings return only those routers that are owned by the
    52  // tenant who submits the request, unless an admin user submits the request.
    53  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    54  	url := rootURL(c)
    55  	//fmt.Printf("url=%s.\n", url)
    56  	if opts != nil {
    57  		query, err := opts.ToLoadBalancerListQuery()
    58  		if err != nil {
    59  			return pagination.Pager{Err: err}
    60  		}
    61  		url += query
    62  	}
    63  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    64  		return LoadBalancerPage{pagination.LinkedPageBase{PageResult: r}}
    65  	})
    66  }
    67  
    68  // CreateOptsBuilder is the interface options structs have to satisfy in order
    69  // to be used in the main Create operation in this package. Since many
    70  // extensions decorate or modify the common logic, it is useful for them to
    71  // satisfy a basic interface in order for them to be used.
    72  type CreateOptsBuilder interface {
    73  	ToLoadBalancerCreateMap() (map[string]interface{}, error)
    74  }
    75  
    76  // CreateOpts is the common options struct used in this package's Create
    77  // operation.
    78  type CreateOpts struct {
    79  	// Required. Specifies the load balancer name.
    80  	// The name is a string of 1 to 64 characters that consist of letters, digits, underscores (_), and hyphens (-).
    81  	Name string `json:"name" required:"true"`
    82  	// Optional. Provides supplementary information about the load balancer.
    83  	// The value is a string of 0 to 128 characters and cannot contain angle brackets (<>).
    84  	Description string `json:"description,omitempty"`
    85  	// Required. Specifies the VPC ID.
    86  	VpcID string `json:"vpc_id" required:"true"`
    87  	// Optional. Specifies the bandwidth (Mbit/s). This parameter is mandatory when type is
    88  	// set to External, and it is invalid when type is set to Internal.
    89  	// The value ranges from 1 to 300.
    90  	Bandwidth int `json:"bandwidth,omitempty"`
    91  	// Required. Specifies the load balancer type.
    92  	// The value can be Internal or External.
    93  	Type string `json:"type" required:"true"`
    94  	// Required.  Specifies the status of the load balancer.
    95  	// Optional values:
    96  	// 0 or false: indicates that the load balancer is stopped. Only tenants are allowed to enter these two values.
    97  	// 1 or true: indicates that the load balancer is running properly.
    98  	// 2 or false: indicates that the load balancer is frozen. Only tenants are allowed to enter these two values.
    99  	AdminStateUp *bool `json:"admin_state_up" required:"true"`
   100  	// Optional.  Specifies the subnet ID of backend ECSs. This parameter is mandatory when type is set to Internal.
   101  	VipSubnetID string `json:"vip_subnet_id,omitempty"`
   102  	// Optional.  Specifies the ID of the availability zone (AZ). This parameter is mandatory when type
   103  	// is set to Internal, and it is invalid when type is set to External.
   104  	AZ string `json:"az,omitempty"`
   105  	// Optional.  Specifies the security group ID.
   106  	// The value is a string of 1 to 200 characters that consists of uppercase and lowercase letters, digits, and hyphens (-).
   107  	// This parameter is mandatory when type is set to Internal.
   108  	SecurityGroupID string `json:"security_group_id,omitempty"`
   109  	// Optional.  Specifies the IP address used by ELB for providing services. When type is set to External,
   110  	// the parameter value is the elastic IP address. When type is set to Internal, the parameter value is
   111  	// the private network IP address.
   112  	// You can select an existing elastic IP address and create a public network load balancer.
   113  	// When this parameter is configured, parameters bandwidth, charge_mode, and eip_type are invalid.
   114  	VipAddress string `json:"vip_address,omitempty"`
   115  	// Specifies the tenant ID. This parameter is mandatory when type is set to Internal
   116  	TenantID string `json:"tenantid,omitempty"`
   117  }
   118  
   119  // ToLoadBalancerCreateMap casts a CreateOpts struct to a map.
   120  func (opts CreateOpts) ToLoadBalancerCreateMap() (map[string]interface{}, error) {
   121  	return golangsdk.BuildRequestBody(opts, "")
   122  }
   123  
   124  // Create is an operation which provisions a new loadbalancer based on the
   125  // configuration defined in the CreateOpts struct. Once the request is
   126  // validated and progress has started on the provisioning process, a
   127  // CreateResult will be returned.
   128  //
   129  // Users with an admin role can create loadbalancers on behalf of other tenants by
   130  // specifying a TenantID attribute different than their own.
   131  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   132  	b, err := opts.ToLoadBalancerCreateMap()
   133  	if err != nil {
   134  		r.Err = err
   135  		return
   136  	}
   137  	//fmt.Printf("Create (%+v): rootURL: %s, b=%+v.\n", c, rootURL(c), b)
   138  	_, r.Err = c.Post(rootURL(c), b, &r.Body, &golangsdk.RequestOpts{
   139  		OkCodes: []int{200},
   140  	})
   141  	return
   142  }
   143  
   144  // Get retrieves a particular Loadbalancer based on its unique ID.
   145  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   146  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   147  	return
   148  }
   149  
   150  // UpdateOptsBuilder is the interface options structs have to satisfy in order
   151  // to be used in the main Update operation in this package. Since many
   152  // extensions decorate or modify the common logic, it is useful for them to
   153  // satisfy a basic interface in order for them to be used.
   154  type UpdateOptsBuilder interface {
   155  	ToLoadBalancerUpdateMap() (map[string]interface{}, error)
   156  }
   157  
   158  // UpdateOpts is the common options struct used in this package's Update
   159  // operation.
   160  type UpdateOpts struct {
   161  	// Required. Specifies the load balancer name.
   162  	// The name is a string of 1 to 64 characters that consist of letters, digits, underscores (_), and hyphens (-).
   163  	Name string `json:"name,omitempty"`
   164  	// Optional. Provides supplementary information about the load balancer.
   165  	// The value is a string of 0 to 128 characters and cannot contain angle brackets (<>).
   166  	Description string `json:"description,omitempty"`
   167  	// Optional. Specifies the bandwidth (Mbit/s). This parameter is mandatory when type is
   168  	// set to External, and it is invalid when type is set to Internal.
   169  	// The value ranges from 1 to 300.
   170  	Bandwidth int `json:"bandwidth,omitempty"`
   171  	// Required.  Specifies the status of the load balancer.
   172  	// Optional values:
   173  	// 0 or false: indicates that the load balancer is stopped. Only tenants are allowed to enter these two values.
   174  	// 1 or true: indicates that the load balancer is running properly.
   175  	// 2 or false: indicates that the load balancer is frozen. Only tenants are allowed to enter these two values.
   176  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   177  }
   178  
   179  // ToLoadBalancerUpdateMap casts a UpdateOpts struct to a map.
   180  func (opts UpdateOpts) ToLoadBalancerUpdateMap() (map[string]interface{}, error) {
   181  	return golangsdk.BuildRequestBody(opts, "")
   182  }
   183  
   184  // Update is an operation which modifies the attributes of the specified LoadBalancer.
   185  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   186  	b, err := opts.ToLoadBalancerUpdateMap()
   187  	if err != nil {
   188  		r.Err = err
   189  		return
   190  	}
   191  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   192  		OkCodes: []int{200, 202},
   193  	})
   194  	return
   195  }
   196  
   197  // Delete will permanently delete a particular LoadBalancer based on its unique ID.
   198  func Delete(c *golangsdk.ServiceClient, id string, keepEIP bool) (r DeleteResult) {
   199  	_, r.Err = c.DeleteWithResponse(deleteURL(c, id, keepEIP), &r.Body, &golangsdk.RequestOpts{
   200  		OkCodes: []int{200},
   201  	})
   202  	return
   203  }