From 19234df048466f4587225609f616969a6f91fb99 Mon Sep 17 00:00:00 2001 From: Ye Chen Date: Wed, 19 Mar 2025 15:33:46 -0400 Subject: [PATCH 1/2] linode quota --- linode_quotas.go | 40 ++++++++++ test/unit/fixtures/linode_quotas_get.json | 8 ++ test/unit/fixtures/linode_quotas_list.json | 22 ++++++ .../fixtures/linode_quotas_usage_get.json | 4 + test/unit/linode_quota_test.go | 77 +++++++++++++++++++ 5 files changed, 151 insertions(+) create mode 100644 linode_quotas.go create mode 100644 test/unit/fixtures/linode_quotas_get.json create mode 100644 test/unit/fixtures/linode_quotas_list.json create mode 100644 test/unit/fixtures/linode_quotas_usage_get.json create mode 100644 test/unit/linode_quota_test.go diff --git a/linode_quotas.go b/linode_quotas.go new file mode 100644 index 000000000..614346637 --- /dev/null +++ b/linode_quotas.go @@ -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) +} diff --git a/test/unit/fixtures/linode_quotas_get.json b/test/unit/fixtures/linode_quotas_get.json new file mode 100644 index 000000000..3264b73b7 --- /dev/null +++ b/test/unit/fixtures/linode_quotas_get.json @@ -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" +} \ No newline at end of file diff --git a/test/unit/fixtures/linode_quotas_list.json b/test/unit/fixtures/linode_quotas_list.json new file mode 100644 index 000000000..695f47ec6 --- /dev/null +++ b/test/unit/fixtures/linode_quotas_list.json @@ -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 +} \ No newline at end of file diff --git a/test/unit/fixtures/linode_quotas_usage_get.json b/test/unit/fixtures/linode_quotas_usage_get.json new file mode 100644 index 000000000..0febe4f58 --- /dev/null +++ b/test/unit/fixtures/linode_quotas_usage_get.json @@ -0,0 +1,4 @@ +{ + "quota_limit": 20, + "usage": 5 +} \ No newline at end of file diff --git a/test/unit/linode_quota_test.go b/test/unit/linode_quota_test.go new file mode 100644 index 000000000..8f7a14c23 --- /dev/null +++ b/test/unit/linode_quota_test.go @@ -0,0 +1,77 @@ +package unit + +import ( + "context" + "github.com/linode/linodego" + "github.com/stretchr/testify/assert" + "testing" +) + +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) +} From df2595c2b5c8164a1590ca9b6b0e72d70763fb44 Mon Sep 17 00:00:00 2001 From: Ye Chen Date: Wed, 19 Mar 2025 15:39:07 -0400 Subject: [PATCH 2/2] fmt --- test/unit/linode_quota_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unit/linode_quota_test.go b/test/unit/linode_quota_test.go index 8f7a14c23..71e581301 100644 --- a/test/unit/linode_quota_test.go +++ b/test/unit/linode_quota_test.go @@ -2,9 +2,10 @@ package unit import ( "context" + "testing" + "github.com/linode/linodego" "github.com/stretchr/testify/assert" - "testing" ) func TestLinodeQuotas_Get(t *testing.T) {