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

     1  package endpoints
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // BatchOpts is the structure that used to add or remove permissions for endpoint service.
     9  type BatchOpts struct {
    10  	// The instance ID to which the endpoint service belong.
    11  	InstanceId string `json:"-" required:"true"`
    12  	// The permissions of endpoint service to add or remove.
    13  	Permissions []string `json:"permissions" required:"true"`
    14  }
    15  
    16  var requestOpts = golangsdk.RequestOpts{
    17  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    18  }
    19  
    20  // AddPermissions is a method used to batch add permissions to endpoint service using given parameters.
    21  func AddPermissions(c *golangsdk.ServiceClient, opts BatchOpts) ([]string, error) {
    22  	b, err := golangsdk.BuildRequestBody(opts, "")
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	var r createResp
    28  	_, err = c.Post(addURL(c, opts.InstanceId), b, &r, &golangsdk.RequestOpts{
    29  		MoreHeaders: requestOpts.MoreHeaders,
    30  	})
    31  	return r.Permissions, err
    32  }
    33  
    34  // ListOpts is the structure that used to query the permissions of endpoint service.
    35  type ListOpts struct {
    36  	// The instance ID to which the endpoint service belong.
    37  	InstanceId string `json:"-" required:"true"`
    38  	// The permission of endpoint service.
    39  	Permission string `json:"permission"`
    40  	// Offset value. The value must be a positive integer.
    41  	Offset int `q:"offset"`
    42  	// Number of records displayed per page.
    43  	// The value must be a positive integer.
    44  	Limit int `q:"limit"`
    45  }
    46  
    47  // ListPermissions is a method used to query the permissions of endpoint service with given parameters.
    48  func ListPermissions(c *golangsdk.ServiceClient, opts ListOpts) ([]EndpointPermission, error) {
    49  	url := listURL(c, opts.InstanceId)
    50  	query, err := golangsdk.BuildQueryString(opts)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	url += query.String()
    55  
    56  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    57  		p := PermissionPage{pagination.OffsetPageBase{PageResult: r}}
    58  		return p
    59  	}).AllPages()
    60  
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return ExtractPermissions(pages)
    65  }
    66  
    67  // DeletePermissions is a method used to batch delete permissions of endpoint service using given parameters.
    68  func DeletePermissions(c *golangsdk.ServiceClient, opts BatchOpts) error {
    69  	b, err := golangsdk.BuildRequestBody(opts, "")
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	_, err = c.Post(deleteURL(c, opts.InstanceId), b, nil, &golangsdk.RequestOpts{
    75  		MoreHeaders: requestOpts.MoreHeaders,
    76  	})
    77  	return err
    78  }