Skip to content

Commit

Permalink
Added update backend CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
clintonk authored and kangarlou committed Apr 18, 2018
1 parent b00a576 commit edc6764
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 12 deletions.
3 changes: 3 additions & 0 deletions cli/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package cmd

import "github.com/spf13/cobra"

var filename string
var b64Data string

func init() {
RootCmd.AddCommand(createCmd)
}
Expand Down
7 changes: 2 additions & 5 deletions cli/cmd/create_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
"github.com/netapp/trident/frontend/rest"
)

var filename string
var b64Data string

func init() {
createCmd.AddCommand(createBackendCmd)
createBackendCmd.Flags().StringVarP(&filename, "filename", "f", "", "Path to YAML or JSON file")
Expand All @@ -33,7 +30,7 @@ var createBackendCmd = &cobra.Command{
Aliases: []string{"b"},
RunE: func(cmd *cobra.Command, args []string) error {

jsonData, err := getBackendCreateData()
jsonData, err := getBackendData()
if err != nil {
return err
}
Expand All @@ -48,7 +45,7 @@ var createBackendCmd = &cobra.Command{
},
}

func getBackendCreateData() ([]byte, error) {
func getBackendData() ([]byte, error) {

var err error
var rawData []byte
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/delete_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}

var deleteBackendCmd = &cobra.Command{
Use: "backend",
Use: "backend <name> [<name>...]",
Short: "Delete one or more storage backends from Trident",
Aliases: []string{"b", "backends"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/delete_storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}

var deleteStorageClassCmd = &cobra.Command{
Use: "storageclass",
Use: "storageclass <name> [<name>...]",
Short: "Delete one or more storage classes from Trident",
Aliases: []string{"sc", "storageclasses"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/delete_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}

var deleteVolumeCmd = &cobra.Command{
Use: "volume",
Use: "volume <name> [<name>...]",
Short: "Delete one or more storage volumes from Trident",
Aliases: []string{"b", "volumes"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/get_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func init() {
}

var getBackendCmd = &cobra.Command{
Use: "backend",
Use: "backend [<name>...]",
Short: "Get one or more storage backends from Trident",
Aliases: []string{"b", "backends"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/get_storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}

var getStorageClassCmd = &cobra.Command{
Use: "storageclass",
Use: "storageclass [<name>...]",
Short: "Get one or more storage classes from Trident",
Aliases: []string{"sc", "storageclasses"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/get_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func init() {
}

var getVolumeCmd = &cobra.Command{
Use: "volume",
Use: "volume [<name>...]",
Short: "Get one or more volumes from Trident",
Aliases: []string{"v", "volumes"},
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
18 changes: 18 additions & 0 deletions cli/cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2018 NetApp, Inc. All Rights Reserved.

package cmd

import "github.com/spf13/cobra"

func init() {
RootCmd.AddCommand(updateCmd)
}

var updateCmd = &cobra.Command{
Use: "update",
Short: "Modify a resource in Trident",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := discoverOperatingMode(cmd)
return err
},
}
93 changes: 93 additions & 0 deletions cli/cmd/update_backend.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2018 NetApp, Inc. All Rights Reserved.

package cmd

import (
"encoding/base64"
"encoding/json"
"errors"
"net/http"

"github.com/spf13/cobra"

"github.com/netapp/trident/cli/api"
"github.com/netapp/trident/frontend/rest"
)

func init() {
updateCmd.AddCommand(updateBackendCmd)
updateBackendCmd.Flags().StringVarP(&filename, "filename", "f", "", "Path to YAML or JSON file")
updateBackendCmd.Flags().StringVarP(&b64Data, "base64", "", "", "Base64 encoding")
updateBackendCmd.Flags().MarkHidden("base64")
}

var updateBackendCmd = &cobra.Command{
Use: "backend <name>",
Short: "Update a backend in Trident",
Aliases: []string{"b"},
RunE: func(cmd *cobra.Command, args []string) error {

jsonData, err := getBackendData()
if err != nil {
return err
}

if OperatingMode == ModeTunnel {
command := []string{
"update", "backend",
"--base64", base64.StdEncoding.EncodeToString(jsonData),
}
TunnelCommand(append(command, args...))
return nil
} else {
return backendUpdate(args, jsonData)
}
},
}

func backendUpdate(backendNames []string, postData []byte) error {

switch len(backendNames) {
case 0:
return errors.New("backend name not specified")
case 1:
break
default:
return errors.New("multiple backend names specified")
}

baseURL, err := GetBaseURL()
if err != nil {
return err
}

// Send the file to Trident
url := baseURL + "/backend/" + backendNames[0]

response, responseBody, err := api.InvokeRESTAPI("POST", url, postData, Debug)
if err != nil {
return err
} else if response.StatusCode != http.StatusOK {
return errors.New(response.Status)
}

var updateBackendResponse rest.UpdateBackendResponse
err = json.Unmarshal(responseBody, &updateBackendResponse)
if err != nil {
return err
}

backends := make([]api.Backend, 0, 1)
backendName := updateBackendResponse.BackendID

// Retrieve the updated backend and write to stdout
backend, err := GetBackend(baseURL, backendName)
if err != nil {
return err
}
backends = append(backends, backend)

WriteBackends(backends)

return nil
}
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const (
ContextKubernetes DriverContext = "kubernetes"

// Minimum and maximum supported Kubernetes versions
KubernetesVersionMin = "v1.6.0"
KubernetesVersionMin = "v1.5.0"
KubernetesVersionMax = "v1.10.0"
)

Expand Down

0 comments on commit edc6764

Please sign in to comment.