Skip to content

Support Linode Quota Limits related feature #699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions linode_quotas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package linodego

import "context"

// LinodeQuota represents a Linode-related quota information on your account.
type LinodeQuota struct {
QuotaID int `json:"quota_id"`
QuotaName string `json:"quota_name"`
Description string `json:"description"`
QuotaLimit int `json:"quota_limit"`
ResourceMetric string `json:"resource_metric"`
RegionApplied string `json:"region_applied"`
}

// LinodeQuotaUsage is the usage data for a specific Linode-related quota on your account.
type LinodeQuotaUsage struct {
QuotaLimit int `json:"quota_limit"`
Usage *int `json:"usage"`
}

// ListLinodeQuotas lists the active Linode-related quotas applied to your account.
// Linode Quota related features are under v4beta and may not currently be available to all users.
func (c *Client) ListLinodeQuotas(ctx context.Context, opts *ListOptions) ([]LinodeQuota, error) {
return getPaginatedResults[LinodeQuota](ctx, c, formatAPIPath("linode/quotas"), opts)
}

// GetLinodeQuota gets information about a specific Linode-related quota on your account.
// The operation includes any quota overrides in the response.
// Linode Quota related features are under v4beta and may not currently be available to all users.
func (c *Client) GetLinodeQuota(ctx context.Context, quotaID int) (*LinodeQuota, error) {
e := formatAPIPath("linode/quotas/%d", quotaID)
return doGETRequest[LinodeQuota](ctx, c, e)
}

// GetLinodeQuotaUsage gets usage data for a specific Linode Quota resource you can have on your account and the current usage for that resource.
// Linode Quota related features are under v4beta and may not currently be available to all users.
func (c *Client) GetLinodeQuotaUsage(ctx context.Context, quotaID int) (*LinodeQuotaUsage, error) {
e := formatAPIPath("linode/quotas/%d/usage", quotaID)
return doGETRequest[LinodeQuotaUsage](ctx, c, e)
}
8 changes: 8 additions & 0 deletions test/unit/fixtures/linode_quotas_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"quota_id": 123,
"quota_name": "Total vCPU for Dedicated Plans",
"description": "Maximum number of vCPUs assigned to Linodes with Dedicated Plans in this Region",
"quota_limit": 20,
"resource_metric": "cpu",
"region_applied": "us-lax"
}
22 changes: 22 additions & 0 deletions test/unit/fixtures/linode_quotas_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"data": [
{
"quota_id": 123,
"quota_name": "Total vCPU for Dedicated Plans",
"description": "Maximum number of vCPUs assigned to Linodes with Dedicated Plans in this Region",
"quota_limit": 20,
"resource_metric": "cpu",
"region_applied": "us-lax"
},
{
"quota_id": 456,
"quota_name": "Total vCPU for Shared Plans",
"description": "Maximum number of vCPUs assigned to Linodes with Shared Plans in this Region",
"quota_limit": 20,
"resource_metric": "cpu",
"region_applied": "us-lax"
}],
"page": 1,
"pages": 1,
"results": 2
}
4 changes: 4 additions & 0 deletions test/unit/fixtures/linode_quotas_usage_get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"quota_limit": 20,
"usage": 5
}
78 changes: 78 additions & 0 deletions test/unit/linode_quota_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package unit

import (
"context"
"testing"

"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
)

func TestLinodeQuotas_Get(t *testing.T) {
fixtureData, err := fixtures.GetFixture("linode_quotas_get")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("linode/quotas/123", fixtureData)

quota, err := base.Client.GetLinodeQuota(context.Background(), 123)
if err != nil {
t.Fatalf("Error getting linode quota: %v", err)
}

assert.Equal(t, 123, quota.QuotaID)
assert.Equal(t, "Total vCPU for Dedicated Plans", quota.QuotaName)
assert.Equal(t, "Maximum number of vCPUs assigned to Linodes with Dedicated Plans in this Region", quota.Description)
assert.Equal(t, 20, quota.QuotaLimit)
assert.Equal(t, "cpu", quota.ResourceMetric)
assert.Equal(t, "us-lax", quota.RegionApplied)
}

func TestLinodeQuotas_List(t *testing.T) {
fixtureData, err := fixtures.GetFixture("linode_quotas_list")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("linode/quotas", fixtureData)

quotas, err := base.Client.ListLinodeQuotas(context.Background(), &linodego.ListOptions{})
if err != nil {
t.Fatalf("Error listing linode quotas: %v", err)
}

if len(quotas) < 1 {
t.Fatalf("Expected to get a list of linode quotas but failed.")
}

assert.Equal(t, 123, quotas[0].QuotaID)
assert.Equal(t, "Total vCPU for Dedicated Plans", quotas[0].QuotaName)
assert.Equal(t, "Maximum number of vCPUs assigned to Linodes with Dedicated Plans in this Region", quotas[0].Description)
assert.Equal(t, 20, quotas[0].QuotaLimit)
assert.Equal(t, "cpu", quotas[0].ResourceMetric)
assert.Equal(t, "us-lax", quotas[0].RegionApplied)
}

func TestLinodeQuotaUsage_Get(t *testing.T) {
fixtureData, err := fixtures.GetFixture("linode_quotas_usage_get")
assert.NoError(t, err)

var base ClientBaseCase
base.SetUp(t)
defer base.TearDown(t)

base.MockGet("linode/quotas/123/usage", fixtureData)

quotaUsage, err := base.Client.GetLinodeQuotaUsage(context.Background(), 123)
if err != nil {
t.Fatalf("Error getting linode quota usage: %v", err)
}

assert.Equal(t, 20, quotaUsage.QuotaLimit)
assert.Equal(t, 5, *quotaUsage.Usage)
}