From e64b05359b4a6cb8c42584a2f25910d62fcbbd40 Mon Sep 17 00:00:00 2001 From: Georgi Farashev Date: Fri, 17 Jan 2020 09:23:58 +0200 Subject: [PATCH] Fix instance dashboard url serialization (#398) * fix instances dashboard url * add test --- storage/postgres/keystore_test.go | 2 +- .../20200116204800_dashboard_url_text.down.sql | 5 +++++ .../20200116204800_dashboard_url_text.up.sql | 5 +++++ storage/postgres/service_instance.go | 8 +++++--- .../service_instance_test/service_instance_test.go | 14 ++++++++++++++ 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 storage/postgres/migrations/20200116204800_dashboard_url_text.down.sql create mode 100644 storage/postgres/migrations/20200116204800_dashboard_url_text.up.sql diff --git a/storage/postgres/keystore_test.go b/storage/postgres/keystore_test.go index 06205adf8..bb6510108 100644 --- a/storage/postgres/keystore_test.go +++ b/storage/postgres/keystore_test.go @@ -59,7 +59,7 @@ var _ = Describe("Secured Storage", func() { mock.ExpectQuery(`SELECT CURRENT_DATABASE()`).WillReturnRows(sqlmock.NewRows([]string{"mock"}).FromCSVString("mock")) mock.ExpectQuery(`SELECT COUNT(1)*`).WillReturnRows(sqlmock.NewRows([]string{"mock"}).FromCSVString("1")) mock.ExpectExec("SELECT pg_advisory_lock*").WithArgs(sqlmock.AnyArg()).WillReturnResult(sqlmock.NewResult(1, 1)) - mock.ExpectQuery(`SELECT version, dirty FROM "schema_migrations" LIMIT 1`).WillReturnRows(sqlmock.NewRows([]string{"version", "dirty"}).FromCSVString("20200114101000,false")) + mock.ExpectQuery(`SELECT version, dirty FROM "schema_migrations" LIMIT 1`).WillReturnRows(sqlmock.NewRows([]string{"version", "dirty"}).FromCSVString("20200116204800,false")) mock.ExpectExec("SELECT pg_advisory_unlock*").WithArgs(sqlmock.AnyArg()).WillReturnResult(sqlmock.NewResult(1, 1)) options := storage.DefaultSettings() options.EncryptionKey = string(envEncryptionKey) diff --git a/storage/postgres/migrations/20200116204800_dashboard_url_text.down.sql b/storage/postgres/migrations/20200116204800_dashboard_url_text.down.sql new file mode 100644 index 000000000..6f49b206c --- /dev/null +++ b/storage/postgres/migrations/20200116204800_dashboard_url_text.down.sql @@ -0,0 +1,5 @@ +BEGIN; + +ALTER TABLE service_instances ALTER COLUMN dashboard_url TYPE varchar(16000); + +COMMIT; \ No newline at end of file diff --git a/storage/postgres/migrations/20200116204800_dashboard_url_text.up.sql b/storage/postgres/migrations/20200116204800_dashboard_url_text.up.sql new file mode 100644 index 000000000..2fa748ce5 --- /dev/null +++ b/storage/postgres/migrations/20200116204800_dashboard_url_text.up.sql @@ -0,0 +1,5 @@ +BEGIN; + +ALTER TABLE service_instances ALTER COLUMN dashboard_url TYPE text; + +COMMIT; \ No newline at end of file diff --git a/storage/postgres/service_instance.go b/storage/postgres/service_instance.go index 8408ccc93..645015ad6 100644 --- a/storage/postgres/service_instance.go +++ b/storage/postgres/service_instance.go @@ -17,6 +17,8 @@ package postgres import ( + "database/sql" + "github.com/Peripli/service-manager/storage" sqlxtypes "github.com/jmoiron/sqlx/types" @@ -30,7 +32,7 @@ type ServiceInstance struct { Name string `db:"name"` ServicePlanID string `db:"service_plan_id"` PlatformID string `db:"platform_id"` - DashboardURL string `db:"dashboard_url"` + DashboardURL sql.NullString `db:"dashboard_url"` MaintenanceInfo sqlxtypes.JSONText `db:"maintenance_info"` Context sqlxtypes.JSONText `db:"context"` PreviousValues sqlxtypes.JSONText `db:"previous_values"` @@ -50,7 +52,7 @@ func (si *ServiceInstance) ToObject() types.Object { Name: si.Name, ServicePlanID: si.ServicePlanID, PlatformID: si.PlatformID, - DashboardURL: si.DashboardURL, + DashboardURL: si.DashboardURL.String, MaintenanceInfo: getJSONRawMessage(si.MaintenanceInfo), Context: getJSONRawMessage(si.Context), PreviousValues: getJSONRawMessage(si.PreviousValues), @@ -75,7 +77,7 @@ func (*ServiceInstance) FromObject(object types.Object) (storage.Entity, bool) { Name: serviceInstance.Name, ServicePlanID: serviceInstance.ServicePlanID, PlatformID: serviceInstance.PlatformID, - DashboardURL: serviceInstance.DashboardURL, + DashboardURL: toNullString(serviceInstance.DashboardURL), MaintenanceInfo: getJSONText(serviceInstance.MaintenanceInfo), Context: getJSONText(serviceInstance.Context), PreviousValues: getJSONText(serviceInstance.PreviousValues), diff --git a/test/service_instance_test/service_instance_test.go b/test/service_instance_test/service_instance_test.go index e9819ff9b..d78bfbcb7 100644 --- a/test/service_instance_test/service_instance_test.go +++ b/test/service_instance_test/service_instance_test.go @@ -19,6 +19,7 @@ package service_test import ( "context" "fmt" + "github.com/Peripli/service-manager/test/testutil/service_instance" "net/http" @@ -122,6 +123,19 @@ var _ = test.DescribeTestsFor(test.TestCase{ } }) }) + When("service instance dashboard_url is not set", func() { + BeforeEach(func() { + _, serviceInstance = service_instance.Prepare(ctx, ctx.TestPlatform.ID, "", fmt.Sprintf(`{"%s":"%s"}`, TenantIdentifier, TenantValue)) + serviceInstance.DashboardURL = "" + _, err := ctx.SMRepository.Create(context.Background(), serviceInstance) + Expect(err).ToNot(HaveOccurred()) + }) + + It("doesn't return dashboard_url", func() { + ctx.SMWithOAuth.GET(web.ServiceInstancesURL + "/" + serviceInstance.ID).Expect(). + Status(http.StatusOK).JSON().Object().NotContainsKey("dashboard_url") + }) + }) }) }) },