github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/rbacpolicies/requests.go (about)

     1  package rbacpolicies
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add additional parameters to the
     9  // List request.
    10  type ListOptsBuilder interface {
    11  	ToRBACPolicyListQuery() (string, error)
    12  }
    13  
    14  // ListOpts allows the filtering and sorting of paginated collections through
    15  // the API. Filtering is achieved by passing in struct field values that map to
    16  // the rbac attributes you want to see returned. SortKey allows you to sort
    17  // by a particular rbac attribute. SortDir sets the direction, and is either
    18  // `asc' or `desc'. Marker and Limit are used for pagination.
    19  type ListOpts struct {
    20  	TargetTenant string       `q:"target_tenant"`
    21  	ObjectType   string       `q:"object_type"`
    22  	ObjectID     string       `q:"object_id"`
    23  	Action       PolicyAction `q:"action"`
    24  	TenantID     string       `q:"tenant_id"`
    25  	ProjectID    string       `q:"project_id"`
    26  	Marker       string       `q:"marker"`
    27  	Limit        int          `q:"limit"`
    28  	SortKey      string       `q:"sort_key"`
    29  	SortDir      string       `q:"sort_dir"`
    30  }
    31  
    32  // ToRBACPolicyListQuery formats a ListOpts into a query string.
    33  func (opts ListOpts) ToRBACPolicyListQuery() (string, error) {
    34  	q, err := golangsdk.BuildQueryString(opts)
    35  	return q.String(), err
    36  }
    37  
    38  // List returns a Pager which allows you to iterate over a collection of
    39  // rbac policies. It accepts a ListOpts struct, which allows you to filter and sort
    40  // the returned collection for greater efficiency.
    41  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    42  	url := listURL(c)
    43  	if opts != nil {
    44  		query, err := opts.ToRBACPolicyListQuery()
    45  		if err != nil {
    46  			return pagination.Pager{Err: err}
    47  		}
    48  		url += query
    49  	}
    50  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    51  		return RBACPolicyPage{pagination.LinkedPageBase{PageResult: r}}
    52  
    53  	})
    54  }
    55  
    56  // Get retrieves a specific rbac policy based on its unique ID.
    57  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    58  	_, r.Err = c.Get(getURL(c, id), &r.Body, nil)
    59  	return
    60  }
    61  
    62  // PolicyAction maps to Action for the RBAC policy.
    63  // Which allows access_as_external or access_as_shared.
    64  type PolicyAction string
    65  
    66  const (
    67  	// ActionAccessExternal returns Action for the RBAC policy as access_as_external.
    68  	ActionAccessExternal PolicyAction = "access_as_external"
    69  
    70  	// ActionAccessShared returns Action for the RBAC policy as access_as_shared.
    71  	ActionAccessShared PolicyAction = "access_as_shared"
    72  )
    73  
    74  // CreateOptsBuilder allows extensions to add additional parameters to the
    75  // Create request.
    76  type CreateOptsBuilder interface {
    77  	ToRBACPolicyCreateMap() (map[string]interface{}, error)
    78  }
    79  
    80  // CreateOpts represents options used to create a rbac-policy.
    81  type CreateOpts struct {
    82  	Action       PolicyAction `json:"action" required:"true"`
    83  	ObjectType   string       `json:"object_type" required:"true"`
    84  	TargetTenant string       `json:"target_tenant" required:"true"`
    85  	ObjectID     string       `json:"object_id" required:"true"`
    86  }
    87  
    88  // ToRBACPolicyCreateMap builds a request body from CreateOpts.
    89  func (opts CreateOpts) ToRBACPolicyCreateMap() (map[string]interface{}, error) {
    90  	return golangsdk.BuildRequestBody(opts, "rbac_policy")
    91  }
    92  
    93  // Create accepts a CreateOpts struct and creates a new rbac-policy using the values
    94  // provided.
    95  //
    96  // The tenant ID that is contained in the URI is the tenant that creates the
    97  // rbac-policy.
    98  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    99  	b, err := opts.ToRBACPolicyCreateMap()
   100  	if err != nil {
   101  		r.Err = err
   102  		return
   103  	}
   104  	_, r.Err = c.Post(createURL(c), b, &r.Body, nil)
   105  	return
   106  }
   107  
   108  // Delete accepts a unique ID and deletes the rbac-policy associated with it.
   109  func Delete(c *golangsdk.ServiceClient, rbacPolicyID string) (r DeleteResult) {
   110  	_, r.Err = c.Delete(deleteURL(c, rbacPolicyID), nil)
   111  	return
   112  }
   113  
   114  // UpdateOptsBuilder allows extensions to add additional parameters to the
   115  // Update request.
   116  type UpdateOptsBuilder interface {
   117  	ToRBACPolicyUpdateMap() (map[string]interface{}, error)
   118  }
   119  
   120  // UpdateOpts represents options used to update a rbac-policy.
   121  type UpdateOpts struct {
   122  	TargetTenant string `json:"target_tenant" required:"true"`
   123  }
   124  
   125  // ToRBACPolicyUpdateMap builds a request body from UpdateOpts.
   126  func (opts UpdateOpts) ToRBACPolicyUpdateMap() (map[string]interface{}, error) {
   127  	return golangsdk.BuildRequestBody(opts, "rbac_policy")
   128  }
   129  
   130  // Update accepts a UpdateOpts struct and updates an existing rbac-policy using the
   131  // values provided.
   132  func Update(c *golangsdk.ServiceClient, rbacPolicyID string, opts UpdateOptsBuilder) (r UpdateResult) {
   133  	b, err := opts.ToRBACPolicyUpdateMap()
   134  	if err != nil {
   135  		r.Err = err
   136  		return
   137  	}
   138  	_, r.Err = c.Put(updateURL(c, rbacPolicyID), b, &r.Body, &golangsdk.RequestOpts{
   139  		OkCodes: []int{200, 201},
   140  	})
   141  	return
   142  }