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

     1  package stackresources
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  // Resource represents a stack resource.
    12  type Resource struct {
    13  	CreationTime time.Time        `json:"-"`
    14  	Links        []golangsdk.Link `json:"links"`
    15  	LogicalID    string           `json:"logical_resource_id"`
    16  	Name         string           `json:"resource_name"`
    17  	PhysicalID   string           `json:"physical_resource_id"`
    18  	RequiredBy   []string         `json:"required_by"`
    19  	Status       string           `json:"resource_status"`
    20  	StatusReason string           `json:"resource_status_reason"`
    21  	Type         string           `json:"resource_type"`
    22  	UpdatedTime  time.Time        `json:"-"`
    23  }
    24  
    25  // ResourcePage is the page returned by a pager when traversing over a
    26  // collection of resources.
    27  type ResourcePage struct {
    28  	pagination.LinkedPageBase
    29  }
    30  
    31  func (r *Resource) UnmarshalJSON(b []byte) error {
    32  	type tmp Resource
    33  	var s struct {
    34  		tmp
    35  		CreationTime golangsdk.JSONRFC3339NoZ `json:"creation_time"`
    36  		UpdatedTime  golangsdk.JSONRFC3339NoZ `json:"updated_time"`
    37  	}
    38  	err := json.Unmarshal(b, &s)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	*r = Resource(s.tmp)
    43  
    44  	r.CreationTime = time.Time(s.CreationTime)
    45  	r.UpdatedTime = time.Time(s.UpdatedTime)
    46  
    47  	return nil
    48  }
    49  
    50  // IsEmpty returns true if a page contains no Server results.
    51  func (r ResourcePage) IsEmpty() (bool, error) {
    52  	resources, err := ExtractResources(r)
    53  	return len(resources) == 0, err
    54  }
    55  
    56  // ExtractResources accepts a Page struct, specifically a ResourcePage struct,
    57  // and extracts the elements into a slice of Resource structs. In other words,
    58  // a generic collection is mapped into a relevant slice.
    59  func ExtractResources(r pagination.Page) ([]Resource, error) {
    60  	var s struct {
    61  		Resources []Resource `json:"resources"`
    62  	}
    63  	err := (r.(ResourcePage)).ExtractInto(&s)
    64  	return s.Resources, err
    65  }