From ffc612ac13cfcdb69ceb8ab899521fd0702e7cf6 Mon Sep 17 00:00:00 2001 From: Wayne Witzel III Date: Thu, 20 Sep 2018 09:53:24 -0400 Subject: [PATCH] Add volume snapshot CLI get command Signed-off-by: Wayne Witzel III --- pkg/cmd/ark/ark.go | 2 + pkg/cmd/cli/get/get.go | 5 ++ pkg/cmd/cli/snapshotlocation/get.go | 57 +++++++++++++++++++ .../cli/snapshotlocation/snapshot_location.go | 37 ++++++++++++ 4 files changed, 101 insertions(+) create mode 100644 pkg/cmd/cli/snapshotlocation/get.go create mode 100644 pkg/cmd/cli/snapshotlocation/snapshot_location.go diff --git a/pkg/cmd/ark/ark.go b/pkg/cmd/ark/ark.go index ddf4532db..c84526893 100644 --- a/pkg/cmd/ark/ark.go +++ b/pkg/cmd/ark/ark.go @@ -35,6 +35,7 @@ import ( "github.com/heptio/ark/pkg/cmd/cli/restic" "github.com/heptio/ark/pkg/cmd/cli/restore" "github.com/heptio/ark/pkg/cmd/cli/schedule" + "github.com/heptio/ark/pkg/cmd/cli/snapshotlocation" "github.com/heptio/ark/pkg/cmd/server" runplugin "github.com/heptio/ark/pkg/cmd/server/plugin" "github.com/heptio/ark/pkg/cmd/version" @@ -73,6 +74,7 @@ operations can also be performed as 'ark backup get' and 'ark schedule create'.` restic.NewCommand(f), bug.NewCommand(), backuplocation.NewCommand(f), + snapshotlocation.NewCommand(f), ) // add the glog flags diff --git a/pkg/cmd/cli/get/get.go b/pkg/cmd/cli/get/get.go index 27b3eee55..1bfc7c0f5 100644 --- a/pkg/cmd/cli/get/get.go +++ b/pkg/cmd/cli/get/get.go @@ -24,6 +24,7 @@ import ( "github.com/heptio/ark/pkg/cmd/cli/backuplocation" "github.com/heptio/ark/pkg/cmd/cli/restore" "github.com/heptio/ark/pkg/cmd/cli/schedule" + "github.com/heptio/ark/pkg/cmd/cli/snapshotlocation" ) func NewCommand(f client.Factory) *cobra.Command { @@ -45,11 +46,15 @@ func NewCommand(f client.Factory) *cobra.Command { backupLocationCommand := backuplocation.NewGetCommand(f, "backup-locations") backupLocationCommand.Aliases = []string{"backup-location"} + snapshotLocationCommand := snapshotlocation.NewGetCommand(f, "snapshot-locations") + snapshotLocationCommand.Aliases = []string{"snapshot-location"} + c.AddCommand( backupCommand, scheduleCommand, restoreCommand, backupLocationCommand, + snapshotLocationCommand, ) return c diff --git a/pkg/cmd/cli/snapshotlocation/get.go b/pkg/cmd/cli/snapshotlocation/get.go new file mode 100644 index 000000000..7b4108761 --- /dev/null +++ b/pkg/cmd/cli/snapshotlocation/get.go @@ -0,0 +1,57 @@ +/* +Copyright 2018 the Heptio Ark contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. + +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package snapshotlocation + +import ( + api "github.com/heptio/ark/pkg/apis/ark/v1" + "github.com/heptio/ark/pkg/client" + "github.com/heptio/ark/pkg/cmd" + "github.com/heptio/ark/pkg/cmd/util/output" + "github.com/spf13/cobra" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func NewGetCommand(f client.Factory, use string) *cobra.Command { + var listOptions metav1.ListOptions + c := &cobra.Command{ + Use: use, + Short: "Get snapshot locations", + Run: func(c *cobra.Command, args []string) { + err := output.ValidateFlags(c) + cmd.CheckError(err) + arkClient, err := f.Client() + cmd.CheckError(err) + var locations *api.VolumeSnapshotLocationList + if len(args) > 0 { + locations = new(api.VolumeSnapshotLocationList) + for _, name := range args { + location, err := arkClient.Ark().VolumeSnapshotLocations(f.Namespace()).Get(name, metav1.GetOptions{}) + cmd.CheckError(err) + locations.Items = append(locations.Items, *location) + } + } else { + locations, err = arkClient.ArkV1().VolumeSnapshotLocations(f.Namespace()).List(listOptions) + cmd.CheckError(err) + } + _, err = output.PrintWithFormat(c, locations) + cmd.CheckError(err) + }, + } + c.Flags().StringVarP(&listOptions.LabelSelector, "selector", "l", listOptions.LabelSelector, "only show items matching this label selector") + output.BindFlags(c.Flags()) + return c +} diff --git a/pkg/cmd/cli/snapshotlocation/snapshot_location.go b/pkg/cmd/cli/snapshotlocation/snapshot_location.go new file mode 100644 index 000000000..c7347e997 --- /dev/null +++ b/pkg/cmd/cli/snapshotlocation/snapshot_location.go @@ -0,0 +1,37 @@ +/* +Copyright 2018 the Heptio Ark contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package snapshotlocation + +import ( + "github.com/spf13/cobra" + + "github.com/heptio/ark/pkg/client" +) + +func NewCommand(f client.Factory) *cobra.Command { + c := &cobra.Command{ + Use: "snapshot-location", + Short: "Work with snapshot locations", + Long: "Work with snapshot locations", + } + + c.AddCommand( + NewGetCommand(f, "get"), + ) + + return c +}