Files
influx-cli/clients/template/template.go
Sam Arnold dfc6f00982 fix: less confusing overloads of 'token' in help (#272)
* chore: refactor GetOrg calls

* fix: less confusing overloads of 'token' in help

Closes #20619

* fix: clean up iscommon function definition
2021-09-20 13:57:45 -04:00

92 lines
2.0 KiB
Go

package template
import (
"context"
"fmt"
"github.com/influxdata/influx-cli/v2/api"
"github.com/influxdata/influx-cli/v2/clients"
"github.com/influxdata/influx-cli/v2/pkg/template"
)
type Client struct {
clients.CLI
api.TemplatesApi
api.OrganizationsApi
}
type SummarizeParams struct {
OrgId string
OrgName string
Sources []template.Source
RenderTableColors bool
RenderTableBorders bool
}
func (c Client) Summarize(ctx context.Context, params *SummarizeParams) error {
orgID, err := c.GetOrgId(ctx, params.OrgId, params.OrgName, c.OrganizationsApi)
if err != nil {
return err
}
templates, err := template.ReadSources(ctx, params.Sources)
if err != nil {
return err
}
// Execute a dry-run to make the server summarize the template(s).
req := api.TemplateApply{
DryRun: true,
OrgID: orgID,
Templates: templates,
}
res, err := c.ApplyTemplate(ctx).TemplateApply(req).Execute()
if err != nil {
return fmt.Errorf("failed to summarize template: %w", err)
}
if c.PrintAsJSON {
return c.PrintJSON(res.Summary)
}
return template.PrintSummary(res.Summary, c.StdIO, params.RenderTableColors, params.RenderTableBorders)
}
type ValidateParams struct {
OrgId string
OrgName string
Sources []template.Source
}
func (c Client) Validate(ctx context.Context, params *ValidateParams) error {
orgID, err := c.GetOrgId(ctx, params.OrgId, params.OrgName, c.OrganizationsApi)
if err != nil {
return err
}
templates, err := template.ReadSources(ctx, params.Sources)
if err != nil {
return err
}
// Execute a dry-run to make the server summarize the template(s).
req := api.TemplateApply{
DryRun: true,
OrgID: orgID,
Templates: templates,
}
_, err = c.ApplyTemplate(ctx).TemplateApply(req).Execute()
if err == nil {
return nil
}
if apiErr, ok := err.(api.GenericOpenAPIError); ok {
if summary, ok := apiErr.Model().(*api.TemplateSummary); ok {
return fmt.Errorf("template failed validation:\n\t%s", summary)
}
}
return fmt.Errorf("failed to validate template: %w", err)
}