chore(pkger): refactor diff into common types for easier access
parent
7a2868f914
commit
988fd0ae80
|
@ -11,6 +11,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -534,17 +535,17 @@ func pkgFromFile(path string) (*pkger.Pkg, error) {
|
|||
}
|
||||
|
||||
func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) {
|
||||
red := color.New(color.FgRed).SprintfFunc()
|
||||
green := color.New(color.FgHiGreen, color.Bold).SprintfFunc()
|
||||
red := color.New(color.FgRed).SprintFunc()
|
||||
green := color.New(color.FgHiGreen, color.Bold).SprintFunc()
|
||||
|
||||
strDiff := func(isNew bool, old, new string) string {
|
||||
diffLn := func(isNew bool, old, new interface{}) string {
|
||||
if isNew {
|
||||
return green(new)
|
||||
}
|
||||
if old == new {
|
||||
return new
|
||||
if reflect.DeepEqual(old, new) {
|
||||
return fmt.Sprint(new)
|
||||
}
|
||||
return fmt.Sprintf("%s\n%s", red("%q", old), green("%q", new))
|
||||
return fmt.Sprintf("%s\n%s", red(old), green(new))
|
||||
}
|
||||
|
||||
boolDiff := func(b bool) string {
|
||||
|
@ -555,105 +556,95 @@ func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) {
|
|||
return bb
|
||||
}
|
||||
|
||||
durDiff := func(isNew bool, oldDur, newDur time.Duration) string {
|
||||
o := oldDur.String()
|
||||
if oldDur == 0 {
|
||||
o = "inf"
|
||||
}
|
||||
n := newDur.String()
|
||||
if newDur == 0 {
|
||||
n = "inf"
|
||||
}
|
||||
if isNew {
|
||||
return green(n)
|
||||
}
|
||||
if oldDur == newDur {
|
||||
return n
|
||||
}
|
||||
return fmt.Sprintf("%s\n%s", red(o), green(n))
|
||||
}
|
||||
|
||||
tablePrintFn := b.tablePrinterGen()
|
||||
if labels := diff.Labels; len(labels) > 0 {
|
||||
headers := []string{"New", "ID", "Name", "Color", "Description"}
|
||||
tablePrintFn("LABELS", headers, len(labels), func(w *tablewriter.Table) {
|
||||
for _, l := range labels {
|
||||
w.Append([]string{
|
||||
boolDiff(l.IsNew()),
|
||||
l.ID.String(),
|
||||
l.Name,
|
||||
strDiff(l.IsNew(), l.OldColor, l.NewColor),
|
||||
strDiff(l.IsNew(), l.OldDesc, l.NewDesc),
|
||||
})
|
||||
tablePrintFn("LABELS", headers, len(labels), func(i int) []string {
|
||||
l := labels[i]
|
||||
var old pkger.DiffLabelValues
|
||||
if l.Old != nil {
|
||||
old = *l.Old
|
||||
}
|
||||
|
||||
return []string{
|
||||
boolDiff(l.IsNew()),
|
||||
l.ID.String(),
|
||||
l.Name,
|
||||
diffLn(l.IsNew(), old.Color, l.New.Color),
|
||||
diffLn(l.IsNew(), old.Description, l.New.Description),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if bkts := diff.Buckets; len(bkts) > 0 {
|
||||
headers := []string{"New", "ID", "Name", "Retention Period", "Description"}
|
||||
tablePrintFn("BUCKETS", headers, len(bkts), func(w *tablewriter.Table) {
|
||||
for _, b := range bkts {
|
||||
w.Append([]string{
|
||||
boolDiff(b.IsNew()),
|
||||
b.ID.String(),
|
||||
b.Name,
|
||||
durDiff(b.IsNew(), b.OldRetention, b.NewRetention),
|
||||
strDiff(b.IsNew(), b.OldDesc, b.NewDesc),
|
||||
})
|
||||
tablePrintFn("BUCKETS", headers, len(bkts), func(i int) []string {
|
||||
b := bkts[i]
|
||||
var old pkger.DiffBucketValues
|
||||
if b.Old != nil {
|
||||
old = *b.Old
|
||||
}
|
||||
return []string{
|
||||
boolDiff(b.IsNew()),
|
||||
b.ID.String(),
|
||||
b.Name,
|
||||
diffLn(b.IsNew(), old.RetentionRules.RP().String(), b.New.RetentionRules.RP().String()),
|
||||
diffLn(b.IsNew(), old.Description, b.New.Description),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if dashes := diff.Dashboards; len(dashes) > 0 {
|
||||
headers := []string{"New", "Name", "Description", "Num Charts"}
|
||||
tablePrintFn("DASHBOARDS", headers, len(dashes), func(w *tablewriter.Table) {
|
||||
for _, d := range dashes {
|
||||
w.Append([]string{
|
||||
boolDiff(true),
|
||||
d.Name,
|
||||
green(d.Desc),
|
||||
green(strconv.Itoa(len(d.Charts))),
|
||||
})
|
||||
tablePrintFn("DASHBOARDS", headers, len(dashes), func(i int) []string {
|
||||
d := dashes[i]
|
||||
return []string{
|
||||
boolDiff(true),
|
||||
d.Name,
|
||||
green(d.Desc),
|
||||
green(strconv.Itoa(len(d.Charts))),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if vars := diff.Variables; len(vars) > 0 {
|
||||
headers := []string{"New", "ID", "Name", "Description", "Arg Type", "Arg Values"}
|
||||
tablePrintFn("VARIABLES", headers, len(vars), func(w *tablewriter.Table) {
|
||||
for _, v := range vars {
|
||||
var oldArgType string
|
||||
if v.OldArgs != nil {
|
||||
oldArgType = v.OldArgs.Type
|
||||
}
|
||||
var newArgType string
|
||||
if v.NewArgs != nil {
|
||||
newArgType = v.NewArgs.Type
|
||||
}
|
||||
w.Append([]string{
|
||||
boolDiff(v.IsNew()),
|
||||
v.ID.String(),
|
||||
v.Name,
|
||||
strDiff(v.IsNew(), v.OldDesc, v.NewDesc),
|
||||
strDiff(v.IsNew(), oldArgType, newArgType),
|
||||
strDiff(v.IsNew(), printVarArgs(v.OldArgs), printVarArgs(v.NewArgs)),
|
||||
})
|
||||
tablePrintFn("VARIABLES", headers, len(vars), func(i int) []string {
|
||||
v := vars[i]
|
||||
var old pkger.DiffVariableValues
|
||||
if v.Old != nil {
|
||||
old = *v.Old
|
||||
}
|
||||
var oldArgType string
|
||||
if old.Args != nil {
|
||||
oldArgType = old.Args.Type
|
||||
}
|
||||
var newArgType string
|
||||
if v.New.Args != nil {
|
||||
newArgType = v.New.Args.Type
|
||||
}
|
||||
return []string{
|
||||
boolDiff(v.IsNew()),
|
||||
v.ID.String(),
|
||||
v.Name,
|
||||
diffLn(v.IsNew(), old.Description, v.New.Description),
|
||||
diffLn(v.IsNew(), oldArgType, newArgType),
|
||||
diffLn(v.IsNew(), printVarArgs(old.Args), printVarArgs(v.New.Args)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if len(diff.LabelMappings) > 0 {
|
||||
headers := []string{"New", "Resource Type", "Resource Name", "Resource ID", "Label Name", "Label ID"}
|
||||
tablePrintFn("LABEL MAPPINGS", headers, len(diff.LabelMappings), func(w *tablewriter.Table) {
|
||||
for _, m := range diff.LabelMappings {
|
||||
w.Append([]string{
|
||||
boolDiff(m.IsNew),
|
||||
string(m.ResType),
|
||||
m.ResName,
|
||||
m.ResID.String(),
|
||||
m.LabelName,
|
||||
m.LabelID.String(),
|
||||
})
|
||||
tablePrintFn("LABEL MAPPINGS", headers, len(diff.LabelMappings), func(i int) []string {
|
||||
m := diff.LabelMappings[i]
|
||||
return []string{
|
||||
boolDiff(m.IsNew),
|
||||
string(m.ResType),
|
||||
m.ResName,
|
||||
m.ResID.String(),
|
||||
m.LabelName,
|
||||
m.LabelID.String(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -663,91 +654,76 @@ func (b *cmdPkgBuilder) printPkgSummary(sum pkger.Summary) {
|
|||
tablePrintFn := b.tablePrinterGen()
|
||||
if labels := sum.Labels; len(labels) > 0 {
|
||||
headers := []string{"ID", "Name", "Description", "Color"}
|
||||
tablePrintFn("LABELS", headers, len(labels), func(w *tablewriter.Table) {
|
||||
for _, l := range labels {
|
||||
w.Append([]string{
|
||||
l.ID.String(),
|
||||
l.Name,
|
||||
l.Properties["description"],
|
||||
l.Properties["color"],
|
||||
})
|
||||
tablePrintFn("LABELS", headers, len(labels), func(i int) []string {
|
||||
l := labels[i]
|
||||
return []string{
|
||||
l.ID.String(),
|
||||
l.Name,
|
||||
l.Properties["description"],
|
||||
l.Properties["color"],
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if buckets := sum.Buckets; len(buckets) > 0 {
|
||||
headers := []string{"ID", "Name", "Retention", "Description"}
|
||||
tablePrintFn("BUCKETS", headers, len(buckets), func(w *tablewriter.Table) {
|
||||
for _, bucket := range buckets {
|
||||
w.Append([]string{
|
||||
bucket.ID.String(),
|
||||
bucket.Name,
|
||||
formatDuration(bucket.RetentionPeriod),
|
||||
bucket.Description,
|
||||
})
|
||||
tablePrintFn("BUCKETS", headers, len(buckets), func(i int) []string {
|
||||
bucket := buckets[i]
|
||||
return []string{
|
||||
bucket.ID.String(),
|
||||
bucket.Name,
|
||||
formatDuration(bucket.RetentionPeriod),
|
||||
bucket.Description,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if dashes := sum.Dashboards; len(dashes) > 0 {
|
||||
headers := []string{"ID", "Name", "Description"}
|
||||
tablePrintFn("DASHBOARDS", headers, len(dashes), func(w *tablewriter.Table) {
|
||||
for _, d := range dashes {
|
||||
w.Append([]string{
|
||||
d.ID.String(),
|
||||
d.Name,
|
||||
d.Description,
|
||||
})
|
||||
}
|
||||
tablePrintFn("DASHBOARDS", headers, len(dashes), func(i int) []string {
|
||||
d := dashes[i]
|
||||
return []string{d.ID.String(), d.Name, d.Description}
|
||||
})
|
||||
}
|
||||
|
||||
if vars := sum.Variables; len(vars) > 0 {
|
||||
headers := []string{"ID", "Name", "Description", "Arg Type", "Arg Values"}
|
||||
tablePrintFn("VARIABLES", headers, len(vars), func(w *tablewriter.Table) {
|
||||
for _, v := range vars {
|
||||
args := v.Arguments
|
||||
w.Append([]string{
|
||||
v.ID.String(),
|
||||
v.Name,
|
||||
v.Description,
|
||||
args.Type,
|
||||
printVarArgs(args),
|
||||
})
|
||||
tablePrintFn("VARIABLES", headers, len(vars), func(i int) []string {
|
||||
v := vars[i]
|
||||
args := v.Arguments
|
||||
return []string{
|
||||
v.ID.String(),
|
||||
v.Name,
|
||||
v.Description,
|
||||
args.Type,
|
||||
printVarArgs(args),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if mappings := sum.LabelMappings; len(mappings) > 0 {
|
||||
headers := []string{"Resource Type", "Resource Name", "Resource ID", "Label Name", "Label ID"}
|
||||
tablePrintFn("LABEL MAPPINGS", headers, len(mappings), func(w *tablewriter.Table) {
|
||||
for _, m := range mappings {
|
||||
w.Append([]string{
|
||||
string(m.ResourceType),
|
||||
m.ResourceName,
|
||||
m.ResourceID.String(),
|
||||
m.LabelName,
|
||||
m.LabelID.String(),
|
||||
})
|
||||
tablePrintFn("LABEL MAPPINGS", headers, len(mappings), func(i int) []string {
|
||||
m := mappings[i]
|
||||
return []string{
|
||||
string(m.ResourceType),
|
||||
m.ResourceName,
|
||||
m.ResourceID.String(),
|
||||
m.LabelName,
|
||||
m.LabelID.String(),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (b *cmdPkgBuilder) tablePrinterGen() func(table string, headers []string, count int, appendFn func(w *tablewriter.Table)) {
|
||||
return func(table string, headers []string, count int, appendFn func(w *tablewriter.Table)) {
|
||||
tablePrinter(b.w, table, headers, count, b.hasColor, b.hasTableBorders, appendFn)
|
||||
func (b *cmdPkgBuilder) tablePrinterGen() func(table string, headers []string, count int, rowFn func(i int) []string) {
|
||||
return func(table string, headers []string, count int, rowFn func(i int) []string) {
|
||||
tablePrinter(b.w, table, headers, count, b.hasColor, b.hasTableBorders, rowFn)
|
||||
}
|
||||
}
|
||||
|
||||
func tablePrinter(wr io.Writer, table string, headers []string, count int, hasColor, hasTableBorders bool, appendFn func(w *tablewriter.Table)) {
|
||||
descrCol := -1
|
||||
for i, h := range headers {
|
||||
if strings.ToLower(h) == "description" {
|
||||
descrCol = i
|
||||
break
|
||||
}
|
||||
}
|
||||
func tablePrinter(wr io.Writer, table string, headers []string, count int, hasColor, hasTableBorders bool, rowFn func(i int) []string) {
|
||||
color.New(color.FgYellow, color.Bold).Fprintln(os.Stdout, strings.ToUpper(table))
|
||||
|
||||
w := tablewriter.NewWriter(wr)
|
||||
w.SetBorder(hasTableBorders)
|
||||
|
@ -757,16 +733,19 @@ func tablePrinter(wr io.Writer, table string, headers []string, count int, hasCo
|
|||
for range headers {
|
||||
alignments = append(alignments, tablewriter.ALIGN_CENTER)
|
||||
}
|
||||
|
||||
descrCol := find("description", headers)
|
||||
if descrCol != -1 {
|
||||
w.SetColMinWidth(descrCol, 30)
|
||||
alignments[descrCol] = tablewriter.ALIGN_LEFT
|
||||
}
|
||||
|
||||
color.New(color.FgYellow, color.Bold).Fprintln(os.Stdout, strings.ToUpper(table))
|
||||
w.SetHeader(headers)
|
||||
w.SetColumnAlignment(alignments)
|
||||
|
||||
appendFn(w)
|
||||
for i := range make([]struct{}, count) {
|
||||
w.Append(rowFn(i))
|
||||
}
|
||||
|
||||
footers := make([]string, len(headers))
|
||||
footers[len(footers)-2] = "TOTAL"
|
||||
|
@ -825,3 +804,12 @@ func formatDuration(d time.Duration) string {
|
|||
}
|
||||
return d.String()
|
||||
}
|
||||
|
||||
func find(needle string, haystack []string) int {
|
||||
for i, h := range haystack {
|
||||
if strings.ToLower(h) == needle {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
|
|
@ -6928,6 +6928,7 @@ components:
|
|||
format: date-time
|
||||
readOnly: true
|
||||
retentionRules:
|
||||
$ref: "#/components/schemas/RetentionRules"
|
||||
type: array
|
||||
description: Rules to expire or retain data. No rules means data never expires.
|
||||
items:
|
||||
|
@ -6957,6 +6958,23 @@ components:
|
|||
type: array
|
||||
items:
|
||||
$ref: "#/components/schemas/Bucket"
|
||||
RetentionRules:
|
||||
type: array
|
||||
description: Rules to expire or retain data. No rules means data never expires.
|
||||
items:
|
||||
type: object
|
||||
properties:
|
||||
type:
|
||||
type: string
|
||||
default: expire
|
||||
enum:
|
||||
- expire
|
||||
everySeconds:
|
||||
type: integer
|
||||
description: Duration in seconds for how long data will be kept in the database.
|
||||
example: 86400
|
||||
minimum: 1
|
||||
required: [type, everySeconds]
|
||||
Link:
|
||||
type: string
|
||||
format: uri
|
||||
|
@ -7224,14 +7242,20 @@ components:
|
|||
type: string
|
||||
name:
|
||||
type: string
|
||||
oldDescription:
|
||||
type: string
|
||||
newDescription:
|
||||
type: string
|
||||
oldRP:
|
||||
type: string
|
||||
newRP:
|
||||
type: string
|
||||
new:
|
||||
type: object
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
retentionRules:
|
||||
$ref: "#/components/schemas/RetentionRules"
|
||||
old:
|
||||
type: object
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
retentionRules:
|
||||
$ref: "#/components/schemas/RetentionRules"
|
||||
dashboards:
|
||||
type: array
|
||||
items:
|
||||
|
@ -7254,14 +7278,20 @@ components:
|
|||
type: string
|
||||
name:
|
||||
type: string
|
||||
oldDescription:
|
||||
type: string
|
||||
newDescription:
|
||||
type: string
|
||||
oldColor:
|
||||
type: string
|
||||
newColor:
|
||||
type: string
|
||||
new:
|
||||
type: object
|
||||
properties:
|
||||
color:
|
||||
type: string
|
||||
description:
|
||||
type: string
|
||||
old:
|
||||
type: object
|
||||
properties:
|
||||
color:
|
||||
type: string
|
||||
description:
|
||||
type: string
|
||||
labelMappings:
|
||||
type: array
|
||||
items:
|
||||
|
@ -7288,14 +7318,20 @@ components:
|
|||
type: string
|
||||
name:
|
||||
type: string
|
||||
oldDescription:
|
||||
type: string
|
||||
newDescription:
|
||||
type: string
|
||||
oldArgs:
|
||||
$ref: "#/components/schemas/VariableProperties"
|
||||
newArgs:
|
||||
$ref: "#/components/schemas/VariableProperties"
|
||||
new:
|
||||
type: object
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
args:
|
||||
$ref: "#/components/schemas/VariableProperties"
|
||||
old:
|
||||
type: object
|
||||
properties:
|
||||
description:
|
||||
type: string
|
||||
args:
|
||||
$ref: "#/components/schemas/VariableProperties"
|
||||
errors:
|
||||
type: array
|
||||
items:
|
||||
|
|
152
pkger/models.go
152
pkger/models.go
|
@ -3,6 +3,7 @@ package pkger
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -126,14 +127,38 @@ func (d Diff) HasConflicts() bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// DiffBucketValues are the varying values for a bucket.
|
||||
type DiffBucketValues struct {
|
||||
Description string `json:"description"`
|
||||
RetentionRules retentionRules `json:"retentionRules"`
|
||||
}
|
||||
|
||||
// DiffBucket is a diff of an individual bucket.
|
||||
type DiffBucket struct {
|
||||
ID SafeID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
OldDesc string `json:"oldDescription"`
|
||||
NewDesc string `json:"newDescription"`
|
||||
OldRetention time.Duration `json:"oldRP"`
|
||||
NewRetention time.Duration `json:"newRP"`
|
||||
ID SafeID
|
||||
Name string
|
||||
New DiffBucketValues `json:"new"`
|
||||
Old *DiffBucketValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
|
||||
}
|
||||
|
||||
func newDiffBucket(b *bucket, i *influxdb.Bucket) DiffBucket {
|
||||
diff := DiffBucket{
|
||||
Name: b.Name,
|
||||
New: DiffBucketValues{
|
||||
Description: b.Description,
|
||||
RetentionRules: b.RetentionRules,
|
||||
},
|
||||
}
|
||||
if i != nil {
|
||||
diff.ID = SafeID(i.ID)
|
||||
diff.Old = &DiffBucketValues{
|
||||
Description: i.Description,
|
||||
}
|
||||
if i.RetentionPeriod > 0 {
|
||||
diff.Old.RetentionRules = retentionRules{newRetentionRule(i.RetentionPeriod)}
|
||||
}
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// IsNew indicates whether a pkg bucket is going to be new to the platform.
|
||||
|
@ -142,18 +167,7 @@ func (d DiffBucket) IsNew() bool {
|
|||
}
|
||||
|
||||
func (d DiffBucket) hasConflict() bool {
|
||||
return !(d.IsNew() || d.NewDesc == d.OldDesc && d.NewRetention == d.OldRetention)
|
||||
}
|
||||
|
||||
func newDiffBucket(b *bucket, i influxdb.Bucket) DiffBucket {
|
||||
return DiffBucket{
|
||||
ID: SafeID(i.ID),
|
||||
Name: b.Name,
|
||||
OldDesc: i.Description,
|
||||
NewDesc: b.Description,
|
||||
OldRetention: i.RetentionPeriod,
|
||||
NewRetention: b.RetentionRules.RP(),
|
||||
}
|
||||
return !d.IsNew() && d.Old != nil && !reflect.DeepEqual(*d.Old, d.New)
|
||||
}
|
||||
|
||||
// DiffDashboard is a diff of an individual dashboard.
|
||||
|
@ -184,14 +198,18 @@ func newDiffDashboard(d *dashboard) DiffDashboard {
|
|||
// the SummaryChart is reused here.
|
||||
type DiffChart SummaryChart
|
||||
|
||||
// DiffLabelValues are the varying values for a label.
|
||||
type DiffLabelValues struct {
|
||||
Color string `json:"color"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// DiffLabel is a diff of an individual label.
|
||||
type DiffLabel struct {
|
||||
ID SafeID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
OldColor string `json:"oldColor"`
|
||||
NewColor string `json:"newColor"`
|
||||
OldDesc string `json:"oldDescription"`
|
||||
NewDesc string `json:"newDescription"`
|
||||
ID SafeID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
New DiffLabelValues `json:"new"`
|
||||
Old *DiffLabelValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
|
||||
}
|
||||
|
||||
// IsNew indicates whether a pkg label is going to be new to the platform.
|
||||
|
@ -200,18 +218,25 @@ func (d DiffLabel) IsNew() bool {
|
|||
}
|
||||
|
||||
func (d DiffLabel) hasConflict() bool {
|
||||
return !(d.IsNew() || d.NewDesc == d.OldDesc || d.NewColor == d.OldColor)
|
||||
return d.IsNew() || d.Old != nil && *d.Old != d.New
|
||||
}
|
||||
|
||||
func newDiffLabel(l *label, i influxdb.Label) DiffLabel {
|
||||
return DiffLabel{
|
||||
ID: SafeID(i.ID),
|
||||
Name: l.Name,
|
||||
OldColor: i.Properties["color"],
|
||||
NewColor: l.Color,
|
||||
OldDesc: i.Properties["description"],
|
||||
NewDesc: l.Description,
|
||||
func newDiffLabel(l *label, i *influxdb.Label) DiffLabel {
|
||||
diff := DiffLabel{
|
||||
Name: l.Name,
|
||||
New: DiffLabelValues{
|
||||
Color: l.Color,
|
||||
Description: l.Description,
|
||||
},
|
||||
}
|
||||
if i != nil {
|
||||
diff.ID = SafeID(i.ID)
|
||||
diff.Old = &DiffLabelValues{
|
||||
Color: i.Properties["color"],
|
||||
Description: i.Properties["description"],
|
||||
}
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// DiffLabelMapping is a diff of an individual label mapping. A
|
||||
|
@ -228,26 +253,37 @@ type DiffLabelMapping struct {
|
|||
LabelName string `json:"labelName"`
|
||||
}
|
||||
|
||||
// DiffVariable is a diff of an individual variable.
|
||||
type DiffVariable struct {
|
||||
ID SafeID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
OldDesc string `json:"oldDescription"`
|
||||
NewDesc string `json:"newDescription"`
|
||||
|
||||
OldArgs *influxdb.VariableArguments `json:"oldArgs"`
|
||||
NewArgs *influxdb.VariableArguments `json:"newArgs"`
|
||||
// DiffVariableValues are the varying values for a variable.
|
||||
type DiffVariableValues struct {
|
||||
Description string `json:"description"`
|
||||
Args *influxdb.VariableArguments `json:"args"`
|
||||
}
|
||||
|
||||
func newDiffVariable(v *variable, iv influxdb.Variable) DiffVariable {
|
||||
return DiffVariable{
|
||||
ID: SafeID(iv.ID),
|
||||
Name: v.Name,
|
||||
OldDesc: iv.Description,
|
||||
NewDesc: v.Description,
|
||||
OldArgs: iv.Arguments,
|
||||
NewArgs: v.influxVarArgs(),
|
||||
// DiffVariable is a diff of an individual variable.
|
||||
type DiffVariable struct {
|
||||
ID SafeID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
New DiffVariableValues `json:"new"`
|
||||
Old *DiffVariableValues `json:"old,omitempty"` // using omitempty here to signal there was no prev state with a nil
|
||||
}
|
||||
|
||||
func newDiffVariable(v *variable, iv *influxdb.Variable) DiffVariable {
|
||||
diff := DiffVariable{
|
||||
Name: v.Name,
|
||||
New: DiffVariableValues{
|
||||
Description: v.Description,
|
||||
Args: v.influxVarArgs(),
|
||||
},
|
||||
}
|
||||
if iv != nil {
|
||||
diff.ID = SafeID(iv.ID)
|
||||
diff.Old = &DiffVariableValues{
|
||||
Description: iv.Description,
|
||||
Args: iv.Arguments,
|
||||
}
|
||||
}
|
||||
|
||||
return diff
|
||||
}
|
||||
|
||||
// IsNew indicates whether a pkg variable is going to be new to the platform.
|
||||
|
@ -256,21 +292,7 @@ func (d DiffVariable) IsNew() bool {
|
|||
}
|
||||
|
||||
func (d DiffVariable) hasConflict() bool {
|
||||
if d.IsNew() {
|
||||
return false
|
||||
}
|
||||
if d.NewDesc != d.OldDesc {
|
||||
return true
|
||||
}
|
||||
|
||||
oArg, nArg := d.OldArgs, d.NewArgs
|
||||
if oArg == nil && nArg == nil {
|
||||
return false
|
||||
}
|
||||
if oArg != nil && nArg == nil || oArg == nil && nArg != nil {
|
||||
return true
|
||||
}
|
||||
return *oArg != *nArg
|
||||
return !d.IsNew() && d.Old != nil && !reflect.DeepEqual(*d.Old, d.New)
|
||||
}
|
||||
|
||||
// Summary is a definition of all the resources that have or
|
||||
|
|
|
@ -502,9 +502,9 @@ func (s *Service) dryRunBuckets(ctx context.Context, orgID influxdb.ID, pkg *Pkg
|
|||
// err isn't a not found (some other error)
|
||||
case nil:
|
||||
b.existing = existingBkt
|
||||
mExistingBkts[b.Name] = newDiffBucket(b, *existingBkt)
|
||||
mExistingBkts[b.Name] = newDiffBucket(b, existingBkt)
|
||||
default:
|
||||
mExistingBkts[b.Name] = newDiffBucket(b, influxdb.Bucket{})
|
||||
mExistingBkts[b.Name] = newDiffBucket(b, nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -547,9 +547,9 @@ func (s *Service) dryRunLabels(ctx context.Context, orgID influxdb.ID, pkg *Pkg)
|
|||
case err == nil && len(existingLabels) > 0:
|
||||
existingLabel := existingLabels[0]
|
||||
pkgLabel.existing = existingLabel
|
||||
mExistingLabels[pkgLabel.Name] = newDiffLabel(pkgLabel, *existingLabel)
|
||||
mExistingLabels[pkgLabel.Name] = newDiffLabel(pkgLabel, existingLabel)
|
||||
default:
|
||||
mExistingLabels[pkgLabel.Name] = newDiffLabel(pkgLabel, influxdb.Label{})
|
||||
mExistingLabels[pkgLabel.Name] = newDiffLabel(pkgLabel, nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -585,14 +585,14 @@ VarLoop:
|
|||
continue
|
||||
}
|
||||
pkgVar.existing = existingVar
|
||||
mExistingLabels[pkgVar.Name] = newDiffVariable(pkgVar, *existingVar)
|
||||
mExistingLabels[pkgVar.Name] = newDiffVariable(pkgVar, existingVar)
|
||||
continue VarLoop
|
||||
}
|
||||
// fallthrough here for when the variable is not found, it'll fall to the
|
||||
// default case and add it as new.
|
||||
fallthrough
|
||||
default:
|
||||
mExistingLabels[pkgVar.Name] = newDiffVariable(pkgVar, influxdb.Variable{})
|
||||
mExistingLabels[pkgVar.Name] = newDiffVariable(pkgVar, nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -36,12 +36,16 @@ func TestService(t *testing.T) {
|
|||
require.Len(t, diff.Buckets, 1)
|
||||
|
||||
expected := DiffBucket{
|
||||
ID: SafeID(1),
|
||||
Name: "rucket_11",
|
||||
OldDesc: "old desc",
|
||||
NewDesc: "bucket 1 description",
|
||||
OldRetention: 30 * time.Hour,
|
||||
NewRetention: time.Hour,
|
||||
ID: SafeID(1),
|
||||
Name: "rucket_11",
|
||||
Old: &DiffBucketValues{
|
||||
Description: "old desc",
|
||||
RetentionRules: retentionRules{newRetentionRule(30 * time.Hour)},
|
||||
},
|
||||
New: DiffBucketValues{
|
||||
Description: "bucket 1 description",
|
||||
RetentionRules: retentionRules{newRetentionRule(time.Hour)},
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, diff.Buckets[0])
|
||||
})
|
||||
|
@ -61,11 +65,14 @@ func TestService(t *testing.T) {
|
|||
require.Len(t, diff.Buckets, 1)
|
||||
|
||||
expected := DiffBucket{
|
||||
Name: "rucket_11",
|
||||
NewDesc: "bucket 1 description",
|
||||
NewRetention: time.Hour,
|
||||
Name: "rucket_11",
|
||||
New: DiffBucketValues{
|
||||
Description: "bucket 1 description",
|
||||
RetentionRules: retentionRules{newRetentionRule(time.Hour)},
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, diff.Buckets[0])
|
||||
t.Log(diff.Buckets[0].Old)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -94,18 +101,22 @@ func TestService(t *testing.T) {
|
|||
require.Len(t, diff.Labels, 2)
|
||||
|
||||
expected := DiffLabel{
|
||||
ID: SafeID(1),
|
||||
Name: "label_1",
|
||||
OldColor: "old color",
|
||||
NewColor: "#FFFFFF",
|
||||
OldDesc: "old description",
|
||||
NewDesc: "label 1 description",
|
||||
ID: SafeID(1),
|
||||
Name: "label_1",
|
||||
Old: &DiffLabelValues{
|
||||
Color: "old color",
|
||||
Description: "old description",
|
||||
},
|
||||
New: DiffLabelValues{
|
||||
Color: "#FFFFFF",
|
||||
Description: "label 1 description",
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, diff.Labels[0])
|
||||
|
||||
expected.Name = "label_2"
|
||||
expected.NewColor = "#000000"
|
||||
expected.NewDesc = "label 2 description"
|
||||
expected.New.Color = "#000000"
|
||||
expected.New.Description = "label 2 description"
|
||||
assert.Equal(t, expected, diff.Labels[1])
|
||||
})
|
||||
})
|
||||
|
@ -124,15 +135,17 @@ func TestService(t *testing.T) {
|
|||
require.Len(t, diff.Labels, 2)
|
||||
|
||||
expected := DiffLabel{
|
||||
Name: "label_1",
|
||||
NewColor: "#FFFFFF",
|
||||
NewDesc: "label 1 description",
|
||||
Name: "label_1",
|
||||
New: DiffLabelValues{
|
||||
Color: "#FFFFFF",
|
||||
Description: "label 1 description",
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, diff.Labels[0])
|
||||
|
||||
expected.Name = "label_2"
|
||||
expected.NewColor = "#000000"
|
||||
expected.NewDesc = "label 2 description"
|
||||
expected.New.Color = "#000000"
|
||||
expected.New.Description = "label 2 description"
|
||||
assert.Equal(t, expected, diff.Labels[1])
|
||||
})
|
||||
})
|
||||
|
@ -162,25 +175,30 @@ func TestService(t *testing.T) {
|
|||
require.Len(t, diff.Variables, 4)
|
||||
|
||||
expected := DiffVariable{
|
||||
ID: SafeID(1),
|
||||
Name: "var_const",
|
||||
OldDesc: "old desc",
|
||||
NewDesc: "var_const desc",
|
||||
NewArgs: &influxdb.VariableArguments{
|
||||
Type: "constant",
|
||||
Values: influxdb.VariableConstantValues{"first val"},
|
||||
ID: SafeID(1),
|
||||
Name: "var_const",
|
||||
Old: &DiffVariableValues{
|
||||
Description: "old desc",
|
||||
},
|
||||
New: DiffVariableValues{
|
||||
Description: "var_const desc",
|
||||
Args: &influxdb.VariableArguments{
|
||||
Type: "constant",
|
||||
Values: influxdb.VariableConstantValues{"first val"},
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, diff.Variables[0])
|
||||
|
||||
expected = DiffVariable{
|
||||
// no ID here since this one would be new
|
||||
Name: "var_map",
|
||||
OldDesc: "",
|
||||
NewDesc: "var_map desc",
|
||||
NewArgs: &influxdb.VariableArguments{
|
||||
Type: "map",
|
||||
Values: influxdb.VariableMapValues{"k1": "v1"},
|
||||
Name: "var_map",
|
||||
New: DiffVariableValues{
|
||||
Description: "var_map desc",
|
||||
Args: &influxdb.VariableArguments{
|
||||
Type: "map",
|
||||
Values: influxdb.VariableMapValues{"k1": "v1"},
|
||||
},
|
||||
},
|
||||
}
|
||||
assert.Equal(t, expected, diff.Variables[1])
|
||||
|
|
Loading…
Reference in New Issue