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

     1  package users
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/chnsz/golangsdk"
     8  	"github.com/chnsz/golangsdk/pagination"
     9  )
    10  
    11  // User represents a User in the OpenStack Identity Service.
    12  type User struct {
    13  	// ID is the unique ID of the user.
    14  	ID string `json:"id"`
    15  
    16  	// Name is the name of the user.
    17  	Name string `json:"name"`
    18  
    19  	// DomainID is the domain ID the user belongs to.
    20  	DomainID string `json:"domain_id"`
    21  
    22  	// Enabled is whether or not the user is enabled.
    23  	Enabled bool `json:"enabled"`
    24  
    25  	// Description is a description of the user.
    26  	Description string `json:"description"`
    27  
    28  	PasswordStatus   bool   `json:"pwd_status"`
    29  	PasswordStrength string `json:"pwd_strength"`
    30  
    31  	// PasswordExpiresAt is the timestamp when the user's password expires.
    32  	PasswordExpiresAt time.Time `json:"-"`
    33  
    34  	LastProjectID    string `json:"last_project_id"`
    35  	DefaultProjectID string `json:"default_project_id"`
    36  
    37  	// Links contains referencing links to the user.
    38  	Links map[string]interface{} `json:"links"`
    39  }
    40  
    41  func (r *User) UnmarshalJSON(b []byte) error {
    42  	type tmp User
    43  	var s struct {
    44  		tmp
    45  		PasswordExpiresAt golangsdk.JSONRFC3339MilliNoZ `json:"password_expires_at"`
    46  	}
    47  	err := json.Unmarshal(b, &s)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	*r = User(s.tmp)
    52  
    53  	r.PasswordExpiresAt = time.Time(s.PasswordExpiresAt)
    54  
    55  	return err
    56  }
    57  
    58  type userResult struct {
    59  	golangsdk.Result
    60  }
    61  
    62  // GetResult is the response from a Get operation. Call its Extract method
    63  // to interpret it as a User.
    64  type GetResult struct {
    65  	userResult
    66  }
    67  
    68  // CreateResult is the response from a Create operation. Call its Extract method
    69  // to interpret it as a User.
    70  type CreateResult struct {
    71  	userResult
    72  }
    73  
    74  // UpdateResult is the response from an Update operation. Call its Extract
    75  // method to interpret it as a User.
    76  type UpdateResult struct {
    77  	userResult
    78  }
    79  
    80  // DeleteResult is the response from a Delete operation. Call its ExtractErr to
    81  // determine if the request succeeded or failed.
    82  type DeleteResult struct {
    83  	golangsdk.ErrResult
    84  }
    85  
    86  // UserPage is a single page of User results.
    87  type UserPage struct {
    88  	pagination.LinkedPageBase
    89  }
    90  
    91  // IsEmpty determines whether or not a UserPage contains any results.
    92  func (r UserPage) IsEmpty() (bool, error) {
    93  	users, err := ExtractUsers(r)
    94  	return len(users) == 0, err
    95  }
    96  
    97  // NextPageURL extracts the "next" link from the links section of the result.
    98  func (r UserPage) NextPageURL() (string, error) {
    99  	var s struct {
   100  		Links struct {
   101  			Next     string `json:"next"`
   102  			Previous string `json:"previous"`
   103  		} `json:"links"`
   104  	}
   105  	err := r.ExtractInto(&s)
   106  	if err != nil {
   107  		return "", err
   108  	}
   109  	return s.Links.Next, err
   110  }
   111  
   112  // ExtractUsers returns a slice of Users contained in a single page of results.
   113  func ExtractUsers(r pagination.Page) ([]User, error) {
   114  	var s struct {
   115  		Users []User `json:"users"`
   116  	}
   117  	err := (r.(UserPage)).ExtractInto(&s)
   118  	return s.Users, err
   119  }
   120  
   121  // Extract interprets any user results as a User.
   122  func (r userResult) Extract() (*User, error) {
   123  	var s struct {
   124  		User *User `json:"user"`
   125  	}
   126  	err := r.ExtractInto(&s)
   127  	return s.User, err
   128  }
   129  
   130  type AddMembershipResult struct {
   131  	golangsdk.ErrResult
   132  }