github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/listeners/requests.go (about)

     1  package listeners
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/l7policies"
     6  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
     7  	"github.com/gophercloud/gophercloud/pagination"
     8  )
     9  
    10  // Type Protocol represents a listener protocol.
    11  type Protocol string
    12  
    13  // Supported attributes for create/update operations.
    14  const (
    15  	ProtocolTCP   Protocol = "TCP"
    16  	ProtocolUDP   Protocol = "UDP"
    17  	ProtocolPROXY Protocol = "PROXY"
    18  	ProtocolHTTP  Protocol = "HTTP"
    19  	ProtocolHTTPS Protocol = "HTTPS"
    20  	// Protocol SCTP requires octavia microversion 2.23
    21  	ProtocolSCTP Protocol = "SCTP"
    22  	// Protocol Prometheus requires octavia microversion 2.25
    23  	ProtocolPrometheus      Protocol = "PROMETHEUS"
    24  	ProtocolTerminatedHTTPS Protocol = "TERMINATED_HTTPS"
    25  )
    26  
    27  // Type TLSVersion represents a tls version
    28  type TLSVersion string
    29  
    30  const (
    31  	TLSVersionSSLv3   TLSVersion = "SSLv3"
    32  	TLSVersionTLSv1   TLSVersion = "TLSv1"
    33  	TLSVersionTLSv1_1 TLSVersion = "TLSv1.1"
    34  	TLSVersionTLSv1_2 TLSVersion = "TLSv1.2"
    35  	TLSVersionTLSv1_3 TLSVersion = "TLSv1.3"
    36  )
    37  
    38  // ListOptsBuilder allows extensions to add additional parameters to the
    39  // List request.
    40  type ListOptsBuilder interface {
    41  	ToListenerListQuery() (string, error)
    42  }
    43  
    44  // ListOpts allows the filtering and sorting of paginated collections through
    45  // the API. Filtering is achieved by passing in struct field values that map to
    46  // the floating IP attributes you want to see returned. SortKey allows you to
    47  // sort by a particular listener attribute. SortDir sets the direction, and is
    48  // either `asc' or `desc'. Marker and Limit are used for pagination.
    49  type ListOpts struct {
    50  	ID                   string `q:"id"`
    51  	Name                 string `q:"name"`
    52  	AdminStateUp         *bool  `q:"admin_state_up"`
    53  	ProjectID            string `q:"project_id"`
    54  	LoadbalancerID       string `q:"loadbalancer_id"`
    55  	DefaultPoolID        string `q:"default_pool_id"`
    56  	Protocol             string `q:"protocol"`
    57  	ProtocolPort         int    `q:"protocol_port"`
    58  	ConnectionLimit      int    `q:"connection_limit"`
    59  	Limit                int    `q:"limit"`
    60  	Marker               string `q:"marker"`
    61  	SortKey              string `q:"sort_key"`
    62  	SortDir              string `q:"sort_dir"`
    63  	TimeoutClientData    *int   `q:"timeout_client_data"`
    64  	TimeoutMemberData    *int   `q:"timeout_member_data"`
    65  	TimeoutMemberConnect *int   `q:"timeout_member_connect"`
    66  	TimeoutTCPInspect    *int   `q:"timeout_tcp_inspect"`
    67  }
    68  
    69  // ToListenerListQuery formats a ListOpts into a query string.
    70  func (opts ListOpts) ToListenerListQuery() (string, error) {
    71  	q, err := gophercloud.BuildQueryString(opts)
    72  	return q.String(), err
    73  }
    74  
    75  // List returns a Pager which allows you to iterate over a collection of
    76  // listeners. It accepts a ListOpts struct, which allows you to filter and sort
    77  // the returned collection for greater efficiency.
    78  //
    79  // Default policy settings return only those listeners that are owned by the
    80  // project who submits the request, unless an admin user submits the request.
    81  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    82  	url := rootURL(c)
    83  	if opts != nil {
    84  		query, err := opts.ToListenerListQuery()
    85  		if err != nil {
    86  			return pagination.Pager{Err: err}
    87  		}
    88  		url += query
    89  	}
    90  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    91  		return ListenerPage{pagination.LinkedPageBase{PageResult: r}}
    92  	})
    93  }
    94  
    95  // CreateOptsBuilder allows extensions to add additional parameters to the
    96  // Create request.
    97  type CreateOptsBuilder interface {
    98  	ToListenerCreateMap() (map[string]interface{}, error)
    99  }
   100  
   101  // CreateOpts represents options for creating a listener.
   102  type CreateOpts struct {
   103  	// The load balancer on which to provision this listener.
   104  	LoadbalancerID string `json:"loadbalancer_id,omitempty"`
   105  
   106  	// The protocol - can either be TCP, SCTP, HTTP, HTTPS or TERMINATED_HTTPS.
   107  	Protocol Protocol `json:"protocol" required:"true"`
   108  
   109  	// The port on which to listen for client traffic.
   110  	ProtocolPort int `json:"protocol_port" required:"true"`
   111  
   112  	// ProjectID is only required if the caller has an admin role and wants
   113  	// to create a pool for another project.
   114  	ProjectID string `json:"project_id,omitempty"`
   115  
   116  	// Human-readable name for the Listener. Does not have to be unique.
   117  	Name string `json:"name,omitempty"`
   118  
   119  	// The ID of the default pool with which the Listener is associated.
   120  	DefaultPoolID string `json:"default_pool_id,omitempty"`
   121  
   122  	// DefaultPool an instance of pools.CreateOpts which allows a
   123  	// (default) pool to be created at the same time the listener is created.
   124  	//
   125  	// This is only possible to use when creating a fully populated
   126  	// load balancer.
   127  	DefaultPool *pools.CreateOpts `json:"default_pool,omitempty"`
   128  
   129  	// Human-readable description for the Listener.
   130  	Description string `json:"description,omitempty"`
   131  
   132  	// The maximum number of connections allowed for the Listener.
   133  	ConnLimit *int `json:"connection_limit,omitempty"`
   134  
   135  	// A reference to a Barbican container of TLS secrets.
   136  	DefaultTlsContainerRef string `json:"default_tls_container_ref,omitempty"`
   137  
   138  	// A list of references to TLS secrets.
   139  	SniContainerRefs []string `json:"sni_container_refs,omitempty"`
   140  
   141  	// The administrative state of the Listener. A valid value is true (UP)
   142  	// or false (DOWN).
   143  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   144  
   145  	// L7Policies is a slice of l7policies.CreateOpts which allows a set
   146  	// of policies to be created at the same time the listener is created.
   147  	//
   148  	// This is only possible to use when creating a fully populated
   149  	// Loadbalancer.
   150  	L7Policies []l7policies.CreateOpts `json:"l7policies,omitempty"`
   151  
   152  	// Frontend client inactivity timeout in milliseconds
   153  	TimeoutClientData *int `json:"timeout_client_data,omitempty"`
   154  
   155  	// Backend member inactivity timeout in milliseconds
   156  	TimeoutMemberData *int `json:"timeout_member_data,omitempty"`
   157  
   158  	// Backend member connection timeout in milliseconds
   159  	TimeoutMemberConnect *int `json:"timeout_member_connect,omitempty"`
   160  
   161  	// Time, in milliseconds, to wait for additional TCP packets for content inspection
   162  	TimeoutTCPInspect *int `json:"timeout_tcp_inspect,omitempty"`
   163  
   164  	// A dictionary of optional headers to insert into the request before it is sent to the backend member.
   165  	InsertHeaders map[string]string `json:"insert_headers,omitempty"`
   166  
   167  	// A list of IPv4, IPv6 or mix of both CIDRs
   168  	AllowedCIDRs []string `json:"allowed_cidrs,omitempty"`
   169  
   170  	// A list of TLS protocol versions. Available from microversion 2.17
   171  	TLSVersions []TLSVersion `json:"tls_versions,omitempty"`
   172  
   173  	// Tags is a set of resource tags. New in version 2.5
   174  	Tags []string `json:"tags,omitempty"`
   175  }
   176  
   177  // ToListenerCreateMap builds a request body from CreateOpts.
   178  func (opts CreateOpts) ToListenerCreateMap() (map[string]interface{}, error) {
   179  	return gophercloud.BuildRequestBody(opts, "listener")
   180  }
   181  
   182  // Create is an operation which provisions a new Listeners based on the
   183  // configuration defined in the CreateOpts struct. Once the request is
   184  // validated and progress has started on the provisioning process, a
   185  // CreateResult will be returned.
   186  //
   187  // Users with an admin role can create Listeners on behalf of other projects by
   188  // specifying a ProjectID attribute different than their own.
   189  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   190  	b, err := opts.ToListenerCreateMap()
   191  	if err != nil {
   192  		r.Err = err
   193  		return
   194  	}
   195  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
   196  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   197  	return
   198  }
   199  
   200  // Get retrieves a particular Listeners based on its unique ID.
   201  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
   202  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
   203  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   204  	return
   205  }
   206  
   207  // UpdateOptsBuilder allows extensions to add additional parameters to the
   208  // Update request.
   209  type UpdateOptsBuilder interface {
   210  	ToListenerUpdateMap() (map[string]interface{}, error)
   211  }
   212  
   213  // UpdateOpts represents options for updating a Listener.
   214  type UpdateOpts struct {
   215  	// Human-readable name for the Listener. Does not have to be unique.
   216  	Name *string `json:"name,omitempty"`
   217  
   218  	// The ID of the default pool with which the Listener is associated.
   219  	DefaultPoolID *string `json:"default_pool_id,omitempty"`
   220  
   221  	// Human-readable description for the Listener.
   222  	Description *string `json:"description,omitempty"`
   223  
   224  	// The maximum number of connections allowed for the Listener.
   225  	ConnLimit *int `json:"connection_limit,omitempty"`
   226  
   227  	// A reference to a Barbican container of TLS secrets.
   228  	DefaultTlsContainerRef *string `json:"default_tls_container_ref,omitempty"`
   229  
   230  	// A list of references to TLS secrets.
   231  	SniContainerRefs *[]string `json:"sni_container_refs,omitempty"`
   232  
   233  	// The administrative state of the Listener. A valid value is true (UP)
   234  	// or false (DOWN).
   235  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   236  
   237  	// Frontend client inactivity timeout in milliseconds
   238  	TimeoutClientData *int `json:"timeout_client_data,omitempty"`
   239  
   240  	// Backend member inactivity timeout in milliseconds
   241  	TimeoutMemberData *int `json:"timeout_member_data,omitempty"`
   242  
   243  	// Backend member connection timeout in milliseconds
   244  	TimeoutMemberConnect *int `json:"timeout_member_connect,omitempty"`
   245  
   246  	// Time, in milliseconds, to wait for additional TCP packets for content inspection
   247  	TimeoutTCPInspect *int `json:"timeout_tcp_inspect,omitempty"`
   248  
   249  	// A dictionary of optional headers to insert into the request before it is sent to the backend member.
   250  	InsertHeaders *map[string]string `json:"insert_headers,omitempty"`
   251  
   252  	// A list of IPv4, IPv6 or mix of both CIDRs
   253  	AllowedCIDRs *[]string `json:"allowed_cidrs,omitempty"`
   254  
   255  	// A list of TLS protocol versions. Available from microversion 2.17
   256  	TLSVersions *[]TLSVersion `json:"tls_versions,omitempty"`
   257  
   258  	// Tags is a set of resource tags. New in version 2.5
   259  	Tags *[]string `json:"tags,omitempty"`
   260  }
   261  
   262  // ToListenerUpdateMap builds a request body from UpdateOpts.
   263  func (opts UpdateOpts) ToListenerUpdateMap() (map[string]interface{}, error) {
   264  	b, err := gophercloud.BuildRequestBody(opts, "listener")
   265  	if err != nil {
   266  		return nil, err
   267  	}
   268  
   269  	if m := b["listener"].(map[string]interface{}); m["default_pool_id"] == "" {
   270  		m["default_pool_id"] = nil
   271  	}
   272  
   273  	return b, nil
   274  }
   275  
   276  // Update is an operation which modifies the attributes of the specified
   277  // Listener.
   278  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOpts) (r UpdateResult) {
   279  	b, err := opts.ToListenerUpdateMap()
   280  	if err != nil {
   281  		r.Err = err
   282  		return
   283  	}
   284  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   285  		OkCodes: []int{200, 202},
   286  	})
   287  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   288  	return
   289  }
   290  
   291  // Delete will permanently delete a particular Listeners based on its unique ID.
   292  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   293  	resp, err := c.Delete(resourceURL(c, id), nil)
   294  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   295  	return
   296  }
   297  
   298  // GetStats will return the shows the current statistics of a particular Listeners.
   299  func GetStats(c *gophercloud.ServiceClient, id string) (r StatsResult) {
   300  	resp, err := c.Get(statisticsRootURL(c, id), &r.Body, nil)
   301  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   302  	return
   303  }