mirror of
https://github.com/rancher/cli.git
synced 2026-02-05 09:48:36 +01:00
Add delete methods to cluster and project
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"github.com/rancher/cli/cliclient"
|
||||
managementClient "github.com/rancher/types/client/management/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
@@ -47,14 +46,14 @@ func ClusterCommand() cli.Command {
|
||||
Name: "import",
|
||||
Usage: "Import an existing Kubernetes cluster into a Rancher cluster",
|
||||
Description: importDescription,
|
||||
ArgsUsage: "[NEWCLUSTERNAME...]",
|
||||
ArgsUsage: "[CLUSTERID]",
|
||||
Action: clusterImport,
|
||||
},
|
||||
{
|
||||
Name: "add-node",
|
||||
Usage: "Returns the command needed to add a node to an existing Rancher cluster",
|
||||
ArgsUsage: "[CLUSTERNAME]",
|
||||
Action: getDockerCommand,
|
||||
ArgsUsage: "[CLUSTERID]",
|
||||
Action: clusterAddNode,
|
||||
Flags: []cli.Flag{
|
||||
cli.StringSliceFlag{
|
||||
Name: "label",
|
||||
@@ -74,6 +73,13 @@ func ClusterCommand() cli.Command {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "delete",
|
||||
Aliases: []string{"rm"},
|
||||
Usage: "Delete a cluster",
|
||||
ArgsUsage: "[CLUSTERID]",
|
||||
Action: deleteCluster,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -93,6 +99,7 @@ func clusterLs(ctx *cli.Context) error {
|
||||
{"ID", "Cluster.ID"},
|
||||
{"NAME", "Cluster.Name"},
|
||||
{"STATE", "Cluster.State"},
|
||||
{"DESCRIPTION", "Cluster.Description"},
|
||||
}, ctx)
|
||||
|
||||
defer writer.Close()
|
||||
@@ -137,7 +144,7 @@ func clusterImport(ctx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cluster, err := getClusterByName(ctx, c, ctx.Args().First())
|
||||
cluster, err := getClusterByID(ctx, c, ctx.Args().First())
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
@@ -147,13 +154,13 @@ func clusterImport(ctx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
//FIXME probably need more info here
|
||||
logrus.Printf("Run the following command in your cluster: %v", clusterToken.Command)
|
||||
fmt.Printf("Run the following command in your cluster: %v", clusterToken.Command)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getDockerCommand prints the command needed to add a node to a cluster
|
||||
func getDockerCommand(ctx *cli.Context) error {
|
||||
// clusterAddNode prints the command needed to add a node to a cluster
|
||||
func clusterAddNode(ctx *cli.Context) error {
|
||||
var clusterName string
|
||||
|
||||
if ctx.NArg() == 0 {
|
||||
@@ -167,7 +174,7 @@ func getDockerCommand(ctx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
cluster, err := getClusterByName(ctx, c, clusterName)
|
||||
cluster, err := getClusterByID(ctx, c, clusterName)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
@@ -205,6 +212,29 @@ func getDockerCommand(ctx *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteCluster(ctx *cli.Context) error {
|
||||
if ctx.NArg() == 0 {
|
||||
return errors.New(clusterNameError)
|
||||
}
|
||||
|
||||
c, err := GetClient(ctx)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
cluster, err := getClusterByID(ctx, c, ctx.Args().First())
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.ManagementClient.Cluster.Delete(&cluster)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// getClusterRegToken will return an existing token or create one if none exist
|
||||
func getClusterRegToken(
|
||||
ctx *cli.Context,
|
||||
@@ -232,22 +262,22 @@ func getClusterRegToken(
|
||||
return clusterTokenCollection.Data[0], nil
|
||||
}
|
||||
|
||||
func getClusterByName(
|
||||
func getClusterByID(
|
||||
ctx *cli.Context,
|
||||
c *cliclient.MasterClient,
|
||||
clusterName string,
|
||||
clusterID string,
|
||||
) (managementClient.Cluster, error) {
|
||||
opts := defaultListOpts(ctx)
|
||||
opts.Filters["name"] = clusterName
|
||||
|
||||
clusterCollection, err := c.ManagementClient.Cluster.List(opts)
|
||||
clusterCollection, err := c.ManagementClient.Cluster.List(defaultListOpts(ctx))
|
||||
if nil != err {
|
||||
return managementClient.Cluster{}, err
|
||||
}
|
||||
|
||||
if len(clusterCollection.Data) == 0 {
|
||||
return managementClient.Cluster{}, fmt.Errorf("no cluster found with the name [%s], run "+
|
||||
"`rancher clusters` to see available clusters", clusterName)
|
||||
for _, cluster := range clusterCollection.Data {
|
||||
if cluster.ID == clusterID {
|
||||
return cluster, nil
|
||||
}
|
||||
}
|
||||
return clusterCollection.Data[0], nil
|
||||
|
||||
return managementClient.Cluster{}, fmt.Errorf("no cluster found with the ID [%s], run "+
|
||||
"`rancher clusters` to see available clusters", clusterID)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@ package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/cli/cliclient"
|
||||
managementClient "github.com/rancher/types/client/management/v3"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
@@ -33,12 +35,24 @@ func ProjectCommand() cli.Command {
|
||||
ArgsUsage: "[NEWPROJECTNAME...]",
|
||||
Action: projectCreate,
|
||||
},
|
||||
{
|
||||
Name: "delete",
|
||||
Aliases: []string{"rm"},
|
||||
Usage: "Delete a project by ID",
|
||||
ArgsUsage: "[PROJECTID]",
|
||||
Action: deleteProject,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func projectLs(ctx *cli.Context) error {
|
||||
collection, err := getProjectList(ctx)
|
||||
c, err := GetClient(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
collection, err := getProjectList(ctx, c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -47,6 +61,7 @@ func projectLs(ctx *cli.Context) error {
|
||||
{"ID", "Project.ID"},
|
||||
{"NAME", "Project.Name"},
|
||||
{"STATE", "Project.State"},
|
||||
{"DESCRIPTION", "Project.Description"},
|
||||
}, ctx)
|
||||
|
||||
defer writer.Close()
|
||||
@@ -61,22 +76,22 @@ func projectLs(ctx *cli.Context) error {
|
||||
}
|
||||
|
||||
func projectCreate(ctx *cli.Context) error {
|
||||
if ctx.NArg() == 0 {
|
||||
return errors.New("project name is required")
|
||||
}
|
||||
|
||||
config, err := lookupConfig(ctx)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
c, err := GetClient(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ctx.NArg() == 0 {
|
||||
return errors.New("name is required")
|
||||
}
|
||||
|
||||
name := ctx.Args().First()
|
||||
newProj := &managementClient.Project{
|
||||
Name: name,
|
||||
Name: ctx.Args().First(),
|
||||
ClusterId: strings.Split(config.Project, ":")[0],
|
||||
}
|
||||
|
||||
@@ -84,15 +99,64 @@ func projectCreate(ctx *cli.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getProjectList(ctx *cli.Context) (*managementClient.ProjectCollection, error) {
|
||||
func deleteProject(ctx *cli.Context) error {
|
||||
if ctx.NArg() == 0 {
|
||||
return errors.New("project name is required")
|
||||
}
|
||||
|
||||
c, err := GetClient(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
project, err := getProjectByID(ctx, c, ctx.Args().First())
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
err = c.ManagementClient.Project.Delete(&project)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getProjectList(
|
||||
ctx *cli.Context,
|
||||
c *cliclient.MasterClient,
|
||||
) (*managementClient.ProjectCollection, error) {
|
||||
config, err := lookupConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
collection, err := c.ManagementClient.Project.List(defaultListOpts(ctx))
|
||||
filter := defaultListOpts(ctx)
|
||||
filter.Filters["clusterId"] = config.FocusedCluster()
|
||||
|
||||
collection, err := c.ManagementClient.Project.List(filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
func getProjectByID(
|
||||
ctx *cli.Context,
|
||||
c *cliclient.MasterClient,
|
||||
projectID string,
|
||||
) (managementClient.Project, error) {
|
||||
projectCollection, err := getProjectList(ctx, c)
|
||||
if nil != err {
|
||||
return managementClient.Project{}, err
|
||||
}
|
||||
|
||||
for _, project := range projectCollection.Data {
|
||||
if project.ID == projectID {
|
||||
return project, nil
|
||||
}
|
||||
}
|
||||
|
||||
return managementClient.Project{}, fmt.Errorf("no project found with the ID [%s], run "+
|
||||
"`rancher projects` to see available projects", projectID)
|
||||
}
|
||||
|
||||
@@ -51,6 +51,10 @@ func (c Config) FocusedServer() *ServerConfig {
|
||||
return c.Servers[c.CurrentServer]
|
||||
}
|
||||
|
||||
func (c ServerConfig) FocusedCluster() string {
|
||||
return strings.Split(c.Project, ":")[0]
|
||||
}
|
||||
|
||||
func (c ServerConfig) EnvironmentURL() (string, error) {
|
||||
url, err := baseURL(c.URL)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user