github.com/gophercloud/gophercloud@v1.11.0/openstack/clustering/v1/nodes/results.go (about)

     1  package nodes
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // Node represents an OpenStack clustering node.
    12  type Node struct {
    13  	ClusterID    string                 `json:"cluster_id"`
    14  	CreatedAt    time.Time              `json:"-"`
    15  	Data         map[string]interface{} `json:"data"`
    16  	Dependents   map[string]interface{} `json:"dependents"`
    17  	Domain       string                 `json:"domain"`
    18  	ID           string                 `json:"id"`
    19  	Index        int                    `json:"index"`
    20  	InitAt       time.Time              `json:"-"`
    21  	Metadata     map[string]interface{} `json:"metadata"`
    22  	Name         string                 `json:"name"`
    23  	PhysicalID   string                 `json:"physical_id"`
    24  	ProfileID    string                 `json:"profile_id"`
    25  	ProfileName  string                 `json:"profile_name"`
    26  	Project      string                 `json:"project"`
    27  	Role         string                 `json:"role"`
    28  	Status       string                 `json:"status"`
    29  	StatusReason string                 `json:"status_reason"`
    30  	UpdatedAt    time.Time              `json:"-"`
    31  	User         string                 `json:"user"`
    32  }
    33  
    34  func (r *Node) UnmarshalJSON(b []byte) error {
    35  	type tmp Node
    36  	var s struct {
    37  		tmp
    38  		CreatedAt string `json:"created_at"`
    39  		InitAt    string `json:"init_at"`
    40  		UpdatedAt string `json:"updated_at"`
    41  	}
    42  
    43  	err := json.Unmarshal(b, &s)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	*r = Node(s.tmp)
    48  
    49  	if s.CreatedAt != "" {
    50  		r.CreatedAt, err = time.Parse(time.RFC3339, s.CreatedAt)
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	if s.InitAt != "" {
    57  		r.InitAt, err = time.Parse(time.RFC3339, s.InitAt)
    58  		if err != nil {
    59  			return err
    60  		}
    61  	}
    62  
    63  	if s.UpdatedAt != "" {
    64  		r.UpdatedAt, err = time.Parse(time.RFC3339, s.UpdatedAt)
    65  		if err != nil {
    66  			return err
    67  		}
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  // commonResult is the response of a base result.
    74  type commonResult struct {
    75  	gophercloud.Result
    76  }
    77  
    78  // Extract interprets any commonResult-based result as a Node.
    79  func (r commonResult) Extract() (*Node, error) {
    80  	var s struct {
    81  		Node *Node `json:"node"`
    82  	}
    83  	err := r.ExtractInto(&s)
    84  	return s.Node, err
    85  }
    86  
    87  // CreateResult is the result of a Create operation. Call its Extract
    88  // method to intepret it as a Node.
    89  type CreateResult struct {
    90  	commonResult
    91  }
    92  
    93  // GetResult is the result of a Get operation. Call its Extract method to
    94  // interpret it as a Node.
    95  type GetResult struct {
    96  	commonResult
    97  }
    98  
    99  // DeleteResult is the result from a Delete operation. Call ExtractErr
   100  // method to determine if the call succeeded or failed.
   101  type DeleteResult struct {
   102  	gophercloud.ErrResult
   103  }
   104  
   105  // UpdateResult is the result of an Update operation. Call its Extract method
   106  // to interpet it as a Node.
   107  type UpdateResult struct {
   108  	commonResult
   109  }
   110  
   111  // NodePage contains a single page of all nodes from a List call.
   112  type NodePage struct {
   113  	pagination.LinkedPageBase
   114  }
   115  
   116  // IsEmpty determines if a NodePage contains any results.
   117  func (page NodePage) IsEmpty() (bool, error) {
   118  	if page.StatusCode == 204 {
   119  		return true, nil
   120  	}
   121  
   122  	nodes, err := ExtractNodes(page)
   123  	return len(nodes) == 0, err
   124  }
   125  
   126  // ExtractNodes returns a slice of Nodes from the List operation.
   127  func ExtractNodes(r pagination.Page) ([]Node, error) {
   128  	var s struct {
   129  		Nodes []Node `json:"nodes"`
   130  	}
   131  	err := (r.(NodePage)).ExtractInto(&s)
   132  	return s.Nodes, err
   133  }
   134  
   135  // ActionResult is the response of Senlin actions. Call its Extract method to
   136  // obtain the Action ID of the action.
   137  type ActionResult struct {
   138  	gophercloud.Result
   139  }
   140  
   141  // Extract interprets any Action result as an Action.
   142  func (r ActionResult) Extract() (string, error) {
   143  	var s struct {
   144  		Action string `json:"action"`
   145  	}
   146  	err := r.ExtractInto(&s)
   147  	return s.Action, err
   148  }