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

     1  package usage
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/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 golangsdk.JSONRFC3339MilliNoZ `json:"start"`
    44  		Stop  golangsdk.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   golangsdk.JSONRFC3339MilliNoZ `json:"ended_at"`
   103  		StartedAt golangsdk.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  	ks, err := ExtractSingleTenant(r)
   126  	return ks == nil, err
   127  }
   128  
   129  // NextPageURL uses the response's embedded link reference to navigate to the
   130  // next page of results.
   131  func (r SingleTenantPage) NextPageURL() (string, error) {
   132  	var s struct {
   133  		Links []golangsdk.Link `json:"tenant_usage_links"`
   134  	}
   135  	err := r.ExtractInto(&s)
   136  	if err != nil {
   137  		return "", err
   138  	}
   139  	return golangsdk.ExtractNextURL(s.Links)
   140  }
   141  
   142  // ExtractSingleTenant interprets a SingleTenantPage as a TenantUsage result.
   143  func ExtractSingleTenant(page pagination.Page) (*TenantUsage, error) {
   144  	var s struct {
   145  		TenantUsage *TenantUsage `json:"tenant_usage"`
   146  	}
   147  	err := (page.(SingleTenantPage)).ExtractInto(&s)
   148  	return s.TenantUsage, err
   149  }
   150  
   151  // AllTenantsPage stores a single, only page of TenantUsage results from a
   152  // AllTenants call.
   153  type AllTenantsPage struct {
   154  	pagination.LinkedPageBase
   155  }
   156  
   157  // ExtractAllTenants interprets a AllTenantsPage as a TenantUsage result.
   158  func ExtractAllTenants(page pagination.Page) ([]TenantUsage, error) {
   159  	var s struct {
   160  		TenantUsages []TenantUsage `json:"tenant_usages"`
   161  	}
   162  	err := (page.(AllTenantsPage)).ExtractInto(&s)
   163  	return s.TenantUsages, err
   164  }
   165  
   166  // IsEmpty determines whether or not an AllTenantsPage is empty.
   167  func (r AllTenantsPage) IsEmpty() (bool, error) {
   168  	usages, err := ExtractAllTenants(r)
   169  	return len(usages) == 0, err
   170  }
   171  
   172  // NextPageURL uses the response's embedded link reference to navigate to the
   173  // next page of results.
   174  func (r AllTenantsPage) NextPageURL() (string, error) {
   175  	var s struct {
   176  		Links []golangsdk.Link `json:"tenant_usages_links"`
   177  	}
   178  	err := r.ExtractInto(&s)
   179  	if err != nil {
   180  		return "", err
   181  	}
   182  	return golangsdk.ExtractNextURL(s.Links)
   183  }