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

     1  package plugins
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure used to create a new plugin.
     9  type CreateOpts struct {
    10  	// The ID of the dedicated instance to which the plugin belongs.
    11  	InstanceId string `json:"-" required:"true"`
    12  	// Plugin name.
    13  	// The valid length is limited from `3` to `255`, only Chinese characters, English letters, digits, hyphens (-) and
    14  	// underscores (_) are allowed. The name must start with an English letter or Chinese character.
    15  	Name string `json:"plugin_name" required:"true"`
    16  	// Plugin type.
    17  	// The valid values are as follows:
    18  	// + 'cors': CORS, specify preflight request headers and response headers and automatically create preflight
    19  	//   request APIs for cross-origin API access.
    20  	// + 'set_resp_headers': HTTP Response Header Management, customize HTTP headers that will be contained in an API
    21  	//   response.
    22  	// + 'rate_limit': Request Throttling 2.0, limits the number of times an API can be called within a specific time
    23  	//   period. It supports parameter-based, basic, and excluded throttling.
    24  	// + 'kafka_log': Kafka Log Push, Push detailed API calling logs to kafka for you to easily obtain logs.
    25  	// + 'breaker': Circuit Breaker, circuit breaker protect the system when performance issues occur on backend
    26  	//   service.
    27  	Type string `json:"plugin_type" required:"true"`
    28  	// The available scope for plugin, the valid value is 'global'.
    29  	Scope string `json:"plugin_scope" required:"true"`
    30  	// The configuration details for plugin.
    31  	Content string `json:"plugin_content" required:"true"`
    32  	// The plugin description.
    33  	// The valid length is limited from `3` to `255` characters.
    34  	Description string `json:"remark,omitempty"`
    35  }
    36  
    37  var requestOpts = golangsdk.RequestOpts{
    38  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    39  }
    40  
    41  // Create is a method used to create a new plugin using given parameters.
    42  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Plugin, error) {
    43  	b, err := golangsdk.BuildRequestBody(opts, "")
    44  	if err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	var r Plugin
    49  	_, err = c.Post(rootURL(c, opts.InstanceId), b, &r, &golangsdk.RequestOpts{
    50  		MoreHeaders: requestOpts.MoreHeaders,
    51  	})
    52  	return &r, err
    53  }
    54  
    55  // Get is a method to obtain an existing plugin detail by its ID and related instance ID.
    56  func Get(client *golangsdk.ServiceClient, instanceId, pluginId string) (*Plugin, error) {
    57  	var r Plugin
    58  	_, err := client.Get(resourceURL(client, instanceId, pluginId), &r, &golangsdk.RequestOpts{
    59  		MoreHeaders: requestOpts.MoreHeaders,
    60  	})
    61  	return &r, err
    62  }
    63  
    64  // UpdateOpts is the structure used to update the plugin configuration.
    65  type UpdateOpts struct {
    66  	// The instnace ID to which the plugin belongs.
    67  	InstanceId string `json:"-" required:"true"`
    68  	// The plugin ID.
    69  	ID string `json:"-" required:"true"`
    70  	// Plugin name.
    71  	// The valid length is limited from `3` to `255`, only Chinese characters, English letters, digits, hyphens (-) and
    72  	// underscores (_) are allowed. The name must start with an English letter or Chinese character.
    73  	Name string `json:"plugin_name" required:"true"`
    74  	// Plugin type.
    75  	// The valid values are as follows:
    76  	// + 'cors': CORS, specify preflight request headers and response headers and automatically create preflight
    77  	//   request APIs for cross-origin API access.
    78  	// + 'set_resp_headers': HTTP Response Header Management, customize HTTP headers that will be contained in an API
    79  	//   response.
    80  	// + 'rate_limit': Request Throttling 2.0, limits the number of times an API can be called within a specific time
    81  	//   period. It supports parameter-based, basic, and excluded throttling.
    82  	// + 'kafka_log': Kafka Log Push, Push detailed API calling logs to kafka for you to easily obtain logs.
    83  	// + 'breaker': Circuit Breaker, circuit breaker protect the system when performance issues occur on backend
    84  	//   service.
    85  	Type string `json:"plugin_type" required:"true"`
    86  	// The available scope for plugin, the valid value is 'global'.
    87  	Scope string `json:"plugin_scope" required:"true"`
    88  	// The configuration details for plugin.
    89  	Content string `json:"plugin_content" required:"true"`
    90  	// The plugin description.
    91  	// The valid length is limited from `3` to `255` characters.
    92  	Description *string `json:"remark,omitempty"`
    93  }
    94  
    95  // Update is a method used to update a plugin using given parameters.
    96  func Update(c *golangsdk.ServiceClient, opts UpdateOpts) (*Plugin, error) {
    97  	b, err := golangsdk.BuildRequestBody(opts, "")
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	var r Plugin
   103  	_, err = c.Put(resourceURL(c, opts.InstanceId, opts.ID), b, &r, &golangsdk.RequestOpts{
   104  		MoreHeaders: requestOpts.MoreHeaders,
   105  	})
   106  	return &r, err
   107  }
   108  
   109  // Delete is a method to remove the specified plugin using its ID and related dedicated instance ID.
   110  func Delete(c *golangsdk.ServiceClient, instanceId, pluginId string) error {
   111  	_, err := c.Delete(resourceURL(c, instanceId, pluginId), &golangsdk.RequestOpts{
   112  		MoreHeaders: requestOpts.MoreHeaders,
   113  	})
   114  	return err
   115  }
   116  
   117  // BindOpts is the structure that used to bind a plugin to the published APIs.
   118  type BindOpts struct {
   119  	// The instnace ID to which the plugin belongs.
   120  	InstanceId string `json:"-" required:"true"`
   121  	// The plugin ID.
   122  	PluginId string `json:"-" required:"true"`
   123  	// The environment ID where the API is published.
   124  	EnvId string `json:"env_id" required:"true"`
   125  	// The IDs of the API publish record.
   126  	ApiIds []string `json:"api_ids" required:"true"`
   127  }
   128  
   129  // Bind is a method to bind a plugin to one or more APIs.
   130  func Bind(c *golangsdk.ServiceClient, opts BindOpts) ([]PluginBindDetail, error) {
   131  	b, err := golangsdk.BuildRequestBody(opts, "")
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	var r BindResp
   137  	_, err = c.Post(bindURL(c, opts.InstanceId, opts.PluginId), b, &r, nil)
   138  	return r.Bindings, err
   139  }
   140  
   141  // ListBindOpts is the structure used to querying published API list that plugin associated.
   142  type ListBindOpts struct {
   143  	// The instnace ID to which the plugin belongs.
   144  	InstanceId string `json:"-" required:"true"`
   145  	// The plugin ID.
   146  	PluginId string `json:"-" required:"true"`
   147  	// Offset from which the query starts.
   148  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   149  	Offset int `q:"offset"`
   150  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
   151  	Limit int `q:"limit"`
   152  	// The environment ID where the API is published.
   153  	EnvId string `q:"env_id"`
   154  	// The API name.
   155  	ApiName string `q:"api_name"`
   156  	// The API ID.
   157  	ApiId string `q:"api_id"`
   158  	// The group ID where the API is located.
   159  	GroupId string `q:"group_id"`
   160  	// The request method.
   161  	RequestMethod string `q:"req_method"`
   162  	// The request URI.
   163  	RequestUri string `q:"req_uri"`
   164  }
   165  
   166  // ListBind is a method to obtain all API to which the plugin bound.
   167  func ListBind(c *golangsdk.ServiceClient, opts ListBindOpts) ([]BindApiInfo, error) {
   168  	url := listBindURL(c, opts.InstanceId, opts.PluginId)
   169  	query, err := golangsdk.BuildQueryString(opts)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	url += query.String()
   174  
   175  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   176  		p := BindPage{pagination.OffsetPageBase{PageResult: r}}
   177  		return p
   178  	}).AllPages()
   179  
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  	return ExtractBindInfos(pages)
   184  }
   185  
   186  // UnbindOpts is the structure that used to unbind the published APIs from the plugin.
   187  type UnbindOpts struct {
   188  	// The instnace ID to which the plugin belongs.
   189  	InstanceId string `json:"-" required:"true"`
   190  	// The plugin ID.
   191  	PluginId string `json:"-" required:"true"`
   192  	// The environment ID where the API is published.
   193  	EnvId string `json:"env_id" required:"true"`
   194  	// The IDs of the API publish record.
   195  	ApiIds []string `json:"api_ids" required:"true"`
   196  }
   197  
   198  // Unbind is an method used to unbind one or more APIs from the plugin.
   199  func Unbind(c *golangsdk.ServiceClient, opts UnbindOpts) error {
   200  	b, err := golangsdk.BuildRequestBody(opts, "")
   201  	if err != nil {
   202  		return err
   203  	}
   204  
   205  	_, err = c.Put(unbindURL(c, opts.InstanceId, opts.PluginId), b, nil, &golangsdk.RequestOpts{
   206  		MoreHeaders: requestOpts.MoreHeaders,
   207  		OkCodes:     []int{204},
   208  	})
   209  	return err
   210  }