From bb25d98f46cd0d0a8970ee814c965a5a41c15f0a Mon Sep 17 00:00:00 2001 From: JohnSharpe Date: Tue, 14 May 2024 12:42:24 +0100 Subject: [PATCH] WIP --- fixed_database_test.go | 164 ++++++++++++++++++++++++++++- fixture_test.go | 3 + service/fixed/databases/model.go | 10 +- service/fixed/databases/service.go | 7 +- 4 files changed, 176 insertions(+), 8 deletions(-) diff --git a/fixed_database_test.go b/fixed_database_test.go index b8a8037..9c8ae6e 100644 --- a/fixed_database_test.go +++ b/fixed_database_test.go @@ -95,7 +95,169 @@ func TestFixedDatabase_Create(t *testing.T) { assert.Equal(t, 51055029, actual) } -// TODO List +func TestFixedDatabase_List(t *testing.T) { + server := httptest.NewServer( + testServer( + "apiKey", + "secret", + getRequestWithQuery( + t, + "/fixed/subscriptions/111930/databases", + map[string][]string{ + "limit": { + "100", + }, + "offset": { + "0", + }, + }, + `{ + "accountId": 69369, + "subscription": { + "subscriptionId": 111930, + "numberOfDatabases": 1, + "databases": [ + { + "databaseId": 51055698, + "name": "my-second-test-fixed-database", + "protocol": "memcached", + "provider": "AWS", + "region": "us-west-1", + "respVersion": "resp2", + "status": "draft", + "planMemoryLimit": 1, + "memoryLimitMeasurementUnit": "GB", + "memoryUsedInMb": 7, + "memoryStorage": "ram", + "supportOSSClusterApi": false, + "useExternalEndpointForOSSClusterApi": false, + "dataPersistence": "none", + "replication": false, + "dataEvictionPolicy": "noeviction", + "activatedOn": "2024-05-14T09:27:48Z", + "replicaOf": null, + "replica": null, + "clustering": { + "enabled": true, + "regexRules": [ + { + "ordinal": 0, + "pattern": ".*\\{(?.*)\\}.*" + }, + { + "ordinal": 1, + "pattern": "(?.*)" + } + ], + "hashingPolicy": "standard" + }, + "security": { + "defaultUserEnabled": true, + "sslClientAuthentication": false, + "tlsClientAuthentication": false, + "enableTls": false, + "sourceIps": [ + "0.0.0.0/0" + ] + }, + "modules": [], + "alerts": [], + "backup": { + "remoteBackupEnabled": false, + "status": "idle" + }, + "links": [] + } + ], + "links": [] + }, + "links": [ + { + "rel": "self", + "type": "GET", + "href": "https://api-staging.qa.redislabs.com/v1/fixed/subscriptions/111930/databases?offset=0&limit=100" + } + ] + }`, + ), + getRequestWithQueryAndStatus( + t, + "/fixed/subscriptions/111930/databases", + map[string][]string{ + "limit": { + "100", + }, + "offset": { + "100", + }, + }, + 404, + "", + ), + ), + ) + + subject, err := clientFromTestServer(server, "apiKey", "secret") + require.NoError(t, err) + + list := subject.FixedDatabases.List(context.TODO(), 111930) + + var actual []*fixedDatabases.FixedDatabase + for list.Next() { + actual = append(actual, list.Value()) + } + require.NoError(t, list.Err()) + + assert.Equal(t, []*fixedDatabases.FixedDatabase{ + { + DatabaseId: redis.Int(51055698), + Name: redis.String("my-second-test-fixed-database"), + Protocol: redis.String("memcached"), + Provider: redis.String("AWS"), + Region: redis.String("us-west-1"), + RespVersion: redis.String("resp2"), + Status: redis.String("draft"), + PlanMemoryLimit: redis.Float64(1), + MemoryLimitMeasurementUnit: redis.String("GB"), + MemoryUsedInMb: redis.Float64(7), + MemoryStorage: redis.String("ram"), + SupportOSSClusterAPI: redis.Bool(false), + UseExternalEndpointForOSSClusterAPI: redis.Bool(false), + DataPersistence: redis.String("none"), + Replication: redis.Bool(false), + DataEvictionPolicy: redis.String("noeviction"), + ActivatedOn: redis.Time(time.Date(2024, 5, 14, 9, 27, 48, 0, time.UTC)), + Clustering: &fixedDatabases.Clustering{ + Enabled: redis.Bool(true), + RegexRules: []*databases.RegexRule{ + { + Ordinal: 0, + Pattern: ".*\\{(?.*)\\}.*", + }, + { + Ordinal: 1, + Pattern: "(?.*)", + }, + }, + HashingPolicy: redis.String("standard"), + }, + Security: &fixedDatabases.Security{ + EnableDefaultUser: redis.Bool(true), + SSLClientAuthentication: redis.Bool(false), + TLSClientAuthentication: redis.Bool(false), + EnableTls: redis.Bool(false), + SourceIPs: redis.StringSlice("0.0.0.0/0"), + }, + Modules: &[]*databases.Module{}, + Alerts: &[]*databases.Alert{}, + Backup: &fixedDatabases.Backup{ + Enabled: redis.Bool(false), + Status: redis.String("idle"), + }, + }, + }, actual) + +} func TestFixedDatabase_Get(t *testing.T) { server := httptest.NewServer( diff --git a/fixture_test.go b/fixture_test.go index 4b87efd..0a659f7 100644 --- a/fixture_test.go +++ b/fixture_test.go @@ -1,6 +1,7 @@ package rediscloud_api import ( + fixedDatabases "github.com/RedisLabs/rediscloud-go-api/service/fixed/databases" "testing" "github.com/RedisLabs/rediscloud-go-api/service/access_control_lists/redis_rules" @@ -121,4 +122,6 @@ func TestFixedSubcriptionFixtures(t *testing.T) { assert.Equal(t, "pending", fixedSubscriptions.FixedSubscriptionStatusPending) assert.Equal(t, "error", fixedSubscriptions.FixedSubscriptionStatusError) assert.Equal(t, "deleting", fixedSubscriptions.FixedSubscriptionStatusDeleting) + + assert.Equal(t, []string{"redis", "memcached", "stack"}, fixedDatabases.ProtocolValues()) } diff --git a/service/fixed/databases/model.go b/service/fixed/databases/model.go index 4e42d61..03f8c5a 100644 --- a/service/fixed/databases/model.go +++ b/service/fixed/databases/model.go @@ -156,7 +156,7 @@ func (o Import) String() string { } type listFixedDatabaseResponse struct { - FixedSubscription []*listDbSubscription `json:"subscription,omitempty"` + FixedSubscription *listDbSubscription `json:"subscription,omitempty"` } func (o listFixedDatabaseResponse) String() string { @@ -180,3 +180,11 @@ type NotFound struct { func (f *NotFound) Error() string { return fmt.Sprintf("fixed database %d in subscription %d not found", f.dbId, f.subId) } + +func ProtocolValues() []string { + return []string{ + "redis", + "memcached", + "stack", + } +} diff --git a/service/fixed/databases/service.go b/service/fixed/databases/service.go index bb4f7d4..31df085 100644 --- a/service/fixed/databases/service.go +++ b/service/fixed/databases/service.go @@ -8,7 +8,6 @@ import ( "strconv" "github.com/RedisLabs/rediscloud-go-api/internal" - "github.com/RedisLabs/rediscloud-go-api/redis" ) type Log interface { @@ -188,11 +187,7 @@ func (d *ListFixedDatabase) nextPage() error { return err } - if len(list.FixedSubscription) != 1 || redis.IntValue(list.FixedSubscription[0].ID) != d.subscription { - return fmt.Errorf("server didn't respond with just a single subscription") - } - - d.page = list.FixedSubscription[0].Databases + d.page = list.FixedSubscription.Databases d.offset += d.pageSize return nil