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

     1  package groups
     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  // 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  func (r *Group) UnmarshalJSON(b []byte) error {
    33  	type tmp Group
    34  	var s struct {
    35  		tmp
    36  		Extra map[string]interface{} `json:"extra"`
    37  	}
    38  	err := json.Unmarshal(b, &s)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	*r = Group(s.tmp)
    43  
    44  	// Collect other fields and bundle them into Extra
    45  	// but only if a field titled "extra" wasn't sent.
    46  	if s.Extra != nil {
    47  		r.Extra = s.Extra
    48  	} else {
    49  		var result interface{}
    50  		err := json.Unmarshal(b, &result)
    51  		if err != nil {
    52  			return err
    53  		}
    54  		if resultMap, ok := result.(map[string]interface{}); ok {
    55  			r.Extra = internal.RemainingKeys(Group{}, resultMap)
    56  		}
    57  	}
    58  
    59  	return err
    60  }
    61  
    62  type groupResult struct {
    63  	golangsdk.Result
    64  }
    65  
    66  // GetResult is the response from a Get operation. Call its Extract method
    67  // to interpret it as a Group.
    68  type GetResult struct {
    69  	groupResult
    70  }
    71  
    72  // CreateResult is the response from a Create operation. Call its Extract method
    73  // to interpret it as a Group.
    74  type CreateResult struct {
    75  	groupResult
    76  }
    77  
    78  // UpdateResult is the response from an Update operation. Call its Extract
    79  // method to interpret it as a Group.
    80  type UpdateResult struct {
    81  	groupResult
    82  }
    83  
    84  // DeleteResult is the response from a Delete operation. Call its ExtractErr to
    85  // determine if the request succeeded or failed.
    86  type DeleteResult struct {
    87  	golangsdk.ErrResult
    88  }
    89  
    90  // GroupPage is a single page of Group results.
    91  type GroupPage struct {
    92  	pagination.LinkedPageBase
    93  }
    94  
    95  // IsEmpty determines whether or not a page of Groups contains any results.
    96  func (r GroupPage) IsEmpty() (bool, error) {
    97  	groups, err := ExtractGroups(r)
    98  	return len(groups) == 0, err
    99  }
   100  
   101  // NextPageURL extracts the "next" link from the links section of the result.
   102  func (r GroupPage) NextPageURL() (string, error) {
   103  	var s struct {
   104  		Links struct {
   105  			Next     string `json:"next"`
   106  			Previous string `json:"previous"`
   107  		} `json:"links"`
   108  	}
   109  	err := r.ExtractInto(&s)
   110  	if err != nil {
   111  		return "", err
   112  	}
   113  	return s.Links.Next, err
   114  }
   115  
   116  // ExtractGroups returns a slice of Groups contained in a single page of results.
   117  func ExtractGroups(r pagination.Page) ([]Group, error) {
   118  	var s struct {
   119  		Groups []Group `json:"groups"`
   120  	}
   121  	err := (r.(GroupPage)).ExtractInto(&s)
   122  	return s.Groups, err
   123  }
   124  
   125  // Extract interprets any group results as a Group.
   126  func (r groupResult) Extract() (*Group, error) {
   127  	var s struct {
   128  		Group *Group `json:"group"`
   129  	}
   130  	err := r.ExtractInto(&s)
   131  	return s.Group, err
   132  }