github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/services/results.go (about)

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  // Service represents a Compute service in the OpenStack cloud.
    12  type Service struct {
    13  	// The binary name of the service.
    14  	Binary string `json:"binary"`
    15  
    16  	// The reason for disabling a service.
    17  	DisabledReason string `json:"disabled_reason"`
    18  
    19  	// The name of the host.
    20  	Host string `json:"host"`
    21  
    22  	// The id of the service.
    23  	ID int `json:"id"`
    24  
    25  	// The state of the service. One of up or down.
    26  	State string `json:"state"`
    27  
    28  	// The status of the service. One of enabled or disabled.
    29  	Status string `json:"status"`
    30  
    31  	// The date and time when the resource was updated.
    32  	UpdatedAt time.Time `json:"-"`
    33  
    34  	// The availability zone name.
    35  	Zone string `json:"zone"`
    36  }
    37  
    38  // UnmarshalJSON to override default
    39  func (r *Service) UnmarshalJSON(b []byte) error {
    40  	type tmp Service
    41  	var s struct {
    42  		tmp
    43  		UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
    44  	}
    45  	err := json.Unmarshal(b, &s)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	*r = Service(s.tmp)
    50  
    51  	r.UpdatedAt = time.Time(s.UpdatedAt)
    52  
    53  	return nil
    54  }
    55  
    56  // ServicePage represents a single page of all Services from a List request.
    57  type ServicePage struct {
    58  	pagination.SinglePageBase
    59  }
    60  
    61  // IsEmpty determines whether or not a page of Services contains any results.
    62  func (page ServicePage) IsEmpty() (bool, error) {
    63  	services, err := ExtractServices(page)
    64  	return len(services) == 0, err
    65  }
    66  
    67  func ExtractServices(r pagination.Page) ([]Service, error) {
    68  	var s struct {
    69  		Service []Service `json:"services"`
    70  	}
    71  	err := (r.(ServicePage)).ExtractInto(&s)
    72  	return s.Service, err
    73  }