Skip to content

Commit

Permalink
Merge pull request #11 from github/typed_interface
Browse files Browse the repository at this point in the history
Make structs to replace strings in the API
  • Loading branch information
Nick Canzoneri authored Sep 17, 2018
2 parents b9829d5 + 71a4b14 commit 509cba4
Show file tree
Hide file tree
Showing 14 changed files with 582 additions and 501 deletions.
13 changes: 11 additions & 2 deletions cmd/vulcanizer/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"

"github.com/github/vulcanizer"
"github.com/spf13/cobra"
Expand All @@ -25,7 +26,11 @@ var cmdAllocationEnable = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)
response := v.SetAllocation("enable")
response, err := v.SetAllocation("enable")
if err != nil {
fmt.Printf("Error setting allocation: %s \n", err)
os.Exit(1)
}
fmt.Printf("Enabling allocation:\n")
fmt.Printf("Allocation set to %s\n", response)
},
Expand All @@ -38,7 +43,11 @@ var cmdAllocationDisable = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)
response := v.SetAllocation("disable")
response, err := v.SetAllocation("disable")
if err != nil {
fmt.Printf("Error setting allocation: %s \n", err)
os.Exit(1)
}
fmt.Printf("Disabling allocation:\n")
fmt.Printf("Allocation set to %s\n", response)
},
Expand Down
23 changes: 10 additions & 13 deletions cmd/vulcanizer/drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"os"
"strings"

"github.com/github/vulcanizer"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -38,19 +37,13 @@ var cmdDrainServer = &cobra.Command{
v := vulcanizer.NewClient(host, port)
fmt.Printf("drain server name is: %s\n", serverToDrain)

excludeSettings := v.GetClusterExcludeSettings()

//TODO @nickcanz - make DrainServer take the ExcludeSettings struct or do this work within DrainServer
var existingExcludes string
if len(excludeSettings.Names) == 0 {
existingExcludes = "None"
} else {
existingExcludes = strings.Join(excludeSettings.Names, ",")
excludedServers, err := v.DrainServer(serverToDrain)
if err != nil {
fmt.Printf("Error getting exclude settings: %s \n", err)
os.Exit(1)
}

excludedServers := v.DrainServer(serverToDrain, existingExcludes)

fmt.Printf("draining servers: %s\n", excludedServers)
fmt.Printf("draining servers: %+v\n", excludedServers)
},
}

Expand All @@ -61,7 +54,11 @@ var cmdDrainStatus = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)
excludeSettings := v.GetClusterExcludeSettings()
excludeSettings, err := v.GetClusterExcludeSettings()
if err != nil {
fmt.Printf("Error getting exclude settings: %s \n", err)
os.Exit(1)
}
fmt.Printf("drain status: %+v\n", excludeSettings)
},
}
16 changes: 12 additions & 4 deletions cmd/vulcanizer/fill.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ var cmdFillAll = &cobra.Command{
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)

excludeSettings := v.FillAll()
excludeSettings, err := v.FillAll()
if err != nil {
fmt.Printf("Error calling Elasticsearch: %s \n", err)
os.Exit(1)
}

fmt.Printf("Current allocation exclude settings: %+v\n", excludeSettings)
},
Expand All @@ -50,9 +54,13 @@ var cmdFillServer = &cobra.Command{
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)

serverFilling, excludedServers := v.FillOneServer(serverToFill)
excludeSettings, err := v.FillOneServer(serverToFill)
if err != nil {
fmt.Printf("Error calling Elasticsearch: %s \n", err)
os.Exit(1)
}

fmt.Printf("Server \"%s\" removed from allocation rules.\n", serverFilling)
fmt.Printf("Servers \"%s\" are still being excluded from allocation.\n", excludedServers)
fmt.Printf("Server \"%s\" removed from allocation rules.\n", serverToFill)
fmt.Printf("Current exclude settings: %+v\n", excludeSettings)
},
}
29 changes: 26 additions & 3 deletions cmd/vulcanizer/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"os"
"strconv"

"github.com/github/vulcanizer"
"github.com/spf13/cobra"
Expand All @@ -18,9 +20,30 @@ var cmdHealth = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)
caption, rows, headers := v.GetHealth()
health, err := v.GetHealth()

fmt.Println(caption)
fmt.Println(renderTable(rows, headers))
if err != nil {
fmt.Printf("Error getting cluster health: %s\n", err)
os.Exit(1)
}

fmt.Println(health[0].Message)

header := []string{"Cluster", "Status", "Relocating", "Initializing", "Unassigned", "Active %"}
rows := [][]string{}
for _, cluster := range health {
row := []string{
cluster.Cluster,
cluster.Status,
strconv.Itoa(cluster.RelocatingShards),
strconv.Itoa(cluster.InitializingShards),
strconv.Itoa(cluster.UnassignedShards),
cluster.ActiveShardsPercentage,
}

rows = append(rows, row)
}

fmt.Println(renderTable(rows, header))
},
}
27 changes: 26 additions & 1 deletion cmd/vulcanizer/indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"os"
"strconv"

"github.com/github/vulcanizer"
"github.com/spf13/cobra"
Expand All @@ -18,7 +20,30 @@ var cmdIndices = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)
rows, header := v.GetIndices()
indices, err := v.GetIndices()

if err != nil {
fmt.Printf("Error getting indices: %s\n", err)
os.Exit(1)
}

header := []string{"Health", "Status", "Name", "Primary Shards", "Replica Count", "Index", "Docs"}
rows := [][]string{}

for _, index := range indices {
row := []string{
index.Health,
index.Status,
index.Name,
strconv.Itoa(index.PrimaryShards),
strconv.Itoa(index.ReplicaCount),
index.IndexSize,
strconv.Itoa(index.DocumentCount),
}

rows = append(rows, row)
}

table := renderTable(rows, header)
fmt.Println(table)
},
Expand Down
24 changes: 22 additions & 2 deletions cmd/vulcanizer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"

"github.com/github/vulcanizer"
"github.com/spf13/cobra"
Expand All @@ -18,9 +19,28 @@ var cmdNodes = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)
rows, header := v.GetNodes()
table := renderTable(rows, header)
nodes, err := v.GetNodes()

if err != nil {
fmt.Printf("Error getting nodes: %s\n", err)
os.Exit(1)
}

header := []string{"Master", "Role", "Name", "Ip", "Id"}
rows := [][]string{}
for _, node := range nodes {
row := []string{
node.Master,
node.Role,
node.Name,
node.Ip,
node.Id,
}

rows = append(rows, row)
}

table := renderTable(rows, header)
fmt.Println(table)
},
}
33 changes: 31 additions & 2 deletions cmd/vulcanizer/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"

"github.com/github/vulcanizer"
"github.com/spf13/cobra"
Expand All @@ -11,15 +12,43 @@ func init() {
rootCmd.AddCommand(cmdSettings)
}

func printSettings(settings []vulcanizer.ClusterSetting, name string) {
if len(settings) == 0 {
fmt.Println(fmt.Sprintf("No %s are set.\n", name))
return
}

header := []string{name, "Value"}
rows := [][]string{}

for _, setting := range settings {
row := []string{
setting.Setting,
setting.Value,
}

rows = append(rows, row)
}

table := renderTable(rows, header)
fmt.Println(table)
}

var cmdSettings = &cobra.Command{
Use: "settings",
Short: "Display all the settings of the cluster.",
Long: `This command displays all the transient and persisent settings that have been set on the given cluster.`,
Run: func(cmd *cobra.Command, args []string) {
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)
rows, headers := v.GetSettings()
clusterSettings, err := v.GetSettings()

if err != nil {
fmt.Printf("Error getting settings: %s\n", err)
os.Exit(1)
}

fmt.Println(renderTable(rows, headers))
printSettings(clusterSettings.PersistentSettings, "persistent settings")
printSettings(clusterSettings.TransientSettings, "transient settings")
},
}
25 changes: 22 additions & 3 deletions cmd/vulcanizer/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"fmt"
"os"
"strings"
"time"

"github.com/github/vulcanizer"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -41,7 +43,7 @@ var cmdSnapshotStatus = &cobra.Command{
host, port := getConfiguration()
v := vulcanizer.NewClient(host, port)

snapshot, err := cmd.Flags().GetString("snapshot")
snapshotName, err := cmd.Flags().GetString("snapshot")
if err != nil {
fmt.Printf("Could not retrieve required argument: snapshot. Error: %s\n", err)
os.Exit(1)
Expand All @@ -53,7 +55,24 @@ var cmdSnapshotStatus = &cobra.Command{
os.Exit(1)
}

rows, headers := v.GetSnapshotStatus(repository, snapshot)
fmt.Println(renderTable(rows, headers))
snapshot, err := v.GetSnapshotStatus(repository, snapshotName)
if err != nil {
fmt.Printf("Error getting snapshot. Error: %s\n", err)
os.Exit(1)
}

duration, _ := time.ParseDuration(fmt.Sprintf("%dms", snapshot.DurationMillis))

results := [][]string{
[]string{"State", snapshot.State},
[]string{"Name", snapshot.Name},
[]string{"Indices", strings.Join(snapshot.Indices, ", ")},
[]string{"Started", snapshot.StartTime.Format(time.RFC3339)},
[]string{"Finished", snapshot.EndTime.Format(time.RFC3339)},
[]string{"Duration", fmt.Sprintf("%v", duration)},
[]string{"Shards", fmt.Sprintf("Successful shards: %d, failed shards: %d", snapshot.Shards.Successful, snapshot.Shards.Failed)},
}

fmt.Println(renderTable(results, []string{"Metric", "Value"}))
},
}
27 changes: 25 additions & 2 deletions cmd/vulcanizer/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"time"

"github.com/github/vulcanizer"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -32,8 +33,30 @@ var cmdSnapshots = &cobra.Command{
os.Exit(1)
}

rows, headers := v.GetSnapshots(repository)
snapshots, err := v.GetSnapshots(repository)
if err != nil {
fmt.Printf("Could not query snapshots. Error: %s\n", err)
os.Exit(1)
}

header := []string{"State", "Name", "Finished", "Duration"}

if len(snapshots) > 10 {
snapshots = snapshots[len(snapshots)-10:]
}

rows := [][]string{}
for _, snapshot := range snapshots {
duration, _ := time.ParseDuration(fmt.Sprintf("%dms", snapshot.DurationMillis))
row := []string{
snapshot.State,
snapshot.Name,
snapshot.EndTime.Format(time.RFC3339),
fmt.Sprintf("%v", duration),
}
rows = append(rows, row)
}

fmt.Println(renderTable(rows, headers))
fmt.Println(renderTable(rows, header))
},
}
Loading

0 comments on commit 509cba4

Please sign in to comment.