feat(pkger): update dashboards with unique constraints

pull/17679/head
Johnny Steenbergen 2020-04-08 10:55:05 -07:00 committed by Johnny Steenbergen
parent dd432360cc
commit 845718c0d6
4 changed files with 80 additions and 19 deletions

View File

@ -48,7 +48,7 @@ func exec(dbPath, out string) error {
return err
}
pkg.Objects = append(pkg.Objects, pkger.DashboardToObject(d2, d2.Name))
pkg.Objects = append(pkg.Objects, pkger.DashboardToObject(d2.Name, d2))
for _, v := range vs {
name := strings.ToLower(v.Name)

View File

@ -791,18 +791,30 @@ func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) error {
printer.Render()
}
tablePrintFn := b.tablePrinterGen()
if dashes := diff.Dashboards; len(dashes) > 0 {
headers := []string{"New", "Name", "Description", "Num 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))),
printer := diffPrinterGen("Dashboards", []string{"Description", "Num Charts"})
appendValues := func(id pkger.SafeID, pkgName string, v pkger.DiffDashboardValues) []string {
return []string{pkgName, id.String(), v.Name, v.Desc, strconv.Itoa(len(v.Charts))}
}
for _, d := range dashes {
var oldRow []string
if d.Old != nil {
oldRow = appendValues(d.ID, d.PkgName, *d.Old)
}
})
newRow := appendValues(d.ID, d.PkgName, d.New)
switch {
case d.IsNew():
printer.AppendDiff(nil, newRow)
case d.Remove:
printer.AppendDiff(oldRow, nil)
default:
printer.AppendDiff(oldRow, newRow)
}
}
printer.Render()
}
if endpoints := diff.NotificationEndpoints; len(endpoints) > 0 {
@ -873,6 +885,7 @@ func (b *cmdPkgBuilder) printPkgDiff(diff pkger.Diff) error {
printer.Render()
}
tablePrintFn := b.tablePrinterGen()
if teles := diff.Telegrafs; len(teles) > 0 {
headers := []string{"New", "Name", "Description"}
tablePrintFn("TELEGRAF CONFIGS", headers, len(teles), func(i int) []string {

View File

@ -217,7 +217,7 @@ func (ex *resourceExporter) resourceCloneToKind(ctx context.Context, r ResourceT
if err != nil {
return err
}
mapResource(dash.OrganizationID, dash.ID, KindDashboard, DashboardToObject(*dash, r.Name))
mapResource(dash.OrganizationID, dash.ID, KindDashboard, DashboardToObject(r.Name, *dash))
case r.Kind.is(KindLabel):
l, err := ex.labelSVC.FindLabelByID(ctx, r.ID)
if err != nil {
@ -761,7 +761,7 @@ func convertQueries(iQueries []influxdb.DashboardQuery) queries {
}
// DashboardToObject converts an influxdb.Dashboard to an Object.
func DashboardToObject(dash influxdb.Dashboard, name string) Object {
func DashboardToObject(name string, dash influxdb.Dashboard) Object {
if name == "" {
name = dash.Name
}

View File

@ -275,30 +275,76 @@ func (d DiffCheck) IsNew() bool {
return d.Old == nil
}
// DiffDashboard is a diff of an individual dashboard. This resource is always new.
type DiffDashboard struct {
// DiffDashboardValues are values for a dashboard.
type DiffDashboardValues struct {
Name string `json:"name"`
Desc string `json:"description"`
Charts []DiffChart `json:"charts"`
}
// DiffDashboard is a diff of an individual dashboard.
type DiffDashboard struct {
ID SafeID `json:"id"`
Remove bool `json:"remove"`
PkgName string `json:"pkgName"`
New DiffDashboardValues `json:"new"`
Old *DiffDashboardValues `json:"old"`
}
func newDiffDashboard(d *dashboard) DiffDashboard {
diff := DiffDashboard{
Name: d.Name(),
Desc: d.Description,
ID: SafeID(d.ID()),
PkgName: d.PkgName(),
New: DiffDashboardValues{
Name: d.Name(),
Desc: d.Description,
Charts: make([]DiffChart, 0, len(d.Charts)),
},
}
for _, c := range d.Charts {
diff.Charts = append(diff.Charts, DiffChart{
diff.New.Charts = append(diff.New.Charts, DiffChart{
Properties: c.properties(),
Height: c.Height,
Width: c.Width,
})
}
if !d.Exists() {
return diff
}
oldDiff := DiffDashboardValues{
Name: d.existing.Name,
Desc: d.existing.Description,
Charts: make([]DiffChart, 0, len(d.existing.Cells)),
}
for _, c := range d.existing.Cells {
var props influxdb.ViewProperties
if c.View != nil {
props = c.View.Properties
}
oldDiff.Charts = append(oldDiff.Charts, DiffChart{
Properties: props,
XPosition: int(c.X),
YPosition: int(c.Y),
Height: int(c.H),
Width: int(c.W),
})
}
diff.Old = &oldDiff
return diff
}
// IsNew indicates whether the pkg dashboard is new to the platform.
func (d DiffDashboard) IsNew() bool {
return d.Old != nil
}
// DiffChart is a diff of oa chart. Since all charts are new right now.
// the SummaryChart is reused here.
type DiffChart SummaryChart
@ -2317,6 +2363,8 @@ type dashboard struct {
Charts []chart
labels sortedLabels
existing *influxdb.Dashboard
}
func (d *dashboard) ID() influxdb.ID {
@ -2332,7 +2380,7 @@ func (d *dashboard) ResourceType() influxdb.ResourceType {
}
func (d *dashboard) Exists() bool {
return false
return d.existing != nil
}
func (d *dashboard) summarize() SummaryDashboard {