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

     1  package usage
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  // TenantUsage is a set of usage information about a tenant over the sampling window
    12  type TenantUsage struct {
    13  	// ServerUsages is an array of ServerUsage maps
    14  	ServerUsages []ServerUsage `json:"server_usages"`
    15  
    16  	// Start is the beginning time to calculate usage statistics on compute and storage resources
    17  	Start time.Time `json:"-"`
    18  
    19  	// Stop is the ending time to calculate usage statistics on compute and storage resources
    20  	Stop time.Time `json:"-"`
    21  
    22  	// TenantID is the ID of the tenant whose usage is being reported on
    23  	TenantID string `json:"tenant_id"`
    24  
    25  	// TotalHours is the total duration that servers exist (in hours)
    26  	TotalHours float64 `json:"total_hours"`
    27  
    28  	// TotalLocalGBUsage multiplies the server disk size (in GiB) by hours the server exists, and then adding that all together for each server
    29  	TotalLocalGBUsage float64 `json:"total_local_gb_usage"`
    30  
    31  	// TotalMemoryMBUsage multiplies the server memory size (in MB) by hours the server exists, and then adding that all together for each server
    32  	TotalMemoryMBUsage float64 `json:"total_memory_mb_usage"`
    33  
    34  	// TotalVCPUsUsage multiplies the number of virtual CPUs of the server by hours the server exists, and then adding that all together for each server
    35  	TotalVCPUsUsage float64 `json:"total_vcpus_usage"`
    36  }
    37  
    38  // UnmarshalJSON sets *u to a copy of data.
    39  func (u *TenantUsage) UnmarshalJSON(b []byte) error {
    40  	type tmp TenantUsage
    41  	var s struct {
    42  		tmp
    43  		Start gophercloud.JSONRFC3339MilliNoZ `json:"start"`
    44  		Stop  gophercloud.JSONRFC3339MilliNoZ `json:"stop"`
    45  	}
    46  
    47  	if err := json.Unmarshal(b, &s); err != nil {
    48  		return err
    49  	}
    50  	*u = TenantUsage(s.tmp)
    51  
    52  	u.Start = time.Time(s.Start)
    53  	u.Stop = time.Time(s.Stop)
    54  
    55  	return nil
    56  }
    57  
    58  // ServerUsage is a detailed set of information about a specific instance inside a tenant
    59  type ServerUsage struct {
    60  	// EndedAt is the date and time when the server was deleted
    61  	EndedAt time.Time `json:"-"`
    62  
    63  	// Flavor is the display name of a flavor
    64  	Flavor string `json:"flavor"`
    65  
    66  	// Hours is the duration that the server exists in hours
    67  	Hours float64 `json:"hours"`
    68  
    69  	// InstanceID is the UUID of the instance
    70  	InstanceID string `json:"instance_id"`
    71  
    72  	// LocalGB is the sum of the root disk size of the server and the ephemeral disk size of it (in GiB)
    73  	LocalGB int `json:"local_gb"`
    74  
    75  	// MemoryMB is the memory size of the server (in MB)
    76  	MemoryMB int `json:"memory_mb"`
    77  
    78  	// Name is the name assigned to the server when it was created
    79  	Name string `json:"name"`
    80  
    81  	// StartedAt is the date and time when the server was started
    82  	StartedAt time.Time `json:"-"`
    83  
    84  	// State is the VM power state
    85  	State string `json:"state"`
    86  
    87  	// TenantID is the UUID of the tenant in a multi-tenancy cloud
    88  	TenantID string `json:"tenant_id"`
    89  
    90  	// Uptime is the uptime of the server in seconds
    91  	Uptime int `json:"uptime"`
    92  
    93  	// VCPUs is the number of virtual CPUs that the server uses
    94  	VCPUs int `json:"vcpus"`
    95  }
    96  
    97  // UnmarshalJSON sets *u to a copy of data.
    98  func (u *ServerUsage) UnmarshalJSON(b []byte) error {
    99  	type tmp ServerUsage
   100  	var s struct {
   101  		tmp
   102  		EndedAt   gophercloud.JSONRFC3339MilliNoZ `json:"ended_at"`
   103  		StartedAt gophercloud.JSONRFC3339MilliNoZ `json:"started_at"`
   104  	}
   105  
   106  	if err := json.Unmarshal(b, &s); err != nil {
   107  		return err
   108  	}
   109  	*u = ServerUsage(s.tmp)
   110  
   111  	u.EndedAt = time.Time(s.EndedAt)
   112  	u.StartedAt = time.Time(s.StartedAt)
   113  
   114  	return nil
   115  }
   116  
   117  // SingleTenantPage stores a single, only page of TenantUsage results from a
   118  // SingleTenant call.
   119  type SingleTenantPage struct {
   120  	pagination.LinkedPageBase
   121  }
   122  
   123  // IsEmpty determines whether or not a SingleTenantPage is empty.
   124  func (r SingleTenantPage) IsEmpty() (bool, error) {
   125  	if r.StatusCode == 204 {
   126  		return true, nil
   127  	}
   128  
   129  	ks, err := ExtractSingleTenant(r)
   130  	return ks == nil, err
   131  }
   132  
   133  // NextPageURL uses the response's embedded link reference to navigate to the
   134  // next page of results.
   135  func (r SingleTenantPage) NextPageURL() (string, error) {
   136  	var s struct {
   137  		Links []gophercloud.Link `json:"tenant_usage_links"`
   138  	}
   139  	err := r.ExtractInto(&s)
   140  	if err != nil {
   141  		return "", err
   142  	}
   143  	return gophercloud.ExtractNextURL(s.Links)
   144  }
   145  
   146  // ExtractSingleTenant interprets a SingleTenantPage as a TenantUsage result.
   147  func ExtractSingleTenant(page pagination.Page) (*TenantUsage, error) {
   148  	var s struct {
   149  		TenantUsage *TenantUsage `json:"tenant_usage"`
   150  	}
   151  	err := (page.(SingleTenantPage)).ExtractInto(&s)
   152  	return s.TenantUsage, err
   153  }
   154  
   155  // AllTenantsPage stores a single, only page of TenantUsage results from a
   156  // AllTenants call.
   157  type AllTenantsPage struct {
   158  	pagination.LinkedPageBase
   159  }
   160  
   161  // ExtractAllTenants interprets a AllTenantsPage as a TenantUsage result.
   162  func ExtractAllTenants(page pagination.Page) ([]TenantUsage, error) {
   163  	var s struct {
   164  		TenantUsages []TenantUsage `json:"tenant_usages"`
   165  	}
   166  	err := (page.(AllTenantsPage)).ExtractInto(&s)
   167  	return s.TenantUsages, err
   168  }
   169  
   170  // IsEmpty determines whether or not an AllTenantsPage is empty.
   171  func (r AllTenantsPage) IsEmpty() (bool, error) {
   172  	if r.StatusCode == 204 {
   173  		return true, nil
   174  	}
   175  
   176  	usages, err := ExtractAllTenants(r)
   177  	return len(usages) == 0, err
   178  }
   179  
   180  // NextPageURL uses the response's embedded link reference to navigate to the
   181  // next page of results.
   182  func (r AllTenantsPage) NextPageURL() (string, error) {
   183  	var s struct {
   184  		Links []gophercloud.Link `json:"tenant_usages_links"`
   185  	}
   186  	err := r.ExtractInto(&s)
   187  	if err != nil {
   188  		return "", err
   189  	}
   190  	return gophercloud.ExtractNextURL(s.Links)
   191  }