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

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