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

     1  package usage
     2  
     3  import (
     4  	"net/url"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  // SingleTenantOpts are options for fetching usage of a single tenant.
    12  type SingleTenantOpts struct {
    13  	// The ending time to calculate usage statistics on compute and storage resources.
    14  	End *time.Time `q:"end"`
    15  
    16  	// The beginning time to calculate usage statistics on compute and storage resources.
    17  	Start *time.Time `q:"start"`
    18  }
    19  
    20  // SingleTenantOptsBuilder allows extensions to add additional parameters to the
    21  // SingleTenant request.
    22  type SingleTenantOptsBuilder interface {
    23  	ToUsageSingleTenantQuery() (string, error)
    24  }
    25  
    26  // ToUsageSingleTenantQuery formats a SingleTenantOpts into a query string.
    27  func (opts SingleTenantOpts) ToUsageSingleTenantQuery() (string, error) {
    28  	params := make(url.Values)
    29  	if opts.Start != nil {
    30  		params.Add("start", opts.Start.Format(golangsdk.RFC3339MilliNoZ))
    31  	}
    32  
    33  	if opts.End != nil {
    34  		params.Add("end", opts.End.Format(golangsdk.RFC3339MilliNoZ))
    35  	}
    36  
    37  	q := &url.URL{RawQuery: params.Encode()}
    38  	return q.String(), nil
    39  }
    40  
    41  // SingleTenant returns usage data about a single tenant.
    42  func SingleTenant(client *golangsdk.ServiceClient, tenantID string, opts SingleTenantOptsBuilder) pagination.Pager {
    43  	url := getTenantURL(client, tenantID)
    44  	if opts != nil {
    45  		query, err := opts.ToUsageSingleTenantQuery()
    46  		if err != nil {
    47  			return pagination.Pager{Err: err}
    48  		}
    49  		url += query
    50  	}
    51  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    52  		return SingleTenantPage{pagination.LinkedPageBase{PageResult: r}}
    53  	})
    54  }
    55  
    56  // AllTenantsOpts are options for fetching usage of all tenants.
    57  type AllTenantsOpts struct {
    58  	// Detailed will return detailed results.
    59  	Detailed bool
    60  
    61  	// The ending time to calculate usage statistics on compute and storage resources.
    62  	End *time.Time `q:"end"`
    63  
    64  	// The beginning time to calculate usage statistics on compute and storage resources.
    65  	Start *time.Time `q:"start"`
    66  }
    67  
    68  // AllTenantsOptsBuilder allows extensions to add additional parameters to the
    69  // AllTenants request.
    70  type AllTenantsOptsBuilder interface {
    71  	ToUsageAllTenantsQuery() (string, error)
    72  }
    73  
    74  // ToUsageAllTenantsQuery formats a AllTenantsOpts into a query string.
    75  func (opts AllTenantsOpts) ToUsageAllTenantsQuery() (string, error) {
    76  	params := make(url.Values)
    77  	if opts.Start != nil {
    78  		params.Add("start", opts.Start.Format(golangsdk.RFC3339MilliNoZ))
    79  	}
    80  
    81  	if opts.End != nil {
    82  		params.Add("end", opts.End.Format(golangsdk.RFC3339MilliNoZ))
    83  	}
    84  
    85  	if opts.Detailed == true {
    86  		params.Add("detailed", "1")
    87  	}
    88  
    89  	q := &url.URL{RawQuery: params.Encode()}
    90  	return q.String(), nil
    91  }
    92  
    93  // AllTenants returns usage data about all tenants.
    94  func AllTenants(client *golangsdk.ServiceClient, opts AllTenantsOptsBuilder) pagination.Pager {
    95  	url := allTenantsURL(client)
    96  	if opts != nil {
    97  		query, err := opts.ToUsageAllTenantsQuery()
    98  		if err != nil {
    99  			return pagination.Pager{Err: err}
   100  		}
   101  		url += query
   102  	}
   103  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   104  		return AllTenantsPage{pagination.LinkedPageBase{PageResult: r}}
   105  	})
   106  }