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

     1  package applications
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // AppOpts allows to create or update an application using given parameters.
     9  type AppOpts struct {
    10  	// Application name, which can contain 3 to 64 characters, starting with a letter.
    11  	// Only letters, digits and underscores (_) are allowed.
    12  	Name string `json:"name" required:"true"`
    13  	// Description of the application, which can contain a maximum of 255 characters.
    14  	// Chinese characters must be in UTF-8 or Unicode format.
    15  	Description string `json:"remark,omitempty"`
    16  	// Application key, which can contain 8 to 64 characters, starting with a letter or digit.
    17  	// Only letters, digits, hyphens (-) and underscores (_) are allowed.
    18  	AppKey string `json:"app_key,omitempty"`
    19  	// Application secret, which can contain 8 to 64 characters, starting with a letter or digit.
    20  	// Only letters, digits and the following special characters are allowed: _-!@#$%
    21  	AppSecret string `json:"app_secret,omitempty"`
    22  }
    23  
    24  type AppOptsBuilder interface {
    25  	ToAppOptsMap() (map[string]interface{}, error)
    26  }
    27  
    28  func (opts AppOpts) ToAppOptsMap() (map[string]interface{}, error) {
    29  	return golangsdk.BuildRequestBody(opts, "")
    30  }
    31  
    32  // Create is a method by which to create function that create a APIG application.
    33  func Create(client *golangsdk.ServiceClient, instanceId string, opts AppOptsBuilder) (r CreateResult) {
    34  	reqBody, err := opts.ToAppOptsMap()
    35  	if err != nil {
    36  		r.Err = err
    37  		return
    38  	}
    39  	_, r.Err = client.Post(rootURL(client, instanceId), reqBody, &r.Body, nil)
    40  	return
    41  }
    42  
    43  func Update(client *golangsdk.ServiceClient, instanceId, appId string, opts AppOptsBuilder) (r UpdateResult) {
    44  	reqBody, err := opts.ToAppOptsMap()
    45  	if err != nil {
    46  		r.Err = err
    47  		return
    48  	}
    49  	_, r.Err = client.Put(resourceURL(client, instanceId, appId), reqBody, &r.Body, &golangsdk.RequestOpts{
    50  		OkCodes: []int{200},
    51  	})
    52  	return
    53  }
    54  
    55  //Get is a method to obtain the specified application according to the instanceId and appId.
    56  func Get(client *golangsdk.ServiceClient, instanceId, appId string) (r GetResult) {
    57  	_, r.Err = client.Get(resourceURL(client, instanceId, appId), &r.Body, nil)
    58  	return
    59  }
    60  
    61  // ListOpts allows to filter list data using given parameters.
    62  type ListOpts struct {
    63  	// App ID.
    64  	Id string `q:"id"`
    65  	// App name.
    66  	Name string `q:"name"`
    67  	// App status.
    68  	Status string `q:"status"`
    69  	// App key.
    70  	AppKey string `q:"app_key"`
    71  	// Creator of the application.
    72  	//     USER: The app is created by the API user.
    73  	//     MARKET: The app is allocated by the marketplace.
    74  	Creator string `q:"creator"`
    75  	// Offset from which the query starts.
    76  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    77  	Offset int `q:"offset"`
    78  	// Number of items displayed on each page.
    79  	Limit int `q:"limit"`
    80  	// Parameter name (name) for exact matching.
    81  	PreciseSearch string `q:"precise_search"`
    82  }
    83  
    84  type ListOptsBuilder interface {
    85  	ToAppListQuery() (string, error)
    86  }
    87  
    88  func (opts ListOpts) ToAppListQuery() (string, error) {
    89  	q, err := golangsdk.BuildQueryString(opts)
    90  	if err != nil {
    91  		return "", err
    92  	}
    93  	return q.String(), err
    94  }
    95  
    96  // List is a method to obtain an array of one or more APIG applications according to the query parameters.
    97  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOptsBuilder) pagination.Pager {
    98  	url := rootURL(client, instanceId)
    99  	if opts != nil {
   100  		query, err := opts.ToAppListQuery()
   101  		if err != nil {
   102  			return pagination.Pager{Err: err}
   103  		}
   104  		url += query
   105  	}
   106  
   107  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   108  		return ApplicationPage{pagination.SinglePageBase(r)}
   109  	})
   110  }
   111  
   112  // SecretResetOpts allows to reset application secret value using given parameters.
   113  type SecretResetOpts struct {
   114  	AppSecret string `json:"app_secret"`
   115  }
   116  
   117  type SecretResetOptsBuilder interface {
   118  	ToSecretResetOptsMap() (map[string]interface{}, error)
   119  }
   120  
   121  func (opts SecretResetOpts) ToSecretResetOptsMap() (map[string]interface{}, error) {
   122  	return golangsdk.BuildRequestBody(opts, "")
   123  }
   124  
   125  func ResetAppSecret(client *golangsdk.ServiceClient, instanceId, appId string,
   126  	opts SecretResetOptsBuilder) (r ResetSecretResult) {
   127  	reqBody, err := opts.ToSecretResetOptsMap()
   128  	if err != nil {
   129  		r.Err = err
   130  		return
   131  	}
   132  	_, r.Err = client.Put(resetSecretURL(client, instanceId, appId), reqBody, &r.Body, &golangsdk.RequestOpts{
   133  		OkCodes: []int{200},
   134  	})
   135  	return
   136  }
   137  
   138  // Delete is a method to delete an existing application.
   139  func Delete(client *golangsdk.ServiceClient, instanceId, appId string) (r DeleteResult) {
   140  	_, r.Err = client.Delete(resourceURL(client, instanceId, appId), nil)
   141  	return
   142  }
   143  
   144  // AppCodeOpts allows to create an application code using given parameters.
   145  type AppCodeOpts struct {
   146  	// AppCode value, which contains 64 to 180 characters, starting with a letter, plus sign (+) or slash (/).
   147  	// Only letters and the following special characters are allowed: +-_!@#$%/=
   148  	AppCode string `json:"app_code" required:"true"`
   149  }
   150  
   151  type AppCodeOptsBuilder interface {
   152  	ToAppCodeOptsMap() (map[string]interface{}, error)
   153  }
   154  
   155  func (opts AppCodeOpts) ToAppCodeOptsMap() (map[string]interface{}, error) {
   156  	return golangsdk.BuildRequestBody(opts, "")
   157  }
   158  
   159  // CreateAppCode is a method by which to create function that create a code at sepcified APIG application using
   160  // instanceId, appId and AppCodeOpts (code value).
   161  func CreateAppCode(client *golangsdk.ServiceClient, instanceId, appId string,
   162  	opts AppCodeOptsBuilder) (r CreateCodeResult) {
   163  	reqBody, err := opts.ToAppCodeOptsMap()
   164  	if err != nil {
   165  		r.Err = err
   166  		return
   167  	}
   168  	_, r.Err = client.Post(codeURL(client, instanceId, appId), reqBody, &r.Body, nil)
   169  	return
   170  }
   171  
   172  // AutoGenerateAppCode is a method used to automatically create code in a specified application.
   173  func AutoGenerateAppCode(client *golangsdk.ServiceClient, instanceId, appId string) (r AutoGenerateCodeResult) {
   174  	_, r.Err = client.Put(codeURL(client, instanceId, appId), nil, &r.Body, nil)
   175  	return
   176  }
   177  
   178  // GetAppCode is a method to obtain the specified code of the specified application of the specified instance using
   179  // instanceId, appId and codeId.
   180  func GetAppCode(client *golangsdk.ServiceClient, instanceId, appId, codeId string) (r GetCodeResult) {
   181  	_, r.Err = client.Get(codeResourceURL(client, instanceId, appId, codeId), &r.Body, &golangsdk.RequestOpts{
   182  		OkCodes: []int{200, 201},
   183  	})
   184  	return
   185  }
   186  
   187  // ListCodeOpts allows to filter application code list data using given parameters.
   188  type ListCodeOpts struct {
   189  	// Offset from which the query starts.
   190  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   191  	Offset int `q:"offset"`
   192  	// Number of items displayed on each page.
   193  	Limit int `q:"limit"`
   194  }
   195  
   196  type ListCodeOptsBuilder interface {
   197  	ToAppCodeListQuery() (string, error)
   198  }
   199  
   200  func (opts ListCodeOpts) ToAppCodeListQuery() (string, error) {
   201  	q, err := golangsdk.BuildQueryString(opts)
   202  	if err != nil {
   203  		return "", err
   204  	}
   205  	return q.String(), err
   206  }
   207  
   208  // ListAppCode is a method to obtain the application code list of the specified application of the specified instance.
   209  func ListAppCode(client *golangsdk.ServiceClient, instanceId, appId string, opts ListCodeOptsBuilder) pagination.Pager {
   210  	url := codeURL(client, instanceId, appId)
   211  	if opts != nil {
   212  		query, err := opts.ToAppCodeListQuery()
   213  		if err != nil {
   214  			return pagination.Pager{Err: err}
   215  		}
   216  		url += query
   217  	}
   218  
   219  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   220  		return AppCodePage{pagination.SinglePageBase(r)}
   221  	})
   222  }
   223  
   224  //RemoveAppCode is a method to delete an existing code from a specified application.
   225  func RemoveAppCode(client *golangsdk.ServiceClient, instanceId, appId, codeId string) (r DeleteResult) {
   226  	_, r.Err = client.Delete(codeResourceURL(client, instanceId, appId, codeId), nil)
   227  	return
   228  }