Skip to content

Commit

Permalink
Subscription plans list endpoint support (#170)
Browse files Browse the repository at this point in the history
* feat: Adding new subscription endpoint

* feat: adding api to client, fixing up api

* test: subscription test for plan subscriptions
  • Loading branch information
burythehammer authored Jul 8, 2024
1 parent 1038b17 commit 8a5814f
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 6 deletions.
16 changes: 10 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import (
"github.com/RedisLabs/rediscloud-go-api/service/databases"
fixedDatabases "github.com/RedisLabs/rediscloud-go-api/service/fixed/databases"
"github.com/RedisLabs/rediscloud-go-api/service/fixed/plans"
"github.com/RedisLabs/rediscloud-go-api/service/fixed/plans/plan_subscriptions"
fixedSubscriptions "github.com/RedisLabs/rediscloud-go-api/service/fixed/subscriptions"

"github.com/RedisLabs/rediscloud-go-api/service/latest_backups"
"github.com/RedisLabs/rediscloud-go-api/service/latest_imports"
"github.com/RedisLabs/rediscloud-go-api/service/pricing"
Expand All @@ -38,9 +40,10 @@ type Client struct {
LatestImport *latest_imports.API
Pricing *pricing.API
// fixed
FixedPlans *plans.API
FixedSubscriptions *fixedSubscriptions.API
FixedDatabases *fixedDatabases.API
FixedPlans *plans.API
FixedSubscriptions *fixedSubscriptions.API
FixedPlanSubscriptions *plan_subscriptions.API
FixedDatabases *fixedDatabases.API
// acl
RedisRules *redis_rules.API
Roles *roles.API
Expand Down Expand Up @@ -82,9 +85,10 @@ func NewClient(configs ...Option) (*Client, error) {
LatestImport: latest_imports.NewAPI(client, t, config.logger),
Pricing: pricing.NewAPI(client),
// fixed
FixedPlans: plans.NewAPI(client, config.logger),
FixedSubscriptions: fixedSubscriptions.NewAPI(client, t, config.logger),
FixedDatabases: fixedDatabases.NewAPI(client, t, config.logger),
FixedPlans: plans.NewAPI(client, config.logger),
FixedPlanSubscriptions: plan_subscriptions.NewAPI(client, config.logger),
FixedSubscriptions: fixedSubscriptions.NewAPI(client, t, config.logger),
FixedDatabases: fixedDatabases.NewAPI(client, t, config.logger),
// acl
RedisRules: redis_rules.NewAPI(client, t, config.logger),
Roles: roles.NewAPI(client, t, config.logger),
Expand Down
158 changes: 158 additions & 0 deletions fixed_plans_subscriptions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package rediscloud_api

import (
"context"
"net/http/httptest"
"testing"

"github.com/RedisLabs/rediscloud-go-api/redis"
"github.com/RedisLabs/rediscloud-go-api/service/fixed/plans"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const responseBody = `
{
"plans": [
{
"id": 98183,
"name": "Multi-AZ 5GB",
"size": 5,
"sizeMeasurementUnit": "GB",
"provider": "AWS",
"region": "us-east-1",
"regionId": 1,
"price": 100,
"priceCurrency": "USD",
"pricePeriod": "Month",
"maximumDatabases": 1,
"availability": "Multi-zone",
"connections": "unlimited",
"cidrAllowRules": 16,
"supportDataPersistence": true,
"supportInstantAndDailyBackups": true,
"supportReplication": true,
"supportClustering": false,
"supportedAlerts": [
"datasets-size",
"latency",
"throughput-higher-than",
"throughput-lower-than"
],
"customerSupport": "Standard",
"links": []
},
{
"id": 98181,
"name": "Multi-AZ 1GB",
"size": 1,
"sizeMeasurementUnit": "GB",
"provider": "AWS",
"region": "us-east-1",
"regionId": 1,
"price": 22,
"priceCurrency": "USD",
"pricePeriod": "Month",
"maximumDatabases": 1,
"availability": "Multi-zone",
"connections": "1024",
"cidrAllowRules": 8,
"supportDataPersistence": true,
"supportInstantAndDailyBackups": true,
"supportReplication": true,
"supportClustering": false,
"supportedAlerts": [
"datasets-size",
"throughput-higher-than",
"throughput-lower-than",
"latency",
"connections-limit"
],
"customerSupport": "Standard",
"links": []
}
],
"links": [
{
"rel": "self",
"href": "http://localhost:8081/v1/fixed/plans?cloud_provider=AWS",
"type": "GET"
}
]
}`

func Test_Plans_Subscriptions_List(t *testing.T) {
s := httptest.NewServer(
testServer("apiKey", "secret",
getRequest(
t,
"/fixed/plans/subscriptions/98183",
responseBody,
),
),
)

subject, err := clientFromTestServer(s, "apiKey", "secret")
require.NoError(t, err)

actualResponse, err := subject.FixedPlanSubscriptions.List(context.TODO(), 98183)

require.NoError(t, err)

expectedResponse := []*plans.GetPlanResponse{
{
ID: redis.Int(98183),
Name: redis.String("Multi-AZ 5GB"),
Size: redis.Float64(5),
SizeMeasurementUnit: redis.String("GB"),
Provider: redis.String("AWS"),
Region: redis.String("us-east-1"),
RegionID: redis.Int(1),
Price: redis.Int(100),
PriceCurrency: redis.String("USD"),
PricePeriod: redis.String("Month"),
MaximumDatabases: redis.Int(1),
Availability: redis.String("Multi-zone"),
Connections: redis.String("unlimited"),
CidrAllowRules: redis.Int(16),
SupportDataPersistence: redis.Bool(true),
SupportInstantAndDailyBackups: redis.Bool(true),
SupportReplication: redis.Bool(true),
SupportClustering: redis.Bool(false),
SupportedAlerts: redis.StringSlice(
"datasets-size",
"latency",
"throughput-higher-than",
"throughput-lower-than"),
CustomerSupport: redis.String("Standard"),
},
{
ID: redis.Int(98181),
Name: redis.String("Multi-AZ 1GB"),
Size: redis.Float64(1),
SizeMeasurementUnit: redis.String("GB"),
Provider: redis.String("AWS"),
Region: redis.String("us-east-1"),
RegionID: redis.Int(1),
Price: redis.Int(22),
PriceCurrency: redis.String("USD"),
PricePeriod: redis.String("Month"),
MaximumDatabases: redis.Int(1),
Availability: redis.String("Multi-zone"),
Connections: redis.String("1024"),
CidrAllowRules: redis.Int(8),
SupportDataPersistence: redis.Bool(true),
SupportInstantAndDailyBackups: redis.Bool(true),
SupportReplication: redis.Bool(true),
SupportClustering: redis.Bool(false),
SupportedAlerts: redis.StringSlice(
"datasets-size",
"throughput-higher-than",
"throughput-lower-than",
"latency",
"connections-limit"),
CustomerSupport: redis.String("Standard"),
},
}
assert.Equal(t, expectedResponse, actualResponse)
}
1 change: 1 addition & 0 deletions service/fixed/plans/plan_subscriptions/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package plan_subscriptions
42 changes: 42 additions & 0 deletions service/fixed/plans/plan_subscriptions/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package plan_subscriptions

import (
"context"
"fmt"

"github.com/RedisLabs/rediscloud-go-api/service/fixed/plans"
)

const root = "/fixed/plans/subscriptions"

type Log interface {
Printf(format string, args ...interface{})
}

type HttpClient interface {
Get(ctx context.Context, name, path string, responseBody interface{}) error
}

type API struct {
client HttpClient
logger Log
}

func NewAPI(client HttpClient, logger Log) *API {
return &API{client: client, logger: logger}
}

// List will list all plans upgradable from a given subscription
func (a *API) List(ctx context.Context, id int) ([]*plans.GetPlanResponse, error) {
var response plans.ListPlansResponse

path := fmt.Sprintf("%s/%d", root, id)
err := a.client.Get(ctx, "list plans for subscription plans", path, &response)
if err != nil {
return nil, err
}

a.logger.Printf("Listing fixed plans applicable to subscription %d, there are %d available", id, len(response.Plans))

return response.Plans, nil
}

0 comments on commit 8a5814f

Please sign in to comment.