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

     1  package profiles
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strconv"
     7  	"time"
     8  
     9  	"github.com/gophercloud/gophercloud"
    10  	"github.com/gophercloud/gophercloud/pagination"
    11  )
    12  
    13  // Profile represent a detailed profile.
    14  type Profile struct {
    15  	CreatedAt time.Time              `json:"-"`
    16  	Domain    string                 `json:"domain"`
    17  	ID        string                 `json:"id"`
    18  	Metadata  map[string]interface{} `json:"metadata"`
    19  	Name      string                 `json:"name"`
    20  	Project   string                 `json:"project"`
    21  	Spec      Spec                   `json:"spec"`
    22  	Type      string                 `json:"type"`
    23  	UpdatedAt time.Time              `json:"-"`
    24  	User      string                 `json:"user"`
    25  }
    26  
    27  func (r *Profile) UnmarshalJSON(b []byte) error {
    28  	type tmp Profile
    29  	var s struct {
    30  		tmp
    31  		CreatedAt string `json:"created_at"`
    32  		UpdatedAt string `json:"updated_at"`
    33  	}
    34  
    35  	err := json.Unmarshal(b, &s)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	*r = Profile(s.tmp)
    40  
    41  	if s.CreatedAt != "" {
    42  		r.CreatedAt, err = time.Parse(time.RFC3339, s.CreatedAt)
    43  		if err != nil {
    44  			return err
    45  		}
    46  	}
    47  
    48  	if s.UpdatedAt != "" {
    49  		r.UpdatedAt, err = time.Parse(time.RFC3339, s.UpdatedAt)
    50  		if err != nil {
    51  			return err
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  // Spec represents a profile spec.
    59  type Spec struct {
    60  	Type       string                 `json:"type"`
    61  	Version    string                 `json:"-"`
    62  	Properties map[string]interface{} `json:"properties"`
    63  }
    64  
    65  func (r *Spec) UnmarshalJSON(b []byte) error {
    66  	type tmp Spec
    67  	var s struct {
    68  		tmp
    69  		Version interface{} `json:"version"`
    70  	}
    71  
    72  	err := json.Unmarshal(b, &s)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	*r = Spec(s.tmp)
    77  
    78  	switch t := s.Version.(type) {
    79  	case float64:
    80  		if t == 1 {
    81  			r.Version = fmt.Sprintf("%.1f", t)
    82  		} else {
    83  			r.Version = strconv.FormatFloat(t, 'f', -1, 64)
    84  		}
    85  	case string:
    86  		r.Version = t
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func (r Spec) MarshalJSON() ([]byte, error) {
    93  	spec := struct {
    94  		Type       string                 `json:"type"`
    95  		Version    string                 `json:"version"`
    96  		Properties map[string]interface{} `json:"properties"`
    97  	}{
    98  		Type:       r.Type,
    99  		Version:    r.Version,
   100  		Properties: r.Properties,
   101  	}
   102  	return json.Marshal(spec)
   103  }
   104  
   105  // commonResult is the base result of a Profile operation.
   106  type commonResult struct {
   107  	gophercloud.Result
   108  }
   109  
   110  // Extract provides access to Profile returned by the Get and Create functions.
   111  func (r commonResult) Extract() (*Profile, error) {
   112  	var s struct {
   113  		Profile *Profile `json:"profile"`
   114  	}
   115  	err := r.ExtractInto(&s)
   116  	return s.Profile, err
   117  }
   118  
   119  // CreateResult is the result of a Create operation. Call its Extract
   120  // method to interpret it as a Profile.
   121  type CreateResult struct {
   122  	commonResult
   123  }
   124  
   125  // GetResult is the result of a Get operations. Call its Extract
   126  // method to interpret it as a Profile.
   127  type GetResult struct {
   128  	commonResult
   129  }
   130  
   131  // UpdateResult is the result of a Update operations. Call its Extract
   132  // method to interpret it as a Profile.
   133  type UpdateResult struct {
   134  	commonResult
   135  }
   136  
   137  // DeleteResult is the result from a Delete operation. Call its ExtractErr
   138  // method to determine if the call succeeded or failed.
   139  type DeleteResult struct {
   140  	gophercloud.ErrResult
   141  }
   142  
   143  // ValidateResult is the response of a Validate operations.
   144  type ValidateResult struct {
   145  	commonResult
   146  }
   147  
   148  // ProfilePage contains a single page of all profiles from a List operation.
   149  type ProfilePage struct {
   150  	pagination.LinkedPageBase
   151  }
   152  
   153  // IsEmpty determines if a ProfilePage contains any results.
   154  func (page ProfilePage) IsEmpty() (bool, error) {
   155  	if page.StatusCode == 204 {
   156  		return true, nil
   157  	}
   158  
   159  	profiles, err := ExtractProfiles(page)
   160  	return len(profiles) == 0, err
   161  }
   162  
   163  // ExtractProfiles returns a slice of Profiles from the List operation.
   164  func ExtractProfiles(r pagination.Page) ([]Profile, error) {
   165  	var s struct {
   166  		Profiles []Profile `json:"profiles"`
   167  	}
   168  	err := (r.(ProfilePage)).ExtractInto(&s)
   169  	return s.Profiles, err
   170  }