Skip to content

Commit

Permalink
Merge pull request #176 from squarescale/fix-lb-commands
Browse files Browse the repository at this point in the history
Fix lb commands
  • Loading branch information
obourdon authored Oct 19, 2023
2 parents 80f9fdd + 379df24 commit 4ff205d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 45 deletions.
4 changes: 0 additions & 4 deletions command/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,6 @@ func externalNodePublicIP(f *flag.FlagSet) *string {
return f.String("public-ip", "", "External node public IP")
}

func loadBalancerIDFlag(f *flag.FlagSet) *int64 {
return f.Int64("id", 0, "id of the targeted load balancer inside project")
}

func loadBalancerDisableFlag(f *flag.FlagSet) *bool {
return f.Bool("disable", false, "Disable load balancer")
}
Expand Down
46 changes: 22 additions & 24 deletions command/lb-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"flag"
"fmt"
"log"
"strings"

"github.com/squarescale/squarescale-cli/squarescale"
Expand All @@ -21,7 +22,7 @@ func (c *LBGetCommand) Run(args []string) int {
endpoint := endpointFlag(c.flagSet)
projectUUID := projectUUIDFlag(c.flagSet)
projectName := projectNameFlag(c.flagSet)
lbID := loadBalancerIDFlag(c.flagSet)

if err := c.flagSet.Parse(args); err != nil {
return 1
}
Expand All @@ -34,10 +35,6 @@ func (c *LBGetCommand) Run(args []string) int {
return c.errorWithUsage(errors.New("Project name or uuid is mandatory"))
}

if *lbID == 0 {
return c.errorWithUsage(errors.New("Load balancer ID is mandatory"))
}

return c.runWithSpinner("load balancer config", endpoint.String(), func(client *squarescale.Client) (string, error) {
var UUID string
var err error
Expand All @@ -57,30 +54,31 @@ func (c *LBGetCommand) Run(args []string) int {
if err != nil {
return "", err
}
if len(loadBalancers) != 1 {
log.Printf("Warning project %s has %d Load Balancers (only 1 expected)", UUID, len(loadBalancers))
}

var msg string
for _, lb := range loadBalancers {
if lb.ID == *lbID {
msg += fmt.Sprintf("Public URL: %s\n", lb.PublicURL)
if lb.Active {
msg += fmt.Sprintf("Active: ✅\n")
} else {
msg += fmt.Sprintf("Active: ❌\n")
}
if lb.HTTPS {
msg += fmt.Sprintf("HTTPS: ✅\n")
} else {
msg += fmt.Sprintf("HTTPS: ❌\n")
}
if lb.CertificateBody != "" {
msg += fmt.Sprintf("Certificate body:\n%s\n", lb.CertificateBody)
} else {
msg += fmt.Sprintf("Certificate body: ❌\n")
}
return msg, nil
msg += fmt.Sprintf("Public URL: %s\n", lb.PublicURL)
if lb.Active {
msg += fmt.Sprintf("Active: ✅\n")
} else {
msg += fmt.Sprintf("Active: ❌\n")
}
if lb.HTTPS {
msg += fmt.Sprintf("HTTPS: ✅\n")
} else {
msg += fmt.Sprintf("HTTPS: ❌\n")
}
if lb.CertificateBody != "" {
msg += fmt.Sprintf("Certificate body:\n%s\n", lb.CertificateBody)
} else {
msg += fmt.Sprintf("Certificate body: ❌\n")
}
return msg, nil
}
return "", errors.New(fmt.Sprintf("Load balancer with ID %d not found in project %s", lbID, projectToShow))
return "", errors.New(fmt.Sprintf("No load balancer found in project %s", projectToShow))
})
}

Expand Down
40 changes: 30 additions & 10 deletions command/lb-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import (
"errors"
"flag"
"fmt"
"os"
"regexp"
"strings"

"github.com/olekukonko/tablewriter"
"github.com/squarescale/squarescale-cli/squarescale"
"github.com/squarescale/squarescale-cli/ui"
)

// LBListCommand gets the URL of the load balancer associated to a projects and prints it on the standard output.
Expand Down Expand Up @@ -50,12 +54,21 @@ func (c *LBListCommand) Run(args []string) int {
return "", err
}

var msg string
tableString := &strings.Builder{}
table := tablewriter.NewWriter(tableString)
// reset by ui/table.go FormatTable function: table.SetAutoFormatHeaders(false)
// seems like this should be taken into account earlier than in the ui/table.go FormatTable function to have effect on fields
table.SetAutoWrapText(false)
table.SetHeader([]string{"Active", "Certificate Body", "HTTPS", "Public URL"})
var activeIcon string
var certBodyIcon string
var httpsIcon string
msg += fmt.Sprintf("ID\tActive\tCertificateBody\tHTTPS\tPublicURL\n")
msg += fmt.Sprintf("--\t------\t---------------\t-----\t---------\n")
extraMsg := ""
termType := os.Getenv("TERM_PROGRAM")
matchTerm, _ := regexp.MatchString(".*[Tt][Mm][Uu][Xx].*", termType)
if matchTerm {
extraMsg = "\n\nPlease note that as you are using Tmux the UTF-8 icons might not be displayed properly unless you used the `-u` option"
}
for _, lb := range loadBalancers {
if lb.Active {
activeIcon = "✅"
Expand All @@ -72,25 +85,32 @@ func (c *LBListCommand) Run(args []string) int {
} else {
httpsIcon = "❌"
}
msg += fmt.Sprintf("%d\t%s\t%s\t\t%s\t%s\n", lb.ID, activeIcon, certBodyIcon, httpsIcon, lb.PublicURL)
table.Append([]string{
activeIcon,
certBodyIcon,
httpsIcon,
lb.PublicURL,
})
}
return msg, nil
ui.FormatTable(table)

table.Render()
// Remove trailing \n and HT
return string(regexp.MustCompile(`[\n\x09][\n\x09]*$`).ReplaceAll([]byte(tableString.String()), []byte(""))) + extraMsg, nil
})
}

// Synopsis is part of cli.Command implementation.
func (c *LBListCommand) Synopsis() string {
return "Display project's public URL if available"
return "Display project's list of load balancers"
}

// Help is part of cli.Command implementation.
func (c *LBListCommand) Help() string {
helpText := `
usage: sqsc lb get [options]
usage: sqsc lb list [options]
Display load balancer state (enabled, disabled). In case the load
balancer is enabled, all the project containers are displayed together
with their ports.
Display load balancer list for given project.
`
return strings.TrimSpace(helpText + optionsFromFlags(c.flagSet))
}
18 changes: 11 additions & 7 deletions command/lb-set.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"strings"

"github.com/squarescale/squarescale-cli/squarescale"
Expand All @@ -22,7 +23,6 @@ func (c *LBSetCommand) Run(args []string) int {
endpoint := endpointFlag(c.flagSet)
projectUUID := projectUUIDFlag(c.flagSet)
projectName := projectNameFlag(c.flagSet)
loadBalancerID := loadBalancerIDFlag(c.flagSet)
disableArg := loadBalancerDisableFlag(c.flagSet)
certArg := certFlag(c.flagSet)
certChainArg := certChainFlag(c.flagSet)
Expand All @@ -39,10 +39,6 @@ func (c *LBSetCommand) Run(args []string) int {
return c.errorWithUsage(errors.New("Project name or uuid is mandatory"))
}

if *loadBalancerID == 0 {
return c.errorWithUsage(errors.New("Load balancer ID is mandatory"))
}

var cert, secretKey string
var certChain string

Expand Down Expand Up @@ -87,12 +83,20 @@ func (c *LBSetCommand) Run(args []string) int {
UUID = *projectUUID
}

loadBalancers, err := client.LoadBalancerGet(UUID)
if err != nil {
return "", err
}
if len(loadBalancers) != 1 {
log.Printf("Warning project %s has %d Load Balancers (only 1 expected)", UUID, len(loadBalancers))
}

if *disableArg {
err = client.LoadBalancerDisable(UUID, *loadBalancerID)
err = client.LoadBalancerDisable(UUID, loadBalancers[0].ID)
return fmt.Sprintf("Successfully disable load balancer for project '%s'", projectToShow), err
}

err = client.LoadBalancerEnable(UUID, *loadBalancerID, cert, certChain, secretKey)
err = client.LoadBalancerEnable(UUID, loadBalancers[0].ID, cert, certChain, secretKey)
return fmt.Sprintf("Successfully update load balancer for project '%s'", projectToShow), err
})

Expand Down

0 comments on commit 4ff205d

Please sign in to comment.