github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/apigw/v2/throttles/requests.go (about)

     1  package throttles
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type ThrottlingPolicyOpts struct {
     9  	// Maximum number of times an API can be accessed within a specified period.
    10  	// The value of this parameter cannot exceed the default limit 200 TPS.
    11  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    12  	ApiCallLimits int `json:"api_call_limits" required:"true"`
    13  	// Request throttling policy name, which can contain 3 to 64 characters, starting with a letter.
    14  	// Only letters, digits, and underscores (_) are allowed.
    15  	// Chinese characters must be in UTF-8 or Unicode format.
    16  	Name string `json:"name" required:"true"`
    17  	// Period of time for limiting the number of API calls.
    18  	// This parameter applies with each of the preceding three API call limits.
    19  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    20  	TimeInterval int `json:"time_interval" required:"true"`
    21  	// Time unit for limiting the number of API calls.
    22  	// The valid values are as following:
    23  	//     SECOND
    24  	//     MINUTE
    25  	//     HOUR
    26  	//     DAY
    27  	TimeUnit string `json:"time_unit" required:"true"`
    28  	// Maximum number of times the API can be accessed by an app within the same period.
    29  	// The value of this parameter must be less than that of user_call_limits.
    30  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    31  	AppCallLimits int `json:"app_call_limits,omitempty"`
    32  	// Description of the request throttling policy, which can contain a maximum of 255 characters.
    33  	// Chinese characters must be in UTF-8 or Unicode format.
    34  	Description string `json:"remark,omitempty"`
    35  	// Type of the request throttling policy.
    36  	// 1: exclusive, limiting the maximum number of times a single API bound to the policy can be called within
    37  	// the specified period.
    38  	// 2: shared, limiting the maximum number of times all APIs bound to the policy can be called within the
    39  	// specified period.
    40  	Type int `json:"type,omitempty"`
    41  	// Maximum number of times the API can be accessed by a user within the same period.
    42  	// The value of this parameter must be less than that of api_call_limits.
    43  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    44  	UserCallLimits int `json:"user_call_limits,omitempty"`
    45  	// Maximum number of times the API can be accessed by an IP address within the same period.
    46  	// The value of this parameter must be less than that of api_call_limits.
    47  	// This value must be a positive integer and cannot exceed 2,147,483,647.
    48  	IpCallLimits int `json:"ip_call_limits,omitempty"`
    49  }
    50  
    51  type ThrottlingPolicyOptsBuilder interface {
    52  	ToThrottlingPolicyOptsMap() (map[string]interface{}, error)
    53  }
    54  
    55  func (opts ThrottlingPolicyOpts) ToThrottlingPolicyOptsMap() (map[string]interface{}, error) {
    56  	return golangsdk.BuildRequestBody(opts, "")
    57  }
    58  
    59  // Create is a method by which to create function that create a new throttling policy.
    60  func Create(client *golangsdk.ServiceClient, instanceId string, opts ThrottlingPolicyOptsBuilder) (r CreateResult) {
    61  	reqBody, err := opts.ToThrottlingPolicyOptsMap()
    62  	if err != nil {
    63  		r.Err = err
    64  		return
    65  	}
    66  	_, r.Err = client.Post(rootURL(client, instanceId), reqBody, &r.Body, nil)
    67  	return
    68  }
    69  
    70  // Update is a method by which to udpate an existing throttle policy.
    71  func Update(client *golangsdk.ServiceClient, instanceId, policyId string,
    72  	opts ThrottlingPolicyOptsBuilder) (r UpdateResult) {
    73  	reqBody, err := opts.ToThrottlingPolicyOptsMap()
    74  	if err != nil {
    75  		r.Err = err
    76  		return
    77  	}
    78  	_, r.Err = client.Put(resourceURL(client, instanceId, policyId), reqBody, &r.Body, &golangsdk.RequestOpts{
    79  		OkCodes: []int{200},
    80  	})
    81  	return
    82  }
    83  
    84  // Get is a method to obtain an existing APIG throttling policy by policy ID.
    85  func Get(client *golangsdk.ServiceClient, instanceId, policyId string) (r GetResult) {
    86  	_, r.Err = client.Get(resourceURL(client, instanceId, policyId), &r.Body, nil)
    87  	return
    88  }
    89  
    90  type ListOpts struct {
    91  	// Request throttling policy ID.
    92  	Id string `q:"id"`
    93  	// Request throttling policy name.
    94  	Name string `q:"name"`
    95  	// Offset from which the query starts.
    96  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    97  	Offset int `q:"offset"`
    98  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
    99  	Limit int `q:"limit"`
   100  	// Parameter name (name) for exact matching.
   101  	PreciseSearch string `q:"precise_search"`
   102  }
   103  
   104  type ListOptsBuilder interface {
   105  	ToListQuery() (string, error)
   106  }
   107  
   108  func (opts ListOpts) ToListQuery() (string, error) {
   109  	q, err := golangsdk.BuildQueryString(opts)
   110  	if err != nil {
   111  		return "", err
   112  	}
   113  	return q.String(), err
   114  }
   115  
   116  // List is a method to obtain an array of one or more throttling policies according to the query parameters.
   117  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOptsBuilder) pagination.Pager {
   118  	url := rootURL(client, instanceId)
   119  	if opts != nil {
   120  		query, err := opts.ToListQuery()
   121  		if err != nil {
   122  			return pagination.Pager{Err: err}
   123  		}
   124  		url += query
   125  	}
   126  
   127  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   128  		return ThorttlePage{pagination.SinglePageBase(r)}
   129  	})
   130  }
   131  
   132  // Delete is a method to delete an existing throttling policy.
   133  func Delete(client *golangsdk.ServiceClient, instanceId, policyId string) (r DeleteResult) {
   134  	_, r.Err = client.Delete(resourceURL(client, instanceId, policyId), nil)
   135  	return
   136  }
   137  
   138  // SpecThrottleCreateOpts is a struct which will be used to create a new special throttling policy.
   139  type SpecThrottleCreateOpts struct {
   140  	// Maximum number of times the excluded object can access an API within the throttling period.
   141  	CallLimits int `json:"call_limits" required:"true"`
   142  	// Excluded app ID or excluded account ID.
   143  	ObjectId string `json:"object_id" required:"true"`
   144  	// Excluded object type, which supports APP and USER.
   145  	ObjectType string `json:"object_type" required:"true"`
   146  }
   147  
   148  // SpecThrottleCreateOptsBuilder is an interface which to support request body build of
   149  // the special throttling policy creation.
   150  type SpecThrottleCreateOptsBuilder interface {
   151  	ToSpecThrottleCreateOptsMap() (map[string]interface{}, error)
   152  }
   153  
   154  // ToSpecThrottleCreateOptsMap is a method which to build a request body by the SpecThrottleCreateOpts.
   155  func (opts SpecThrottleCreateOpts) ToSpecThrottleCreateOptsMap() (map[string]interface{}, error) {
   156  	return golangsdk.BuildRequestBody(opts, "")
   157  }
   158  
   159  // CreateSpecThrottle is a method by which to create a new special throttling policy.
   160  func CreateSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId string,
   161  	opts SpecThrottleCreateOptsBuilder) (r SpecThrottleResult) {
   162  	reqBody, err := opts.ToSpecThrottleCreateOptsMap()
   163  	if err != nil {
   164  		r.Err = err
   165  		return
   166  	}
   167  	_, r.Err = client.Post(specRootURL(client, instanceId, policyId), reqBody, &r.Body, nil)
   168  	return
   169  }
   170  
   171  // SpecThrottleUpdateOpts is a struct which will be used to update an existing special throttling policy.
   172  type SpecThrottleUpdateOpts struct {
   173  	// Maximum number of times the excluded object can access an API within the throttling period.
   174  	CallLimits int `json:"call_limits" required:"true"`
   175  }
   176  
   177  // SpecThrottleUpdateOptsBuilder is an interface which to support request body build of
   178  // the special throttling policy updation.
   179  type SpecThrottleUpdateOptsBuilder interface {
   180  	ToSpecThrottleUpdateOptsMap() (map[string]interface{}, error)
   181  }
   182  
   183  // ToSpecThrottleUpdateOptsMap is a method which to build a request body by the SpecThrottleUpdateOpts.
   184  func (opts SpecThrottleUpdateOpts) ToSpecThrottleUpdateOptsMap() (map[string]interface{}, error) {
   185  	return golangsdk.BuildRequestBody(opts, "")
   186  }
   187  
   188  // UpdateSpecThrottle is a method by which to update an existing special throttle policy.
   189  func UpdateSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId, strategyId string,
   190  	opts SpecThrottleUpdateOptsBuilder) (r SpecThrottleResult) {
   191  	reqBody, err := opts.ToSpecThrottleUpdateOptsMap()
   192  	if err != nil {
   193  		r.Err = err
   194  		return
   195  	}
   196  	_, r.Err = client.Put(specResourceURL(client, instanceId, policyId, strategyId), reqBody, &r.Body,
   197  		&golangsdk.RequestOpts{
   198  			OkCodes: []int{200},
   199  		})
   200  	return
   201  }
   202  
   203  // SpecThrottlesListOpts allows to filter list data using given parameters.
   204  type SpecThrottlesListOpts struct {
   205  	// Object type, which can be APP or USER.
   206  	ObjectType string `q:"object_type"`
   207  	// Name of an excluded app.
   208  	AppName string `q:"app_name"`
   209  	// Offset from which the query starts.
   210  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   211  	Offset int `q:"offset"`
   212  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
   213  	Limit int `q:"limit"`
   214  }
   215  
   216  // SpecThrottlesListOptsBuilder is an interface which to support request query build of
   217  // the special throttling policies search.
   218  type SpecThrottlesListOptsBuilder interface {
   219  	ToSpecThrottlesListQuery() (string, error)
   220  }
   221  
   222  // ToSpecThrottlesListQuery is a method which to build a request query by the SpecThrottlesListOpts.
   223  func (opts SpecThrottlesListOpts) ToSpecThrottlesListQuery() (string, error) {
   224  	q, err := golangsdk.BuildQueryString(opts)
   225  	if err != nil {
   226  		return "", err
   227  	}
   228  	return q.String(), err
   229  }
   230  
   231  // ListSpecThrottles is a method to obtain an array of one or more special throttling policies
   232  // according to the query parameters.
   233  func ListSpecThrottles(client *golangsdk.ServiceClient, instanceId, policyId string,
   234  	opts SpecThrottlesListOptsBuilder) pagination.Pager {
   235  	url := specRootURL(client, instanceId, policyId)
   236  	if opts != nil {
   237  		query, err := opts.ToSpecThrottlesListQuery()
   238  		if err != nil {
   239  			return pagination.Pager{Err: err}
   240  		}
   241  		url += query
   242  	}
   243  
   244  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   245  		return SpecThrottlePage{pagination.SinglePageBase(r)}
   246  	})
   247  }
   248  
   249  // DeleteSpecThrottle is a method to delete an existing special throttling policy.
   250  func DeleteSpecThrottle(client *golangsdk.ServiceClient, instanceId, policyId, strategyId string) (r DeleteResult) {
   251  	_, r.Err = client.Delete(specResourceURL(client, instanceId, policyId, strategyId), nil)
   252  	return
   253  }