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

     1  package groups
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  	"github.com/chnsz/golangsdk/internal"
     8  	"github.com/chnsz/golangsdk/pagination"
     9  )
    10  
    11  // Group helps manage related users.
    12  type Group struct {
    13  	// Description describes the group purpose.
    14  	Description string `json:"description"`
    15  
    16  	// DomainID is the domain ID the group belongs to.
    17  	DomainID string `json:"domain_id"`
    18  
    19  	// ID is the unique ID of the group.
    20  	ID string `json:"id"`
    21  
    22  	// Extra is a collection of miscellaneous key/values.
    23  	Extra map[string]interface{} `json:"-"`
    24  
    25  	// Links contains referencing links to the group.
    26  	Links map[string]interface{} `json:"links"`
    27  
    28  	// Name is the name of the group.
    29  	Name string `json:"name"`
    30  }
    31  
    32  type User struct {
    33  	// IAM user name.
    34  	Name string `json:"name"`
    35  
    36  	// Links contains referencing links to the User.
    37  	Links map[string]interface{} `json:"links"`
    38  
    39  	// DomainID is the domain ID the user belongs to.
    40  	DomainId string `json:"domain_id"`
    41  
    42  	// Enabling status of the IAM user.
    43  	Enabled bool `json:"enabled"`
    44  
    45  	// ID is the unique ID of the User.
    46  	Id string `json:"id"`
    47  
    48  	// Time when the password will expire. null indicates that the password has unlimited validity.
    49  	PasswordExpiresAt string `json:"password_expires_at"`
    50  
    51  	// Description of the IAM user.
    52  	Description string `json:"description"`
    53  
    54  	// Password status. true means that the password needs to be changed, and false means that the password is normal.
    55  	PwdStatus bool `json:"pwd_status"`
    56  
    57  	// ID of the project that the IAM user lastly accessed before exiting the system.
    58  	LastProjectId string `json:"last_project_id"`
    59  
    60  	// Password strength. The value can be high, mid, or low.
    61  	PwdStrength string `json:"pwd_strength"`
    62  
    63  	// Other information about the IAM user.
    64  	Extra map[string]interface{} `json:"-"`
    65  }
    66  
    67  func (r *Group) UnmarshalJSON(b []byte) error {
    68  	type tmp Group
    69  	var s struct {
    70  		tmp
    71  		Extra map[string]interface{} `json:"extra"`
    72  	}
    73  	err := json.Unmarshal(b, &s)
    74  	if err != nil {
    75  		return err
    76  	}
    77  	*r = Group(s.tmp)
    78  
    79  	// Collect other fields and bundle them into Extra
    80  	// but only if a field titled "extra" wasn't sent.
    81  	if s.Extra != nil {
    82  		r.Extra = s.Extra
    83  	} else {
    84  		var result interface{}
    85  		err := json.Unmarshal(b, &result)
    86  		if err != nil {
    87  			return err
    88  		}
    89  		if resultMap, ok := result.(map[string]interface{}); ok {
    90  			r.Extra = internal.RemainingKeys(Group{}, resultMap)
    91  		}
    92  	}
    93  
    94  	return err
    95  }
    96  
    97  type groupResult struct {
    98  	golangsdk.Result
    99  }
   100  
   101  // GetResult is the response from a Get operation. Call its Extract method
   102  // to interpret it as a Group.
   103  type GetResult struct {
   104  	groupResult
   105  }
   106  
   107  // CreateResult is the response from a Create operation. Call its Extract method
   108  // to interpret it as a Group.
   109  type CreateResult struct {
   110  	groupResult
   111  }
   112  
   113  // UpdateResult is the response from an Update operation. Call its Extract
   114  // method to interpret it as a Group.
   115  type UpdateResult struct {
   116  	groupResult
   117  }
   118  
   119  // DeleteResult is the response from a Delete operation. Call its ExtractErr to
   120  // determine if the request succeeded or failed.
   121  type DeleteResult struct {
   122  	golangsdk.ErrResult
   123  }
   124  
   125  type UserResult struct {
   126  	golangsdk.Result
   127  }
   128  
   129  // GroupPage is a single page of Group results.
   130  type GroupPage struct {
   131  	pagination.LinkedPageBase
   132  }
   133  
   134  // IsEmpty determines whether or not a page of Groups contains any results.
   135  func (r GroupPage) IsEmpty() (bool, error) {
   136  	groups, err := ExtractGroups(r)
   137  	return len(groups) == 0, err
   138  }
   139  
   140  // NextPageURL extracts the "next" link from the links section of the result.
   141  func (r GroupPage) NextPageURL() (string, error) {
   142  	var s struct {
   143  		Links struct {
   144  			Next     string `json:"next"`
   145  			Previous string `json:"previous"`
   146  		} `json:"links"`
   147  	}
   148  	err := r.ExtractInto(&s)
   149  	if err != nil {
   150  		return "", err
   151  	}
   152  	return s.Links.Next, err
   153  }
   154  
   155  // ExtractGroups returns a slice of Groups contained in a single page of results.
   156  func ExtractGroups(r pagination.Page) ([]Group, error) {
   157  	var s struct {
   158  		Groups []Group `json:"groups"`
   159  	}
   160  	err := (r.(GroupPage)).ExtractInto(&s)
   161  	return s.Groups, err
   162  }
   163  
   164  // ExtractUsers returns a slice of users contained in a single page of results.
   165  func (r UserResult) Extract() ([]User, error) {
   166  	var s struct {
   167  		Users []User `json:"users"`
   168  	}
   169  	err := r.ExtractInto(&s)
   170  	return s.Users, err
   171  }
   172  
   173  // Extract interprets any group results as a Group.
   174  func (r groupResult) Extract() (*Group, error) {
   175  	var s struct {
   176  		Group *Group `json:"group"`
   177  	}
   178  	err := r.ExtractInto(&s)
   179  	return s.Group, err
   180  }