Skip to content

Commit

Permalink
Show repository snapshot list (#417)
Browse files Browse the repository at this point in the history
* Implement Get,List,Delete method for snapshots
* Added test for snapshots
* Added doc for snapshot
* Remove unused snapshot handler.
  • Loading branch information
Md. Emruz Hossain authored and tamalsaha committed Apr 10, 2018
1 parent 6d8ef78 commit 5c6713f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 92 deletions.
55 changes: 55 additions & 0 deletions forget.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmds

import (
"fmt"

"github.com/appscode/kutil/meta"
cs "github.com/appscode/stash/client/clientset/versioned/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/registry/snapshot"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
)

func NewCmdForget() *cobra.Command {
var (
masterURL string
kubeconfigPath string
repositoryName string
)

cmd := &cobra.Command{
Use: "forget [snapshotID ...]",
Short: "Delete snapshots from a restic repository",
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
fmt.Errorf(err.Error())
}

stashClient := cs.NewForConfigOrDie(config)

if repositoryName == "" {
fmt.Errorf("repository name not found")
return
}
repo, err := stashClient.Repositories(meta.Namespace()).Get(repositoryName, metav1.GetOptions{})
if err != nil {
fmt.Errorf(err.Error())
return
}

r := snapshot.NewREST(config)
err = r.ForgetSnapshots(repo, args)
if err != nil {
fmt.Errorf(err.Error())
}
},
}
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
cmd.Flags().StringVar(&repositoryName, "repo-name", repositoryName, "Name of the Repository CRD.")

return cmd
}
2 changes: 2 additions & 0 deletions root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func NewRootCmd() *cobra.Command {
rootCmd.AddCommand(NewCmdRecover())
rootCmd.AddCommand(NewCmdCheck())
rootCmd.AddCommand(NewCmdScaleDown())
rootCmd.AddCommand(NewCmdSnapshots())
rootCmd.AddCommand(NewCmdForget())

return rootCmd
}
58 changes: 58 additions & 0 deletions snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package cmds

import (
"encoding/json"
"fmt"

"github.com/appscode/kutil/meta"
cs "github.com/appscode/stash/client/clientset/versioned/typed/stash/v1alpha1"
"github.com/appscode/stash/pkg/registry/snapshot"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
)

func NewCmdSnapshots() *cobra.Command {
var (
masterURL string
kubeconfigPath string
repositoryName string
)

cmd := &cobra.Command{
Use: "snapshots [snapshotID ...]",
Short: "Get snapshots of restic repo",
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
if err != nil {
fmt.Errorf(err.Error())
}

stashClient := cs.NewForConfigOrDie(config)

if repositoryName == "" {
fmt.Errorf("repository name not found")
return
}
repo, err := stashClient.Repositories(meta.Namespace()).Get(repositoryName, metav1.GetOptions{})
if err != nil {
fmt.Errorf(err.Error())
return
}

r := snapshot.NewREST(config)
snapshots, err := r.GetSnapshots(repo, args)
if err != nil {
fmt.Errorf(err.Error())
}
jsonSnaps, err := json.MarshalIndent(snapshots, "", " ")
fmt.Println(string(jsonSnaps))
},
}
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
cmd.Flags().StringVar(&repositoryName, "repo-name", repositoryName, "Name of the Repository CRD.")

return cmd
}
92 changes: 0 additions & 92 deletions snapshot_handler.go

This file was deleted.

0 comments on commit 5c6713f

Please sign in to comment.