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

     1  package signs
     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 signature key.
     9  type CreateOpts struct {
    10  	// The instnace ID to which the signature belongs.
    11  	InstanceId string `json:"-" required:"true"`
    12  	// Signature key name. It can contain letters, digits, and underscores(_) and must start with a letter.
    13  	Name string `json:"name" required:"true"`
    14  	// Signature key type.
    15  	// + hmac
    16  	// + basic
    17  	// + public_key
    18  	// + aes
    19  	SignType string `json:"sign_type,omitempty"`
    20  	// Signature key.
    21  	// + For 'hmac' type: The value contains 8 to 32 characters, including letters, digits, underscores (_), and
    22  	//   hyphens (-). It must start with a letter or digit. If not specified, a key is automatically generated.
    23  	// + For 'basic' type: The value contains 4 to 32 characters, including letters, digits, underscores (_), and
    24  	//   hyphens (-). It must start with a letter. If not specified, a key is automatically generated.
    25  	// + For 'public_key' type: The value contains 8 to 512 characters, including letters, digits, and special
    26  	//   characters (_-+/=). It must start with a letter, digit, plus sign (+), or slash (/). If not specified, a key
    27  	//   is automatically generated.
    28  	// + For 'aes' type: The value contains 16 characters if the aes-128-cfb algorithm is used, or 32 characters if the
    29  	//   aes-256-cfb algorithm is used. Letters, digits, and special characters (_-!@#$%+/=) are allowed. It must start
    30  	//   with a letter, digit, plus sign (+), or slash (/). If not specified, a key is automatically generated.
    31  	SignKey string `json:"sign_key,omitempty"`
    32  	// Signature secret.
    33  	// + For 'hmac' type: The value contains 16 to 64 characters. Letters, digits, and special characters (_-!@#$%) are
    34  	//   allowed. It must start with a letter or digit. If not specified, a value is automatically generated.
    35  	// + For 'basic' type: The value contains 8 to 64 characters. Letters, digits, and special characters (_-!@#$%) are
    36  	//   allowed. It must start with a letter or digit. If not specified, a value is automatically generated.
    37  	// + For 'public_key' type: The value contains 15 to 2048 characters, including letters, digits, and special
    38  	//   characters (_-!@#$%+/=). It must start with a letter, digit, plus sign (+), or slash (/). If not specified, a
    39  	//   value is automatically generated.
    40  	// + For 'aes' type: The value contains 16 characters, including letters, digits, and special
    41  	//   characters (_-!@#$%+/=). It must start with a letter, digit, plus sign (+), or slash (/). If not specified, a
    42  	//   value is automatically generated.
    43  	SignSecret string `json:"sign_secret,omitempty"`
    44  	// Signature algorithm. Specify a signature algorithm only when using an AES signature key. By default, no algorithm
    45  	// is used.
    46  	// + aes-128-cfb
    47  	// + aes-256-cfb
    48  	SignAlgorithm string `json:"sign_algorithm,omitempty"`
    49  }
    50  
    51  var requestOpts = golangsdk.RequestOpts{
    52  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    53  }
    54  
    55  // Create is a method used to create a new signature key using given parameters.
    56  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Signature, error) {
    57  	b, err := golangsdk.BuildRequestBody(opts, "")
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	var r Signature
    63  	_, err = c.Post(rootURL(c, opts.InstanceId), b, &r, &golangsdk.RequestOpts{
    64  		MoreHeaders: requestOpts.MoreHeaders,
    65  	})
    66  	return &r, err
    67  }
    68  
    69  // ListOpts is the structure used to querying signature list.
    70  type ListOpts struct {
    71  	// The instnace ID to which the signature belongs.
    72  	InstanceId string `json:"-" required:"true"`
    73  	// Offset from which the query starts.
    74  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    75  	Offset int `q:"offset"`
    76  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
    77  	Limit int `q:"limit"`
    78  	// The signature ID.
    79  	ID string `q:"id"`
    80  	// The signature name.
    81  	Name string `q:"name"`
    82  	// Parameter name (only 'name' is supported) for exact matching.
    83  	PreciseSearch string `q:"precise_search"`
    84  }
    85  
    86  // List is a method to obtain the signature list under a specified instance using given parameters.
    87  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Signature, error) {
    88  	url := rootURL(c, opts.InstanceId)
    89  	query, err := golangsdk.BuildQueryString(opts)
    90  	if err != nil {
    91  		return nil, err
    92  	}
    93  	url += query.String()
    94  
    95  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    96  		p := SignaturePage{pagination.OffsetPageBase{PageResult: r}}
    97  		return p
    98  	}).AllPages()
    99  
   100  	if err != nil {
   101  		return nil, err
   102  	}
   103  	return ExtractSignatures(pages)
   104  }
   105  
   106  // UpdateOpts is the structure used to update the signature key.
   107  type UpdateOpts struct {
   108  	// The instnace ID to which the signature belongs.
   109  	InstanceId string `json:"-" required:"true"`
   110  	// The signature ID.
   111  	SignatureId string `json:"-" required:"true"`
   112  	// Signature key name. It can contain letters, digits, and underscores(_) and must start with a letter.
   113  	Name string `json:"name" required:"true"`
   114  	// Signature key type.
   115  	// + hmac
   116  	// + basic
   117  	// + public_key
   118  	// + aes
   119  	SignType string `json:"sign_type,omitempty"`
   120  	// Signature key.
   121  	// + For 'hmac' type: The value contains 8 to 32 characters, including letters, digits, underscores (_), and
   122  	//   hyphens (-). It must start with a letter or digit. If not specified, a key is automatically generated.
   123  	// + For 'basic' type: The value contains 4 to 32 characters, including letters, digits, underscores (_), and
   124  	//   hyphens (-). It must start with a letter. If not specified, a key is automatically generated.
   125  	// + For 'public_key' type: The value contains 8 to 512 characters, including letters, digits, and special
   126  	//   characters (_-+/=). It must start with a letter, digit, plus sign (+), or slash (/). If not specified, a key
   127  	//   is automatically generated.
   128  	// + For 'aes' type: The value contains 16 characters if the aes-128-cfb algorithm is used, or 32 characters if the
   129  	//   aes-256-cfb algorithm is used. Letters, digits, and special characters (_-!@#$%+/=) are allowed. It must start
   130  	//   with a letter, digit, plus sign (+), or slash (/). If not specified, a key is automatically generated.
   131  	SignKey string `json:"sign_key,omitempty"`
   132  	// Signature secret.
   133  	// + For 'hmac' type: The value contains 16 to 64 characters. Letters, digits, and special characters (_-!@#$%) are
   134  	//   allowed. It must start with a letter or digit. If not specified, a value is automatically generated.
   135  	// + For 'basic' type: The value contains 8 to 64 characters. Letters, digits, and special characters (_-!@#$%) are
   136  	//   allowed. It must start with a letter or digit. If not specified, a value is automatically generated.
   137  	// + For 'public_key' type: The value contains 15 to 2048 characters, including letters, digits, and special
   138  	//   characters (_-!@#$%+/=). It must start with a letter, digit, plus sign (+), or slash (/). If not specified, a
   139  	//   value is automatically generated.
   140  	// + For 'aes' type: The value contains 16 characters, including letters, digits, and special
   141  	//   characters (_-!@#$%+/=). It must start with a letter, digit, plus sign (+), or slash (/). If not specified, a
   142  	//   value is automatically generated.
   143  	SignSecret string `json:"sign_secret,omitempty"`
   144  	// Signature algorithm. Specify a signature algorithm only when using an AES signature key. By default, no algorithm
   145  	// is used.
   146  	// + aes-128-cfb
   147  	// + aes-256-cfb
   148  	SignAlgorithm string `json:"sign_algorithm,omitempty"`
   149  }
   150  
   151  // Update is a method used to update a signature using given parameters.
   152  func Update(c *golangsdk.ServiceClient, opts UpdateOpts) (*Signature, error) {
   153  	b, err := golangsdk.BuildRequestBody(opts, "")
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  
   158  	var r Signature
   159  	_, err = c.Put(resourceURL(c, opts.InstanceId, opts.SignatureId), b, &r, &golangsdk.RequestOpts{
   160  		MoreHeaders: requestOpts.MoreHeaders,
   161  	})
   162  	return &r, err
   163  }
   164  
   165  // Delete is a method to remove the specified signature using its ID and related dedicated instance ID.
   166  func Delete(c *golangsdk.ServiceClient, instanceId, signatureId string) error {
   167  	_, err := c.Delete(resourceURL(c, instanceId, signatureId), &golangsdk.RequestOpts{
   168  		MoreHeaders: requestOpts.MoreHeaders,
   169  	})
   170  	return err
   171  }
   172  
   173  // BindOpts is the structure that used to bind a signature to the published APIs.
   174  type BindOpts struct {
   175  	// The instnace ID to which the signature belongs.
   176  	InstanceId string `json:"-" required:"true"`
   177  	// Signature ID.
   178  	SignatureId string `json:"sign_id" required:"true"`
   179  	// The IDs of the API publish record.
   180  	PublishIds []string `json:"publish_ids" required:"true"`
   181  }
   182  
   183  // Bind is a method to bind a signature to one or more APIs.
   184  func Bind(c *golangsdk.ServiceClient, opts BindOpts) ([]SignBindApiInfo, error) {
   185  	b, err := golangsdk.BuildRequestBody(opts, "")
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	var r BindResp
   191  	_, err = c.Post(bindURL(c, opts.InstanceId), b, &r, nil)
   192  	return r.Bindings, err
   193  }
   194  
   195  // ListBindOpts is the structure used to querying published API list that signature associated.
   196  type ListBindOpts struct {
   197  	// The instnace ID to which the API belongs.
   198  	InstanceId string `json:"-" required:"true"`
   199  	// Offset from which the query starts.
   200  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   201  	Offset int `q:"offset"`
   202  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
   203  	Limit int `q:"limit"`
   204  	// The signature ID.
   205  	SignatureId string `q:"sign_id"`
   206  	// The environment ID where the API is published.
   207  	EnvId string `q:"env_id"`
   208  	// The API ID.
   209  	ApiId string `q:"api_id"`
   210  	// The API name.
   211  	ApiName string `q:"api_name"`
   212  	// The group ID where the API is located.
   213  	GroupId string `q:"group_id"`
   214  }
   215  
   216  // ListBind is a method to obtain all API to which the signature bound.
   217  func ListBind(c *golangsdk.ServiceClient, opts ListBindOpts) ([]SignBindApiInfo, error) {
   218  	url := listBindURL(c, opts.InstanceId)
   219  	query, err := golangsdk.BuildQueryString(opts)
   220  	if err != nil {
   221  		return nil, err
   222  	}
   223  	url += query.String()
   224  
   225  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   226  		p := BindPage{pagination.OffsetPageBase{PageResult: r}}
   227  		return p
   228  	}).AllPages()
   229  
   230  	if err != nil {
   231  		return nil, err
   232  	}
   233  	return ExtractBindInfos(pages)
   234  }
   235  
   236  // Unbind is an method used to unbind a specified API from the signature.
   237  func Unbind(c *golangsdk.ServiceClient, instanceId, bindId string) error {
   238  	_, err := c.Delete(unbindURL(c, instanceId, bindId), nil)
   239  	return err
   240  }