github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/rbacpolicies/requests.go (about)

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