diff --git a/http/swagger.yml b/http/swagger.yml index 85f1a03bfb4..460bf7b38e3 100644 --- a/http/swagger.yml +++ b/http/swagger.yml @@ -8486,10 +8486,15 @@ components: type: string name: type: string - description: - type: string - retentionPeriod: - type: string + properties: + type: object + properties: + color: + type: string + description: + type: string + envReferences: + $ref: "#/components/schemas/PkgEnvReferences" PkgChart: type: object properties: diff --git a/pkger/models.go b/pkger/models.go index 939a9b40ef4..0aca9711f52 100644 --- a/pkger/models.go +++ b/pkger/models.go @@ -597,6 +597,7 @@ type SummaryLabel struct { Color string `json:"color"` Description string `json:"description"` } `json:"properties"` + EnvReferences []SummaryReference `json:"envReferences"` } // SummaryLabelMapping provides a summary of a label mapped with a single resource. diff --git a/pkger/parser_models.go b/pkger/parser_models.go index dd16c69a852..9baa0630f79 100644 --- a/pkger/parser_models.go +++ b/pkger/parser_models.go @@ -1134,6 +1134,7 @@ func (l *label) summarize() SummaryLabel { Color: l.Color, Description: l.Description, }, + EnvReferences: l.identity.summarizeReferences(), } } diff --git a/pkger/parser_test.go b/pkger/parser_test.go index 3fb220a2d61..09746045970 100644 --- a/pkger/parser_test.go +++ b/pkger/parser_test.go @@ -189,46 +189,38 @@ spec: labels := pkg.Summary().Labels require.Len(t, labels, 3) - expectedLabel := SummaryLabel{ - PkgName: "label-1", - Name: "label-1", - Properties: struct { - Color string `json:"color"` - Description string `json:"description"` - }{ - Color: "#FFFFFF", - Description: "label 1 description", - }, - } + expectedLabel := sumLabelGen("label-1", "label-1", "#FFFFFF", "label 1 description") assert.Equal(t, expectedLabel, labels[0]) - expectedLabel = SummaryLabel{ - PkgName: "label-2", - Name: "label-2", - Properties: struct { - Color string `json:"color"` - Description string `json:"description"` - }{ - Color: "#000000", - Description: "label 2 description", - }, - } + expectedLabel = sumLabelGen("label-2", "label-2", "#000000", "label 2 description") assert.Equal(t, expectedLabel, labels[1]) - expectedLabel = SummaryLabel{ - PkgName: "label-3", - Name: "display name", - Properties: struct { - Color string `json:"color"` - Description string `json:"description"` - }{ - Description: "label 3 description", - }, - } + expectedLabel = sumLabelGen("label-3", "display name", "", "label 3 description") assert.Equal(t, expectedLabel, labels[2]) }) }) + t.Run("with env refs should be valid", func(t *testing.T) { + testfileRunner(t, "testdata/label_ref.yml", func(t *testing.T, pkg *Pkg) { + actual := pkg.Summary().Labels + require.Len(t, actual, 1) + + expected := sumLabelGen("env-meta-name", "env-spec-name", "", "", + SummaryReference{ + Field: "metadata.name", + EnvRefKey: "meta-name", + DefaultValue: "env-meta-name", + }, + SummaryReference{ + Field: "spec.name", + EnvRefKey: "spec-name", + DefaultValue: "env-spec-name", + }, + ) + assert.Contains(t, actual, expected) + }) + }) + t.Run("with missing label name should error", func(t *testing.T) { tests := []testPkgResourceError{ { @@ -3659,14 +3651,7 @@ spec: sum := pkg.Summary() labels := []SummaryLabel{ - { - PkgName: "label-1", - Name: "label-1", - Properties: struct { - Color string `json:"color"` - Description string `json:"description"` - }{Color: "#eee888", Description: "desc_1"}, - }, + sumLabelGen("label-1", "label-1", "#eee888", "desc_1"), } assert.Equal(t, labels, sum.Labels) @@ -4142,6 +4127,24 @@ func testfileRunner(t *testing.T, path string, testFn func(t *testing.T, pkg *Pk } } +func sumLabelGen(pkgName, name, color, desc string, envRefs ...SummaryReference) SummaryLabel { + if envRefs == nil { + envRefs = make([]SummaryReference, 0) + } + return SummaryLabel{ + PkgName: pkgName, + Name: name, + Properties: struct { + Color string `json:"color"` + Description string `json:"description"` + }{ + Color: color, + Description: desc, + }, + EnvReferences: envRefs, + } +} + func strPtr(s string) *string { return &s } diff --git a/pkger/service_test.go b/pkger/service_test.go index b22f0fb9020..6bbbee54d69 100644 --- a/pkger/service_test.go +++ b/pkger/service_test.go @@ -678,33 +678,15 @@ func TestService(t *testing.T) { sum := impact.Summary require.Len(t, sum.Labels, 3) - assert.Contains(t, sum.Labels, SummaryLabel{ - ID: 1, - OrgID: SafeID(orgID), - PkgName: "label-1", - Name: "label-1", - Properties: struct { - Color string `json:"color"` - Description string `json:"description"` - }{ - Color: "#FFFFFF", - Description: "label 1 description", - }, - }) - - assert.Contains(t, sum.Labels, SummaryLabel{ - ID: 2, - OrgID: SafeID(orgID), - PkgName: "label-2", - Name: "label-2", - Properties: struct { - Color string `json:"color"` - Description string `json:"description"` - }{ - Color: "#000000", - Description: "label 2 description", - }, - }) + expectedLabel := sumLabelGen("label-1", "label-1", "#FFFFFF", "label 1 description") + expectedLabel.ID = 1 + expectedLabel.OrgID = SafeID(orgID) + assert.Contains(t, sum.Labels, expectedLabel) + + expectedLabel = sumLabelGen("label-2", "label-2", "#000000", "label 2 description") + expectedLabel.ID = 2 + expectedLabel.OrgID = SafeID(orgID) + assert.Contains(t, sum.Labels, expectedLabel) }) }) @@ -736,7 +718,6 @@ func TestService(t *testing.T) { stubExisting := func(name string, id influxdb.ID) *influxdb.Label { pkgLabel := pkg.mLabels[name] - fmt.Println(name, pkgLabel) return &influxdb.Label{ // makes all pkg changes same as they are on the existing ID: id, @@ -785,33 +766,15 @@ func TestService(t *testing.T) { sum := impact.Summary require.Len(t, sum.Labels, 3) - assert.Contains(t, sum.Labels, SummaryLabel{ - ID: 1, - OrgID: SafeID(orgID), - PkgName: "label-1", - Name: "label-1", - Properties: struct { - Color string `json:"color"` - Description string `json:"description"` - }{ - Color: "#FFFFFF", - Description: "label 1 description", - }, - }) + expectedLabel := sumLabelGen("label-1", "label-1", "#FFFFFF", "label 1 description") + expectedLabel.ID = 1 + expectedLabel.OrgID = SafeID(orgID) + assert.Contains(t, sum.Labels, expectedLabel) - assert.Contains(t, sum.Labels, SummaryLabel{ - ID: 2, - OrgID: SafeID(orgID), - PkgName: "label-2", - Name: "label-2", - Properties: struct { - Color string `json:"color"` - Description string `json:"description"` - }{ - Color: "#000000", - Description: "label 2 description", - }, - }) + expectedLabel = sumLabelGen("label-2", "label-2", "#000000", "label 2 description") + expectedLabel.ID = 2 + expectedLabel.OrgID = SafeID(orgID) + assert.Contains(t, sum.Labels, expectedLabel) assert.Equal(t, 1, fakeLabelSVC.CreateLabelCalls.Count()) // only called for second label }) diff --git a/pkger/testdata/label_ref.yml b/pkger/testdata/label_ref.yml new file mode 100644 index 00000000000..9696a24e16f --- /dev/null +++ b/pkger/testdata/label_ref.yml @@ -0,0 +1,10 @@ +apiVersion: influxdata.com/v2alpha1 +kind: Label +metadata: + name: + envRef: + key: meta-name +spec: + name: + envRef: + key: spec-name