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

     1  package clusters
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // Cluster represents an OpenStack Clustering cluster.
    12  type Cluster struct {
    13  	Config          map[string]interface{} `json:"config"`
    14  	CreatedAt       time.Time              `json:"-"`
    15  	Data            map[string]interface{} `json:"data"`
    16  	Dependents      map[string]interface{} `json:"dependents"`
    17  	DesiredCapacity int                    `json:"desired_capacity"`
    18  	Domain          string                 `json:"domain"`
    19  	ID              string                 `json:"id"`
    20  	InitAt          time.Time              `json:"-"`
    21  	MaxSize         int                    `json:"max_size"`
    22  	Metadata        map[string]interface{} `json:"metadata"`
    23  	MinSize         int                    `json:"min_size"`
    24  	Name            string                 `json:"name"`
    25  	Nodes           []string               `json:"nodes"`
    26  	Policies        []string               `json:"policies"`
    27  	ProfileID       string                 `json:"profile_id"`
    28  	ProfileName     string                 `json:"profile_name"`
    29  	Project         string                 `json:"project"`
    30  	Status          string                 `json:"status"`
    31  	StatusReason    string                 `json:"status_reason"`
    32  	Timeout         int                    `json:"timeout"`
    33  	UpdatedAt       time.Time              `json:"-"`
    34  	User            string                 `json:"user"`
    35  }
    36  
    37  func (r *Cluster) UnmarshalJSON(b []byte) error {
    38  	type tmp Cluster
    39  	var s struct {
    40  		tmp
    41  		CreatedAt string `json:"created_at"`
    42  		InitAt    string `json:"init_at"`
    43  		UpdatedAt string `json:"updated_at"`
    44  	}
    45  
    46  	err := json.Unmarshal(b, &s)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	*r = Cluster(s.tmp)
    51  
    52  	if s.CreatedAt != "" {
    53  		r.CreatedAt, err = time.Parse(gophercloud.RFC3339Milli, s.CreatedAt)
    54  		if err != nil {
    55  			return err
    56  		}
    57  	}
    58  
    59  	if s.InitAt != "" {
    60  		r.InitAt, err = time.Parse(gophercloud.RFC3339Milli, s.InitAt)
    61  		if err != nil {
    62  			return err
    63  		}
    64  	}
    65  
    66  	if s.UpdatedAt != "" {
    67  		r.UpdatedAt, err = time.Parse(gophercloud.RFC3339Milli, s.UpdatedAt)
    68  		if err != nil {
    69  			return err
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  // ClusterPolicy represents and OpenStack Clustering cluster policy.
    77  type ClusterPolicy struct {
    78  	ClusterID   string `json:"cluster_id"`
    79  	ClusterName string `json:"cluster_name"`
    80  	Enabled     bool   `json:"enabled"`
    81  	ID          string `json:"id"`
    82  	PolicyID    string `json:"policy_id"`
    83  	PolicyName  string `json:"policy_name"`
    84  	PolicyType  string `json:"policy_type"`
    85  }
    86  
    87  type ClusterAttributes struct {
    88  	ID    string      `json:"id"`
    89  	Value interface{} `json:"value"`
    90  }
    91  
    92  // Action represents an OpenStack Clustering action.
    93  type Action struct {
    94  	Action string `json:"action"`
    95  }
    96  
    97  // commonResult is the response of a base result.
    98  type commonResult struct {
    99  	gophercloud.Result
   100  }
   101  
   102  // Extract interprets any commonResult-based result as a Cluster.
   103  func (r commonResult) Extract() (*Cluster, error) {
   104  	var s struct {
   105  		Cluster *Cluster `json:"cluster"`
   106  	}
   107  
   108  	err := r.ExtractInto(&s)
   109  	return s.Cluster, err
   110  }
   111  
   112  // CreateResult is the response of a Create operations. Call its Extract method
   113  // to interpret it as a Cluster.
   114  type CreateResult struct {
   115  	commonResult
   116  }
   117  
   118  // GetResult is the response of a Get operations. Call its Extract method to
   119  // interpret it as a Cluster.
   120  type GetResult struct {
   121  	commonResult
   122  }
   123  
   124  // UpdateResult is the response of a Update operations. Call its Extract method
   125  // to interpret it as a Cluster.
   126  type UpdateResult struct {
   127  	commonResult
   128  }
   129  
   130  // GetPolicyResult is the response of a Get operations. Call its Extract method
   131  // to interpret it as a ClusterPolicy.
   132  type GetPolicyResult struct {
   133  	gophercloud.Result
   134  }
   135  
   136  // Extract interprets a GetPolicyResult as a ClusterPolicy.
   137  func (r GetPolicyResult) Extract() (*ClusterPolicy, error) {
   138  	var s struct {
   139  		ClusterPolicy *ClusterPolicy `json:"cluster_policy"`
   140  	}
   141  	err := r.ExtractInto(&s)
   142  	return s.ClusterPolicy, err
   143  }
   144  
   145  // DeleteResult is the result from a Delete operation. Call its ExtractErr
   146  // method to determine if the call succeeded or failed.
   147  type DeleteResult struct {
   148  	gophercloud.ErrResult
   149  }
   150  
   151  // ClusterPage contains a single page of all clusters from a List call.
   152  type ClusterPage struct {
   153  	pagination.LinkedPageBase
   154  }
   155  
   156  // IsEmpty determines whether or not a page of Clusters contains any results.
   157  func (page ClusterPage) IsEmpty() (bool, error) {
   158  	if page.StatusCode == 204 {
   159  		return true, nil
   160  	}
   161  
   162  	clusters, err := ExtractClusters(page)
   163  	return len(clusters) == 0, err
   164  }
   165  
   166  // ClusterPolicyPage contains a single page of all policies from a List call
   167  type ClusterPolicyPage struct {
   168  	pagination.SinglePageBase
   169  }
   170  
   171  // IsEmpty determines whether or not a page of ClusterPolicies contains any
   172  // results.
   173  func (page ClusterPolicyPage) IsEmpty() (bool, error) {
   174  	if page.StatusCode == 204 {
   175  		return true, nil
   176  	}
   177  
   178  	clusterPolicies, err := ExtractClusterPolicies(page)
   179  	return len(clusterPolicies) == 0, err
   180  }
   181  
   182  // ActionResult is the response of Senlin actions. Call its Extract method to
   183  // obtain the Action ID of the action.
   184  type ActionResult struct {
   185  	gophercloud.Result
   186  }
   187  
   188  // Extract interprets any Action result as an Action.
   189  func (r ActionResult) Extract() (string, error) {
   190  	var s struct {
   191  		Action string `json:"action"`
   192  	}
   193  	err := r.ExtractInto(&s)
   194  	return s.Action, err
   195  }
   196  
   197  type CollectResult struct {
   198  	gophercloud.Result
   199  }
   200  
   201  // ExtractClusters returns a slice of Clusters from the List operation.
   202  func ExtractClusters(r pagination.Page) ([]Cluster, error) {
   203  	var s struct {
   204  		Clusters []Cluster `json:"clusters"`
   205  	}
   206  	err := (r.(ClusterPage)).ExtractInto(&s)
   207  	return s.Clusters, err
   208  }
   209  
   210  // ExtractClusterPolicies returns a slice of ClusterPolicies from the
   211  // ListClusterPolicies operation.
   212  func ExtractClusterPolicies(r pagination.Page) ([]ClusterPolicy, error) {
   213  	var s struct {
   214  		ClusterPolicies []ClusterPolicy `json:"cluster_policies"`
   215  	}
   216  	err := (r.(ClusterPolicyPage)).ExtractInto(&s)
   217  	return s.ClusterPolicies, err
   218  }
   219  
   220  // Extract returns collected attributes across a cluster
   221  func (r CollectResult) Extract() ([]ClusterAttributes, error) {
   222  	var s struct {
   223  		Attributes []ClusterAttributes `json:"cluster_attributes"`
   224  	}
   225  	err := r.ExtractInto(&s)
   226  	return s.Attributes, err
   227  }