Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pkger): add support for selected fields to variable #18655

Merged
merged 1 commit into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
1. [18601](https://github.com/influxdata/influxdb/pull/18601): Provide active config running influx config without args
1. [18606](https://github.com/influxdata/influxdb/pull/18606): Enable influxd binary to look for a config file on startup
1. [18647](https://github.com/influxdata/influxdb/pull/18647): Add support for env ref default values to the template parser
1. [18655](https://github.com/influxdata/influxdb/pull/18655): Add support for platfrom variable selected field to templates

### Bug Fixes

Expand Down
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
4 changes: 4 additions & 0 deletions pkger/clone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,10 @@ func VariableToObject(name string, v influxdb.Variable) Object {

assignNonZeroStrings(o.Spec, map[string]string{fieldDescription: v.Description})

if len(v.Selected) > 0 {
o.Spec[fieldVariableSelected] = v.Selected
}

args := v.Arguments
if args == nil {
return o
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
36 changes: 22 additions & 14 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 Expand Up @@ -2975,6 +2978,7 @@ func TestService(t *testing.T) {
ID: 1,
Name: "old name",
Description: "desc",
Selected: []string{"val"},
Arguments: &influxdb.VariableArguments{
Type: "constant",
Values: influxdb.VariableConstantValues{"val"},
Expand All @@ -2985,8 +2989,9 @@ func TestService(t *testing.T) {
name: "with new name",
newName: "new name",
expectedVar: influxdb.Variable{
ID: 1,
Name: "old name",
ID: 1,
Name: "old name",
Selected: []string{"val"},
Arguments: &influxdb.VariableArguments{
Type: "constant",
Values: influxdb.VariableConstantValues{"val"},
Expand All @@ -2996,8 +3001,9 @@ func TestService(t *testing.T) {
{
name: "with map arg",
expectedVar: influxdb.Variable{
ID: 1,
Name: "old name",
ID: 1,
Name: "old name",
Selected: []string{"v"},
Arguments: &influxdb.VariableArguments{
Type: "map",
Values: influxdb.VariableMapValues{"k": "v"},
Expand All @@ -3007,12 +3013,13 @@ func TestService(t *testing.T) {
{
name: "with query arg",
expectedVar: influxdb.Variable{
ID: 1,
Name: "old name",
ID: 1,
Name: "old name",
Selected: []string{"bucket-foo"},
Arguments: &influxdb.VariableArguments{
Type: "query",
Values: influxdb.VariableQueryValues{
Query: "query",
Query: "buckets()",
Language: "flux",
},
},
Expand Down Expand Up @@ -3052,6 +3059,7 @@ func TestService(t *testing.T) {
}
assert.Equal(t, expectedName, actual.Name)
assert.Equal(t, tt.expectedVar.Description, actual.Description)
assert.Equal(t, tt.expectedVar.Selected, actual.Selected)
assert.Equal(t, tt.expectedVar.Arguments, actual.Arguments)
}
t.Run(tt.name, fn)
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