From f1f3de26e3b7915d54462fcb0129c13fa2986534 Mon Sep 17 00:00:00 2001 From: Chris Stephenson Date: Wed, 17 Jan 2024 12:06:33 +0100 Subject: [PATCH 1/2] feat: use resource id aliases in deltas for non-default classes Signed-off-by: Chris Stephenson --- internal/humanitec/convert.go | 4 ++++ internal/humanitec/convert_test.go | 25 +++++++++++++++++++++++++ internal/humanitec/templates.go | 4 ++++ 3 files changed, 33 insertions(+) diff --git a/internal/humanitec/convert.go b/internal/humanitec/convert.go index 336606e..26b5687 100644 --- a/internal/humanitec/convert.go +++ b/internal/humanitec/convert.go @@ -323,6 +323,10 @@ func ConvertSpec(name, envID, baseDir, workloadSourceURL string, spec *score.Wor if len(res.Params) > 0 { sharedRes["params"] = ctx.SubstituteAll(res.Params) } + if class != "default" { + sharedRes["id"] = resId + resName = resName + "-class-" + class + } shared = append(shared, humanitec.UpdateAction{ Operation: "add", Path: "/" + resName, diff --git a/internal/humanitec/convert_test.go b/internal/humanitec/convert_test.go index a2ed323..7305445 100644 --- a/internal/humanitec/convert_test.go +++ b/internal/humanitec/convert_test.go @@ -255,6 +255,7 @@ func TestScoreConvert(t *testing.T) { "CONNECTION_STRING": "postgresql://${resources.db.host}:${resources.db.port}/${resources.db.name}", "DOMAIN_NAME": "${resources.dns.domain}", "EXTERNAL_RESOURCE": "${resources.external-resource.name}", + "SENSITIVE_BUCKET": "${resources.sensitive-bucket.name}", }, Files: []score.FileMountSpec{ { @@ -340,6 +341,15 @@ func TestScoreConvert(t *testing.T) { "host": "${resources.dns.host}", }, }, + "sensitive-bucket": { + Metadata: score.ResourceMeta{ + Annotations: map[string]string{ + AnnotationLabelResourceId: "shared.sensitive-bucket", + }, + }, + Type: "bucket", + Class: "sensitive", + }, }, }, Extensions: &extensions.HumanitecExtensionsSpec{ @@ -392,6 +402,7 @@ func TestScoreConvert(t *testing.T) { "CONNECTION_STRING": "postgresql://${externals.annotations-db-id.host}:${externals.annotations-db-id.port}/${externals.annotations-db-id.name}", "DOMAIN_NAME": "${shared.dns.domain}", "EXTERNAL_RESOURCE": "${modules.test-module.externals.test-resource.name}", + "SENSITIVE_BUCKET": "${shared.sensitive-bucket-class-sensitive.name}", }, "files": map[string]interface{}{ "/etc/backend/config.yaml": map[string]interface{}{ @@ -461,6 +472,15 @@ func TestScoreConvert(t *testing.T) { }, }, Shared: []humanitec.UpdateAction{ + { + Operation: "add", + Path: "/sensitive-bucket-class-sensitive", + Value: map[string]interface{}{ + "type": "bucket", + "class": "sensitive", + "id": "shared.sensitive-bucket", + }, + }, { Operation: "add", Path: "/dns", @@ -489,7 +509,12 @@ func TestScoreConvert(t *testing.T) { // On Success // assert.NoError(t, err) + expectedShared := tt.Output.Shared + tt.Output.Shared = nil + actualShared := res.Shared + res.Shared = nil assert.Equal(t, tt.Output, res) + assert.ElementsMatch(t, expectedShared, actualShared) } }) } diff --git a/internal/humanitec/templates.go b/internal/humanitec/templates.go index 9a654d6..f573ab1 100644 --- a/internal/humanitec/templates.go +++ b/internal/humanitec/templates.go @@ -152,6 +152,10 @@ func (ctx *templatesContext) mapVar(ref string) string { } // END (DEPRECATED) + if hasAnnotation && strings.HasPrefix(resId, "shared.") && (res.Class != "" && res.Class != "default") { + resId = "shared." + resName + "-class-" + res.Class + } + if resId != "" { source = resId } else { From 8ac6d51b4e7f30149be31f6039aca764d5a85052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=BCrbach?= Date: Fri, 19 Jan 2024 17:55:49 +0100 Subject: [PATCH 2/2] fix: resource placeholder substitution MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Würbach --- internal/humanitec/templates.go | 2 +- internal/humanitec/templates_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/humanitec/templates.go b/internal/humanitec/templates.go index f573ab1..d5a3244 100644 --- a/internal/humanitec/templates.go +++ b/internal/humanitec/templates.go @@ -153,7 +153,7 @@ func (ctx *templatesContext) mapVar(ref string) string { // END (DEPRECATED) if hasAnnotation && strings.HasPrefix(resId, "shared.") && (res.Class != "" && res.Class != "default") { - resId = "shared." + resName + "-class-" + res.Class + resId = resId + "-class-" + res.Class } if resId != "" { diff --git a/internal/humanitec/templates_test.go b/internal/humanitec/templates_test.go index 685904e..f425d25 100644 --- a/internal/humanitec/templates_test.go +++ b/internal/humanitec/templates_test.go @@ -126,6 +126,23 @@ func TestSubstitute(t *testing.T) { "service-a": score.ResourceSpec{ Type: "service", }, + "shared-res": score.ResourceSpec{ + Type: "s3", + Metadata: score.ResourceMeta{ + Annotations: map[string]string{ + AnnotationLabelResourceId: "shared.main-s3", + }, + }, + }, + "shared-res-admin": score.ResourceSpec{ + Type: "s3", + Class: "admin", + Metadata: score.ResourceMeta{ + Annotations: map[string]string{ + AnnotationLabelResourceId: "shared.main-s3", + }, + }, + }, } var ext = extensions.HumanitecResourcesSpecs{ @@ -150,6 +167,9 @@ func TestSubstitute(t *testing.T) { assert.Equal(t, "postgresql://${externals.db.user}:${externals.db.password}@${externals.db.host}:${externals.db.port}/${externals.db.name}", ctx.Substitute("postgresql://${resources.db.user}:${resources.db.password}@${resources.db.host}:${resources.db.port}/${resources.db.name}")) + + assert.Equal(t, "${shared.main-s3.name}", ctx.Substitute("${resources.shared-res.name}")) + assert.Equal(t, "${shared.main-s3-class-admin.name}", ctx.Substitute("${resources.shared-res-admin.name}")) } func TestSubstituteAll(t *testing.T) {