github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/elb/v3/l7policies/requests.go (about)

     1  package l7policies
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/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  	// Requests matching this policy will be redirected to the URL.
    66  	// Only valid if action is REDIRECT_TO_URL.
    67  	RedirectUrlConfig *RedirectUrlConfig `json:"redirect_url_config,omitempty"`
    68  
    69  	// Requests matching this policy will be redirected to the configuration of the page.
    70  	// Only valid if action is FIXED_RESPONSE.
    71  	FixedResponseConfig *FixedResponseConfig `json:"fixed_response_config,omitempty"`
    72  
    73  	// The config of the redirected pool.
    74  	// Only valid if action is REDIRECT_TO_POOL.
    75  	RedirectPoolsExtendConfig *RedirectPoolsExtendConfig `json:"redirect_pools_extend_config,omitempty"`
    76  
    77  	// The administrative state of the Loadbalancer. A valid value is true (UP)
    78  	// or false (DOWN).
    79  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
    80  }
    81  
    82  type RedirectUrlConfig struct {
    83  	// The protocol for redirection.
    84  	Protocol string `json:"protocol,omitempty"`
    85  
    86  	// The host name that requests are redirected to.
    87  	Host string `json:"host,omitempty"`
    88  
    89  	// The port that requests are redirected to.
    90  	Port string `json:"port,omitempty"`
    91  
    92  	// The path that requests are redirected to.
    93  	Path string `json:"path,omitempty"`
    94  
    95  	// The query string set in the URL for redirection.
    96  	Query string `json:"query"`
    97  
    98  	// The status code returned after the requests are redirected.
    99  	StatusCode string `json:"status_code" required:"true"`
   100  }
   101  
   102  type FixedResponseConfig struct {
   103  	// The fixed HTTP status code configured in the forwarding rule.
   104  	StatusCode string `json:"status_code" required:"true"`
   105  
   106  	// The format of the response body.
   107  	ContentType string `json:"content_type,omitempty"`
   108  
   109  	// The content of the response message body.
   110  	MessageBody string `json:"message_body"`
   111  }
   112  
   113  type RedirectPoolsExtendConfig struct {
   114  	// Whether the rewriter url enable
   115  	RewriteUrlEnable bool `json:"rewrite_url_enable,omitempty"`
   116  
   117  	// The rewriter url config
   118  	RewriteUrlConfig *RewriteUrlConfig `json:"rewrite_url_config,omitempty"`
   119  }
   120  
   121  type RewriteUrlConfig struct {
   122  	// The host of the rewriter url
   123  	Host string `json:"host,omitempty"`
   124  
   125  	// The path that requests are redirected to.
   126  	Path string `json:"path,omitempty"`
   127  
   128  	// The query string set in the URL for redirection.
   129  	Query string `json:"query"`
   130  }
   131  
   132  // ToL7PolicyCreateMap builds a request body from CreateOpts.
   133  func (opts CreateOpts) ToL7PolicyCreateMap() (map[string]interface{}, error) {
   134  	return golangsdk.BuildRequestBody(opts, "l7policy")
   135  }
   136  
   137  // Create accepts a CreateOpts struct and uses the values to create a new l7policy.
   138  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   139  	b, err := opts.ToL7PolicyCreateMap()
   140  	if err != nil {
   141  		r.Err = err
   142  		return
   143  	}
   144  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
   145  	return
   146  }
   147  
   148  // ListOptsBuilder allows extensions to add additional parameters to the
   149  // List request.
   150  type ListOptsBuilder interface {
   151  	ToL7PolicyListQuery() (string, error)
   152  }
   153  
   154  // ListOpts allows the filtering and sorting of paginated collections through
   155  // the API.
   156  type ListOpts struct {
   157  	Name               string `q:"name"`
   158  	Description        string `q:"description"`
   159  	ListenerID         string `q:"listener_id"`
   160  	Action             string `q:"action"`
   161  	TenantID           string `q:"tenant_id"`
   162  	RedirectPoolID     string `q:"redirect_pool_id"`
   163  	RedirectListenerID string `q:"redirect_listener_id"`
   164  	Position           int32  `q:"position"`
   165  	AdminStateUp       bool   `q:"admin_state_up"`
   166  	ID                 string `q:"id"`
   167  	Limit              int    `q:"limit"`
   168  	Marker             string `q:"marker"`
   169  	SortKey            string `q:"sort_key"`
   170  	SortDir            string `q:"sort_dir"`
   171  }
   172  
   173  // ToL7PolicyListQuery formats a ListOpts into a query string.
   174  func (opts ListOpts) ToL7PolicyListQuery() (string, error) {
   175  	q, err := golangsdk.BuildQueryString(opts)
   176  	return q.String(), err
   177  }
   178  
   179  // List returns a Pager which allows you to iterate over a collection of
   180  // l7policies. It accepts a ListOpts struct, which allows you to filter and sort
   181  // the returned collection for greater efficiency.
   182  //
   183  // Default policy settings return only those l7policies that are owned by the
   184  // project who submits the request, unless an admin user submits the request.
   185  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   186  	url := rootURL(c)
   187  	if opts != nil {
   188  		query, err := opts.ToL7PolicyListQuery()
   189  		if err != nil {
   190  			return pagination.Pager{Err: err}
   191  		}
   192  		url += query
   193  	}
   194  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   195  		return L7PolicyPage{pagination.LinkedPageBase{PageResult: r}}
   196  	})
   197  }
   198  
   199  // Get retrieves a particular l7policy based on its unique ID.
   200  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   201  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
   202  	return
   203  }
   204  
   205  // Delete will permanently delete a particular l7policy based on its unique ID.
   206  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
   207  	_, r.Err = c.Delete(resourceURL(c, id), nil)
   208  	return
   209  }
   210  
   211  // UpdateOptsBuilder allows extensions to add additional parameters to the
   212  // Update request.
   213  type UpdateOptsBuilder interface {
   214  	ToL7PolicyUpdateMap() (map[string]interface{}, error)
   215  }
   216  
   217  // UpdateOpts is the common options struct used in this package's Update
   218  // operation.
   219  type UpdateOpts struct {
   220  	// Name of the L7 policy, empty string is allowed.
   221  	Name *string `json:"name,omitempty"`
   222  
   223  	// The L7 policy action. One of REDIRECT_TO_POOL, REDIRECT_TO_URL, or REJECT.
   224  	Action Action `json:"action,omitempty"`
   225  
   226  	// The position of this policy on the listener.
   227  	Position int32 `json:"position,omitempty"`
   228  
   229  	// The priority of this policy on the listener.
   230  	Priority int32 `json:"priority,omitempty"`
   231  
   232  	// A human-readable description for the resource, empty string is allowed.
   233  	Description *string `json:"description,omitempty"`
   234  
   235  	// Requests matching this policy will be redirected to the pool with this ID.
   236  	// Only valid if action is REDIRECT_TO_POOL.
   237  	RedirectPoolID *string `json:"redirect_pool_id,omitempty"`
   238  
   239  	// Requests matching this policy will be redirected to this LISTENER.
   240  	// Only valid if action is REDIRECT_TO_LISTENER.
   241  	RedirectListenerID *string `json:"redirect_listener_id,omitempty"`
   242  
   243  	// Requests matching this policy will be redirected to the URL.
   244  	// Only valid if action is REDIRECT_TO_URL.
   245  	RedirectUrlConfig *RedirectUrlConfig `json:"redirect_url_config,omitempty"`
   246  
   247  	// Requests matching this policy will be redirected to the configuration of the page.
   248  	// Only valid if action is FIXED_RESPONSE.
   249  	FixedResponseConfig *FixedResponseConfig `json:"fixed_response_config,omitempty"`
   250  
   251  	// The config of the redirected pool.
   252  	// Only valid if action is REDIRECT_TO_POOL.
   253  	RedirectPoolsExtendConfig *RedirectPoolsExtendConfig `json:"redirect_pools_extend_config,omitempty"`
   254  
   255  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   256  	// or false (DOWN).
   257  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   258  }
   259  
   260  // ToL7PolicyUpdateMap builds a request body from UpdateOpts.
   261  func (opts UpdateOpts) ToL7PolicyUpdateMap() (map[string]interface{}, error) {
   262  	b, err := golangsdk.BuildRequestBody(opts, "l7policy")
   263  	if err != nil {
   264  		return nil, err
   265  	}
   266  
   267  	m := b["l7policy"].(map[string]interface{})
   268  
   269  	if m["redirect_pool_id"] == "" {
   270  		m["redirect_pool_id"] = nil
   271  	}
   272  
   273  	if m["redirect_url"] == "" {
   274  		m["redirect_url"] = nil
   275  	}
   276  
   277  	return b, nil
   278  }
   279  
   280  // Update allows l7policy to be updated.
   281  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   282  	b, err := opts.ToL7PolicyUpdateMap()
   283  	if err != nil {
   284  		r.Err = err
   285  		return
   286  	}
   287  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{
   288  		OkCodes: []int{200},
   289  	})
   290  	return
   291  }
   292  
   293  // CreateRuleOpts is the common options struct used in this package's CreateRule
   294  // operation.
   295  type CreateRuleOpts struct {
   296  	// The L7 rule type. One of HOST_NAME, PATH, METHOD, HEADER, QUERY_STRING, or SOURCE_IP.
   297  	RuleType RuleType `json:"type" required:"true"`
   298  
   299  	// The comparison type for the L7 rule. One of EQUAL_TO, REGEX, or STARTS_WITH.
   300  	CompareType CompareType `json:"compare_type" required:"true"`
   301  
   302  	// The value to use for the comparison.
   303  	Value string `json:"value,omitempty"`
   304  
   305  	// TenantID is the UUID of the tenant who owns the rule in octavia.
   306  	// Only administrative users can specify a project UUID other than their own.
   307  	TenantID string `json:"tenant_id,omitempty"`
   308  
   309  	// The key to use for the comparison. For example, the name of the cookie to evaluate.
   310  	Key string `json:"key,omitempty"`
   311  
   312  	// When true the logic of the rule is inverted. For example, with invert true,
   313  	// equal to would become not equal to. Default is false.
   314  	Invert bool `json:"invert,omitempty"`
   315  
   316  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   317  	// or false (DOWN).
   318  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   319  
   320  	// The matching conditions of the forwarding rule.
   321  	// This parameter is available only when enhance_l7policy_enable of the listener is set to true.
   322  	Conditions []Condition `json:"conditions,omitempty"`
   323  }
   324  
   325  type Condition struct {
   326  	// The key of the match item.
   327  	Key string `json:"key,omitempty"`
   328  
   329  	// The value of the match item.
   330  	Value string `json:"value" required:"true"`
   331  }
   332  
   333  // ToRuleCreateMap builds a request body from CreateRuleOpts.
   334  func (opts CreateRuleOpts) ToRuleCreateMap() (map[string]interface{}, error) {
   335  	return golangsdk.BuildRequestBody(opts, "rule")
   336  }
   337  
   338  // CreateRule will create and associate a Rule with a particular L7Policy.
   339  func CreateRule(c *golangsdk.ServiceClient, policyID string, opts CreateRuleOpts) (r CreateRuleResult) {
   340  	b, err := opts.ToRuleCreateMap()
   341  	if err != nil {
   342  		r.Err = err
   343  		return
   344  	}
   345  	_, r.Err = c.Post(ruleRootURL(c, policyID), b, &r.Body, nil)
   346  	return
   347  }
   348  
   349  // ListRulesOptsBuilder allows extensions to add additional parameters to the
   350  // ListRules request.
   351  type ListRulesOptsBuilder interface {
   352  	ToRulesListQuery() (string, error)
   353  }
   354  
   355  // ListRulesOpts allows the filtering and sorting of paginated collections
   356  // through the API.
   357  type ListRulesOpts struct {
   358  	RuleType     RuleType    `q:"type"`
   359  	TenantID     string      `q:"tenant_id"`
   360  	CompareType  CompareType `q:"compare_type"`
   361  	Value        string      `q:"value"`
   362  	Key          string      `q:"key"`
   363  	Invert       bool        `q:"invert"`
   364  	AdminStateUp bool        `q:"admin_state_up"`
   365  	ID           string      `q:"id"`
   366  	Limit        int         `q:"limit"`
   367  	Marker       string      `q:"marker"`
   368  	SortKey      string      `q:"sort_key"`
   369  	SortDir      string      `q:"sort_dir"`
   370  }
   371  
   372  // ToRulesListQuery formats a ListOpts into a query string.
   373  func (opts ListRulesOpts) ToRulesListQuery() (string, error) {
   374  	q, err := golangsdk.BuildQueryString(opts)
   375  	return q.String(), err
   376  }
   377  
   378  // ListRules returns a Pager which allows you to iterate over a collection of
   379  // rules. It accepts a ListRulesOptsBuilder, which allows you to filter and
   380  // sort the returned collection for greater efficiency.
   381  //
   382  // Default policy settings return only those rules that are owned by the
   383  // project who submits the request, unless an admin user submits the request.
   384  func ListRules(c *golangsdk.ServiceClient, policyID string, opts ListRulesOptsBuilder) pagination.Pager {
   385  	url := ruleRootURL(c, policyID)
   386  	if opts != nil {
   387  		query, err := opts.ToRulesListQuery()
   388  		if err != nil {
   389  			return pagination.Pager{Err: err}
   390  		}
   391  		url += query
   392  	}
   393  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   394  		return RulePage{pagination.LinkedPageBase{PageResult: r}}
   395  	})
   396  }
   397  
   398  // GetRule retrieves a particular L7Policy Rule based on its unique ID.
   399  func GetRule(c *golangsdk.ServiceClient, policyID string, ruleID string) (r GetRuleResult) {
   400  	_, r.Err = c.Get(ruleResourceURL(c, policyID, ruleID), &r.Body, nil)
   401  	return
   402  }
   403  
   404  // DeleteRule will remove a Rule from a particular L7Policy.
   405  func DeleteRule(c *golangsdk.ServiceClient, policyID string, ruleID string) (r DeleteRuleResult) {
   406  	_, r.Err = c.Delete(ruleResourceURL(c, policyID, ruleID), nil)
   407  	return
   408  }
   409  
   410  // UpdateRuleOptsBuilder allows to add additional parameters to the PUT request.
   411  type UpdateRuleOptsBuilder interface {
   412  	ToRuleUpdateMap() (map[string]interface{}, error)
   413  }
   414  
   415  // UpdateRuleOpts is the common options struct used in this package's Update
   416  // operation.
   417  type UpdateRuleOpts struct {
   418  	// The L7 rule type. One of HOST_NAME, PATH, METHOD, HEADER, QUERY_STRING, or SOURCE_IP.
   419  	RuleType RuleType `json:"type,omitempty"`
   420  
   421  	// The comparison type for the L7 rule. One of EQUAL_TO, REGEX, or STARTS_WITH.
   422  	CompareType CompareType `json:"compare_type,omitempty"`
   423  
   424  	// The value to use for the comparison.
   425  	Value string `json:"value,omitempty"`
   426  
   427  	// The key to use for the comparison. For example, the name of the cookie to evaluate.
   428  	Key *string `json:"key,omitempty"`
   429  
   430  	// When true the logic of the rule is inverted. For example, with invert true,
   431  	// equal to would become not equal to. Default is false.
   432  	Invert *bool `json:"invert,omitempty"`
   433  
   434  	// The administrative state of the Loadbalancer. A valid value is true (UP)
   435  	// or false (DOWN).
   436  	AdminStateUp *bool `json:"admin_state_up,omitempty"`
   437  
   438  	// The matching conditions of the forwarding rule.
   439  	// This parameter is available only when enhance_l7policy_enable of the listener is set to true.
   440  	Conditions []Condition `json:"conditions,omitempty"`
   441  }
   442  
   443  // ToRuleUpdateMap builds a request body from UpdateRuleOpts.
   444  func (opts UpdateRuleOpts) ToRuleUpdateMap() (map[string]interface{}, error) {
   445  	b, err := golangsdk.BuildRequestBody(opts, "rule")
   446  	if err != nil {
   447  		return nil, err
   448  	}
   449  
   450  	if m := b["rule"].(map[string]interface{}); m["key"] == "" {
   451  		m["key"] = nil
   452  	}
   453  
   454  	return b, nil
   455  }
   456  
   457  // UpdateRule allows Rule to be updated.
   458  func UpdateRule(c *golangsdk.ServiceClient, policyID string, ruleID string, opts UpdateRuleOptsBuilder) (r UpdateRuleResult) {
   459  	b, err := opts.ToRuleUpdateMap()
   460  	if err != nil {
   461  		r.Err = err
   462  		return
   463  	}
   464  	_, r.Err = c.Put(ruleResourceURL(c, policyID, ruleID), b, &r.Body, &golangsdk.RequestOpts{
   465  		OkCodes: []int{200, 201, 202},
   466  	})
   467  	return
   468  }