Skip to content

Commit

Permalink
chore(pkger): refactor parser variable state out into stateful type
Browse files Browse the repository at this point in the history
references: #17434
  • Loading branch information
jsteenb2 committed Apr 14, 2020
1 parent cb7f8ba commit c8a79f5
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 288 deletions.
23 changes: 15 additions & 8 deletions cmd/influxd/launcher/pkger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,14 +837,21 @@ spec:
mappings := sum.LabelMappings
require.Len(t, mappings, 11)
assert.Contains(t, mappings, newSumMapping(bkts[0].ID, bkts[0].PkgName, bkts[0].Name, influxdb.BucketsResourceType))
//hasMapping(t, mappings, newSumMapping(pkger.SafeID(checks[0].Check.GetID()), checks[0].Check.GetName(), influxdb.ChecksResourceType))
//hasMapping(t, mappings, newSumMapping(pkger.SafeID(checks[1].Check.GetID()), checks[1].Check.GetName(), influxdb.ChecksResourceType))
//hasMapping(t, mappings, newSumMapping(dashs[0].ID, dashs[0].Name, influxdb.DashboardsResourceType))
//hasMapping(t, mappings, newSumMapping(pkger.SafeID(endpoints[0].NotificationEndpoint.GetID()), endpoints[0].NotificationEndpoint.GetName(), influxdb.NotificationEndpointResourceType))
//hasMapping(t, mappings, newSumMapping(rule.ID, rule.Name, influxdb.NotificationRuleResourceType))
//hasMapping(t, mappings, newSumMapping(task.ID, task.Name, influxdb.TasksResourceType))
//hasMapping(t, mappings, newSumMapping(pkger.SafeID(teles[0].TelegrafConfig.ID), teles[0].TelegrafConfig.Name, influxdb.TelegrafsResourceType))
//hasMapping(t, mappings, newSumMapping(vars[0].ID, vars[0].Name, influxdb.VariablesResourceType))

ch0 := checks[0]
assert.Contains(t, mappings, newSumMapping(pkger.SafeID(ch0.Check.GetID()), ch0.PkgName, ch0.Check.GetName(), influxdb.ChecksResourceType))

ch1 := checks[0]
assert.Contains(t, mappings, newSumMapping(pkger.SafeID(ch1.Check.GetID()), ch1.PkgName, ch1.Check.GetName(), influxdb.ChecksResourceType))

ne := endpoints[0]
assert.Contains(t, mappings, newSumMapping(pkger.SafeID(ne.NotificationEndpoint.GetID()), ne.PkgName, ne.NotificationEndpoint.GetName(), influxdb.NotificationEndpointResourceType))

assert.Contains(t, mappings, newSumMapping(dashs[0].ID, dashs[0].PkgName, dashs[0].Name, influxdb.DashboardsResourceType))
assert.Contains(t, mappings, newSumMapping(rule.ID, rule.PkgName, rule.Name, influxdb.NotificationRuleResourceType))
assert.Contains(t, mappings, newSumMapping(task.ID, task.PkgName, task.Name, influxdb.TasksResourceType))
assert.Contains(t, mappings, newSumMapping(pkger.SafeID(teles[0].TelegrafConfig.ID), teles[0].PkgName, teles[0].TelegrafConfig.Name, influxdb.TelegrafsResourceType))
assert.Contains(t, mappings, newSumMapping(vars[0].ID, vars[0].PkgName, vars[0].Name, influxdb.VariablesResourceType))
})

t.Run("filtered by resource types", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7825,8 +7825,8 @@ components:
items:
type: object
properties:
remove:
type: boolean
stateStatus:
type: string
id:
type: string
pkgName:
Expand Down
161 changes: 0 additions & 161 deletions pkger/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,31 +603,6 @@ type (
}
)

func newDiffVariable(v *variable, iv *influxdb.Variable) DiffVariable {
diff := DiffVariable{
DiffIdentifier: DiffIdentifier{
ID: SafeID(v.ID()),
Remove: v.shouldRemove,
PkgName: v.PkgName(),
},
New: DiffVariableValues{
Name: v.Name(),
Description: v.Description,
Args: v.influxVarArgs(),
},
}
if iv != nil {
diff.ID = SafeID(iv.ID)
diff.Old = &DiffVariableValues{
Name: iv.Name,
Description: iv.Description,
Args: iv.Arguments,
}
}

return diff
}

func (d DiffVariable) hasConflict() bool {
return !d.IsNew() && d.Old != nil && !reflect.DeepEqual(*d.Old, d.New)
}
Expand Down Expand Up @@ -1682,142 +1657,6 @@ func (m mapperTelegrafs) Len() int {
return len(m)
}

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

type variable struct {
identity

id influxdb.ID
OrgID influxdb.ID
Description string
Type string
Query string
Language string
ConstValues []string
MapValues map[string]string

labels sortedLabels

existing *influxdb.Variable
}

func (v *variable) ID() influxdb.ID {
if v.existing != nil {
return v.existing.ID
}
return v.id
}

func (v *variable) Exists() bool {
return v.existing != nil
}

func (v *variable) Labels() []*label {
return v.labels
}

func (v *variable) ResourceType() influxdb.ResourceType {
return KindVariable.ResourceType()
}

func (v *variable) shouldApply() bool {
return v.existing == nil ||
v.existing.Description != v.Description ||
v.existing.Arguments == nil ||
!reflect.DeepEqual(v.existing.Arguments, v.influxVarArgs())
}

func (v *variable) summarize() SummaryVariable {
return SummaryVariable{
PkgName: v.PkgName(),
ID: SafeID(v.ID()),
OrgID: SafeID(v.OrgID),
Name: v.Name(),
Description: v.Description,
Arguments: v.influxVarArgs(),
LabelAssociations: toSummaryLabels(v.labels...),
}
}

func (v *variable) influxVarArgs() *influxdb.VariableArguments {
// this zero value check is for situations where we want to marshal/unmarshal
// a variable and not have the invalid args blow up during unmarshaling. When
// that validation is decoupled from the unmarshaling, we can clean this up.
if v.Type == "" {
return nil
}

args := &influxdb.VariableArguments{
Type: v.Type,
}
switch args.Type {
case "query":
args.Values = influxdb.VariableQueryValues{
Query: v.Query,
Language: v.Language,
}
case "constant":
args.Values = influxdb.VariableConstantValues(v.ConstValues)
case "map":
args.Values = influxdb.VariableMapValues(v.MapValues)
}
return args
}

func (v *variable) valid() []validationErr {
var failures []validationErr
switch v.Type {
case "map":
if len(v.MapValues) == 0 {
failures = append(failures, validationErr{
Field: fieldValues,
Msg: "map variable must have at least 1 key/val pair",
})
}
case "constant":
if len(v.ConstValues) == 0 {
failures = append(failures, validationErr{
Field: fieldValues,
Msg: "constant variable must have a least 1 value provided",
})
}
case "query":
if v.Query == "" {
failures = append(failures, validationErr{
Field: fieldQuery,
Msg: "query variable must provide a query string",
})
}
if v.Language != "influxql" && v.Language != "flux" {
failures = append(failures, validationErr{
Field: fieldLanguage,
Msg: fmt.Sprintf(`query variable language must be either "influxql" or "flux"; got %q`, v.Language),
})
}
}
if len(failures) > 0 {
return []validationErr{
objectValidationErr(fieldSpec, failures...),
}
}

return nil
}

type mapperVariables []*variable

func (m mapperVariables) Association(i int) labelAssociater {
return m[i]
}

func (m mapperVariables) Len() int {
return len(m)
}

const (
fieldDashCharts = "charts"
)
Expand Down
5 changes: 0 additions & 5 deletions pkger/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,11 +506,6 @@ func TestPkg(t *testing.T) {
kind: KindTelegraf,
validName: "first_tele_config",
},
{
pkgFile: "testdata/variables.yml",
kind: KindVariable,
validName: "var_query_1",
},
}

for _, tt := range tests {
Expand Down
10 changes: 0 additions & 10 deletions pkger/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,6 @@ func (p *Pkg) addObjectForRemoval(k Kind, pkgName string, id influxdb.ID) {
identity: newIdentity,
config: influxdb.TelegrafConfig{ID: id},
}
case KindVariable:
p.mVariables[pkgName] = &variable{
identity: newIdentity,
id: id,
}
}
}

Expand Down Expand Up @@ -485,11 +480,6 @@ func (p *Pkg) getObjectIDSetter(k Kind, pkgName string) (func(influxdb.ID), bool
return func(id influxdb.ID) {
t.config.ID = id
}, ok
case KindVariable:
v, ok := p.mVariables[pkgName]
return func(id influxdb.ID) {
v.id = id
}, ok
default:
return nil, false
}
Expand Down
103 changes: 103 additions & 0 deletions pkger/parser_models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkger

import (
"fmt"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -468,3 +469,105 @@ func (s sortedLabels) Less(i, j int) bool {
func (s sortedLabels) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

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

type variable struct {
identity

Description string
Type string
Query string
Language string
ConstValues []string
MapValues map[string]string

labels sortedLabels
}

func (v *variable) Labels() []*label {
return v.labels
}

func (v *variable) ResourceType() influxdb.ResourceType {
return KindVariable.ResourceType()
}

func (v *variable) summarize() SummaryVariable {
return SummaryVariable{
PkgName: v.PkgName(),
Name: v.Name(),
Description: v.Description,
Arguments: v.influxVarArgs(),
LabelAssociations: toSummaryLabels(v.labels...),
}
}

func (v *variable) influxVarArgs() *influxdb.VariableArguments {
// this zero value check is for situations where we want to marshal/unmarshal
// a variable and not have the invalid args blow up during unmarshaling. When
// that validation is decoupled from the unmarshaling, we can clean this up.
if v.Type == "" {
return nil
}

args := &influxdb.VariableArguments{
Type: v.Type,
}
switch args.Type {
case "query":
args.Values = influxdb.VariableQueryValues{
Query: v.Query,
Language: v.Language,
}
case "constant":
args.Values = influxdb.VariableConstantValues(v.ConstValues)
case "map":
args.Values = influxdb.VariableMapValues(v.MapValues)
}
return args
}

func (v *variable) valid() []validationErr {
var failures []validationErr
switch v.Type {
case "map":
if len(v.MapValues) == 0 {
failures = append(failures, validationErr{
Field: fieldValues,
Msg: "map variable must have at least 1 key/val pair",
})
}
case "constant":
if len(v.ConstValues) == 0 {
failures = append(failures, validationErr{
Field: fieldValues,
Msg: "constant variable must have a least 1 value provided",
})
}
case "query":
if v.Query == "" {
failures = append(failures, validationErr{
Field: fieldQuery,
Msg: "query variable must provide a query string",
})
}
if v.Language != "influxql" && v.Language != "flux" {
failures = append(failures, validationErr{
Field: fieldLanguage,
Msg: fmt.Sprintf(`query variable language must be either "influxql" or "flux"; got %q`, v.Language),
})
}
}
if len(failures) > 0 {
return []validationErr{
objectValidationErr(fieldSpec, failures...),
}
}

return nil
}
Loading

0 comments on commit c8a79f5

Please sign in to comment.