...

Source file src/edge-infra.dev/hack/tools/hr/utils/utils.go

Documentation: edge-infra.dev/hack/tools/hr/utils

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"flag"
     7  	"fmt"
     8  
     9  	"edge-infra.dev/pkg/edge/api/clients"
    10  
    11  	"github.com/peterbourgon/ff/v3"
    12  	"github.com/peterbourgon/ff/v3/ffcli"
    13  )
    14  
    15  const (
    16  	getClusterEdgeID = "SELECT cluster_edge_id from clusters"
    17  	LongHelp         = `
    18  CLI program to create helm workloads in sql db from what is in big query.
    19  
    20  The cli program can be run like so:
    21  
    22  hr --databaseHost=localhost --databaseName=dev0 --databaseUser=postgres --databasePassword=**** 
    23  --bigQueryTableName='ret-edge-dev0-foreman.ctlfishpubsub.resources' --gcpSA=path
    24  		
    25  The following flags are required for the cli to run:
    26  databaseHost, databaseName, databaseUser, bigQueryTableName gcpSA
    27  
    28  database password is optional and will use iam auth if not provided
    29  		
    30  The dryRun flag is an optional and defaults to false if not specified. launchdarklyKey is also an optional flag that defaults to using cluster id if not set.`
    31  )
    32  
    33  type HRCommand struct {
    34  	DBConnection *sql.DB
    35  	BQClient     clients.BQClient
    36  }
    37  
    38  func New() *HRCommand {
    39  	return &HRCommand{}
    40  }
    41  
    42  func (e *HRCommand) SetDBConnection(db *sql.DB) *HRCommand {
    43  	e.DBConnection = db
    44  	return e
    45  }
    46  
    47  func (e *HRCommand) SetBQClient(db clients.BQClient) *HRCommand {
    48  	e.BQClient = db
    49  	return e
    50  }
    51  
    52  func (e HRCommand) Exec(name, shortUsage, shortHelp string, bqTableName string, callback func(ctx context.Context, db *sql.DB, BQClient clients.BQClient, bqTableName string, clusterEdgeID string) bool, fs *flag.FlagSet) *ffcli.Command {
    53  	return &ffcli.Command{
    54  		Name:       name,
    55  		ShortUsage: shortUsage,
    56  		ShortHelp:  shortHelp,
    57  		LongHelp:   LongHelp,
    58  		FlagSet:    fs,
    59  		Options:    []ff.Option{ff.WithEnvVarNoPrefix()},
    60  		Exec: func(ctx context.Context, _ []string) error {
    61  			return queryClusters(ctx, e.DBConnection, e.BQClient, bqTableName, callback)
    62  		},
    63  	}
    64  }
    65  
    66  func queryClusters(ctx context.Context, db *sql.DB, bqClient clients.BQClient, bqTableName string, callback func(ctx context.Context, db *sql.DB, BQClient clients.BQClient, bqTableName string, clusterEdgeID string) bool) error {
    67  	clusters, err := db.QueryContext(ctx, getClusterEdgeID)
    68  	if err != nil {
    69  		fmt.Println("db error occurred ", err)
    70  		return err
    71  	}
    72  	fmt.Println("Getting Clusters")
    73  	for clusters.Next() {
    74  		var clusterEdgeID string
    75  		if err := clusters.Scan(&clusterEdgeID); err != nil {
    76  			fmt.Println("failed to scan banner clusterEdgeID ", err)
    77  			return err
    78  		}
    79  
    80  		fmt.Printf("processing cluster: %s\n", clusterEdgeID)
    81  		_ = callback(ctx, db, bqClient, bqTableName, clusterEdgeID)
    82  	}
    83  	return nil
    84  }
    85  

View as plain text