Skip to content

Commit

Permalink
feat(pkger): add support for selected fields to variable
Browse files Browse the repository at this point in the history
references: #18240
  • Loading branch information
jsteenb2 committed Jun 22, 2020
1 parent 5d3b9e2 commit 78d976d
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 34 deletions.
5 changes: 5 additions & 0 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2383,6 +2383,7 @@ spec:
require.Len(t, vars, 1)
assert.NotZero(t, vars[0].ID)
assert.Equal(t, "query var", vars[0].Name)
assert.Equal(t, []string{"rucketeer"}, vars[0].Selected)
hasLabelAssociations(t, vars[0].LabelAssociations, 1, "label-1")
varArgs := vars[0].Arguments
require.NotNil(t, varArgs)
Expand All @@ -2391,6 +2392,8 @@ spec:
Query: "buckets() |> filter(fn: (r) => r.name !~ /^_/) |> rename(columns: {name: \"_value\"}) |> keep(columns: [\"_value\"])",
Language: "flux",
}, varArgs.Values)
platformVar := resourceCheck.mustGetVariable(t, byID(influxdb.ID(vars[0].ID)))
assert.Equal(t, []string{"rucketeer"}, platformVar.Selected)

newSumMapping := func(id pkger.SafeID, pkgName, name string, rt influxdb.ResourceType) pkger.SummaryLabelMapping {
return pkger.SummaryLabelMapping{
Expand Down Expand Up @@ -3222,6 +3225,8 @@ spec:
language: flux
query: |
buckets() |> filter(fn: (r) => r.name !~ /^_/) |> rename(columns: {name: "_value"}) |> keep(columns: ["_value"])
selected:
- rucketeer
associations:
- kind: Label
name: label-1
Expand Down
1 change: 1 addition & 0 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ type SummaryVariable struct {
OrgID SafeID `json:"orgID,omitempty"`
Name string `json:"name"`
Description string `json:"description"`
Selected []string `json:"variables"`
Arguments *influxdb.VariableArguments `json:"arguments"`

EnvReferences []SummaryReference `json:"envReferences"`
Expand Down
54 changes: 32 additions & 22 deletions pkger/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,12 @@ func (p *Pkg) graphVariables() *parseErr {
MapValues: o.Spec.mapStrStr(fieldValues),
}

if iSelected, ok := o.Spec[fieldVariableSelected].([]interface{}); ok {
for _, res := range iSelected {
newVar.selected = append(newVar.selected, ifaceToReference(res))
}
}

failures := p.parseNestedLabels(o.Spec, func(l *label) error {
newVar.labels = append(newVar.labels, l)
p.mLabels[l.PkgName()].setMapping(newVar, false)
Expand All @@ -1176,6 +1182,7 @@ func (p *Pkg) graphVariables() *parseErr {

p.mVariables[newVar.PkgName()] = newVar
p.setRefs(newVar.name, newVar.displayName)
p.setRefs(newVar.selected...)

return append(failures, newVar.valid()...)
})
Expand Down Expand Up @@ -1628,28 +1635,7 @@ func (r Resource) references(key string) *references {
if !ok {
return &references{}
}

var ref references
for _, f := range []string{fieldReferencesSecret, fieldReferencesEnv} {
resBody, ok := ifaceToResource(v)
if !ok {
continue
}
if keyRes, ok := ifaceToResource(resBody[f]); ok {
switch f {
case fieldReferencesEnv:
ref.EnvRef = keyRes.stringShort(fieldKey)
ref.defaultVal = keyRes.stringShort(fieldDefault)
case fieldReferencesSecret:
ref.Secret = keyRes.stringShort(fieldKey)
}
}
}
if ref.hasValue() {
return &ref
}

return &references{val: v}
return ifaceToReference(v)
}

func (r Resource) string(key string) (string, bool) {
Expand Down Expand Up @@ -1770,6 +1756,30 @@ func ifaceToResource(i interface{}) (Resource, bool) {
return newRes, true
}

func ifaceToReference(i interface{}) *references {
var ref references
for _, f := range []string{fieldReferencesSecret, fieldReferencesEnv} {
resBody, ok := ifaceToResource(i)
if !ok {
continue
}
if keyRes, ok := ifaceToResource(resBody[f]); ok {
switch f {
case fieldReferencesEnv:
ref.EnvRef = keyRes.stringShort(fieldKey)
ref.defaultVal = keyRes.stringShort(fieldDefault)
case fieldReferencesSecret:
ref.Secret = keyRes.stringShort(fieldKey)
}
}
}
if ref.hasValue() {
return &ref
}

return &references{val: i}
}

func ifaceToStr(v interface{}) (string, bool) {
if v == nil {
return "", false
Expand Down
31 changes: 27 additions & 4 deletions pkger/parser_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -1881,9 +1881,10 @@ func (t *telegraf) valid() []validationErr {
}

const (
fieldArgTypeConstant = "constant"
fieldArgTypeMap = "map"
fieldArgTypeQuery = "query"
fieldArgTypeConstant = "constant"
fieldArgTypeMap = "map"
fieldArgTypeQuery = "query"
fieldVariableSelected = "selected"
)

type variable struct {
Expand All @@ -1895,6 +1896,7 @@ type variable struct {
Language string
ConstValues []string
MapValues map[string]string
selected []*references

labels sortedLabels
}
Expand All @@ -1907,14 +1909,35 @@ func (v *variable) ResourceType() influxdb.ResourceType {
return KindVariable.ResourceType()
}

func (v *variable) Selected() []string {
selected := make([]string, 0, len(v.selected))
for _, sel := range v.selected {
s := sel.String()
if s == "" {
continue
}
selected = append(selected, s)
}
return selected
}

func (v *variable) summarize() SummaryVariable {
envRefs := summarizeCommonReferences(v.identity, v.labels)
for i, sel := range v.selected {
if sel.hasEnvRef() {
field := fmt.Sprintf("spec.%s[%d]", fieldVariableSelected, i)
envRefs = append(envRefs, convertRefToRefSummary(field, sel))
}
}

return SummaryVariable{
PkgName: v.PkgName(),
Name: v.Name(),
Description: v.Description,
Selected: v.Selected(),
Arguments: v.influxVarArgs(),
LabelAssociations: toSummaryLabels(v.labels...),
EnvReferences: summarizeCommonReferences(v.identity, v.labels),
EnvReferences: envRefs,
}
}

Expand Down
20 changes: 19 additions & 1 deletion pkger/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3471,11 +3471,15 @@ spec:

require.Len(t, sum.Variables, 4)

varEquals := func(t *testing.T, name, vType string, vals interface{}, v SummaryVariable) {
varEquals := func(t *testing.T, name, vType string, vals interface{}, selected []string, v SummaryVariable) {
t.Helper()

assert.Equal(t, name, v.Name)
assert.Equal(t, name+" desc", v.Description)
if selected == nil {
selected = []string{}
}
assert.Equal(t, selected, v.Selected)
require.NotNil(t, v.Arguments)
assert.Equal(t, vType, v.Arguments.Type)
assert.Equal(t, vals, v.Arguments.Values)
Expand All @@ -3486,13 +3490,15 @@ spec:
"var-const-3",
"constant",
influxdb.VariableConstantValues([]string{"first val"}),
nil,
sum.Variables[0],
)

varEquals(t,
"var-map-4",
"map",
influxdb.VariableMapValues{"k1": "v1"},
nil,
sum.Variables[1],
)

Expand All @@ -3503,6 +3509,7 @@ spec:
Query: `buckets() |> filter(fn: (r) => r.name !~ /^_/) |> rename(columns: {name: "_value"}) |> keep(columns: ["_value"])`,
Language: "flux",
},
[]string{"rucket"},
sum.Variables[2],
)

Expand All @@ -3513,6 +3520,7 @@ spec:
Query: "an influxql query of sorts",
Language: "influxql",
},
nil,
sum.Variables[3],
)
})
Expand All @@ -3539,6 +3547,16 @@ spec:
EnvRefKey: "label-meta-name",
DefaultValue: "env-label-meta-name",
},
{
Field: "spec.selected[0]",
EnvRefKey: "the-selected",
DefaultValue: "second val",
},
{
Field: "spec.selected[1]",
EnvRefKey: "the-2nd",
DefaultValue: "env-the-2nd",
},
}
assert.Equal(t, expectedEnvRefs, actual[0].EnvReferences)
})
Expand Down
3 changes: 3 additions & 0 deletions pkger/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2771,6 +2771,7 @@ func (s *Service) rollbackVariables(ctx context.Context, variables []*stateVaria
_, err = s.varSVC.UpdateVariable(ctx, v.ID(), &influxdb.VariableUpdate{
Name: v.existing.Name,
Description: v.existing.Description,
Selected: v.existing.Selected,
Arguments: v.existing.Arguments,
})
err = ierrors.Wrap(err, "rolling back updated variable")
Expand Down Expand Up @@ -2807,6 +2808,7 @@ func (s *Service) applyVariable(ctx context.Context, v *stateVariable) (influxdb
case IsExisting(v.stateStatus) && v.existing != nil:
updatedVar, err := s.varSVC.UpdateVariable(ctx, v.ID(), &influxdb.VariableUpdate{
Name: v.parserVar.Name(),
Selected: v.parserVar.Selected(),
Description: v.parserVar.Description,
Arguments: v.parserVar.influxVarArgs(),
})
Expand All @@ -2820,6 +2822,7 @@ func (s *Service) applyVariable(ctx context.Context, v *stateVariable) (influxdb
influxVar := influxdb.Variable{
OrganizationID: v.orgID,
Name: v.parserVar.Name(),
Selected: v.parserVar.Selected(),
Description: v.parserVar.Description,
Arguments: v.parserVar.influxVarArgs(),
}
Expand Down
17 changes: 10 additions & 7 deletions pkger/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1661,13 +1661,16 @@ func TestService(t *testing.T) {
sum := impact.Summary
require.Len(t, sum.Variables, 4)

expected := sum.Variables[0]
assert.True(t, expected.ID > 0 && expected.ID < 5)
assert.Equal(t, SafeID(orgID), expected.OrgID)
assert.Equal(t, "var-const-3", expected.Name)
assert.Equal(t, "var-const-3 desc", expected.Description)
require.NotNil(t, expected.Arguments)
assert.Equal(t, influxdb.VariableConstantValues{"first val"}, expected.Arguments.Values)
actual := sum.Variables[0]
assert.True(t, actual.ID > 0 && actual.ID < 5)
assert.Equal(t, SafeID(orgID), actual.OrgID)
assert.Equal(t, "var-const-3", actual.Name)
assert.Equal(t, "var-const-3 desc", actual.Description)
require.NotNil(t, actual.Arguments)
assert.Equal(t, influxdb.VariableConstantValues{"first val"}, actual.Arguments.Values)

actual = sum.Variables[2]
assert.Equal(t, []string{"rucket"}, actual.Selected)

for _, actual := range sum.Variables {
assert.Containsf(t, []SafeID{1, 2, 3, 4}, actual.ID, "actual var: %+v", actual)
Expand Down
8 changes: 8 additions & 0 deletions pkger/testdata/variable_ref.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ spec:
type: constant
values:
- first val
- second val
- third val
selected:
- envRef:
key: the-selected
default: second val
- envRef:
key: the-2nd
associations:
- kind: Label
name:
Expand Down
1 change: 1 addition & 0 deletions pkger/testdata/variables.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"description": "query var desc",
"type": "query",
"query": "buckets() |> filter(fn: (r) => r.name !~ /^_/) |> rename(columns: {name: \"_value\"}) |> keep(columns: [\"_value\"])",
"selected": ["rucket"],
"language": "flux"
}
},
Expand Down
2 changes: 2 additions & 0 deletions pkger/testdata/variables.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ spec:
description: query var desc
type: query
language: flux
selected:
- rucket
query: |
buckets() |> filter(fn: (r) => r.name !~ /^_/) |> rename(columns: {name: "_value"}) |> keep(columns: ["_value"])
---
Expand Down

0 comments on commit 78d976d

Please sign in to comment.