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

     1  package environments
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // EnvironmentOpts allows to create a new environment or update an existing environment using given parameters.
     9  type EnvironmentOpts struct {
    10  	// Environment name, which can contain 3 to 64 characters, starting with a letter.
    11  	// Only letters, digits and underscores (_) are allowed.
    12  	// Chinese characters must be in UTF-8 or Unicode format.
    13  	Name string `json:"name" required:"true"`
    14  	// Description of the environment, which can contain a maximum of 255 characters,
    15  	// and the angle brackets (< and >) are not allowed.
    16  	// Chinese characters must be in UTF-8 or Unicode format.
    17  	Description *string `json:"remark,omitempty"`
    18  }
    19  
    20  type EnvironmentOptsBuilder interface {
    21  	ToEnvironmentOptsMap() (map[string]interface{}, error)
    22  }
    23  
    24  func (opts EnvironmentOpts) ToEnvironmentOptsMap() (map[string]interface{}, error) {
    25  	return golangsdk.BuildRequestBody(opts, "")
    26  }
    27  
    28  // Create is a method by which to create function that create a new environment.
    29  func Create(client *golangsdk.ServiceClient, instanceId string, opts EnvironmentOptsBuilder) (r CreateResult) {
    30  	reqBody, err := opts.ToEnvironmentOptsMap()
    31  	if err != nil {
    32  		r.Err = err
    33  		return
    34  	}
    35  	_, r.Err = client.Post(rootURL(client, instanceId, "envs"), reqBody, &r.Body, nil)
    36  	return
    37  }
    38  
    39  // Update is a method by which to udpate an existing environment.
    40  func Update(client *golangsdk.ServiceClient, instanceId, envId string, opts EnvironmentOptsBuilder) (r UpdateResult) {
    41  	reqBody, err := opts.ToEnvironmentOptsMap()
    42  	if err != nil {
    43  		r.Err = err
    44  		return
    45  	}
    46  	_, r.Err = client.Put(resourceURL(client, instanceId, "envs", envId), reqBody, &r.Body, &golangsdk.RequestOpts{
    47  		OkCodes: []int{200},
    48  	})
    49  	return
    50  }
    51  
    52  // ListOpts allows to filter list data using given parameters.
    53  type ListOpts struct {
    54  	// Environment name.
    55  	Name string `q:"name"`
    56  	// Offset from which the query starts.
    57  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    58  	Offset int `q:"offset"`
    59  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
    60  	Limit int `q:"limit"`
    61  }
    62  
    63  type ListOptsBuilder interface {
    64  	ToListQuery() (string, error)
    65  }
    66  
    67  func (opts ListOpts) ToListQuery() (string, error) {
    68  	q, err := golangsdk.BuildQueryString(opts)
    69  	if err != nil {
    70  		return "", err
    71  	}
    72  	return q.String(), err
    73  }
    74  
    75  // List is a method to obtain an array of one or more groups according to the query parameters.
    76  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOptsBuilder) pagination.Pager {
    77  	url := rootURL(client, instanceId, "envs")
    78  	if opts != nil {
    79  		query, err := opts.ToListQuery()
    80  		if err != nil {
    81  			return pagination.Pager{Err: err}
    82  		}
    83  		url += query
    84  	}
    85  
    86  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    87  		return EnvironmentPage{pagination.SinglePageBase(r)}
    88  	})
    89  }
    90  
    91  // Delete is a method to delete an existing group.
    92  func Delete(client *golangsdk.ServiceClient, instanceId, envId string) (r DeleteResult) {
    93  	_, r.Err = client.Delete(resourceURL(client, instanceId, "envs", envId), nil)
    94  	return
    95  }
    96  
    97  // CreateVariableOpts allows to create a new envirable variable for an existing group using given parameters.
    98  type CreateVariableOpts struct {
    99  	// Variable name, which can contain 3 to 32 characters, starting with a letter.
   100  	// Only letters, digits, hyphens (-), and underscores (_) are allowed.
   101  	// In the definition of an API, #Name# (case-sensitive) indicates a variable.
   102  	// It is replaced by the actual value when the API is published in an environment.
   103  	Name string `json:"variable_name" required:"true"`
   104  	// Variable value, which can contain 1 to 255 characters.
   105  	// Only letters, digits, and special characters (_-/.:) are allowed.
   106  	Value string `json:"variable_value" required:"true"`
   107  	// Environment ID.
   108  	EnvId string `json:"env_id" required:"true"`
   109  	// Group ID.
   110  	GroupId string `json:"group_id" required:"true"`
   111  }
   112  
   113  type CreateVariableOptsBuilder interface {
   114  	ToCreateVariableOptsMap() (map[string]interface{}, error)
   115  }
   116  
   117  func (opts CreateVariableOpts) ToCreateVariableOptsMap() (map[string]interface{}, error) {
   118  	return golangsdk.BuildRequestBody(opts, "")
   119  }
   120  
   121  // CreateVariable is a method by which to create function that create a new environment variable.
   122  func CreateVariable(client *golangsdk.ServiceClient, instanceId string,
   123  	opts CreateVariableOptsBuilder) (r VariableCreateResult) {
   124  	reqBody, err := opts.ToCreateVariableOptsMap()
   125  	if err != nil {
   126  		r.Err = err
   127  		return
   128  	}
   129  	_, r.Err = client.Post(rootURL(client, instanceId, "env-variables"), reqBody, &r.Body, nil)
   130  	return
   131  }
   132  
   133  // GetVariable is a method to obtain the specified environment variable according to the instance id and variable id.
   134  func GetVariable(client *golangsdk.ServiceClient, instanceId, varId string) (r VariableGetResult) {
   135  	_, r.Err = client.Get(resourceURL(client, instanceId, "env-variables", varId), &r.Body, nil)
   136  	return
   137  }
   138  
   139  // ListVariablesOpts allows to filter list data using given parameters.
   140  type ListVariablesOpts struct {
   141  	// API group ID.
   142  	GroupId string `q:"group_id"`
   143  	// Environment ID.
   144  	EnvId string `q:"env_id"`
   145  	// Variable name.
   146  	Name string `q:"variable_name"`
   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  	// Parameter name for exact matching. Only API variable names are supported.
   153  	PreciseSearch string `q:"precise_search"`
   154  }
   155  
   156  type ListVariablesOptsBuilder interface {
   157  	ToListVariablesQuery() (string, error)
   158  }
   159  
   160  func (opts ListVariablesOpts) ToListVariablesQuery() (string, error) {
   161  	q, err := golangsdk.BuildQueryString(opts)
   162  	if err != nil {
   163  		return "", err
   164  	}
   165  	return q.String(), err
   166  }
   167  
   168  // ListVariables is a method to obtain an array of one or more variables according to the query parameters.
   169  func ListVariables(client *golangsdk.ServiceClient, instanceId string, opts ListVariablesOptsBuilder) pagination.Pager {
   170  	url := rootURL(client, instanceId, "env-variables")
   171  	if opts != nil {
   172  		query, err := opts.ToListVariablesQuery()
   173  		if err != nil {
   174  			return pagination.Pager{Err: err}
   175  		}
   176  		url += query
   177  	}
   178  
   179  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   180  		return VariablePage{pagination.SinglePageBase(r)}
   181  	})
   182  }
   183  
   184  // DeleteVariable is a method to delete an existing variable.
   185  func DeleteVariable(client *golangsdk.ServiceClient, instanceId, varId string) (r DeleteResult) {
   186  	_, r.Err = client.Delete(resourceURL(client, instanceId, "env-variables", varId), nil)
   187  	return
   188  }