github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/evs/extensions/services/list.go (about)

     1  package services
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/opentelekomcloud/gophertelekomcloud"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
     9  )
    10  
    11  type ListOpts struct {
    12  	// Filter the service list result by binary name of the service.
    13  	Binary string `q:"binary"`
    14  	// Filter the service list result by host name of the service.
    15  	Host string `q:"host"`
    16  }
    17  
    18  func List(client *golangsdk.ServiceClient, opts ListOpts) ([]Service, error) {
    19  	url, err := golangsdk.NewURLBuilder().WithEndpoints("os-services").WithQueryParams(&opts).Build()
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  
    24  	raw, err := client.Get(client.ServiceURL(url.String()), nil, nil)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	var res []Service
    30  	err = extract.IntoSlicePtr(raw.Body, &res, "services")
    31  	return res, err
    32  }
    33  
    34  type Service struct {
    35  	// The binary name of the service.
    36  	Binary string `json:"binary"`
    37  	// The reason for disabling a service.
    38  	DisabledReason string `json:"disabled_reason"`
    39  	// The name of the host.
    40  	Host string `json:"host"`
    41  	// The state of the service. One of up or down.
    42  	State string `json:"state"`
    43  	// The status of the service. One of available or unavailable.
    44  	Status string `json:"status"`
    45  	// The date and time stamp when the extension was last updated.
    46  	UpdatedAt time.Time `json:"-"`
    47  	// The availability zone name.
    48  	Zone string `json:"zone"`
    49  	// The following fields are optional
    50  	// The host is frozen or not. Only in cinder-volume service.
    51  	Frozen bool `json:"frozen"`
    52  	// The cluster name. Only in cinder-volume service.
    53  	Cluster string `json:"cluster"`
    54  	// The volume service replication status. Only in cinder-volume service.
    55  	ReplicationStatus string `json:"replication_status"`
    56  	// The ID of active storage backend. Only in cinder-volume service.
    57  	ActiveBackendID string `json:"active_backend_id"`
    58  }
    59  
    60  func (r *Service) UnmarshalJSON(b []byte) error {
    61  	type tmp Service
    62  	var res struct {
    63  		tmp
    64  		UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
    65  	}
    66  	err := json.Unmarshal(b, &res)
    67  	if err != nil {
    68  		return err
    69  	}
    70  	*r = Service(res.tmp)
    71  
    72  	r.UpdatedAt = time.Time(res.UpdatedAt)
    73  
    74  	return nil
    75  }