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

     1  package appauths
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure that used to authorize APIs to allow applications access.
     9  type CreateOpts struct {
    10  	// The Dedicated instance ID.
    11  	InstanceId string `json:"-" required:"true"`
    12  	// The ID of the environment in which the apps will be authorized.
    13  	EnvId string `json:"env_id" required:"true"`
    14  	// The ID list of the applications authorized to access the APIs.
    15  	AppIds []string `json:"app_ids" required:"true"`
    16  	// The authorized API IDs.
    17  	ApiIds []string `json:"api_ids" required:"true"`
    18  }
    19  
    20  var requestOpts = golangsdk.RequestOpts{
    21  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    22  }
    23  
    24  // Create is a method used to authorize APIs to allow applications access using given parameters.
    25  func Create(c *golangsdk.ServiceClient, opts CreateOpts) ([]Authorization, error) {
    26  	b, err := golangsdk.BuildRequestBody(opts, "")
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	var r createResp
    32  	_, err = c.Post(rootURL(c, opts.InstanceId), b, &r, &golangsdk.RequestOpts{
    33  		MoreHeaders: requestOpts.MoreHeaders,
    34  	})
    35  	return r.Auths, err
    36  }
    37  
    38  // ListOpts is the structure used to querying authorized and unauthorized API information.
    39  type ListOpts struct {
    40  	// The instnace ID to which the application and APIs belong.
    41  	InstanceId string `json:"-" required:"true"`
    42  	// The application ID.
    43  	AppId string `q:"app_id" required:"true"`
    44  	// Offset from which the query starts.
    45  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    46  	Offset int `q:"offset"`
    47  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
    48  	Limit int `q:"limit"`
    49  	// The authorized API ID.
    50  	ApiId string `q:"app_id"`
    51  	// The authorized API name.
    52  	ApiName string `q:"api_name"`
    53  	// The ID of the API group to which the authorized APIs belong.
    54  	GroupId string `q:"group_id"`
    55  	// The name of the API group to which the authorized APIs belong.
    56  	GroupName string `q:"group_name"`
    57  	// Parameter name (only 'name' is supported) for exact matching.
    58  	EnvId string `q:"env_id"`
    59  }
    60  
    61  // List is a method to obtain the authorized API list under a specified application using given parameters.
    62  func ListAuthorized(c *golangsdk.ServiceClient, opts ListOpts) ([]ApiAuthInfo, error) {
    63  	url := listAuthorizedURL(c, opts.InstanceId)
    64  	query, err := golangsdk.BuildQueryString(opts)
    65  	if err != nil {
    66  		return nil, err
    67  	}
    68  	url += query.String()
    69  
    70  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    71  		p := AuthorizedPage{pagination.OffsetPageBase{PageResult: r}}
    72  		return p
    73  	}).AllPages()
    74  
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	return ExtractAuthorizedApis(pages)
    79  }
    80  
    81  // List is a method to obtain the unauthorized API list under a specified application using given parameters.
    82  func ListUnauthorized(c *golangsdk.ServiceClient, opts ListOpts) ([]ApiOutlineInfo, error) {
    83  	url := listUnathorizedURL(c, opts.InstanceId)
    84  	query, err := golangsdk.BuildQueryString(opts)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	url += query.String()
    89  
    90  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    91  		p := UnauthorizedPage{pagination.OffsetPageBase{PageResult: r}}
    92  		return p
    93  	}).AllPages()
    94  
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	return ExtractUnauthorizedApis(pages)
    99  }
   100  
   101  // Delete is a method used to unauthorize API from specified application using given parameters.
   102  func Delete(c *golangsdk.ServiceClient, instanceId, authId string) error {
   103  	_, err := c.Delete(resourceURL(c, instanceId, authId), &golangsdk.RequestOpts{
   104  		MoreHeaders: requestOpts.MoreHeaders,
   105  	})
   106  	return err
   107  }