github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/osinherit/requests.go (about)

     1  package osinherit
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  )
     8  
     9  // AssignOpts provides options to assign an inherited role
    10  type AssignOpts struct {
    11  	// UserID is the ID of a user to assign an inherited role
    12  	// Note: exactly one of UserID or GroupID must be provided
    13  	UserID string `xor:"GroupID"`
    14  
    15  	// GroupID is the ID of a group to assign an inherited role
    16  	// Note: exactly one of UserID or GroupID must be provided
    17  	GroupID string `xor:"UserID"`
    18  
    19  	// ProjectID is the ID of a project to assign an inherited role on
    20  	// Note: exactly one of ProjectID or DomainID must be provided
    21  	ProjectID string `xor:"DomainID"`
    22  
    23  	// DomainID is the ID of a domain to assign an inherited role on
    24  	// Note: exactly one of ProjectID or DomainID must be provided
    25  	DomainID string `xor:"ProjectID"`
    26  }
    27  
    28  // ValidateOpts provides options to which role to validate
    29  type ValidateOpts struct {
    30  	// UserID is the ID of a user to validate an inherited role
    31  	// Note: exactly one of UserID or GroupID must be provided
    32  	UserID string `xor:"GroupID"`
    33  
    34  	// GroupID is the ID of a group to validate an inherited role
    35  	// Note: exactly one of UserID or GroupID must be provided
    36  	GroupID string `xor:"UserID"`
    37  
    38  	// ProjectID is the ID of a project to validate an inherited role on
    39  	// Note: exactly one of ProjectID or DomainID must be provided
    40  	ProjectID string `xor:"DomainID"`
    41  
    42  	// DomainID is the ID of a domain to validate an inherited role on
    43  	// Note: exactly one of ProjectID or DomainID must be provided
    44  	DomainID string `xor:"ProjectID"`
    45  }
    46  
    47  // UnassignOpts provides options to unassign an inherited role
    48  type UnassignOpts struct {
    49  	// UserID is the ID of a user to unassign an inherited role
    50  	// Note: exactly one of UserID or GroupID must be provided
    51  	UserID string `xor:"GroupID"`
    52  
    53  	// GroupID is the ID of a group to unassign an inherited role
    54  	// Note: exactly one of UserID or GroupID must be provided
    55  	GroupID string `xor:"UserID"`
    56  
    57  	// ProjectID is the ID of a project to assign an inherited role on
    58  	// Note: exactly one of ProjectID or DomainID must be provided
    59  	ProjectID string `xor:"DomainID"`
    60  
    61  	// DomainID is the ID of a domain to assign an inherited role on
    62  	// Note: exactly one of ProjectID or DomainID must be provided
    63  	DomainID string `xor:"ProjectID"`
    64  }
    65  
    66  // Assign is the operation responsible for assigning an inherited role
    67  // to a user/group on a project/domain.
    68  func Assign(ctx context.Context, client *gophercloud.ServiceClient, roleID string, opts AssignOpts) (r AssignmentResult) {
    69  	// Check xor conditions
    70  	_, err := gophercloud.BuildRequestBody(opts, "")
    71  	if err != nil {
    72  		r.Err = err
    73  		return
    74  	}
    75  
    76  	// Get corresponding URL
    77  	var targetID string
    78  	var targetType string
    79  	if opts.ProjectID != "" {
    80  		targetID = opts.ProjectID
    81  		targetType = "projects"
    82  	} else {
    83  		targetID = opts.DomainID
    84  		targetType = "domains"
    85  	}
    86  
    87  	var actorID string
    88  	var actorType string
    89  	if opts.UserID != "" {
    90  		actorID = opts.UserID
    91  		actorType = "users"
    92  	} else {
    93  		actorID = opts.GroupID
    94  		actorType = "groups"
    95  	}
    96  
    97  	resp, err := client.Put(ctx, assignURL(client, targetType, targetID, actorType, actorID, roleID), nil, nil, &gophercloud.RequestOpts{
    98  		OkCodes: []int{204},
    99  	})
   100  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   101  	return
   102  }
   103  
   104  // Validate is the operation responsible for validating an inherited role
   105  // of a user/group on a project/domain.
   106  func Validate(ctx context.Context, client *gophercloud.ServiceClient, roleID string, opts ValidateOpts) (r ValidateResult) {
   107  	// Check xor conditions
   108  	_, err := gophercloud.BuildRequestBody(opts, "")
   109  	if err != nil {
   110  		r.Err = err
   111  		return
   112  	}
   113  
   114  	// Get corresponding URL
   115  	var targetID string
   116  	var targetType string
   117  	if opts.ProjectID != "" {
   118  		targetID = opts.ProjectID
   119  		targetType = "projects"
   120  	} else {
   121  		targetID = opts.DomainID
   122  		targetType = "domains"
   123  	}
   124  
   125  	var actorID string
   126  	var actorType string
   127  	if opts.UserID != "" {
   128  		actorID = opts.UserID
   129  		actorType = "users"
   130  	} else {
   131  		actorID = opts.GroupID
   132  		actorType = "groups"
   133  	}
   134  
   135  	resp, err := client.Head(ctx, assignURL(client, targetType, targetID, actorType, actorID, roleID), &gophercloud.RequestOpts{
   136  		OkCodes: []int{204},
   137  	})
   138  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   139  	return
   140  }
   141  
   142  // Unassign is the operation responsible for unassigning an inherited
   143  // role to a user/group on a project/domain.
   144  func Unassign(ctx context.Context, client *gophercloud.ServiceClient, roleID string, opts UnassignOpts) (r UnassignmentResult) {
   145  	// Check xor conditions
   146  	_, err := gophercloud.BuildRequestBody(opts, "")
   147  	if err != nil {
   148  		r.Err = err
   149  		return
   150  	}
   151  
   152  	// Get corresponding URL
   153  	var targetID string
   154  	var targetType string
   155  	if opts.ProjectID != "" {
   156  		targetID = opts.ProjectID
   157  		targetType = "projects"
   158  	} else {
   159  		targetID = opts.DomainID
   160  		targetType = "domains"
   161  	}
   162  
   163  	var actorID string
   164  	var actorType string
   165  	if opts.UserID != "" {
   166  		actorID = opts.UserID
   167  		actorType = "users"
   168  	} else {
   169  		actorID = opts.GroupID
   170  		actorType = "groups"
   171  	}
   172  
   173  	resp, err := client.Delete(ctx, assignURL(client, targetType, targetID, actorType, actorID, roleID), &gophercloud.RequestOpts{
   174  		OkCodes: []int{204},
   175  	})
   176  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   177  	return
   178  }