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

     1  package backendecs
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/opentelekomcloud/gophertelekomcloud"
     7  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/elb"
     8  )
     9  
    10  // CreateOptsBuilder is the interface options structs have to satisfy in order
    11  // to be used in the main Create operation in this package. Since many
    12  // extensions decorate or modify the common logic, it is useful for them to
    13  // satisfy a basic interface in order for them to be used.
    14  type CreateOptsBuilder interface {
    15  	ToBackendECSCreateMap() (map[string]interface{}, error)
    16  }
    17  
    18  // CreateOpts is the common options struct used in this package's Create
    19  // operation.
    20  type CreateOpts struct {
    21  	ServerId string `json:"server_id" required:"true"`
    22  	Address  string `json:"address" required:"true"`
    23  }
    24  
    25  // ToBackendECSCreateMap casts a CreateOpts struct to a map.
    26  func (opts CreateOpts) ToBackendECSCreateMap() (map[string]interface{}, error) {
    27  	return golangsdk.BuildRequestBody(opts, "")
    28  }
    29  
    30  // Create is an operation which provisions a new loadbalancer based on the
    31  // configuration defined in the CreateOpts struct. Once the request is
    32  // validated and progress has started on the provisioning process, a
    33  // CreateResult will be returned.
    34  //
    35  // Users with an admin role can create loadbalancers on behalf of other tenants by
    36  // specifying a TenantID attribute different than their own.
    37  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder, lId string) (r elb.JobResult) {
    38  	b, err := opts.ToBackendECSCreateMap()
    39  	if err != nil {
    40  		r.Err = err
    41  		return
    42  	}
    43  
    44  	// API takes an array of these...
    45  	body := []map[string]interface{}{b}
    46  	log.Printf("[DEBUG] create ELB-BackendECS url:%q, body=%#v", rootURL(c, lId), body)
    47  
    48  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    49  	_, r.Err = c.Post(rootURL(c, lId), body, &r.Body, reqOpt)
    50  	return
    51  }
    52  
    53  type GetOptsBuilder interface {
    54  	ToBackendECSListQuery() (string, error)
    55  }
    56  
    57  type getOpts struct {
    58  	ID string `q:"id"`
    59  }
    60  
    61  func (opts getOpts) ToBackendECSListQuery() (string, error) {
    62  	q, err := golangsdk.BuildQueryString(opts)
    63  	if err != nil {
    64  		return "", err
    65  	}
    66  	return q.String(), err
    67  }
    68  
    69  // Get retrieves a particular Loadbalancer based on its unique ID.
    70  func Get(c *golangsdk.ServiceClient, lId string, backendId string) (r GetResult) {
    71  	url := rootURL(c, lId)
    72  	opts := getOpts{ID: backendId}
    73  	query, err := opts.ToBackendECSListQuery()
    74  	if err != nil {
    75  		r.Err = err
    76  		return
    77  	}
    78  	log.Printf("[DEBUG] get ELB-BackendECS opt=%#v, query=%s", opts, query)
    79  	url += query
    80  	log.Printf("[DEBUG] get ELB-BackendECS url:%q, backendId:%q", url, backendId)
    81  
    82  	_, r.Err = c.Get(url, &r.Body, nil)
    83  	return
    84  }
    85  
    86  // UpdateOptsBuilder is the interface options structs have to satisfy in order
    87  // to be used in the main Update operation in this package. Since many
    88  // extensions decorate or modify the common logic, it is useful for them to
    89  // satisfy a basic interface in order for them to be used.
    90  type DeleteOptsBuilder interface {
    91  	ToBackendECSDeleteMap() (map[string]interface{}, error)
    92  }
    93  
    94  // UpdateOpts is the common options struct used in this package's Update
    95  // operation.
    96  
    97  type RemoveMemberField struct {
    98  	ID string `json:"id" required:"true"`
    99  }
   100  
   101  type DeleteOpts struct {
   102  	RemoveMember []RemoveMemberField `json:"removeMember" required:"true"`
   103  }
   104  
   105  // ToBackendECSUpdateMap casts a UpdateOpts struct to a map.
   106  func (opts DeleteOpts) ToBackendECSDeleteMap() (map[string]interface{}, error) {
   107  	/*
   108  		o, err := golangsdk.BuildRequestBody(opts, "")
   109  		if err != nil {
   110  			return nil, err
   111  		}
   112  		rm = removeMeber{RemoveMember: []map[string]string{o.([string]string)}}
   113  	*/
   114  	return golangsdk.BuildRequestBody(opts, "")
   115  }
   116  
   117  // Update is an operation which modifies the attributes of the specified BackendECS.
   118  func Delete(c *golangsdk.ServiceClient, lId string, opts DeleteOpts) (r elb.JobResult) {
   119  	b, err := opts.ToBackendECSDeleteMap()
   120  	if err != nil {
   121  		r.Err = err
   122  		return
   123  	}
   124  	log.Printf("[DEBUG] deleting ELB-BackendECS, request opts=%#v", b)
   125  
   126  	_, r.Err = c.Post(actionURL(c, lId), b, &r.Body, &golangsdk.RequestOpts{
   127  		OkCodes: []int{200},
   128  	})
   129  	return
   130  }