github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/rbacpolicies/requests.go (about)

     1  package rbacpolicies
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/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  	if err != nil {
    36  		return "", err
    37  	}
    38  	return q.String(), err
    39  }
    40  
    41  // List returns a Pager which allows you to iterate over a collection of
    42  // rbac policies. It accepts a ListOpts struct, which allows you to filter and sort
    43  // the returned collection for greater efficiency.
    44  func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    45  	url := listURL(c)
    46  	if opts != nil {
    47  		query, err := opts.ToRBACPolicyListQuery()
    48  		if err != nil {
    49  			return pagination.Pager{Err: err}
    50  		}
    51  		url += query
    52  	}
    53  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    54  		return RBACPolicyPage{pagination.LinkedPageBase{PageResult: r}}
    55  
    56  	})
    57  }
    58  
    59  // Get retrieves a specific rbac policy based on its unique ID.
    60  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    61  	_, r.Err = c.Get(getURL(c, id), &r.Body, nil)
    62  	return
    63  }
    64  
    65  // PolicyAction maps to Action for the RBAC policy.
    66  // Which allows access_as_external or access_as_shared.
    67  type PolicyAction string
    68  
    69  const (
    70  	// ActionAccessExternal returns Action for the RBAC policy as access_as_external.
    71  	ActionAccessExternal PolicyAction = "access_as_external"
    72  
    73  	// ActionAccessShared returns Action for the RBAC policy as access_as_shared.
    74  	ActionAccessShared PolicyAction = "access_as_shared"
    75  )
    76  
    77  // CreateOptsBuilder allows extensions to add additional parameters to the
    78  // Create request.
    79  type CreateOptsBuilder interface {
    80  	ToRBACPolicyCreateMap() (map[string]interface{}, error)
    81  }
    82  
    83  // CreateOpts represents options used to create a rbac-policy.
    84  type CreateOpts struct {
    85  	Action       PolicyAction `json:"action" required:"true"`
    86  	ObjectType   string       `json:"object_type" required:"true"`
    87  	TargetTenant string       `json:"target_tenant" required:"true"`
    88  	ObjectID     string       `json:"object_id" required:"true"`
    89  }
    90  
    91  // ToRBACPolicyCreateMap builds a request body from CreateOpts.
    92  func (opts CreateOpts) ToRBACPolicyCreateMap() (map[string]interface{}, error) {
    93  	return golangsdk.BuildRequestBody(opts, "rbac_policy")
    94  }
    95  
    96  // Create accepts a CreateOpts struct and creates a new rbac-policy using the values
    97  // provided.
    98  //
    99  // The tenant ID that is contained in the URI is the tenant that creates the
   100  // rbac-policy.
   101  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   102  	b, err := opts.ToRBACPolicyCreateMap()
   103  	if err != nil {
   104  		r.Err = err
   105  		return
   106  	}
   107  	_, r.Err = c.Post(createURL(c), b, &r.Body, nil)
   108  	return
   109  }
   110  
   111  // Delete accepts a unique ID and deletes the rbac-policy associated with it.
   112  func Delete(c *golangsdk.ServiceClient, rbacPolicyID string) (r DeleteResult) {
   113  	_, r.Err = c.Delete(deleteURL(c, rbacPolicyID), nil)
   114  	return
   115  }
   116  
   117  // UpdateOptsBuilder allows extensions to add additional parameters to the
   118  // Update request.
   119  type UpdateOptsBuilder interface {
   120  	ToRBACPolicyUpdateMap() (map[string]interface{}, error)
   121  }
   122  
   123  // UpdateOpts represents options used to update a rbac-policy.
   124  type UpdateOpts struct {
   125  	TargetTenant string `json:"target_tenant" required:"true"`
   126  }
   127  
   128  // ToRBACPolicyUpdateMap builds a request body from UpdateOpts.
   129  func (opts UpdateOpts) ToRBACPolicyUpdateMap() (map[string]interface{}, error) {
   130  	return golangsdk.BuildRequestBody(opts, "rbac_policy")
   131  }
   132  
   133  // Update accepts a UpdateOpts struct and updates an existing rbac-policy using the
   134  // values provided.
   135  func Update(c *golangsdk.ServiceClient, rbacPolicyID string, opts UpdateOptsBuilder) (r UpdateResult) {
   136  	b, err := opts.ToRBACPolicyUpdateMap()
   137  	if err != nil {
   138  		r.Err = err
   139  		return
   140  	}
   141  	_, r.Err = c.Put(updateURL(c, rbacPolicyID), b, &r.Body, &golangsdk.RequestOpts{
   142  		OkCodes: []int{200, 201},
   143  	})
   144  	return
   145  }