github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/identity/v3/roles/requests.go (about)

     1  package roles
     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
     9  // the List request
    10  type ListOptsBuilder interface {
    11  	ToRoleListQuery() (string, error)
    12  }
    13  
    14  // ListOpts provides options to filter the List results.
    15  type ListOpts struct {
    16  	// DomainID filters the response by a domain ID.
    17  	DomainID string `q:"domain_id"`
    18  
    19  	// Name filters the response by role name.
    20  	Name string `q:"name"`
    21  }
    22  
    23  // ToRoleListQuery formats a ListOpts into a query string.
    24  func (opts ListOpts) ToRoleListQuery() (string, error) {
    25  	q, err := golangsdk.BuildQueryString(opts)
    26  	if err != nil {
    27  		return "", err
    28  	}
    29  	return q.String(), err
    30  }
    31  
    32  // List enumerates the roles to which the current token has access.
    33  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    34  	url := listURL(client)
    35  	if opts != nil {
    36  		query, err := opts.ToRoleListQuery()
    37  		if err != nil {
    38  			return pagination.Pager{Err: err}
    39  		}
    40  		url += query
    41  	}
    42  
    43  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    44  		return RolePage{pagination.LinkedPageBase{PageResult: r}}
    45  	})
    46  }
    47  
    48  // Get retrieves details on a single role, by ID.
    49  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    50  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    51  	return
    52  }
    53  
    54  // CreateOptsBuilder allows extensions to add additional parameters to
    55  // the Create request.
    56  type CreateOptsBuilder interface {
    57  	ToRoleCreateMap() (map[string]interface{}, error)
    58  }
    59  
    60  // CreateOpts provides options used to create a role.
    61  type CreateOpts struct {
    62  	// Name is the name of the new role.
    63  	Name string `json:"name" required:"true"`
    64  
    65  	// DomainID is the ID of the domain the role belongs to.
    66  	DomainID string `json:"domain_id,omitempty"`
    67  
    68  	// Extra is free-form extra key/value pairs to describe the role.
    69  	Extra map[string]interface{} `json:"-"`
    70  }
    71  
    72  // ToRoleCreateMap formats a CreateOpts into a create request.
    73  func (opts CreateOpts) ToRoleCreateMap() (map[string]interface{}, error) {
    74  	b, err := golangsdk.BuildRequestBody(opts, "role")
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	if opts.Extra != nil {
    80  		if v, ok := b["role"].(map[string]interface{}); ok {
    81  			for key, value := range opts.Extra {
    82  				v[key] = value
    83  			}
    84  		}
    85  	}
    86  
    87  	return b, nil
    88  }
    89  
    90  // Create creates a new Role.
    91  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    92  	b, err := opts.ToRoleCreateMap()
    93  	if err != nil {
    94  		r.Err = err
    95  		return
    96  	}
    97  	_, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{
    98  		OkCodes: []int{201},
    99  	})
   100  	return
   101  }
   102  
   103  // UpdateOptsBuilder allows extensions to add additional parameters to
   104  // the Update request.
   105  type UpdateOptsBuilder interface {
   106  	ToRoleUpdateMap() (map[string]interface{}, error)
   107  }
   108  
   109  // UpdateOpts provides options for updating a role.
   110  type UpdateOpts struct {
   111  	// Name is the name of the new role.
   112  	Name string `json:"name,omitempty"`
   113  
   114  	// Extra is free-form extra key/value pairs to describe the role.
   115  	Extra map[string]interface{} `json:"-"`
   116  }
   117  
   118  // ToRoleUpdateMap formats a UpdateOpts into an update request.
   119  func (opts UpdateOpts) ToRoleUpdateMap() (map[string]interface{}, error) {
   120  	b, err := golangsdk.BuildRequestBody(opts, "role")
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	if opts.Extra != nil {
   126  		if v, ok := b["role"].(map[string]interface{}); ok {
   127  			for key, value := range opts.Extra {
   128  				v[key] = value
   129  			}
   130  		}
   131  	}
   132  
   133  	return b, nil
   134  }
   135  
   136  // Update updates an existing Role.
   137  func Update(client *golangsdk.ServiceClient, roleID string, opts UpdateOptsBuilder) (r UpdateResult) {
   138  	b, err := opts.ToRoleUpdateMap()
   139  	if err != nil {
   140  		r.Err = err
   141  		return
   142  	}
   143  	_, r.Err = client.Patch(updateURL(client, roleID), &b, &r.Body, &golangsdk.RequestOpts{
   144  		OkCodes: []int{200},
   145  	})
   146  	return
   147  }
   148  
   149  // Delete deletes a role.
   150  func Delete(client *golangsdk.ServiceClient, roleID string) (r DeleteResult) {
   151  	_, r.Err = client.Delete(deleteURL(client, roleID), nil)
   152  	return
   153  }
   154  
   155  // ListAssignmentsOptsBuilder allows extensions to add additional parameters to
   156  // the ListAssignments request.
   157  type ListAssignmentsOptsBuilder interface {
   158  	extractAssignment() (string, string, string, string, error)
   159  }
   160  
   161  // ListAssignmentsOpts allows you to query the ListAssignments method.
   162  // Specify one of or a combination of GroupId, RoleId, ScopeDomainId,
   163  // ScopeProjectId, and/or UserId to search for roles assigned to corresponding
   164  // entities.
   165  type ListAssignmentsOpts struct {
   166  	// GroupID is the group ID to query.
   167  	GroupID string `q:"group.id"`
   168  
   169  	// ScopeDomainID filters the results by the given domain ID.
   170  	ScopeDomainID string `q:"scope.domain.id"`
   171  
   172  	// ScopeProjectID filters the results by the given Project ID.
   173  	ScopeProjectID string `q:"scope.project.id"`
   174  
   175  	// UserID filterst he results by the given User ID.
   176  	UserID string `q:"user.id"`
   177  }
   178  
   179  // ToRolesListAssignmentsQuery formats a ListAssignmentsOpts into a query string.
   180  func (opts ListAssignmentsOpts) extractAssignment() (string, string, string, string, error) {
   181  	// Get corresponding URL
   182  	var targetID string
   183  	var targetType string
   184  	if opts.ScopeProjectID != "" {
   185  		targetID = opts.ScopeProjectID
   186  		targetType = "projects"
   187  	} else {
   188  		targetID = opts.ScopeDomainID
   189  		targetType = "domains"
   190  	}
   191  
   192  	var actorID string
   193  	var actorType string
   194  	if opts.UserID != "" {
   195  		actorID = opts.UserID
   196  		actorType = "users"
   197  	} else {
   198  		actorID = opts.GroupID
   199  		actorType = "groups"
   200  	}
   201  
   202  	return targetType, targetID, actorType, actorID, nil
   203  }
   204  
   205  // ListAssignments enumerates the roles assigned to a specified resource.
   206  func ListAssignments(client *golangsdk.ServiceClient, opts ListAssignmentsOptsBuilder) pagination.Pager {
   207  	targetType, targetID, actorType, actorID, _ := opts.extractAssignment()
   208  
   209  	url := listAssignmentsURL(client, targetType, targetID, actorType, actorID)
   210  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   211  		return RoleAssignmentPage{pagination.LinkedPageBase{PageResult: r}}
   212  	})
   213  }
   214  
   215  // AssignOpts provides options to assign a role
   216  type AssignOpts struct {
   217  	// UserID is the ID of a user to assign a role
   218  	// Note: exactly one of UserID or GroupID must be provided
   219  	UserID string `xor:"GroupID"`
   220  
   221  	// GroupID is the ID of a group to assign a role
   222  	// Note: exactly one of UserID or GroupID must be provided
   223  	GroupID string `xor:"UserID"`
   224  
   225  	// ProjectID is the ID of a project to assign a role on
   226  	// Note: exactly one of ProjectID or DomainID must be provided
   227  	ProjectID string `xor:"DomainID"`
   228  
   229  	// DomainID is the ID of a domain to assign a role on
   230  	// Note: exactly one of ProjectID or DomainID must be provided
   231  	DomainID string `xor:"ProjectID"`
   232  }
   233  
   234  // UnassignOpts provides options to unassign a role
   235  type UnassignOpts struct {
   236  	// UserID is the ID of a user to unassign a role
   237  	// Note: exactly one of UserID or GroupID must be provided
   238  	UserID string `xor:"GroupID"`
   239  
   240  	// GroupID is the ID of a group to unassign a role
   241  	// Note: exactly one of UserID or GroupID must be provided
   242  	GroupID string `xor:"UserID"`
   243  
   244  	// ProjectID is the ID of a project to unassign a role on
   245  	// Note: exactly one of ProjectID or DomainID must be provided
   246  	ProjectID string `xor:"DomainID"`
   247  
   248  	// DomainID is the ID of a domain to unassign a role on
   249  	// Note: exactly one of ProjectID or DomainID must be provided
   250  	DomainID string `xor:"ProjectID"`
   251  }
   252  
   253  // Assign is the operation responsible for assigning a role
   254  // to a user/group on a project/domain.
   255  func Assign(client *golangsdk.ServiceClient, roleID string, opts AssignOpts) (r AssignmentResult) {
   256  	// Check xor conditions
   257  	_, err := golangsdk.BuildRequestBody(opts, "")
   258  	if err != nil {
   259  		r.Err = err
   260  		return
   261  	}
   262  
   263  	// Get corresponding URL
   264  	var targetID string
   265  	var targetType string
   266  	if opts.ProjectID != "" {
   267  		targetID = opts.ProjectID
   268  		targetType = "projects"
   269  	} else {
   270  		targetID = opts.DomainID
   271  		targetType = "domains"
   272  	}
   273  
   274  	var actorID string
   275  	var actorType string
   276  	if opts.UserID != "" {
   277  		actorID = opts.UserID
   278  		actorType = "users"
   279  	} else {
   280  		actorID = opts.GroupID
   281  		actorType = "groups"
   282  	}
   283  
   284  	_, r.Err = client.Put(assignURL(client, targetType, targetID, actorType, actorID, roleID), nil, nil, &golangsdk.RequestOpts{
   285  		OkCodes: []int{204},
   286  	})
   287  	return
   288  }
   289  
   290  // Unassign is the operation responsible for unassigning a role
   291  // from a user/group on a project/domain.
   292  func Unassign(client *golangsdk.ServiceClient, roleID string, opts UnassignOpts) (r UnassignmentResult) {
   293  	// Check xor conditions
   294  	_, err := golangsdk.BuildRequestBody(opts, "")
   295  	if err != nil {
   296  		r.Err = err
   297  		return
   298  	}
   299  
   300  	// Get corresponding URL
   301  	var targetID string
   302  	var targetType string
   303  	if opts.ProjectID != "" {
   304  		targetID = opts.ProjectID
   305  		targetType = "projects"
   306  	} else {
   307  		targetID = opts.DomainID
   308  		targetType = "domains"
   309  	}
   310  
   311  	var actorID string
   312  	var actorType string
   313  	if opts.UserID != "" {
   314  		actorID = opts.UserID
   315  		actorType = "users"
   316  	} else {
   317  		actorID = opts.GroupID
   318  		actorType = "groups"
   319  	}
   320  
   321  	_, r.Err = client.Delete(assignURL(client, targetType, targetID, actorType, actorID, roleID), &golangsdk.RequestOpts{
   322  		OkCodes: []int{204},
   323  	})
   324  	return
   325  }