From 13840c0992e30928ae87c3031fcdfd90febff0ad Mon Sep 17 00:00:00 2001 From: Mark Boyd Date: Thu, 19 Dec 2024 14:46:58 -0500 Subject: [PATCH] Fix RDS instance JSON handling (#394) * update README * fix handling of JSONB field for EnabledCloudWatchLogGroupExports property --- README.md | 10 +++++-- main_test.go | 53 +++++++++++++++++++++++++++++++++++++ services/rds/broker.go | 19 ++++++------- services/rds/rdsinstance.go | 16 ++++++++++- 4 files changed, 86 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ed19b83f..beff51fa 100644 --- a/README.md +++ b/README.md @@ -79,8 +79,14 @@ secrets.yml contains the all of the secrets for the different resources. ## Testing and development -Make sure you have a valid secrets.yml and catalog.yml. The easiest way is to copy catalog-test.yml and secrets-test.yml to catalog.yml and secrets.yml. Once you have these in place -run `go test` to run the tests +Make sure you have a valid `secrets.yml` and `catalog.yml`: + +```shell +cp catalog-test.yml catalog.yml +cp secrets-test.yml secrets.yml +``` + +Once you have these in place, run `go test ./...` to run the tests. ### How to deploy it diff --git a/main_test.go b/main_test.go index 6f7ff715..a0d65c32 100644 --- a/main_test.go +++ b/main_test.go @@ -2,6 +2,7 @@ package main import ( "bytes" + "slices" "strings" "github.com/go-martini/martini" @@ -39,6 +40,17 @@ var createRDSInstanceReq = []byte( "space_guid":"a-space" }`) +var createRDSInstanceWithEnabledLogGroupsReq = []byte( + `{ + "service_id":"db80ca29-2d1b-4fbc-aad3-d03c0bfa7593", + "plan_id":"da91e15c-98c9-46a9-b114-02b8d28062c6", + "organization_guid":"an-org", + "space_guid":"a-space", + "parameters": { + "enable_cloudwatch_log_groups_exports": ["foo"] + } +}`) + var createRDSPGWithVersionInstanceReq = []byte( `{ "service_id":"db80ca29-2d1b-4fbc-aad3-d03c0bfa7593", @@ -481,6 +493,47 @@ func TestCreateRDSPGWithInvaildVersionInstance(t *testing.T) { } } +func TestCreateRDSInstanceWithEnabledLogGroups(t *testing.T) { + urlAcceptsIncomplete := "/v2/service_instances/the_RDS_instance?accepts_incomplete=true" + res, _ := doRequest(nil, urlAcceptsIncomplete, "PUT", true, bytes.NewBuffer(createRDSInstanceWithEnabledLogGroupsReq)) + + if res.Code != http.StatusAccepted { + t.Logf("Unable to create instance. Body is: " + res.Body.String()) + t.Error(urlAcceptsIncomplete, "with auth should return 202 and it returned", res.Code) + } + + // Is it a valid JSON? + validJSON(res.Body.Bytes(), urlAcceptsIncomplete, t) + + // Does it say "accepted"? + if !strings.Contains(res.Body.String(), "accepted") { + t.Error(urlAcceptsIncomplete, "should return the instance accepted message") + } + // Is it in the database and has a username and password? + i := rds.RDSInstance{} + brokerDB.Where("uuid = ?", "the_RDS_instance").First(&i) + if i.Uuid == "0" { + t.Error("The instance should be saved in the DB") + } + + if i.Username == "" || i.Password == "" { + t.Error("The instance should have a username and password") + } + + if i.PlanID == "" || i.OrganizationGUID == "" || i.SpaceGUID == "" { + t.Error("The instance should have metadata") + } + + var enabledGroups []string + err := i.EnabledCloudWatchLogGroupExports.AssignTo(&enabledGroups) + if err != nil { + t.Error(err) + } + if !slices.Contains(enabledGroups, "foo") { + t.Error("expected EnabledCloudWatchLogGroupExports to contain 'foo'") + } +} + func TestModifyRDSInstance(t *testing.T) { // We need to create an instance first before we can try to modify it. createURL := "/v2/service_instances/the_RDS_instance?accepts_incomplete=true" diff --git a/services/rds/broker.go b/services/rds/broker.go index 8253be9f..233518e7 100644 --- a/services/rds/broker.go +++ b/services/rds/broker.go @@ -23,15 +23,16 @@ import ( // the broker for the "cf create-service" and "cf update-service" commands - // they are passed in via the "-c " flag. type Options struct { - AllocatedStorage int64 `json:"storage"` - EnableFunctions bool `json:"enable_functions"` - PubliclyAccessible bool `json:"publicly_accessible"` - Version string `json:"version"` - BackupRetentionPeriod *int64 `json:"backup_retention_period"` - BinaryLogFormat string `json:"binary_log_format"` - EnablePgCron *bool `json:"enable_pg_cron"` - RotateCredentials *bool `json:"rotate_credentials"` - StorageType string `json:"storage_type"` + AllocatedStorage int64 `json:"storage"` + EnableFunctions bool `json:"enable_functions"` + PubliclyAccessible bool `json:"publicly_accessible"` + Version string `json:"version"` + BackupRetentionPeriod *int64 `json:"backup_retention_period"` + BinaryLogFormat string `json:"binary_log_format"` + EnablePgCron *bool `json:"enable_pg_cron"` + RotateCredentials *bool `json:"rotate_credentials"` + StorageType string `json:"storage_type"` + EnableCloudWatchLogGroupExports []string `json:"enable_cloudwatch_log_groups_exports"` } // Validate the custom parameters passed in via the "-c " diff --git a/services/rds/rdsinstance.go b/services/rds/rdsinstance.go index 941de7fe..b4cb0ce1 100644 --- a/services/rds/rdsinstance.go +++ b/services/rds/rdsinstance.go @@ -61,7 +61,7 @@ type RDSInstance struct { ParameterGroupFamily string `sql:"-"` ParameterGroupName string `sql:"size(255)"` - EnabledCloudWatchLogGroupExports pgtype.JSONB `sql:"type:jsonb"` + EnabledCloudWatchLogGroupExports pgtype.JSONB `sql:"type:jsonb;default:'[]';not null"` StorageType string `sql:"size(255)"` } @@ -242,6 +242,8 @@ func (i *RDSInstance) modify(options Options, plan catalog.RDSPlan, settings *co } } + i.setEnabledCloudwatchLogGroupExports(options.EnableCloudWatchLogGroupExports) + return nil } @@ -309,6 +311,8 @@ func (i *RDSInstance) init( i.BinaryLogFormat = options.BinaryLogFormat i.EnablePgCron = options.EnablePgCron + i.setEnabledCloudwatchLogGroupExports(options.EnableCloudWatchLogGroupExports) + return nil } @@ -326,3 +330,13 @@ func (i *RDSInstance) setTags( } return nil } + +func (i *RDSInstance) setEnabledCloudwatchLogGroupExports(enabledLogGroups []string) error { + // TODO: update this to set the enabled log groups when + // enabling log groups is supported by the broker + if enabledLogGroups != nil { + err := i.EnabledCloudWatchLogGroupExports.Set(enabledLogGroups) + return err + } + return nil +}