github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dds/v3/roles/results.go (about)

     1  package roles
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/chnsz/golangsdk/pagination"
     7  )
     8  
     9  // ListResp is the structure that represents the API response of 'List' method.
    10  type ListResp struct {
    11  	// Total number of query results.
    12  	TotalCount int `json:"total_count"`
    13  	// List of roles to query.
    14  	Roles string `json:"roles"`
    15  }
    16  
    17  // RoleResp is the structure that represents the detail of the database role.
    18  type RoleResp struct {
    19  	// Whether role is built-in.
    20  	IsBuiltin bool `json:"isBuiltin"`
    21  	// Role name.
    22  	Name string `json:"role"`
    23  	// Database name.
    24  	DbName string `json:"db"`
    25  	// The list of privileges inherited by the newly created role.
    26  	Privileges []Privilege `json:"privileges"`
    27  	// The list of privileges inherited by the newly created role, includes all privileges inherited by inherited roles.
    28  	InheritedPrivileges []Privilege `json:"inheritedPrivileges"`
    29  	// The list of roles inherited by the newly created role.
    30  	Roles []RoleDetail `json:"roles"`
    31  	// The list of roles inherited by the newly created role, includes all roles inherited by inherited roles.
    32  	InheritedRoles []RoleDetail `json:"inheritedRoles"`
    33  }
    34  
    35  // Privilege is the structure that represents the privilege detail for database.
    36  type Privilege struct {
    37  	// The details of the resource to which the privilege belongs.
    38  	Resource Resource `json:"resource"`
    39  	// The operation permission list.
    40  	Actions []string `json:"actions"`
    41  }
    42  
    43  // Resource is the structure that represents the database details to which the role and user belongs.
    44  type Resource struct {
    45  	// The database to which the privilege belongs.
    46  	Collection string `json:"collection"`
    47  	// The database name.
    48  	DbName string `json:"db"`
    49  }
    50  
    51  // RoleDetail is the structure that represents the inherited role details.
    52  type RoleDetail struct {
    53  	// Role name.
    54  	Name string `json:"role"`
    55  	// The database name to which the role belongs.
    56  	DbName string `json:"db"`
    57  }
    58  
    59  // RolePage is a single page maximum result representing a query by offset page.
    60  type RolePage struct {
    61  	pagination.OffsetPageBase
    62  }
    63  
    64  // IsEmpty checks whether a RolePage struct is empty.
    65  func (p RolePage) IsEmpty() (bool, error) {
    66  	arr, err := ExtractRoles(p)
    67  	return len(arr) == 0, err
    68  }
    69  
    70  // ExtractRoles is a method to extract the list of database role for DDS service.
    71  func ExtractRoles(p pagination.Page) ([]RoleResp, error) {
    72  	var r ListResp
    73  	err := (p.(RolePage)).ExtractInto(&r)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	var ur []RoleResp
    78  	if r.Roles != "" {
    79  		err = json.Unmarshal([]byte(r.Roles), &ur)
    80  	}
    81  	return ur, err
    82  }