diff --git a/CHANGELOG.md b/CHANGELOG.md index fd47f609a02..b02c45d8881 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cmd/influxd/launcher/pkger_test.go b/cmd/influxd/launcher/pkger_test.go index 15cb60b015d..4f40d073dc4 100644 --- a/cmd/influxd/launcher/pkger_test.go +++ b/cmd/influxd/launcher/pkger_test.go @@ -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) @@ -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{ @@ -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 diff --git a/pkger/clone_resource.go b/pkger/clone_resource.go index 55d80437199..3cf606bc6f3 100644 --- a/pkger/clone_resource.go +++ b/pkger/clone_resource.go @@ -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 diff --git a/pkger/models.go b/pkger/models.go index d1aef4076a7..1dc7390a7a8 100644 --- a/pkger/models.go +++ b/pkger/models.go @@ -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"` diff --git a/pkger/parser.go b/pkger/parser.go index adeb35cab20..911b59889ba 100644 --- a/pkger/parser.go +++ b/pkger/parser.go @@ -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) @@ -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()...) }) @@ -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) { @@ -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 diff --git a/pkger/parser_models.go b/pkger/parser_models.go index da61e00a523..a53ff12de71 100644 --- a/pkger/parser_models.go +++ b/pkger/parser_models.go @@ -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 { @@ -1895,6 +1896,7 @@ type variable struct { Language string ConstValues []string MapValues map[string]string + selected []*references labels sortedLabels } @@ -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, } } diff --git a/pkger/parser_test.go b/pkger/parser_test.go index 9f3c65b1c9f..ed34523a9a9 100644 --- a/pkger/parser_test.go +++ b/pkger/parser_test.go @@ -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) @@ -3486,6 +3490,7 @@ spec: "var-const-3", "constant", influxdb.VariableConstantValues([]string{"first val"}), + nil, sum.Variables[0], ) @@ -3493,6 +3498,7 @@ spec: "var-map-4", "map", influxdb.VariableMapValues{"k1": "v1"}, + nil, sum.Variables[1], ) @@ -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], ) @@ -3513,6 +3520,7 @@ spec: Query: "an influxql query of sorts", Language: "influxql", }, + nil, sum.Variables[3], ) }) @@ -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) }) diff --git a/pkger/service.go b/pkger/service.go index 2a9d78d4a25..e83541d93fd 100644 --- a/pkger/service.go +++ b/pkger/service.go @@ -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") @@ -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(), }) @@ -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(), } diff --git a/pkger/service_test.go b/pkger/service_test.go index 88a90955bd5..28d1e402c9c 100644 --- a/pkger/service_test.go +++ b/pkger/service_test.go @@ -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) @@ -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"}, @@ -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"}, @@ -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"}, @@ -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", }, }, @@ -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) diff --git a/pkger/testdata/variable_ref.yml b/pkger/testdata/variable_ref.yml index 199ca4f3375..2427566a7b7 100644 --- a/pkger/testdata/variable_ref.yml +++ b/pkger/testdata/variable_ref.yml @@ -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: diff --git a/pkger/testdata/variables.json b/pkger/testdata/variables.json index 0d8038b4170..41ea2c8e8d8 100644 --- a/pkger/testdata/variables.json +++ b/pkger/testdata/variables.json @@ -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" } }, diff --git a/pkger/testdata/variables.yml b/pkger/testdata/variables.yml index 6d4896d6627..5848f2388bf 100644 --- a/pkger/testdata/variables.yml +++ b/pkger/testdata/variables.yml @@ -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"]) ---