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

     1  package l7policies
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // CreateOptsBuilder allows extensions to add additional parameters to the
     9  // Create request.
    10  type CreateOptsBuilder interface {
    11  	ToL7PolicyCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  type Action string
    15  type RuleType string
    16  type CompareType string
    17  
    18  const (
    19  	ActionRedirectPrefix Action = "REDIRECT_PREFIX"
    20  	ActionRedirectToPool Action = "REDIRECT_TO_POOL"
    21  	ActionRedirectToURL  Action = "REDIRECT_TO_URL"
    22  	ActionReject         Action = "REJECT"
    23  
    24  	TypeCookie   RuleType = "COOKIE"
    25  	TypeFileType RuleType = "FILE_TYPE"
    26  	TypeHeader   RuleType = "HEADER"
    27  	TypeHostName RuleType = "HOST_NAME"
    28  	TypePath     RuleType = "PATH"
    29  
    30  	CompareTypeContains  CompareType = "CONTAINS"
    31  	CompareTypeEndWith   CompareType = "ENDS_WITH"
    32  	CompareTypeEqual     CompareType = "EQUAL_TO"
    33  	CompareTypeRegex     CompareType = "REGEX"
    34  	CompareTypeStartWith CompareType = "STARTS_WITH"
    35  )
    36  
    37  // CreateOpts is the common options struct used in this package's Create
    38  // operation.
    39  type CreateOpts struct {
    40  	// Name of the L7 policy.
    41  	Name string `json:"name,omitempty"`
    42  
    43  	// The ID of the listener.
    44  	ListenerID string `json:"listener_id,omitempty"`
    45  
    46  	// The L7 policy action. One of REDIRECT_PREFIX, REDIRECT_TO_POOL, REDIRECT_TO_URL, or REJECT.
    47  	Action Action `json:"action" required:"true"`
    48  
    49  	// The position of this policy on the listener.
    50  	Position int32 `json:"position,omitempty"`
    51  
    52  	// A human-readable description for the resource.
    53  	Description string `json:"description,omitempty"`
    54  
    55  	// ProjectID is the UUID of the project who owns the L7 policy in octavia.
    56  	// Only administrative users can specify a project UUID other than their own.
    57  	ProjectID string `json:"project_id,omitempty"`
    58  
    59  	// Requests matching this policy will be redirected to this Prefix URL.
    60  	// Only valid if action is REDIRECT_PREFIX.
    61  	RedirectPrefix string `json:"redirect_prefix,omitempty"`
    62  
    63  	// Requests matching this policy will be redirected to the pool with this ID.
    64  	// Only valid if action is REDIRECT_TO_POOL.
    65  	RedirectPoolID string `json:"redirect_pool_id,omitempty"`
    66  
    67  	// Requests matching this policy will be redirected to this URL.
    68  	// Only valid if action is REDIRECT_TO_URL.
    69  	RedirectURL string `json:"redirect_url,omitempty"`
    70  
    71  	// Requests matching this policy will be redirected to the specified URL or Prefix URL
    72  	// with the HTTP response code. Valid if action is REDIRECT_TO_URL or REDIRECT_PREFIX.
    73  	// Valid options are: 301, 302, 303, 307, or 308. Default is 302. Requires version 2.9
    74  	RedirectHttpCode int32 `json:"redirect_http_code,omitempty"`
    75  
    76  	// The administrative state of the Loadbalancer. A valid value is true (UP)
    77  	// or false (DOWN).
    78  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
    79  
    80  	// Rules is a slice of CreateRuleOpts which allows a set of rules
    81  	// to be created at the same time the policy is created.
    82  	//
    83  	// This is only possible to use when creating a fully populated
    84  	// Loadbalancer.
    85  	Rules []CreateRuleOpts `json:"rules,omitempty"`
    86  }
    87  
    88  // ToL7PolicyCreateMap builds a request body from CreateOpts.
    89  func (opts CreateOpts) ToL7PolicyCreateMap() (map[string]interface{}, error) {
    90  	return gophercloud.BuildRequestBody(opts, "l7policy")
    91  }
    92  
    93  // Create accepts a CreateOpts struct and uses the values to create a new l7policy.
    94  func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    95  	b, err := opts.ToL7PolicyCreateMap()
    96  	if err != nil {
    97  		r.Err = err
    98  		return
    99  	}
   100  	resp, err := c.Post(rootURL(c), b, &r.Body, nil)
   101  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   102  	return
   103  }
   104  
   105  // ListOptsBuilder allows extensions to add additional parameters to the
   106  // List request.
   107  type ListOptsBuilder interface {
   108  	ToL7PolicyListQuery() (string, error)
   109  }
   110  
   111  // ListOpts allows the filtering and sorting of paginated collections through
   112  // the API.
   113  type ListOpts struct {
   114  	Name           string `q:"name"`
   115  	Description    string `q:"description"`
   116  	ListenerID     string `q:"listener_id"`
   117  	Action         string `q:"action"`
   118  	ProjectID      string `q:"project_id"`
   119  	RedirectPoolID string `q:"redirect_pool_id"`
   120  	RedirectURL    string `q:"redirect_url"`
   121  	Position       int32  `q:"position"`
   122  	AdminStateUp   bool   `q:"admin_state_up"`
   123  	ID             string `q:"id"`
   124  	Limit          int    `q:"limit"`
   125  	Marker         string `q:"marker"`
   126  	SortKey        string `q:"sort_key"`
   127  	SortDir        string `q:"sort_dir"`
   128  }
   129  
   130  // ToL7PolicyListQuery formats a ListOpts into a query string.
   131  func (opts ListOpts) ToL7PolicyListQuery() (string, error) {
   132  	q, err := gophercloud.BuildQueryString(opts)
   133  	return q.String(), err
   134  }
   135  
   136  // List returns a Pager which allows you to iterate over a collection of
   137  // l7policies. It accepts a ListOpts struct, which allows you to filter and sort
   138  // the returned collection for greater efficiency.
   139  //
   140  // Default policy settings return only those l7policies that are owned by the
   141  // project who submits the request, unless an admin user submits the request.
   142  func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   143  	url := rootURL(c)
   144  	if opts != nil {
   145  		query, err := opts.ToL7PolicyListQuery()
   146  		if err != nil {
   147  			return pagination.Pager{Err: err}
   148  		}
   149  		url += query
   150  	}
   151  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   152  		return L7PolicyPage{pagination.LinkedPageBase{PageResult: r}}
   153  	})
   154  }
   155  
   156  // Get retrieves a particular l7policy based on its unique ID.
   157  func Get(c *gophercloud.ServiceClient, id string) (r GetResult) {
   158  	resp, err := c.Get(resourceURL(c, id), &r.Body, nil)
   159  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   160  	return
   161  }
   162  
   163  // Delete will permanently delete a particular l7policy based on its unique ID.
   164  func Delete(c *gophercloud.ServiceClient, id string) (r DeleteResult) {
   165  	resp, err := c.Delete(resourceURL(c, id), nil)
   166  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   167  	return
   168  }
   169  
   170  // UpdateOptsBuilder allows extensions to add additional parameters to the
   171  // Update request.
   172  type UpdateOptsBuilder interface {
   173  	ToL7PolicyUpdateMap() (map[string]interface{}, error)
   174  }
   175  
   176  // UpdateOpts is the common options struct used in this package's Update
   177  // operation.
   178  type UpdateOpts struct {
   179  	// Name of the L7 policy, empty string is allowed.
   180  	Name *string `json:"name,omitempty"`
   181  
   182  	// The L7 policy action. One of REDIRECT_PREFIX, REDIRECT_TO_POOL, REDIRECT_TO_URL, or REJECT.
   183  	Action Action `json:"action,omitempty"`
   184  
   185  	// The position of this policy on the listener.
   186  	Position int32 `json:"position,omitempty"`
   187  
   188  	// A human-readable description for the resource, empty string is allowed.
   189  	Description *string `json:"description,omitempty"`
   190  
   191  	// Requests matching this policy will be redirected to this Prefix URL.
   192  	// Only valid if action is REDIRECT_PREFIX.
   193  	RedirectPrefix *string `json:"redirect_prefix,omitempty"`
   194  
   195  	// Requests matching this policy will be redirected to the pool with this ID.
   196  	// Only valid if action is REDIRECT_TO_POOL.
   197  	RedirectPoolID *string `json:"redirect_pool_id,omitempty"`
   198  
   199  	// Requests matching this policy will be redirected to this URL.
   200  	// Only valid if action is REDIRECT_TO_URL.
   201  	RedirectURL *string `json:"redirect_url,omitempty"`
   202  
   203  	// Requests matching this policy will be redirected to the specified URL or Prefix URL
   204  	// with the HTTP response code. Valid if action is REDIRECT_TO_URL or REDIRECT_PREFIX.
   205  	// Valid options are: 301, 302, 303, 307, or 308. Default is 302. Requires version 2.9
   206  	RedirectHttpCode int32 `json:"redirect_http_code,omitempty"`
   207  
   208  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   209  	// or false (DOWN).
   210  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   211  }
   212  
   213  // ToL7PolicyUpdateMap builds a request body from UpdateOpts.
   214  func (opts UpdateOpts) ToL7PolicyUpdateMap() (map[string]interface{}, error) {
   215  	b, err := gophercloud.BuildRequestBody(opts, "l7policy")
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  
   220  	m := b["l7policy"].(map[string]interface{})
   221  
   222  	if m["redirect_pool_id"] == "" {
   223  		m["redirect_pool_id"] = nil
   224  	}
   225  
   226  	if m["redirect_url"] == "" {
   227  		m["redirect_url"] = nil
   228  	}
   229  
   230  	if m["redirect_prefix"] == "" {
   231  		m["redirect_prefix"] = nil
   232  	}
   233  
   234  	if m["redirect_http_code"] == 0 {
   235  		m["redirect_http_code"] = nil
   236  	}
   237  
   238  	return b, nil
   239  }
   240  
   241  // Update allows l7policy to be updated.
   242  func Update(c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   243  	b, err := opts.ToL7PolicyUpdateMap()
   244  	if err != nil {
   245  		r.Err = err
   246  		return
   247  	}
   248  	resp, err := c.Put(resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{
   249  		OkCodes: []int{200},
   250  	})
   251  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   252  	return
   253  }
   254  
   255  // CreateRuleOpts is the common options struct used in this package's CreateRule
   256  // operation.
   257  type CreateRuleOpts struct {
   258  	// The L7 rule type. One of COOKIE, FILE_TYPE, HEADER, HOST_NAME, or PATH.
   259  	RuleType RuleType `json:"type" required:"true"`
   260  
   261  	// The comparison type for the L7 rule. One of CONTAINS, ENDS_WITH, EQUAL_TO, REGEX, or STARTS_WITH.
   262  	CompareType CompareType `json:"compare_type" required:"true"`
   263  
   264  	// The value to use for the comparison. For example, the file type to compare.
   265  	Value string `json:"value" required:"true"`
   266  
   267  	// ProjectID is the UUID of the project who owns the rule in octavia.
   268  	// Only administrative users can specify a project UUID other than their own.
   269  	ProjectID string `json:"project_id,omitempty"`
   270  
   271  	// The key to use for the comparison. For example, the name of the cookie to evaluate.
   272  	Key string `json:"key,omitempty"`
   273  
   274  	// When true the logic of the rule is inverted. For example, with invert true,
   275  	// equal to would become not equal to. Default is false.
   276  	Invert bool `json:"invert,omitempty"`
   277  
   278  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   279  	// or false (DOWN).
   280  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   281  }
   282  
   283  // ToRuleCreateMap builds a request body from CreateRuleOpts.
   284  func (opts CreateRuleOpts) ToRuleCreateMap() (map[string]interface{}, error) {
   285  	return gophercloud.BuildRequestBody(opts, "rule")
   286  }
   287  
   288  // CreateRule will create and associate a Rule with a particular L7Policy.
   289  func CreateRule(c *gophercloud.ServiceClient, policyID string, opts CreateRuleOpts) (r CreateRuleResult) {
   290  	b, err := opts.ToRuleCreateMap()
   291  	if err != nil {
   292  		r.Err = err
   293  		return
   294  	}
   295  	resp, err := c.Post(ruleRootURL(c, policyID), b, &r.Body, nil)
   296  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   297  	return
   298  }
   299  
   300  // ListRulesOptsBuilder allows extensions to add additional parameters to the
   301  // ListRules request.
   302  type ListRulesOptsBuilder interface {
   303  	ToRulesListQuery() (string, error)
   304  }
   305  
   306  // ListRulesOpts allows the filtering and sorting of paginated collections
   307  // through the API.
   308  type ListRulesOpts struct {
   309  	RuleType     RuleType    `q:"type"`
   310  	ProjectID    string      `q:"project_id"`
   311  	CompareType  CompareType `q:"compare_type"`
   312  	Value        string      `q:"value"`
   313  	Key          string      `q:"key"`
   314  	Invert       bool        `q:"invert"`
   315  	AdminStateUp bool        `q:"admin_state_up"`
   316  	ID           string      `q:"id"`
   317  	Limit        int         `q:"limit"`
   318  	Marker       string      `q:"marker"`
   319  	SortKey      string      `q:"sort_key"`
   320  	SortDir      string      `q:"sort_dir"`
   321  }
   322  
   323  // ToRulesListQuery formats a ListOpts into a query string.
   324  func (opts ListRulesOpts) ToRulesListQuery() (string, error) {
   325  	q, err := gophercloud.BuildQueryString(opts)
   326  	return q.String(), err
   327  }
   328  
   329  // ListRules returns a Pager which allows you to iterate over a collection of
   330  // rules. It accepts a ListRulesOptsBuilder, which allows you to filter and
   331  // sort the returned collection for greater efficiency.
   332  //
   333  // Default policy settings return only those rules that are owned by the
   334  // project who submits the request, unless an admin user submits the request.
   335  func ListRules(c *gophercloud.ServiceClient, policyID string, opts ListRulesOptsBuilder) pagination.Pager {
   336  	url := ruleRootURL(c, policyID)
   337  	if opts != nil {
   338  		query, err := opts.ToRulesListQuery()
   339  		if err != nil {
   340  			return pagination.Pager{Err: err}
   341  		}
   342  		url += query
   343  	}
   344  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   345  		return RulePage{pagination.LinkedPageBase{PageResult: r}}
   346  	})
   347  }
   348  
   349  // GetRule retrieves a particular L7Policy Rule based on its unique ID.
   350  func GetRule(c *gophercloud.ServiceClient, policyID string, ruleID string) (r GetRuleResult) {
   351  	resp, err := c.Get(ruleResourceURL(c, policyID, ruleID), &r.Body, nil)
   352  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   353  	return
   354  }
   355  
   356  // DeleteRule will remove a Rule from a particular L7Policy.
   357  func DeleteRule(c *gophercloud.ServiceClient, policyID string, ruleID string) (r DeleteRuleResult) {
   358  	resp, err := c.Delete(ruleResourceURL(c, policyID, ruleID), nil)
   359  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   360  	return
   361  }
   362  
   363  // UpdateRuleOptsBuilder allows to add additional parameters to the PUT request.
   364  type UpdateRuleOptsBuilder interface {
   365  	ToRuleUpdateMap() (map[string]interface{}, error)
   366  }
   367  
   368  // UpdateRuleOpts is the common options struct used in this package's Update
   369  // operation.
   370  type UpdateRuleOpts struct {
   371  	// The L7 rule type. One of COOKIE, FILE_TYPE, HEADER, HOST_NAME, or PATH.
   372  	RuleType RuleType `json:"type,omitempty"`
   373  
   374  	// The comparison type for the L7 rule. One of CONTAINS, ENDS_WITH, EQUAL_TO, REGEX, or STARTS_WITH.
   375  	CompareType CompareType `json:"compare_type,omitempty"`
   376  
   377  	// The value to use for the comparison. For example, the file type to compare.
   378  	Value string `json:"value,omitempty"`
   379  
   380  	// The key to use for the comparison. For example, the name of the cookie to evaluate.
   381  	Key *string `json:"key,omitempty"`
   382  
   383  	// When true the logic of the rule is inverted. For example, with invert true,
   384  	// equal to would become not equal to. Default is false.
   385  	Invert *bool `json:"invert,omitempty"`
   386  
   387  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   388  	// or false (DOWN).
   389  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   390  }
   391  
   392  // ToRuleUpdateMap builds a request body from UpdateRuleOpts.
   393  func (opts UpdateRuleOpts) ToRuleUpdateMap() (map[string]interface{}, error) {
   394  	b, err := gophercloud.BuildRequestBody(opts, "rule")
   395  	if err != nil {
   396  		return nil, err
   397  	}
   398  
   399  	if m := b["rule"].(map[string]interface{}); m["key"] == "" {
   400  		m["key"] = nil
   401  	}
   402  
   403  	return b, nil
   404  }
   405  
   406  // UpdateRule allows Rule to be updated.
   407  func UpdateRule(c *gophercloud.ServiceClient, policyID string, ruleID string, opts UpdateRuleOptsBuilder) (r UpdateRuleResult) {
   408  	b, err := opts.ToRuleUpdateMap()
   409  	if err != nil {
   410  		r.Err = err
   411  		return
   412  	}
   413  	resp, err := c.Put(ruleResourceURL(c, policyID, ruleID), b, &r.Body, &gophercloud.RequestOpts{
   414  		OkCodes: []int{200, 201, 202},
   415  	})
   416  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   417  	return
   418  }