Skip to content

Commit

Permalink
feat(pkger): extend SummaryLabel with its env references
Browse files Browse the repository at this point in the history
references: #18407
  • Loading branch information
jsteenb2 committed Jun 10, 2020
1 parent 82185dd commit bb270f1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 98 deletions.
13 changes: 9 additions & 4 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions pkger/parser_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ func (l *label) summarize() SummaryLabel {
Color: l.Color,
Description: l.Description,
},
EnvReferences: l.identity.summarizeReferences(),
}
}

Expand Down
83 changes: 43 additions & 40 deletions pkger/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
Expand Down
71 changes: 17 additions & 54 deletions pkger/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
})
Expand Down
10 changes: 10 additions & 0 deletions pkger/testdata/label_ref.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: influxdata.com/v2alpha1
kind: Label
metadata:
name:
envRef:
key: meta-name
spec:
name:
envRef:
key: spec-name

0 comments on commit bb270f1

Please sign in to comment.