github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/elbaas/loadbalancer_elbs/requests.go (about)

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