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

Commit

Permalink
Add 'delete app' command
Browse files Browse the repository at this point in the history
  • Loading branch information
Wulf-Thilo Schreiter committed Apr 19, 2020
1 parent e0c1da7 commit a166a01
Show file tree
Hide file tree
Showing 34 changed files with 315 additions and 13,353 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go:
- 1.12.x
- 1.14.x
env:
- GO111MODULE=on
install: true
Expand Down
75 changes: 75 additions & 0 deletions cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2020 Bitgrip <[email protected]>
//
// 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 delete

import (
"github.com/bitgrip/cattlectl/internal/pkg/config"
"github.com/bitgrip/cattlectl/internal/pkg/ctl"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
validArgs = []string{"app"}
deleteCmd = &cobra.Command{
Use: "delete TYPE NAME",
Short: "Deletes an rancher resouce",
Long: "Deletes an rancher resouce",
Run: delete,
ValidArgs: validArgs,
}
rootConfig config.Config
initCommand = func() {}
)

// BaseCommand is accessor to the package base command
func BaseCommand(config config.Config, init func()) *cobra.Command {
rootConfig = config
initCommand = init
return deleteCmd
}

func delete(cmd *cobra.Command, args []string) {
if len(args) != 2 {
logrus.Warn(cmd.UsageString())
return
}
resouceType := args[0]
resourceName := args[1]
projectName := viper.GetString("rancher.project_name")
logrus.
WithField("project-name", projectName).
WithField("resouce-type", resouceType).
WithField("resouce-name", resourceName).
WithField("cluster-name", rootConfig.ClusterName()).
Info("Delete project resouce")
err := ctl.DeleteProjectResouce(projectName, resouceType, resourceName, rootConfig)
if err != nil {
logrus.
WithField("project-name", projectName).
WithField("resouce-type", resouceType).
WithField("resouce-name", resourceName).
WithField("cluster-name", rootConfig.ClusterName()).
Fatal(err)
}
}

func init() {

deleteCmd.PersistentFlags().String("project-name", "", "The name of the project to delete resouces from")
viper.BindPFlag("rancher.project_name", deleteCmd.PersistentFlags().Lookup("project-name"))
viper.BindEnv("rancher.project_name", "RANCHER_PROJECT_NAME")
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"

"github.com/bitgrip/cattlectl/cmd/apply"
"github.com/bitgrip/cattlectl/cmd/delete"
"github.com/bitgrip/cattlectl/cmd/show"
homedir "github.com/mitchellh/go-homedir"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -87,6 +88,7 @@ func init() {
viper.BindEnv("rancher.cluster_name", "RANCHER_CLUSTER_NAME")

rootCmd.AddCommand(apply.BaseCommand(rancherConfig, initSubCommand))
rootCmd.AddCommand(delete.BaseCommand(rancherConfig, initSubCommand))
rootCmd.AddCommand(show.BaseCommand(rancherConfig, initSubCommand))
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(genDocCmd)
Expand Down
1 change: 1 addition & 0 deletions docs/cattlectl.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ deployement, if you run cattlectl twice.

* [cattlectl apply](cattlectl_apply.md) - Apply a project descriptor to your rancher
* [cattlectl completion](cattlectl_completion.md) - Generates bash completion scripts
* [cattlectl delete](cattlectl_delete.md) - Deletes an rancher resouce
* [cattlectl gen-doc](cattlectl_gen-doc.md) - genrates the markdown documentation
* [cattlectl show](cattlectl_show.md) - Show the resulting project descriptor
* [cattlectl version](cattlectl_version.md) - version of cattlectl
Expand Down
37 changes: 37 additions & 0 deletions docs/cattlectl_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
## cattlectl delete

Deletes an rancher resouce

### Synopsis

Deletes an rancher resouce

```
cattlectl delete TYPE NAME [flags]
```

### Options

```
-h, --help help for delete
--project-name string The name of the project to delete resouces from
```

### Options inherited from parent commands

```
--access-key string The access key to access rancher with
--cluster-id string The ID of the cluster the project is part of
--cluster-name string The name of the cluster the project is part of
--config string config file (default is $HOME/.cattlectl.yaml)
--insecure-api If Rancher uses a self signed certificate
--log-json if to log using json format
--rancher-url string The URL to reach the rancher
--secret-key string The secret key to access rancher with
-v, --verbosity int verbosity level to use
```

### SEE ALSO

* [cattlectl](cattlectl.md) - controll your cattle on the ranch

60 changes: 2 additions & 58 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,80 +1,24 @@
module github.com/bitgrip/cattlectl

go 1.14

require (
cloud.google.com/go v0.39.0 // indirect
github.com/766b/go-outliner v0.0.0-20180511142203-fc6edecdadd7 // indirect
github.com/Masterminds/semver v1.4.2
github.com/NYTimes/gziphandler v1.1.1 // indirect
github.com/OneOfOne/xxhash v1.2.5 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/dgryski/go-sip13 v0.0.0-20190329191031-25c5027a8c7b // indirect
github.com/docker/spdystream v0.0.0-20181023171402-6480d4af844c // indirect
github.com/elazarl/goproxy v0.0.0-20190421051319-9d40249d3c2f // indirect
github.com/emicklei/go-restful v2.9.6+incompatible // indirect
github.com/evanphx/json-patch v4.2.0+incompatible // indirect
github.com/go-openapi/jsonpointer v0.19.0 // indirect
github.com/go-openapi/jsonreference v0.19.0 // indirect
github.com/go-openapi/spec v0.19.0 // indirect
github.com/go-openapi/swag v0.19.0 // indirect
github.com/golang/mock v1.3.1 // indirect
github.com/google/gofuzz v1.0.0 // indirect
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/googleapis/gnostic v0.2.0 // indirect
github.com/hhatto/gocloc v0.2.1 // indirect
github.com/jessevdk/go-flags v1.4.0 // indirect
github.com/json-iterator/go v1.1.6 // indirect
github.com/kisielk/errcheck v1.2.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pty v1.1.4 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mailru/easyjson v0.0.0-20190403194419-1ea4449da983 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/munnerz/goautoneg v0.0.0-20190414153302-2ae31c8b6b30 // indirect
github.com/onsi/ginkgo v1.8.0 // indirect
github.com/onsi/gomega v1.5.0 // indirect
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/common v0.4.1 // indirect
github.com/prometheus/procfs v0.0.2 // indirect
github.com/prometheus/tsdb v0.8.0 // indirect
github.com/rancher/norman v0.0.0-20190529014815-f5744043a6fb
github.com/rancher/types v0.0.0-20190605225901-9125ab265f1b
github.com/rogpeppe/fastuuid v1.1.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sergi/go-diff v1.0.0
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/afero v1.2.2
github.com/spf13/cobra v0.0.4
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.4.0
github.com/stretchr/objx v0.2.0 // indirect
github.com/toqueteos/trie v1.0.0 // indirect
github.com/ugorji/go v1.1.5-pre // indirect
go.opencensus.io v0.22.0 // indirect
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 // indirect
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522 // indirect
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff // indirect
golang.org/x/lint v0.0.0-20190409202823-959b441ac422 // indirect
golang.org/x/mobile v0.0.0-20190509164839-32b2708ab171 // indirect
golang.org/x/mod v0.1.0 // indirect
golang.org/x/net v0.0.0-20190603091049-60506f45cf65 // indirect
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 // indirect
golang.org/x/sys v0.0.0-20190606122018-79a91cf218c4 // indirect
golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b // indirect
google.golang.org/appengine v1.6.0 // indirect
google.golang.org/genproto v0.0.0-20190605220351-eb0b1bdb6ae6 // indirect
google.golang.org/grpc v1.21.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/src-d/enry.v1 v1.7.3 // indirect
gopkg.in/toqueteos/substring.v1 v1.0.2 // indirect
gopkg.in/yaml.v2 v2.2.2
honnef.co/go/tools v0.0.0-20190605142022-0a11fc526260 // indirect
k8s.io/apimachinery v0.0.0-20190602183612-63a6072eb563
k8s.io/gengo v0.0.0-20190327210449-e17681d19d3a // indirect
k8s.io/kube-openapi v0.0.0-20190603182131-db7b694dc208 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fq
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db h1:6/JqlYfC1CCaLnGceQTI+sDGhC9UBSPAsBqI0Gun6kU=
golang.org/x/text v0.3.1-0.20181227161524-e6919f6577db/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions internal/pkg/ctl/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// 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 ctl

import (
"fmt"

"github.com/bitgrip/cattlectl/internal/pkg/config"
"github.com/sirupsen/logrus"
)

var (
deletableProjectResouceTypes = map[string]func(string, string, config.Config) error{
"app": deleteApp,
}
)

func DeleteProjectResouce(projectName, resouceType, name string, config config.Config) (err error) {
deleteFunc, supportedType := deletableProjectResouceTypes[resouceType]
if !supportedType {
return fmt.Errorf("Not supported resouce type [%s]", resouceType)
}
return deleteFunc(projectName, name, config)
}

func deleteApp(projectName, name string, config config.Config) (err error) {
_, _, projectClient, err := getProjectClient(projectName, config)
if err != nil {
return
}

app, err := projectClient.App(name)
if err != nil {
return
}

if exists, err := app.Exists(); err != nil || !exists {
if err != nil {
return err
}
logrus.
WithField("project-name", projectName).
WithField("resouce-name", name).
WithField("cluster-name", config.ClusterName()).
Info("No app skip delete")
return nil
}

err = app.Delete()

return
}
66 changes: 66 additions & 0 deletions internal/pkg/ctl/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// 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 ctl

import (
"fmt"

"github.com/bitgrip/cattlectl/internal/pkg/config"
rancher_client "github.com/bitgrip/cattlectl/internal/pkg/rancher/client"
)

var (
getRancherClient = doGetRancherClient
getClusterClient = doGetClusterClient
getProjectClient = doGetProjectClient
)

func doGetProjectClient(projectName string, config config.Config) (rancherClient rancher_client.RancherClient, clusterClient rancher_client.ClusterClient, projectClient rancher_client.ProjectClient, err error) {
rancherClient, clusterClient, err = getClusterClient(config)
if err != nil {
return
}
projectClient, err = clusterClient.Project(projectName)
if err != nil {
return
}
if exists, err := projectClient.Exists(); err != nil || !exists {
if err != nil {
return nil, nil, nil, err
}
return nil, nil, nil, fmt.Errorf("Project not found [%s]", projectName)
}
return
}

func doGetClusterClient(config config.Config) (rancherClient rancher_client.RancherClient, clusterClient rancher_client.ClusterClient, err error) {
rancherClient, err = getRancherClient(config)
if err != nil {
return
}
clusterClient, err = rancherClient.Cluster(config.ClusterName())
if err != nil {
return
}
return
}

func doGetRancherClient(config config.Config) (rancherClient rancher_client.RancherClient, err error) {
rancherClient, err = newRancherClient(rancher_client.RancherConfig{
RancherURL: config.RancherURL(),
AccessKey: config.AccessKey(),
SecretKey: config.SecretKey(),
Insecure: config.InsecureAPI(),
CACerts: config.CACerts(),
MergeAnswers: config.MergeAnswers(),
})
if err != nil {
return
}
return
}
Loading

0 comments on commit a166a01

Please sign in to comment.