github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/extensions/trusts/requests.go (about)

     1  package trusts
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/gophercloud/gophercloud"
     7  	"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // AuthOptsExt extends the base Identity v3 tokens AuthOpts with a TrustID.
    12  type AuthOptsExt struct {
    13  	tokens.AuthOptionsBuilder
    14  
    15  	// TrustID is the ID of the trust.
    16  	TrustID string `json:"id"`
    17  }
    18  
    19  // ToTokenV3CreateMap builds a create request body from the AuthOpts.
    20  func (opts AuthOptsExt) ToTokenV3CreateMap(scope map[string]interface{}) (map[string]interface{}, error) {
    21  	return opts.AuthOptionsBuilder.ToTokenV3CreateMap(scope)
    22  }
    23  
    24  // ToTokenV3ScopeMap builds a scope from AuthOpts.
    25  func (opts AuthOptsExt) ToTokenV3ScopeMap() (map[string]interface{}, error) {
    26  	b, err := opts.AuthOptionsBuilder.ToTokenV3ScopeMap()
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	if opts.TrustID != "" {
    32  		if b == nil {
    33  			b = make(map[string]interface{})
    34  		}
    35  		b["OS-TRUST:trust"] = map[string]interface{}{
    36  			"id": opts.TrustID,
    37  		}
    38  	}
    39  
    40  	return b, nil
    41  }
    42  
    43  func (opts AuthOptsExt) CanReauth() bool {
    44  	return opts.AuthOptionsBuilder.CanReauth()
    45  }
    46  
    47  // CreateOptsBuilder allows extensions to add additional parameters to
    48  // the Create request.
    49  type CreateOptsBuilder interface {
    50  	ToTrustCreateMap() (map[string]interface{}, error)
    51  }
    52  
    53  // CreateOpts provides options used to create a new trust.
    54  type CreateOpts struct {
    55  	// Impersonation allows the trustee to impersonate the trustor.
    56  	Impersonation bool `json:"impersonation"`
    57  
    58  	// TrusteeUserID is a user who is capable of consuming the trust.
    59  	TrusteeUserID string `json:"trustee_user_id" required:"true"`
    60  
    61  	// TrustorUserID is a user who created the trust.
    62  	TrustorUserID string `json:"trustor_user_id" required:"true"`
    63  
    64  	// AllowRedelegation enables redelegation of a trust.
    65  	AllowRedelegation bool `json:"allow_redelegation,omitempty"`
    66  
    67  	// ExpiresAt sets expiration time on trust.
    68  	ExpiresAt *time.Time `json:"-"`
    69  
    70  	// ProjectID identifies the project.
    71  	ProjectID string `json:"project_id,omitempty"`
    72  
    73  	// RedelegationCount specifies a depth of the redelegation chain.
    74  	RedelegationCount int `json:"redelegation_count,omitempty"`
    75  
    76  	// RemainingUses specifies how many times a trust can be used to get a token.
    77  	RemainingUses int `json:"remaining_uses,omitempty"`
    78  
    79  	// Roles specifies roles that need to be granted to trustee.
    80  	Roles []Role `json:"roles,omitempty"`
    81  }
    82  
    83  // ToTrustCreateMap formats a CreateOpts into a create request.
    84  func (opts CreateOpts) ToTrustCreateMap() (map[string]interface{}, error) {
    85  	parent := "trust"
    86  	b, err := gophercloud.BuildRequestBody(opts, parent)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	if opts.ExpiresAt != nil {
    92  		if v, ok := b[parent].(map[string]interface{}); ok {
    93  			v["expires_at"] = opts.ExpiresAt.Format(gophercloud.RFC3339Milli)
    94  		}
    95  	}
    96  
    97  	return b, nil
    98  }
    99  
   100  type ListOptsBuilder interface {
   101  	ToTrustListQuery() (string, error)
   102  }
   103  
   104  // ListOpts provides options to filter the List results.
   105  type ListOpts struct {
   106  	// TrustorUserID filters the response by a trustor user Id.
   107  	TrustorUserID string `q:"trustor_user_id"`
   108  
   109  	// TrusteeUserID filters the response by a trustee user Id.
   110  	TrusteeUserID string `q:"trustee_user_id"`
   111  }
   112  
   113  // ToTrustListQuery formats a ListOpts into a query string.
   114  func (opts ListOpts) ToTrustListQuery() (string, error) {
   115  	q, err := gophercloud.BuildQueryString(opts)
   116  	return q.String(), err
   117  }
   118  
   119  // Create creates a new Trust.
   120  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   121  	b, err := opts.ToTrustCreateMap()
   122  	if err != nil {
   123  		r.Err = err
   124  		return
   125  	}
   126  	resp, err := client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
   127  		OkCodes: []int{201},
   128  	})
   129  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   130  	return
   131  }
   132  
   133  // Delete deletes a Trust.
   134  func Delete(client *gophercloud.ServiceClient, trustID string) (r DeleteResult) {
   135  	resp, err := client.Delete(deleteURL(client, trustID), nil)
   136  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   137  	return
   138  }
   139  
   140  // List enumerates the Trust to which the current token has access.
   141  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   142  	url := listURL(client)
   143  	if opts != nil {
   144  		query, err := opts.ToTrustListQuery()
   145  		if err != nil {
   146  			return pagination.Pager{Err: err}
   147  		}
   148  		url += query
   149  	}
   150  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   151  		return TrustPage{pagination.LinkedPageBase{PageResult: r}}
   152  	})
   153  }
   154  
   155  // Get retrieves details on a single Trust, by ID.
   156  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
   157  	resp, err := client.Get(resourceURL(client, id), &r.Body, nil)
   158  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   159  	return
   160  }
   161  
   162  // ListRoles lists roles delegated by a Trust.
   163  func ListRoles(client *gophercloud.ServiceClient, id string) pagination.Pager {
   164  	url := listRolesURL(client, id)
   165  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   166  		return RolesPage{pagination.LinkedPageBase{PageResult: r}}
   167  	})
   168  }
   169  
   170  // GetRole retrieves details on a single role delegated by a Trust.
   171  func GetRole(client *gophercloud.ServiceClient, id string, roleID string) (r GetRoleResult) {
   172  	resp, err := client.Get(getRoleURL(client, id, roleID), &r.Body, nil)
   173  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   174  	return
   175  }
   176  
   177  // CheckRole checks whether a role ID is delegated by a Trust.
   178  func CheckRole(client *gophercloud.ServiceClient, id string, roleID string) (r CheckRoleResult) {
   179  	resp, err := client.Head(getRoleURL(client, id, roleID), nil)
   180  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   181  	return
   182  }