Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Commit

Permalink
Added support for deployment names as well as ids
Browse files Browse the repository at this point in the history
  • Loading branch information
codepope committed Jun 6, 2018
1 parent c18e7b2 commit 047a43d
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 43 deletions.
12 changes: 8 additions & 4 deletions cmd/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ import (

// alertsCmd represents the alerts command
var alertsCmd = &cobra.Command{
Use: "alerts [deployment id]",
Use: "alerts [deployment id/name]",
Short: "Show Alerts for deployment",
Long: `Show the alerts for a deployment`,
Run: func(cmd *cobra.Command, args []string) {
c := getComposeAPI()
if len(args) == 0 {
log.Fatal("Need a deployment id")
log.Fatal("Need a deployment id/name")
}
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
if outputRaw {
text, errs := c.GetAlertsForDeploymentJSON(args[0])
text, errs := c.GetAlertsForDeploymentJSON(depid)
bailOnErrs(errs)
fmt.Println(text)
} else {
alerts, errs := c.GetAlertsForDeployment(args[0])
alerts, errs := c.GetAlertsForDeployment(depid)
bailOnErrs(errs)

if !outputJSON {
Expand Down
13 changes: 9 additions & 4 deletions cmd/backupget.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,25 @@ import (

// backupgetCmd represents the backups get command
var backupgetCmd = &cobra.Command{
Use: "get [deployment id] [backup id]",
Use: "get [deployment id] [backup id/name]",
Short: "Show Backup details for deployment",
Long: `Show the backup details for a deployment's backup`,
Run: func(cmd *cobra.Command, args []string) {
c := getComposeAPI()
if len(args) != 2 {
log.Fatal("Need a deployment id and a backup id")
log.Fatal("Need a deployment id/name and a backup id")
}
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
backupid := args[1]
if outputRaw {
text, errs := c.GetBackupDetailsForDeploymentJSON(args[0], args[1])
text, errs := c.GetBackupDetailsForDeploymentJSON(depid, backupid)
bailOnErrs(errs)
fmt.Println(text)
} else {
backup, errs := c.GetBackupDetailsForDeployment(args[0], args[1])
backup, errs := c.GetBackupDetailsForDeployment(depid, backupid)
bailOnErrs(errs)

if !outputJSON {
Expand Down
12 changes: 8 additions & 4 deletions cmd/backuplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ import (

// backuplistCmd represents the backups list command
var backuplistCmd = &cobra.Command{
Use: "list [deployment id]",
Use: "list [deployment id/name]",
Short: "Show Backups for deployment",
Long: `Show the backups for a deployment`,
Run: func(cmd *cobra.Command, args []string) {
c := getComposeAPI()
if len(args) == 0 {
log.Fatal("Need a deployment id")
log.Fatal("Need a deployment id/name")
}
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
if outputRaw {
text, errs := c.GetBackupsForDeploymentJSON(args[0])
text, errs := c.GetBackupsForDeploymentJSON(depid)
bailOnErrs(errs)
fmt.Println(text)
} else {
backups, errs := c.GetBackupsForDeployment(args[0])
backups, errs := c.GetBackupsForDeployment(depid)
bailOnErrs(errs)

if !outputJSON {
Expand Down
11 changes: 7 additions & 4 deletions cmd/backuprestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@ var restoressl bool

// backuprestoreCmd represents the backups restore command
var backuprestoreCmd = &cobra.Command{
Use: "restore [deployment id] [backup id] [new deployment name]",
Use: "restore [deployment id/name] [backup id] [new deployment name]",
Short: "Restore a deployment",
Long: `Restores a deployment. Requires deployment id, backup id, and new deployment name.`,
Long: `Restores a deployment. Requires deployment id/name, backup id, and new deployment name.`,
Run: func(cmd *cobra.Command, args []string) {
c := getComposeAPI()
if outputRaw {
log.Fatal("Raw mode not supported for backup restore")
}

if len(args) != 3 {
log.Fatal("Need deployment id, backup id and new deployment name")
log.Fatal("Need deployment id/name, backup id and new deployment name")
}

if restoredatacenterid == "" && restoreclusterid == "" {
log.Fatal("Must supply either a --cluster id or --datacenter region")
}

deploymentid := args[0]
deploymentid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
backupid := args[1]
deploymentname := args[2]

Expand Down
12 changes: 8 additions & 4 deletions cmd/backupstart.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ import (

// backupstartCmd represents the backups start command
var backupstartCmd = &cobra.Command{
Use: "start [deployment id]",
Use: "start [deployment id/name]",
Short: "Start backups for a deployment",
Long: `Start an on-demand backups for a deployment. Will return the recipe performing the backup.`,
Run: func(cmd *cobra.Command, args []string) {
c := getComposeAPI()
if len(args) == 0 {
log.Fatal("Need a deployment id")
log.Fatal("Need a deployment id/name")
}
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
if outputRaw {
text, errs := c.StartBackupForDeploymentJSON(args[0])
text, errs := c.StartBackupForDeploymentJSON(depid)
bailOnErrs(errs)
fmt.Println(text)
} else {
recipe, errs := c.StartBackupForDeployment(args[0])
recipe, errs := c.StartBackupForDeployment(depid)
bailOnErrs(errs)

if !outputJSON {
Expand Down
10 changes: 7 additions & 3 deletions cmd/cacert.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,19 @@ import (

// cacertCmd represents the cacert command
var cacertCmd = &cobra.Command{
Use: "cacert [deployment id]",
Use: "cacert [deployment id/name]",
Short: "Returns the self-signed cert for the deployment",
Long: `Returns the self-signed cert for the deployment`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Need a deployment id")
log.Fatal("Need a deployment id/name")
}
c := getComposeAPI()
deployment, errs := c.GetDeployment(args[0])
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
deployment, errs := c.GetDeployment(depid)
bailOnErrs(errs)
decoded, err := base64.StdEncoding.DecodeString(deployment.CACertificateBase64)
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions cmd/deprovision.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ import (

// deprovisionCmd represents the deprovision command
var deprovisionCmd = &cobra.Command{
Use: "deprovision [deployment id]",
Use: "deprovision [deployment id/name]",
Short: "Deprovision a deployment",
Long: `.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Need a deployment id")
log.Fatal("Need a deployment id/name")
}
c := getComposeAPI()
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
if outputRaw {
text, errs := c.DeprovisionDeploymentJSON(args[0])
text, errs := c.DeprovisionDeploymentJSON(depid)
bailOnErrs(errs)
fmt.Println(text)
} else {
recipe, errs := c.DeprovisionDeployment(args[0])
recipe, errs := c.DeprovisionDeployment(depid)
bailOnErrs(errs)

if !outputJSON {
Expand Down
12 changes: 8 additions & 4 deletions cmd/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ import (

// detailsCmd represents the details command
var detailsCmd = &cobra.Command{
Use: "details [deployment id]",
Use: "details [deployment id/name]",
Short: "Show details for a deployment",
Long: `Show the details for a deployment including connection strings and certificates`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Need a deployment id")
log.Fatal("Need a deployment id/name")
}
c := getComposeAPI()
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
if outputRaw {
text, errs := c.GetDeploymentJSON(args[0])
text, errs := c.GetDeploymentJSON(depid)
bailOnErrs(errs)
fmt.Println(text)
} else {
deployment, errs := c.GetDeployment(args[0])
deployment, errs := c.GetDeployment(depid)
bailOnErrs(errs)

if !outputJSON {
Expand Down
12 changes: 8 additions & 4 deletions cmd/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ import (

// scaleCmd represents the scale command
var scaleCmd = &cobra.Command{
Use: "scale [deployment id]",
Use: "scale [deployment id/name]",
Short: "Show scale information for a deployment",
Long: `Show Scale information (including unit size) for a deployment`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Need deployment id")
log.Fatal("Need deployment id/name")
}
c := getComposeAPI()
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
if outputRaw {
text, errs := c.GetScalingsJSON(args[0])
text, errs := c.GetScalingsJSON(depid)
bailOnErrs(errs)
fmt.Println(text)
} else {
scalings, errs := c.GetScalings(args[0])
scalings, errs := c.GetScalings(depid)
bailOnErrs(errs)

if !outputJSON {
Expand Down
11 changes: 7 additions & 4 deletions cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,21 @@ import (

// setCmd represents the scale set command
var setCmd = &cobra.Command{
Use: "set [deployment id] [scale in integer units]",
Use: "set [deployment id/name] [scale in integer units]",
Short: "Set scale for a deployment",
Long: `Sets the number of resource units (storage/memory) that should be available.`,

Run: func(cmd *cobra.Command, args []string) {
if len(args) != 2 {
log.Fatal("Need a deployment id and new units value")
log.Fatal("Need a deployment id/name and new units value")
}
c := getComposeAPI()

depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
params := composeAPI.ScalingsParams{}
params.DeploymentID = args[0]
params.DeploymentID = depid
scaleval, err := strconv.Atoi(args[1])
if err != nil {
log.Fatal("Scale units must be integer")
Expand Down
32 changes: 32 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package cmd

import (
"encoding/hex"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -41,6 +42,37 @@ func getComposeAPI() (client *composeAPI.Client) {
return client
}

func resolveDepID(client *composeAPI.Client, arg string) (depid string, err error) {
// Test for being just deployment id
if len(arg) == 24 && isHexString(arg) {
return arg, nil
}

// Get all the deployments and search
deployments, errs := client.GetDeployments()

if errs != nil {
bailOnErrs(errs)
return "", errs[0]
}

for _, deployment := range *deployments {
if deployment.Name == arg {
return deployment.ID, nil
}
}

return "", fmt.Errorf("deployment not found: %s", arg)
}

func isHexString(s string) bool {
_, err := hex.DecodeString(s)
if err == nil {
return true
}
return false
}

func watchRecipeTillComplete(client *composeAPI.Client, recipeid string) {
var lastRecipe *composeAPI.Recipe

Expand Down
12 changes: 8 additions & 4 deletions cmd/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ import (

// versionsCmd represents the versions command
var versionsCmd = &cobra.Command{
Use: "versions [deployment id]",
Use: "versions [deployment id/name]",
Short: "Show versions for deployment database",
Long: `Shows all available upgrade versions for the database installed within a deployment`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
log.Fatal("Need a deployment id to examine")
log.Fatal("Need a deployment id/name to examine")
}
c := getComposeAPI()
depid, err := resolveDepID(c, args[0])
if err != nil {
log.Fatal(err)
}
if outputRaw {
text, errs := c.GetVersionsForDeploymentJSON(args[0])
text, errs := c.GetVersionsForDeploymentJSON(depid)
bailOnErrs(errs)
fmt.Println(text)
} else {
versions, errs := c.GetVersionsForDeployment(args[0])
versions, errs := c.GetVersionsForDeployment(depid)
bailOnErrs(errs)
if !outputJSON {
for _, v := range *versions {
Expand Down

0 comments on commit 047a43d

Please sign in to comment.