github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/lbaas/vips/requests.go (about)

     1  package vips
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // ListOpts allows the filtering and sorting of paginated collections through
     9  // the API. Filtering is achieved by passing in struct field values that map to
    10  // the floating IP attributes you want to see returned. SortKey allows you to
    11  // sort by a particular network attribute. SortDir sets the direction, and is
    12  // either `asc' or `desc'. Marker and Limit are used for pagination.
    13  type ListOpts struct {
    14  	ID              string `q:"id"`
    15  	Name            string `q:"name"`
    16  	AdminStateUp    *bool  `q:"admin_state_up"`
    17  	Status          string `q:"status"`
    18  	TenantID        string `q:"tenant_id"`
    19  	SubnetID        string `q:"subnet_id"`
    20  	Address         string `q:"address"`
    21  	PortID          string `q:"port_id"`
    22  	Protocol        string `q:"protocol"`
    23  	ProtocolPort    int    `q:"protocol_port"`
    24  	ConnectionLimit int    `q:"connection_limit"`
    25  	Limit           int    `q:"limit"`
    26  	Marker          string `q:"marker"`
    27  	SortKey         string `q:"sort_key"`
    28  	SortDir         string `q:"sort_dir"`
    29  }
    30  
    31  // List returns a Pager which allows you to iterate over a collection of
    32  // Virtual IPs. It accepts a ListOpts struct, which allows you to filter and
    33  // sort the returned collection for greater efficiency.
    34  //
    35  // Default policy settings return only those virtual IPs that are owned by the
    36  // tenant who submits the request, unless an admin user submits the request.
    37  func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
    38  	q, err := gophercloud.BuildQueryString(&opts)
    39  	if err != nil {
    40  		return pagination.Pager{Err: err}
    41  	}
    42  	u := rootURL(c) + q.String()
    43  	return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page {
    44  		return VIPPage{pagination.LinkedPageBase{PageResult: r}}
    45  	})
    46  }
    47  
    48  // CreateOptsBuilder allows extensions to add additional parameters to the
    49  // Create Request.
    50  type CreateOptsBuilder interface {
    51  	ToVIPCreateMap() (map[string]interface{}, error)
    52  }
    53  
    54  // CreateOpts contains all the values needed to create a new virtual IP.
    55  type CreateOpts struct {
    56  	// Name is the human-readable name for the VIP. Does not have to be unique.
    57  	Name string `json:"name" required:"true"`
    58  
    59  	// SubnetID is the network on which to allocate the VIP's address. A tenant
    60  	// can only create VIPs on networks authorized by policy (e.g. networks that
    61  	// belong to them or networks that are shared).
    62  	SubnetID string `json:"subnet_id" required:"true"`
    63  
    64  	// Protocol - can either be TCP, HTTP or HTTPS.
    65  	Protocol string `json:"protocol" required:"true"`
    66  
    67  	// ProtocolPort is the port on which to listen for client traffic.
    68  	ProtocolPort int `json:"protocol_port" required:"true"`
    69  
    70  	// PoolID is the ID of the pool with which the VIP is associated.
    71  	PoolID string `json:"pool_id" required:"true"`
    72  
    73  	// TenantID is only required if the caller has an admin role and wants
    74  	// to create a pool for another tenant.
    75  	TenantID string `json:"tenant_id,omitempty"`
    76  
    77  	// Address is the IP address of the VIP.
    78  	Address string `json:"address,omitempty"`
    79  
    80  	// Description is the human-readable description for the VIP.
    81  	Description string `json:"description,omitempty"`
    82  
    83  	// Persistence is the the of session persistence to use.
    84  	// Omit this field to prevent session persistence.
    85  	Persistence *SessionPersistence `json:"session_persistence,omitempty"`
    86  
    87  	// ConnLimit is the maximum number of connections allowed for the VIP.
    88  	ConnLimit *int `json:"connection_limit,omitempty"`
    89  
    90  	// AdminStateUp is the administrative state of the VIP. A valid value is
    91  	// true (UP) or false (DOWN).
    92  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
    93  }
    94  
    95  // ToVIPCreateMap builds a request body from CreateOpts.
    96  func (opts CreateOpts) ToVIPCreateMap() (map[string]interface{}, error) {
    97  	return gophercloud.BuildRequestBody(opts, "vip")
    98  }
    99  
   100  // Create is an operation which provisions a new virtual IP based on the
   101  // configuration defined in the CreateOpts struct. Once the request is
   102  // validated and progress has started on the provisioning process, a
   103  // CreateResult will be returned.
   104  //
   105  // Please note that the PoolID should refer to a pool that is not already
   106  // associated with another vip. If the pool is already used by another vip,
   107  // then the operation will fail with a 409 Conflict error will be returned.
   108  //
   109  // Users with an admin role can create VIPs on behalf of other tenants by
   110  // specifying a TenantID attribute different than their own.
   111  func Create(c *gophercloud.ServiceClient, opts CreateOpts) (r CreateResult) {
   112  	b, err := opts.ToVIPCreateMap()
   113  	if err != nil {
   114  		r.Err = err
   115  		return
   116  	}
   117  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
   118  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   119  	return
   120  }
   121  
   122  // Get retrieves a particular virtual IP based on its unique ID.
   123  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
   124  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
   125  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   126  	return
   127  }
   128  
   129  // UpdateOptsBuilder allows extensions to add additional parameters to the
   130  // Update request.
   131  type UpdateOptsBuilder interface {
   132  	ToVIPUpdateMap() (map[string]interface{}, error)
   133  }
   134  
   135  // UpdateOpts contains all the values needed to update an existing virtual IP.
   136  // Attributes not listed here but appear in CreateOpts are immutable and cannot
   137  // be updated.
   138  type UpdateOpts struct {
   139  	// Name is the human-readable name for the VIP. Does not have to be unique.
   140  	Name *string `json:"name,omitempty"`
   141  
   142  	// PoolID is the ID of the pool with which the VIP is associated.
   143  	PoolID *string `json:"pool_id,omitempty"`
   144  
   145  	// Description is the human-readable description for the VIP.
   146  	Description *string `json:"description,omitempty"`
   147  
   148  	// Persistence is the the of session persistence to use.
   149  	// Omit this field to prevent session persistence.
   150  	Persistence *SessionPersistence `json:"session_persistence,omitempty"`
   151  
   152  	// ConnLimit is the maximum number of connections allowed for the VIP.
   153  	ConnLimit *int `json:"connection_limit,omitempty"`
   154  
   155  	// AdminStateUp is the administrative state of the VIP. A valid value is
   156  	// true (UP) or false (DOWN).
   157  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   158  }
   159  
   160  // ToVIPUpdateMap builds a request body based on UpdateOpts.
   161  func (opts UpdateOpts) ToVIPUpdateMap() (map[string]interface{}, error) {
   162  	return gophercloud.BuildRequestBody(opts, "vip")
   163  }
   164  
   165  // Update is an operation which modifies the attributes of the specified VIP.
   166  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   167  	b, err := opts.ToVIPUpdateMap()
   168  	if err != nil {
   169  		r.Err = err
   170  		return
   171  	}
   172  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   173  		OkCodes: []int{200, 202},
   174  	})
   175  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   176  	return
   177  }
   178  
   179  // Delete will permanently delete a particular virtual IP based on its unique ID.
   180  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   181  	resp, err := c.Delete(resourceURL(c, id), nil)
   182  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   183  	return
   184  }