github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/apigw/dedicated/v2/throttles/requests.go (about)

     1  package throttles
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  	"github.com/chnsz/golangsdk/pagination"
     8  )
     9  
    10  type ThrottlingPolicyOpts struct {
    11  	// Maximum number of times an API can be accessed within a specified period.
    12  	// The value of this parameter cannot exceed the default limit 200 TPS.
    13  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    14  	ApiCallLimits int `json:"api_call_limits" required:"true"`
    15  	// Request throttling policy name, which can contain 3 to 64 characters, starting with a letter.
    16  	// Only letters, digits, and underscores (_) are allowed.
    17  	// Chinese characters must be in UTF-8 or Unicode format.
    18  	Name string `json:"name" required:"true"`
    19  	// Period of time for limiting the number of API calls.
    20  	// This parameter applies with each of the preceding three API call limits.
    21  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    22  	TimeInterval int `json:"time_interval" required:"true"`
    23  	// Time unit for limiting the number of API calls.
    24  	// The valid values are as following:
    25  	//     SECOND
    26  	//     MINUTE
    27  	//     HOUR
    28  	//     DAY
    29  	TimeUnit string `json:"time_unit" required:"true"`
    30  	// Maximum number of times the API can be accessed by an app within the same period.
    31  	// The value of this parameter must be less than that of user_call_limits.
    32  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    33  	AppCallLimits int `json:"app_call_limits,omitempty"`
    34  	// Description of the request throttling policy, which can contain a maximum of 255 characters.
    35  	// Chinese characters must be in UTF-8 or Unicode format.
    36  	Description string `json:"remark,omitempty"`
    37  	// Type of the request throttling policy.
    38  	// 1: exclusive, limiting the maximum number of times a single API bound to the policy can be called within
    39  	// the specified period.
    40  	// 2: shared, limiting the maximum number of times all APIs bound to the policy can be called within the
    41  	// specified period.
    42  	Type int `json:"type,omitempty"`
    43  	// Maximum number of times the API can be accessed by a user within the same period.
    44  	// The value of this parameter must be less than that of api_call_limits.
    45  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    46  	UserCallLimits int `json:"user_call_limits,omitempty"`
    47  	// Maximum number of times the API can be accessed by an IP address within the same period.
    48  	// The value of this parameter must be less than that of api_call_limits.
    49  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    50  	IpCallLimits int `json:"ip_call_limits,omitempty"`
    51  }
    52  
    53  type ThrottlingPolicyOptsBuilder interface {
    54  	ToThrottlingPolicyOptsMap() (map[string]interface{}, error)
    55  }
    56  
    57  func (opts ThrottlingPolicyOpts) ToThrottlingPolicyOptsMap() (map[string]interface{}, error) {
    58  	return golangsdk.BuildRequestBody(opts, "")
    59  }
    60  
    61  // Create is a method by which to create function that create a new throttling policy.
    62  func Create(client *golangsdk.ServiceClient, instanceId string, opts ThrottlingPolicyOptsBuilder) (r CreateResult) {
    63  	reqBody, err := opts.ToThrottlingPolicyOptsMap()
    64  	if err != nil {
    65  		r.Err = err
    66  		return
    67  	}
    68  	_, r.Err = client.Post(rootURL(client, instanceId), reqBody, &r.Body, nil)
    69  	return
    70  }
    71  
    72  // Update is a method by which to udpate an existing throttle policy.
    73  func Update(client *golangsdk.ServiceClient, instanceId, policyId string,
    74  	opts ThrottlingPolicyOptsBuilder) (r UpdateResult) {
    75  	reqBody, err := opts.ToThrottlingPolicyOptsMap()
    76  	if err != nil {
    77  		r.Err = err
    78  		return
    79  	}
    80  	_, r.Err = client.Put(resourceURL(client, instanceId, policyId), reqBody, &r.Body, &golangsdk.RequestOpts{
    81  		OkCodes: []int{200},
    82  	})
    83  	return
    84  }
    85  
    86  // Get is a method to obtain an existing APIG throttling policy by policy ID.
    87  func Get(client *golangsdk.ServiceClient, instanceId, policyId string) (r GetResult) {
    88  	_, r.Err = client.Get(resourceURL(client, instanceId, policyId), &r.Body, nil)
    89  	return
    90  }
    91  
    92  type ListOpts struct {
    93  	// Request throttling policy ID.
    94  	Id string `q:"id"`
    95  	// Request throttling policy name.
    96  	Name string `q:"name"`
    97  	// Offset from which the query starts.
    98  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    99  	Offset int `q:"offset"`
   100  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
   101  	Limit int `q:"limit"`
   102  	// Parameter name (name) for exact matching.
   103  	PreciseSearch string `q:"precise_search"`
   104  }
   105  
   106  type ListOptsBuilder interface {
   107  	ToListQuery() (string, error)
   108  }
   109  
   110  func (opts ListOpts) ToListQuery() (string, error) {
   111  	q, err := golangsdk.BuildQueryString(opts)
   112  	if err != nil {
   113  		return "", err
   114  	}
   115  	return q.String(), err
   116  }
   117  
   118  // List is a method to obtain an array of one or more throttling policies according to the query parameters.
   119  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOptsBuilder) pagination.Pager {
   120  	url := rootURL(client, instanceId)
   121  	if opts != nil {
   122  		query, err := opts.ToListQuery()
   123  		if err != nil {
   124  			return pagination.Pager{Err: err}
   125  		}
   126  		url += query
   127  	}
   128  
   129  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   130  		return ThorttlePage{pagination.SinglePageBase(r)}
   131  	})
   132  }
   133  
   134  // Delete is a method to delete an existing throttling policy.
   135  func Delete(client *golangsdk.ServiceClient, instanceId, policyId string) (r DeleteResult) {
   136  	_, r.Err = client.Delete(resourceURL(client, instanceId, policyId), nil)
   137  	return
   138  }
   139  
   140  // SpecThrottleCreateOpts is a struct which will be used to create a new special throttling policy.
   141  type SpecThrottleCreateOpts struct {
   142  	// Maximum number of times the excluded object can access an API within the throttling period.
   143  	CallLimits int `json:"call_limits" required:"true"`
   144  	// Excluded app ID or excluded account ID.
   145  	ObjectId string `json:"object_id" required:"true"`
   146  	// Excluded object type, which supports APP and USER.
   147  	ObjectType string `json:"object_type" required:"true"`
   148  }
   149  
   150  // SpecThrottleCreateOptsBuilder is an interface which to support request body build of
   151  // the special throttling policy creation.
   152  type SpecThrottleCreateOptsBuilder interface {
   153  	ToSpecThrottleCreateOptsMap() (map[string]interface{}, error)
   154  }
   155  
   156  // ToSpecThrottleCreateOptsMap is a method which to build a request body by the SpecThrottleCreateOpts.
   157  func (opts SpecThrottleCreateOpts) ToSpecThrottleCreateOptsMap() (map[string]interface{}, error) {
   158  	return golangsdk.BuildRequestBody(opts, "")
   159  }
   160  
   161  // CreateSpecThrottle is a method by which to create a new special throttling policy.
   162  func CreateSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId string,
   163  	opts SpecThrottleCreateOptsBuilder) (r SpecThrottleResult) {
   164  	reqBody, err := opts.ToSpecThrottleCreateOptsMap()
   165  	if err != nil {
   166  		r.Err = err
   167  		return
   168  	}
   169  	_, r.Err = client.Post(specRootURL(client, instanceId, policyId), reqBody, &r.Body, nil)
   170  	return
   171  }
   172  
   173  // SpecThrottleUpdateOpts is a struct which will be used to update an existing special throttling policy.
   174  type SpecThrottleUpdateOpts struct {
   175  	// Maximum number of times the excluded object can access an API within the throttling period.
   176  	CallLimits int `json:"call_limits" required:"true"`
   177  }
   178  
   179  // SpecThrottleUpdateOptsBuilder is an interface which to support request body build of
   180  // the special throttling policy updation.
   181  type SpecThrottleUpdateOptsBuilder interface {
   182  	ToSpecThrottleUpdateOptsMap() (map[string]interface{}, error)
   183  }
   184  
   185  // ToSpecThrottleUpdateOptsMap is a method which to build a request body by the SpecThrottleUpdateOpts.
   186  func (opts SpecThrottleUpdateOpts) ToSpecThrottleUpdateOptsMap() (map[string]interface{}, error) {
   187  	return golangsdk.BuildRequestBody(opts, "")
   188  }
   189  
   190  // UpdateSpecThrottle is a method by which to update an existing special throttle policy.
   191  func UpdateSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId, strategyId string,
   192  	opts SpecThrottleUpdateOptsBuilder) (r SpecThrottleResult) {
   193  	reqBody, err := opts.ToSpecThrottleUpdateOptsMap()
   194  	if err != nil {
   195  		r.Err = err
   196  		return
   197  	}
   198  	_, r.Err = client.Put(specResourceURL(client, instanceId, policyId, strategyId), reqBody, &r.Body,
   199  		&golangsdk.RequestOpts{
   200  			OkCodes: []int{200},
   201  		})
   202  	return
   203  }
   204  
   205  // SpecThrottlesListOpts allows to filter list data using given parameters.
   206  type SpecThrottlesListOpts struct {
   207  	// Object type, which can be APP or USER.
   208  	ObjectType string `q:"object_type"`
   209  	// Name of an excluded app.
   210  	AppName string `q:"app_name"`
   211  	// Offset from which the query starts.
   212  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   213  	Offset int `q:"offset"`
   214  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
   215  	Limit int `q:"limit"`
   216  }
   217  
   218  // SpecThrottlesListOptsBuilder is an interface which to support request query build of
   219  // the special throttling policies search.
   220  type SpecThrottlesListOptsBuilder interface {
   221  	ToSpecThrottlesListQuery() (string, error)
   222  }
   223  
   224  // ToSpecThrottlesListQuery is a method which to build a request query by the SpecThrottlesListOpts.
   225  func (opts SpecThrottlesListOpts) ToSpecThrottlesListQuery() (string, error) {
   226  	q, err := golangsdk.BuildQueryString(opts)
   227  	if err != nil {
   228  		return "", err
   229  	}
   230  	return q.String(), err
   231  }
   232  
   233  // ListSpecThrottles is a method to obtain an array of one or more special throttling policies
   234  // according to the query parameters.
   235  func ListSpecThrottles(client *golangsdk.ServiceClient, instanceId, policyId string,
   236  	opts SpecThrottlesListOptsBuilder) pagination.Pager {
   237  	url := specRootURL(client, instanceId, policyId)
   238  	if opts != nil {
   239  		query, err := opts.ToSpecThrottlesListQuery()
   240  		if err != nil {
   241  			return pagination.Pager{Err: err}
   242  		}
   243  		url += query
   244  	}
   245  
   246  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   247  		return SpecThrottlePage{pagination.SinglePageBase(r)}
   248  	})
   249  }
   250  
   251  // DeleteSpecThrottle is a method to delete an existing special throttling policy.
   252  func DeleteSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId, strategyId string) (r DeleteResult) {
   253  	_, r.Err = client.Delete(specResourceURL(client, instanceId, policyId, strategyId), nil)
   254  	return
   255  }
   256  
   257  type BindOpts struct {
   258  	// Throttle policy ID. The valid length is range from 1 to 65.
   259  	ThrottleId string `json:"strategy_id" required:"true"`
   260  	// The IDs of the API publish record.
   261  	PublishIds []string `json:"publish_ids" required:"true"`
   262  	// Instnace ID to which the API belongs.
   263  	InstanceId string `json:"-" required:"true"`
   264  }
   265  
   266  // Bind is a method to bind the policy to one or more APIs.
   267  func Bind(c *golangsdk.ServiceClient, opts BindOpts) ([]BindResp, error) {
   268  	b, err := golangsdk.BuildRequestBody(opts, "")
   269  	if err != nil {
   270  		return nil, err
   271  	}
   272  
   273  	var r struct {
   274  		BindList []BindResp `json:"throttle_applys"`
   275  	}
   276  	_, err = c.Post(bindURL(c, opts.InstanceId), b, &r, nil)
   277  	return r.BindList, err
   278  }
   279  
   280  // Unbind is a method to cancel the relationship between API and throttling policy.
   281  func Unbind(c *golangsdk.ServiceClient, instanceId, bindId string) error {
   282  	_, err := c.Delete(unbindURL(c, instanceId, bindId), nil)
   283  	return err
   284  }
   285  
   286  type BatchUnbindActionOpts struct {
   287  	// List of API and throttling policy binding relationship IDs that need to be unbound.
   288  	Action string `q:"action"`
   289  }
   290  
   291  type BatchUnbindOpts struct {
   292  	// List of API and throttling policy binding relationship IDs that need to be unbound.
   293  	ThrottleBindings string `json:"throttle_bindings,omitempty"`
   294  	// Instance ID.
   295  	InstanceId string `json:"-" required:"true"`
   296  }
   297  
   298  // BatchUnbind is an API to unbind all throttling policies associated with the list.
   299  func BatchUnbind(c *golangsdk.ServiceClient, unbindOpt BatchUnbindOpts,
   300  	queryOpt BatchUnbindActionOpts) (*BatchResp, error) {
   301  	b, err := golangsdk.BuildRequestBody(unbindOpt, "")
   302  	if err != nil {
   303  		return nil, err
   304  	}
   305  
   306  	url := batchUnbindURL(c, unbindOpt.InstanceId)
   307  	query, err := golangsdk.BuildQueryString(queryOpt)
   308  	if err != nil {
   309  		return nil, err
   310  	}
   311  	url += query.String()
   312  
   313  	var r BatchResp
   314  	_, err = c.Put(url, b, &r, nil)
   315  	return &r, err
   316  }
   317  
   318  type ListBindOpts struct {
   319  	// Offset from which the query starts.
   320  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   321  	Offset int `q:"offset"`
   322  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
   323  	Limit int `q:"limit"`
   324  	// throttling policy ID.
   325  	ThrottleId string `q:"throttle_id"`
   326  	// Environment ID.
   327  	EnvId string `q:"env_id"`
   328  	// API group ID.
   329  	GroupId string `q:"group_id"`
   330  	// API ID.
   331  	ApiId string `q:"api_id"`
   332  	// API name.
   333  	ApiName string `q:"api_name"`
   334  	// Instnace ID to which the API belongs.
   335  	InstanceId string `json:"-"`
   336  }
   337  
   338  // ListBind is a method to obtain all API to which the throttling policy bound.
   339  func ListBind(c *golangsdk.ServiceClient, opts ListBindOpts) ([]ApiForThrottle, error) {
   340  	if opts.InstanceId == "" {
   341  		return nil, fmt.Errorf("The instance ID is required.")
   342  	}
   343  	url := listBindURL(c, opts.InstanceId)
   344  	query, err := golangsdk.BuildQueryString(opts)
   345  	if err != nil {
   346  		return nil, err
   347  	}
   348  	url += query.String()
   349  
   350  	var r struct {
   351  		ApiDetails []ApiForThrottle `json:"apis"`
   352  	}
   353  	_, err = c.Get(url, &r, nil)
   354  	return r.ApiDetails, err
   355  }