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

     1  package applications
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure required by the Create method to create a new application.
     9  type CreateOpts struct {
    10  	// Specified the application name with 2 to 64 characters long.
    11  	// It consists of English letters, numbers, underscores (-), and underscores (_).
    12  	// It must start with an English letter and end with an English letter or number.
    13  	Name string `json:"name" required:"true"`
    14  	// Specified the application description.
    15  	// The description can contain a maximum of 96 characters.
    16  	Description *string `json:"description,omitempty"`
    17  	// Specified the enterprise project ID.
    18  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    19  }
    20  
    21  var requestOpts = golangsdk.RequestOpts{
    22  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    23  }
    24  
    25  // Create is a method to create a new ServiceStage application using create option.
    26  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Application, error) {
    27  	b, err := golangsdk.BuildRequestBody(opts, "")
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	var rst Application
    33  	_, err = c.Post(rootURL(c), b, &rst, &golangsdk.RequestOpts{
    34  		MoreHeaders: requestOpts.MoreHeaders,
    35  	})
    36  	return &rst, err
    37  }
    38  
    39  // Get is a method to obtain the details of a specified ServiceStage application using its ID.
    40  func Get(c *golangsdk.ServiceClient, appId string) (*Application, error) {
    41  	var r Application
    42  	_, err := c.Get(resourceURL(c, appId), &r, &golangsdk.RequestOpts{
    43  		MoreHeaders: requestOpts.MoreHeaders,
    44  	})
    45  	return &r, err
    46  }
    47  
    48  // ListOpts allows to filter list data using given parameters.
    49  type ListOpts struct {
    50  	// Number of records to be queried.
    51  	// Value range: 0–100.
    52  	// Default value: 1000, indicating that a maximum of 1000 records can be queried and all records are displayed on
    53  	// the same page.
    54  	Limit int `q:"limit"`
    55  	// The offset number.
    56  	Offset int `q:"offset"`
    57  	// Sorting field. By default, query results are sorted by creation time.
    58  	// The following enumerated values are supported: create_time, name, and update_time.
    59  	OrderBy string `q:"order_by"`
    60  	// Descending or ascending order. Default value: desc.
    61  	Order string `q:"order"`
    62  }
    63  
    64  // List is a method to query the list of the environments using given opts.
    65  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Application, error) {
    66  	url := rootURL(c)
    67  	query, err := golangsdk.BuildQueryString(opts)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  	url += query.String()
    72  
    73  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    74  		p := ApplicationPage{pagination.OffsetPageBase{PageResult: r}}
    75  		return p
    76  	}).AllPages()
    77  
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return ExtractApplications(pages)
    82  }
    83  
    84  // UpdateOpts is the structure required by the Update method to update the configuration of the specified environment.
    85  type UpdateOpts struct {
    86  	// Specified the application description.
    87  	// The description can contain a maximum of 96 characters.
    88  	Description *string `json:"description,omitempty"`
    89  	// Specified the environment name with 2 to 64 characters long.
    90  	// It consists of English letters, numbers, underscores (-), and underscores (_).
    91  	// It must start with an English letter and end with an English letter or number.
    92  	Name string `json:"name,omitempty"`
    93  }
    94  
    95  // Update is a method to update the current dependency configuration.
    96  func Update(c *golangsdk.ServiceClient, appId string, opts UpdateOpts) (*Application, error) {
    97  	b, err := golangsdk.BuildRequestBody(opts, "")
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	var r Application
   103  	_, err = c.Put(resourceURL(c, appId), b, &r, &golangsdk.RequestOpts{
   104  		MoreHeaders: requestOpts.MoreHeaders,
   105  	})
   106  	return &r, err
   107  }
   108  
   109  // Delete is a method to remove an existing application.
   110  func Delete(c *golangsdk.ServiceClient, appId string) *golangsdk.ErrResult {
   111  	var r golangsdk.ErrResult
   112  	_, r.Err = c.Delete(resourceURL(c, appId), &golangsdk.RequestOpts{
   113  		MoreHeaders: requestOpts.MoreHeaders,
   114  	})
   115  	return &r
   116  }
   117  
   118  // ConfigOpts is a structure required bu the UpdateConfig method to bind or unbind the resources.
   119  type ConfigOpts struct {
   120  	// The environment ID.
   121  	EnvId string `json:"environment_id" required:"true"`
   122  	// The application configurations, such as public environment variables.
   123  	Configuration Configuration `json:"configuration" required:"true"`
   124  }
   125  
   126  // Configuration is an object specifying the configuration of the application for a specified environment.
   127  type Configuration struct {
   128  	// The environment variables of the application.
   129  	// If the names of multiple environment variables are the same, only the last environment variable takes effact.
   130  	EnvVariables []Variable `json:"env,omitempty"`
   131  }
   132  
   133  // Variable is an object specifying the key/value pair of the environment variable.
   134  type Variable struct {
   135  	// The name of the environment variable.
   136  	// The name contains 1 to 64 characters, including letters, digits, underscores (_), hyphens (-) and dots (.), and
   137  	// cannot start with a digit or dot.
   138  	Name string `json:"name" required:"true"`
   139  	// The value of the environment variables.
   140  	Value string `json:"value" required:"true"`
   141  }
   142  
   143  // UpdateConfig is a method to add or remove the basic resources and the optional resources.
   144  func UpdateConfig(c *golangsdk.ServiceClient, appId, envId string, config Configuration) (*Application, error) {
   145  	opts := ConfigOpts{
   146  		EnvId:         envId,
   147  		Configuration: config,
   148  	}
   149  	b, err := golangsdk.BuildRequestBody(opts, "")
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	var r Application
   155  	_, err = c.Put(configURL(c, appId), b, &r, &golangsdk.RequestOpts{
   156  		MoreHeaders: requestOpts.MoreHeaders,
   157  	})
   158  	return &r, err
   159  }
   160  
   161  // ListConfigOpts allows to filter list configurations using given parameters.
   162  type ListConfigOpts struct {
   163  	// The environment ID. If this parameter is not specified, all environment are queried.
   164  	EnvId string `q:"environment_id"`
   165  }
   166  
   167  // ListConfig is a method to query the list of configurations from the specified application.
   168  func ListConfig(c *golangsdk.ServiceClient, appId string, opts ListConfigOpts) ([]ConfigResp, error) {
   169  	url := configURL(c, appId)
   170  	query, err := golangsdk.BuildQueryString(opts)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  	url += query.String()
   175  
   176  	var rst golangsdk.Result
   177  	_, err = c.Get(url, &rst.Body, &golangsdk.RequestOpts{
   178  		MoreHeaders: requestOpts.MoreHeaders,
   179  	})
   180  	if err != nil {
   181  		return nil, err
   182  	}
   183  	var r []ConfigResp
   184  	rst.ExtractIntoSlicePtr(&r, "configuration")
   185  	return r, nil
   186  }
   187  
   188  // DeleteConfig is a method to remove an existing configuration or some variables from a specified environment.
   189  func DeleteConfig(c *golangsdk.ServiceClient, appId, envId string) *golangsdk.ErrResult {
   190  	var r golangsdk.ErrResult
   191  
   192  	url := configURL(c, appId)
   193  	opts := ListConfigOpts{
   194  		EnvId: envId,
   195  	}
   196  	query, err := golangsdk.BuildQueryString(opts)
   197  	if err != nil {
   198  		r.Err = err
   199  	}
   200  	url += query.String()
   201  
   202  	_, r.Err = c.Delete(url, &golangsdk.RequestOpts{
   203  		MoreHeaders: requestOpts.MoreHeaders,
   204  	})
   205  	return &r
   206  }