github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/elb/v3/l7policies/requests.go (about)

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