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

     1  package authorizers
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // CustomAuthOpts is a struct which will be used to create a new custom authorizer or
     9  // update an existing custom authorizer.
    10  type CustomAuthOpts struct {
    11  	// Custom authorizer name, which can contain 3 to 64 characters, starting with a letter.
    12  	// Only letters, digits, and underscores (_) are allowed.
    13  	Name string `json:"name" required:"true"`
    14  	// Custom authorizer type, which support 'FRONTEND' and 'BACKEND'.
    15  	Type string `json:"type" required:"true"`
    16  	// Authorizer type, and the value is 'FUNC'.
    17  	AuthorizerType string `json:"authorizer_type" required:"true"`
    18  	// Function URN.
    19  	AuthorizerURI string `json:"authorizer_uri" required:"true"`
    20  	// Indicates whether to send the body.
    21  	IsBodySend *bool `json:"need_body,omitempty"`
    22  	// Identity source.
    23  	Identities []AuthCreateIdentitiesReq `json:"identities,omitempty"`
    24  	// Maximum cache age. The maximum value is 3,600.
    25  	// The maximum length of time that authentication results can be cached for.
    26  	// A value of 0 means that results are not cached.
    27  	TTL *int `json:"ttl,omitempty"`
    28  	// User data.
    29  	UserData *string `json:"user_data,omitempty"`
    30  }
    31  
    32  // AuthCreateIdentitiesReq is an object which will be build up a indentities list.
    33  type AuthCreateIdentitiesReq struct {
    34  	// Parameter name.
    35  	Name string `json:"name" required:"true"`
    36  	// Parameter location, which support 'HEADER' and 'QUERY'.
    37  	Location string `json:"location" required:"true"`
    38  	// Parameter verification expression. The default value is null, indicating that no verification is performed.
    39  	Validation *string `json:"validation,omitempty"`
    40  }
    41  
    42  // CustomAuthOptsBuilder is an interface which to support request body build of
    43  // the custom authorizer creation and updation.
    44  type CustomAuthOptsBuilder interface {
    45  	ToCustomAuthOptsMap() (map[string]interface{}, error)
    46  }
    47  
    48  // ToCustomAuthOptsMap is a method which to build a request body by the CustomAuthOpts.
    49  func (opts CustomAuthOpts) ToCustomAuthOptsMap() (map[string]interface{}, error) {
    50  	return golangsdk.BuildRequestBody(opts, "")
    51  }
    52  
    53  // Create is a method to create a new custom authorizer.
    54  func Create(client *golangsdk.ServiceClient, instanceId string, opts CustomAuthOptsBuilder) (r CreateResult) {
    55  	reqBody, err := opts.ToCustomAuthOptsMap()
    56  	if err != nil {
    57  		r.Err = err
    58  		return
    59  	}
    60  	_, r.Err = client.Post(rootURL(client, instanceId), reqBody, &r.Body, nil)
    61  	return
    62  }
    63  
    64  // Update is a method to update an existing custom authorizer.
    65  func Update(client *golangsdk.ServiceClient, instanceId, authId string, opts CustomAuthOptsBuilder) (r UpdateResult) {
    66  	reqBody, err := opts.ToCustomAuthOptsMap()
    67  	if err != nil {
    68  		r.Err = err
    69  		return
    70  	}
    71  	_, r.Err = client.Put(resourceURL(client, instanceId, authId), reqBody, &r.Body, &golangsdk.RequestOpts{
    72  		OkCodes: []int{200},
    73  	})
    74  	return
    75  }
    76  
    77  // Get is a method to obtain an existing custom authorizer.
    78  func Get(client *golangsdk.ServiceClient, instanceId, authId string) (r GetResult) {
    79  	_, r.Err = client.Get(resourceURL(client, instanceId, authId), &r.Body, nil)
    80  	return
    81  }
    82  
    83  // ListOpts allows to filter list data using given parameters.
    84  type ListOpts struct {
    85  	// ID.
    86  	ID string `q:"id"`
    87  	// Name.
    88  	Name string `q:"name"`
    89  	// Custom authorizer type.
    90  	Type string `q:"type"`
    91  	// Offset from which the query starts.
    92  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    93  	Offset int `q:"offset"`
    94  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
    95  	Limit int `q:"limit"`
    96  }
    97  
    98  // ListOptsBuilder is an interface which to support request query build of
    99  // the custom authorizer search.
   100  type ListOptsBuilder interface {
   101  	ToListQuery() (string, error)
   102  }
   103  
   104  // ToListQuery is a method which to build a request query by the ListOpts.
   105  func (opts ListOpts) ToListQuery() (string, error) {
   106  	q, err := golangsdk.BuildQueryString(opts)
   107  	if err != nil {
   108  		return "", err
   109  	}
   110  	return q.String(), err
   111  }
   112  
   113  // List is a method to obtain an array of one or more custom authorizers for the instance according to the
   114  // query parameters.
   115  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOptsBuilder) pagination.Pager {
   116  	url := rootURL(client, instanceId)
   117  	if opts != nil {
   118  		query, err := opts.ToListQuery()
   119  		if err != nil {
   120  			return pagination.Pager{Err: err}
   121  		}
   122  		url += query
   123  	}
   124  
   125  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   126  		return CustomAuthPage{pagination.SinglePageBase(r)}
   127  	})
   128  }
   129  
   130  // Delete is a method to delete an existing custom authorizer.
   131  func Delete(client *golangsdk.ServiceClient, instanceId, authId string) (r DeleteResult) {
   132  	_, r.Err = client.Delete(resourceURL(client, instanceId, authId), nil)
   133  	return
   134  }