github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/apigw/shared/v1/environments/requests.go (about)

     1  package environments
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  type ListOpts struct {
     9  	// Environment name.
    10  	EnvName string `q:"name"`
    11  	// Number of records displayed on each page. The default value is 20.
    12  	PageSize int `q:"page_size"`
    13  	// Page number. The default value is 1.
    14  	PageNum int `q:"page_no"`
    15  	// Parameter name for exact matching.
    16  	PreciseSearch string `q:"precise_search"`
    17  	// Limit number of records displayed on each page. The default value is 20.
    18  	Limit int `q:"limit"`
    19  	// Offset Page number. The default value is 1.
    20  	Offset int `q:"offset"`
    21  }
    22  
    23  // List is a method to obtain an array of one or more environments according to the query parameters.
    24  // Note: The list returned by the function only contains the environment of the first page. This is because the return
    25  // body does not contain page number information, so the page number of the next page cannot be obtained.
    26  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Environment, error) {
    27  	url := rootURL(c)
    28  	query, err := golangsdk.BuildQueryString(opts)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	url += query.String()
    33  
    34  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    35  		return EnvironmentPage{pagination.SinglePageBase(r)}
    36  	}).AllPages()
    37  	if err != nil {
    38  		return nil, err
    39  	}
    40  
    41  	var s []Environment
    42  	err = pages.(EnvironmentPage).Result.ExtractIntoSlicePtr(&s, "envs")
    43  	return s, err
    44  }
    45  
    46  // CreateOpts allows to create a new APIG environment using given parameters
    47  type CreateOpts struct {
    48  	// Environment name, which can contain 3 to 64 characters, starting with a letter.
    49  	// Only letters, digits and underscores (_) are allowed.
    50  	// Chinese characters must be in UTF-8 or Unicode format.
    51  	Name string `json:"name" required:"true"`
    52  	// Description of the environment, which can contain a maximum of 255 characters,
    53  	// and the angle brackets (< and >) are not allowed.
    54  	// Chinese characters must be in UTF-8 or Unicode format.
    55  	Description string `json:"remark,omitempty"`
    56  }
    57  
    58  var requestOpts = golangsdk.RequestOpts{
    59  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    60  }
    61  
    62  // Create is a method to create a new APIG shared environment using given parameters.
    63  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*Environment, error) {
    64  	b, err := golangsdk.BuildRequestBody(opts, "")
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	var r Environment
    70  	_, err = c.Post(rootURL(c), b, &r, &golangsdk.RequestOpts{
    71  		MoreHeaders: requestOpts.MoreHeaders,
    72  	})
    73  	return &r, err
    74  }
    75  
    76  // UpdateOpts allows to update an existing APIG environment using given parameters
    77  type UpdateOpts struct {
    78  	// Name of the APIG shared environment.
    79  	Name string `json:"name" required:"true"`
    80  	// Description of the APIG shared environment.
    81  	Description *string `json:"remark,omitempty"`
    82  }
    83  
    84  // Update is a method to update a APIG shared environment using given parameters.
    85  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOpts) (*Environment, error) {
    86  	b, err := golangsdk.BuildRequestBody(opts, "")
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	var r Environment
    92  	_, err = c.Put(environmentURL(c, id), b, &r, &golangsdk.RequestOpts{
    93  		MoreHeaders: requestOpts.MoreHeaders,
    94  	})
    95  	return &r, err
    96  }
    97  
    98  // Delete is a method to delete an existing APIG shared environment.
    99  func Delete(c *golangsdk.ServiceClient, id string) error {
   100  	_, err := c.Delete(environmentURL(c, id), &golangsdk.RequestOpts{
   101  		MoreHeaders: requestOpts.MoreHeaders,
   102  	})
   103  	return err
   104  }