github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/roles/results.go (about)

     1  package roles
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	"github.com/huaweicloud/golangsdk/internal"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  // Role grants permissions to a user.
    12  type Role struct {
    13  	// DomainID is the domain ID the role belongs to.
    14  	DomainID string `json:"domain_id"`
    15  
    16  	// ID is the unique ID of the role.
    17  	ID string `json:"id"`
    18  
    19  	// Links contains referencing links to the role.
    20  	Links map[string]interface{} `json:"links"`
    21  
    22  	Name        string `json:"name"`
    23  	Description string `json:"description"`
    24  	DisplayName string `json:"display_name"`
    25  	Flag        string `json:"flag"`
    26  	Catalog     string `json:"catalog"`
    27  	Type        string `json:"type"`
    28  	Policy      Policy `json:"policy"`
    29  
    30  	// Extra is a collection of miscellaneous key/values.
    31  	Extra map[string]interface{} `json:"-"`
    32  }
    33  
    34  func (r *Role) UnmarshalJSON(b []byte) error {
    35  	type tmp Role
    36  	var s struct {
    37  		tmp
    38  		Extra map[string]interface{} `json:"extra"`
    39  	}
    40  	err := json.Unmarshal(b, &s)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	*r = Role(s.tmp)
    45  
    46  	// Collect other fields and bundle them into Extra
    47  	// but only if a field titled "extra" wasn't sent.
    48  	if s.Extra != nil {
    49  		r.Extra = s.Extra
    50  	} else {
    51  		var result interface{}
    52  		err := json.Unmarshal(b, &result)
    53  		if err != nil {
    54  			return err
    55  		}
    56  		if resultMap, ok := result.(map[string]interface{}); ok {
    57  			r.Extra = internal.RemainingKeys(Role{}, resultMap)
    58  		}
    59  	}
    60  
    61  	return err
    62  }
    63  
    64  type roleResult struct {
    65  	golangsdk.Result
    66  }
    67  
    68  // GetResult is the response from a Get operation. Call its Extract method
    69  // to interpret it as a Role.
    70  type GetResult struct {
    71  	roleResult
    72  }
    73  
    74  // CreateResult is the response from a Create operation. Call its Extract method
    75  // to interpret it as a Role
    76  type CreateResult struct {
    77  	roleResult
    78  }
    79  
    80  // UpdateResult is the response from an Update operation. Call its Extract
    81  // method to interpret it as a Role.
    82  type UpdateResult struct {
    83  	roleResult
    84  }
    85  
    86  // DeleteResult is the response from a Delete operation. Call its ExtractErr to
    87  // determine if the request succeeded or failed.
    88  type DeleteResult struct {
    89  	golangsdk.ErrResult
    90  }
    91  
    92  // RolePage is a single page of Role results.
    93  type RolePage struct {
    94  	pagination.LinkedPageBase
    95  }
    96  
    97  // IsEmpty determines whether or not a page of Roles contains any results.
    98  func (r RolePage) IsEmpty() (bool, error) {
    99  	roles, err := ExtractRoles(r)
   100  	return len(roles) == 0, err
   101  }
   102  
   103  // NextPageURL extracts the "next" link from the links section of the result.
   104  func (r RolePage) NextPageURL() (string, error) {
   105  	var s struct {
   106  		Links struct {
   107  			Next     string `json:"next"`
   108  			Previous string `json:"previous"`
   109  		} `json:"links"`
   110  	}
   111  	err := r.ExtractInto(&s)
   112  	if err != nil {
   113  		return "", err
   114  	}
   115  	return s.Links.Next, err
   116  }
   117  
   118  // ExtractProjects returns a slice of Roles contained in a single page of
   119  // results.
   120  func ExtractRoles(r pagination.Page) ([]Role, error) {
   121  	var s struct {
   122  		Roles []Role `json:"roles"`
   123  	}
   124  	err := (r.(RolePage)).ExtractInto(&s)
   125  	return s.Roles, err
   126  }
   127  
   128  // Extract interprets any roleResults as a Role.
   129  func (r roleResult) Extract() (*Role, error) {
   130  	var s struct {
   131  		Role *Role `json:"role"`
   132  	}
   133  	err := r.ExtractInto(&s)
   134  	return s.Role, err
   135  }
   136  
   137  // RoleAssignment is the result of a role assignments query.
   138  type RoleAssignment struct {
   139  	Catalog     string `json:"catalog"`
   140  	Description string `json:"description"`
   141  	DisplayName string `json:"display_name"`
   142  	ID          string `json:"id"`
   143  	Name        string `json:"name"`
   144  	Type        string `json:"type"`
   145  	Policy      Policy `json:"policy"`
   146  }
   147  
   148  type Policy struct {
   149  	Statement []Statement `json:"Statement"`
   150  	Version   string      `json:"Version"`
   151  }
   152  
   153  type Statement struct {
   154  	Action []string `json:"Action"`
   155  	Effect string   `json:"Effect"`
   156  }
   157  
   158  // RoleAssignmentPage is a single page of RoleAssignments results.
   159  type RoleAssignmentPage struct {
   160  	pagination.LinkedPageBase
   161  }
   162  
   163  // IsEmpty returns true if the RoleAssignmentPage contains no results.
   164  func (r RoleAssignmentPage) IsEmpty() (bool, error) {
   165  	roleAssignments, err := ExtractRoleAssignments(r)
   166  	return len(roleAssignments) == 0, err
   167  }
   168  
   169  // NextPageURL uses the response's embedded link reference to navigate to
   170  // the next page of results.
   171  func (r RoleAssignmentPage) NextPageURL() (string, error) {
   172  	var s struct {
   173  		Links struct {
   174  			Next string `json:"next"`
   175  		} `json:"links"`
   176  	}
   177  	err := r.ExtractInto(&s)
   178  	return s.Links.Next, err
   179  }
   180  
   181  // ExtractRoleAssignments extracts a slice of RoleAssignments from a Collection
   182  // acquired from List.
   183  func ExtractRoleAssignments(r pagination.Page) ([]RoleAssignment, error) {
   184  	var s struct {
   185  		RoleAssignments []RoleAssignment `json:"roles"`
   186  	}
   187  	err := (r.(RoleAssignmentPage)).ExtractInto(&s)
   188  	return s.RoleAssignments, err
   189  }
   190  
   191  // AssignmentResult represents the result of an assign operation.
   192  // Call ExtractErr method to determine if the request succeeded or failed.
   193  type AssignmentResult struct {
   194  	golangsdk.ErrResult
   195  }
   196  
   197  // UnassignmentResult represents the result of an unassign operation.
   198  // Call ExtractErr method to determine if the request succeeded or failed.
   199  type UnassignmentResult struct {
   200  	golangsdk.ErrResult
   201  }