...

Source file src/edge-infra.dev/cmd/edge/monitoring/dashman/cli/add.go

Documentation: edge-infra.dev/cmd/edge/monitoring/dashman/cli

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/peterbourgon/ff/v3/ffcli"
    10  
    11  	dashmgr "edge-infra.dev/pkg/lib/gcp/monitoring/dashboardmanager"
    12  )
    13  
    14  type addArgsT struct {
    15  	overwrite    bool
    16  	templatePath string
    17  	validate     bool
    18  	clean        bool
    19  }
    20  
    21  var addArgs addArgsT
    22  var addFlagSet = newAddFlagSet(&addArgs)
    23  
    24  func newAddFlagSet(addArgs *addArgsT) *flag.FlagSet {
    25  	addf := newFlagSet("add")
    26  	addf.BoolVar(&addArgs.overwrite, "overwrite", false, "Forces the overwrite of dashboards in the project if the specified template contains the same Display Name as an existing one\nNOTE: By default, dashboards will be created with the duplicate name without changing existing dashboards")
    27  	addf.StringVar(&addArgs.templatePath, "path", "", "Path to dashboard JSON template file or folder containing multiple dashboard JSON templates")
    28  	addf.BoolVar(&addArgs.validate, "dryrun", false, "Validates the dashboard action without committing the changes.")
    29  	addf.BoolVar(&addArgs.clean, "clean", false, "Remove unnecessary fields from create dashboard request.\nNOTE: Does not save changes to the template, only strips the fields in the API request.")
    30  	return addf
    31  }
    32  
    33  var addCmd = &ffcli.Command{
    34  	Name:       "add",
    35  	ShortUsage: "add [flags]",
    36  	ShortHelp:  "Add dashboard(s) from JSON template or template folder to project",
    37  	LongHelp: strings.TrimSpace(`
    38  Creates dashboards in the project using JSON templates specified with the required [path] parameter.
    39  `),
    40  	FlagSet: withGlobalFlags(addFlagSet),
    41  	Exec:    runAdd,
    42  }
    43  
    44  func runAdd(ctx context.Context, args []string) error {
    45  	var err error
    46  	if len(args) > 0 {
    47  		Fatalf("too many non-flag arguments: %q", args)
    48  	}
    49  	if !checkAddFlags() {
    50  		Println()
    51  		return flag.ErrHelp
    52  	}
    53  
    54  	client, err := dashmgr.New(ctx, projectID)
    55  	if err != nil {
    56  		return Errorf("failed to create dashboards service: %w", err)
    57  	}
    58  
    59  	// Get the dashboard configuration template(s) from the path
    60  	sourceDashboards, err := dashmgr.ReadDashboardsFromPath(tPath)
    61  	if err != nil {
    62  		return Errorf("failed to read dashboard configuration from file: %w", err)
    63  	}
    64  
    65  	for i := 0; i < len(sourceDashboards); i++ {
    66  		if client.DisplayNameExists(sourceDashboards[i].DisplayName) {
    67  			// TODO: remove duplicate name if overwrite specified
    68  			return fmt.Errorf("%s dashboard already exists in project %s", sourceDashboards[i].DisplayName, projectID)
    69  		}
    70  
    71  		err := client.CreateDashboard(sourceDashboards[i], addArgs.validate)
    72  		if err != nil {
    73  			return err
    74  		}
    75  	}
    76  	return nil
    77  }
    78  
    79  // validates the required add subcommand flags have been provided
    80  func checkAddFlags() bool {
    81  	if len(projectID) == 0 {
    82  		Println("Error: no value specified for [project] - a valid project-id is required")
    83  		return false
    84  	}
    85  
    86  	if len(addArgs.templatePath) == 0 {
    87  		Println("Error: no value specified for [path] - a valid template source file or folder path is required")
    88  		return false
    89  	}
    90  
    91  	if !checkPath(addArgs.templatePath, false) {
    92  		Printf("Error: invalid template [path] specified - %s\n", addArgs.templatePath)
    93  		return false
    94  	}
    95  	return true
    96  }
    97  

View as plain text