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

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