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

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