github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/services/results.go (about)

     1  package services
     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  // Service represents a Compute service in the OpenStack cloud.
    14  type Service struct {
    15  	// The binary name of the service.
    16  	Binary string `json:"binary"`
    17  
    18  	// The reason for disabling a service.
    19  	DisabledReason string `json:"disabled_reason"`
    20  
    21  	// Whether or not service was forced down manually.
    22  	ForcedDown bool `json:"forced_down"`
    23  
    24  	// The name of the host.
    25  	Host string `json:"host"`
    26  
    27  	// The id of the service.
    28  	ID string `json:"-"`
    29  
    30  	// The state of the service. One of up or down.
    31  	State string `json:"state"`
    32  
    33  	// The status of the service. One of enabled or disabled.
    34  	Status string `json:"status"`
    35  
    36  	// The date and time when the resource was updated.
    37  	UpdatedAt time.Time `json:"-"`
    38  
    39  	// The availability zone name.
    40  	Zone string `json:"zone"`
    41  }
    42  
    43  // UnmarshalJSON to override default
    44  func (r *Service) UnmarshalJSON(b []byte) error {
    45  	type tmp Service
    46  	var s struct {
    47  		tmp
    48  		ID        interface{}                     `json:"id"`
    49  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
    50  	}
    51  	err := json.Unmarshal(b, &s)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	*r = Service(s.tmp)
    56  
    57  	r.UpdatedAt = time.Time(s.UpdatedAt)
    58  
    59  	// OpenStack Compute service returns ID in string representation since
    60  	// 2.53 microversion API (Pike release).
    61  	switch t := s.ID.(type) {
    62  	case int:
    63  		r.ID = strconv.Itoa(t)
    64  	case float64:
    65  		r.ID = strconv.Itoa(int(t))
    66  	case string:
    67  		r.ID = t
    68  	default:
    69  		return fmt.Errorf("ID has unexpected type: %T", t)
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  type serviceResult struct {
    76  	gophercloud.Result
    77  }
    78  
    79  // Extract interprets any UpdateResult as a service, if possible.
    80  func (r serviceResult) Extract() (*Service, error) {
    81  	var s struct {
    82  		Service Service `json:"service"`
    83  	}
    84  	err := r.ExtractInto(&s)
    85  	return &s.Service, err
    86  }
    87  
    88  // UpdateResult is the response from an Update operation. Call its Extract
    89  // method to interpret it as a Server.
    90  type UpdateResult struct {
    91  	serviceResult
    92  }
    93  
    94  // ServicePage represents a single page of all Services from a List request.
    95  type ServicePage struct {
    96  	pagination.SinglePageBase
    97  }
    98  
    99  // IsEmpty determines whether or not a page of Services contains any results.
   100  func (page ServicePage) IsEmpty() (bool, error) {
   101  	if page.StatusCode == 204 {
   102  		return true, nil
   103  	}
   104  
   105  	services, err := ExtractServices(page)
   106  	return len(services) == 0, err
   107  }
   108  
   109  func ExtractServices(r pagination.Page) ([]Service, error) {
   110  	var s struct {
   111  		Service []Service `json:"services"`
   112  	}
   113  	err := (r.(ServicePage)).ExtractInto(&s)
   114  	return s.Service, err
   115  }
   116  
   117  // DeleteResult is the response from a Delete operation. Call its ExtractErr
   118  // method to determine if the call succeeded or failed.
   119  type DeleteResult struct {
   120  	gophercloud.ErrResult
   121  }