github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/db/v1/users/results.go (about)

     1  package users
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	db "github.com/huaweicloud/golangsdk/openstack/db/v1/databases"
     6  	"github.com/huaweicloud/golangsdk/pagination"
     7  )
     8  
     9  // User represents a database user
    10  type User struct {
    11  	// The user name
    12  	Name string
    13  
    14  	// The user password
    15  	Password string
    16  
    17  	// The databases associated with this user
    18  	Databases []db.Database
    19  }
    20  
    21  // CreateResult represents the result of a create operation.
    22  type CreateResult struct {
    23  	golangsdk.ErrResult
    24  }
    25  
    26  // DeleteResult represents the result of a delete operation.
    27  type DeleteResult struct {
    28  	golangsdk.ErrResult
    29  }
    30  
    31  // UserPage represents a single page of a paginated user collection.
    32  type UserPage struct {
    33  	pagination.LinkedPageBase
    34  }
    35  
    36  // IsEmpty checks to see whether the collection is empty.
    37  func (page UserPage) IsEmpty() (bool, error) {
    38  	users, err := ExtractUsers(page)
    39  	return len(users) == 0, err
    40  }
    41  
    42  // NextPageURL will retrieve the next page URL.
    43  func (page UserPage) NextPageURL() (string, error) {
    44  	var s struct {
    45  		Links []golangsdk.Link `json:"users_links"`
    46  	}
    47  	err := page.ExtractInto(&s)
    48  	if err != nil {
    49  		return "", err
    50  	}
    51  	return golangsdk.ExtractNextURL(s.Links)
    52  }
    53  
    54  // ExtractUsers will convert a generic pagination struct into a more
    55  // relevant slice of User structs.
    56  func ExtractUsers(r pagination.Page) ([]User, error) {
    57  	var s struct {
    58  		Users []User `json:"users"`
    59  	}
    60  	err := (r.(UserPage)).ExtractInto(&s)
    61  	return s.Users, err
    62  }