Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
feat: use resource id aliases in deltas for non-default classes (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Feb 1, 2024
2 parents 111380a + 8ac6d51 commit 6cb503d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/humanitec/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions internal/humanitec/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
4 changes: 4 additions & 0 deletions internal/humanitec/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = resId + "-class-" + res.Class
}

if resId != "" {
source = resId
} else {
Expand Down
20 changes: 20 additions & 0 deletions internal/humanitec/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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) {
Expand Down

0 comments on commit 6cb503d

Please sign in to comment.