github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/rbacpolicies/requests.go (about)

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