Skip to content

Commit

Permalink
feat: Be able to delete bunch of services
Browse files Browse the repository at this point in the history
  • Loading branch information
mzottola committed Oct 5, 2023
1 parent b4d942c commit 708970a
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 17 deletions.
66 changes: 63 additions & 3 deletions cmd/application_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var applicationDeleteCmd = &cobra.Command{
Expand All @@ -23,6 +26,18 @@ var applicationDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationName == "" && applicationNames == "" {
utils.PrintlnError(fmt.Errorf("use neither --application \"<app name>\" nor --applications \"<app1 name>, <app2 name>\""))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationName != "" && applicationNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --application and --applications at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

Expand All @@ -32,6 +47,48 @@ var applicationDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if applicationNames != "" {
// wait until service is ready
for {
if utils.IsEnvironmentInATerminalState(envId, client) {
break
}

utils.Println(fmt.Sprintf("Waiting for environment %s to be ready..", pterm.FgBlue.Sprintf(envId)))
time.Sleep(5 * time.Second)
}

applications, _, err := client.ApplicationsApi.ListApplication(context.Background(), envId).Execute()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

var serviceIds []string
for _, applicationName := range strings.Split(applicationNames, ",") {
trimmedApplicationName := strings.TrimSpace(applicationName)
serviceIds = append(serviceIds, utils.FindByApplicationName(applications.GetResults(), trimmedApplicationName).Id)
}

// stop multiple services
_, err = utils.DeleteServices(client, envId, serviceIds, utils.ApplicationType)
if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Deleting applications %s in progress..", pterm.FgBlue.Sprintf(applicationNames)))
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return
}

applications, _, err := client.ApplicationsApi.ListApplication(context.Background(), envId).Execute()

if err != nil {
Expand All @@ -51,6 +108,10 @@ var applicationDeleteCmd = &cobra.Command{

msg, err := utils.DeleteService(client, envId, application.Id, utils.ApplicationType, watchFlag)

if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
Expand All @@ -76,7 +137,6 @@ func init() {
applicationDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
applicationDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
applicationDeleteCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name")
applicationDeleteCmd.Flags().StringVarP(&applicationNames, "applications", "", "", "Application Names (comma separated) Example: --applications \"app1,app2,app3\"")
applicationDeleteCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch application status until it's ready or an error occurs")

_ = applicationDeleteCmd.MarkFlagRequired("application")
}
62 changes: 59 additions & 3 deletions cmd/container_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var containerDeleteCmd = &cobra.Command{
Expand All @@ -23,6 +26,18 @@ var containerDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if containerName == "" && containerNames == "" {
utils.PrintlnError(fmt.Errorf("use neither --container \"<container name>\" nor --containers \"<container1 name>, <container2 name>\""))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if containerName != "" && containerNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --container and --containers at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

Expand All @@ -32,6 +47,48 @@ var containerDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if containerNames != "" {
// wait until service is ready
for {
if utils.IsEnvironmentInATerminalState(envId, client) {
break
}

utils.Println(fmt.Sprintf("Waiting for environment %s to be ready..", pterm.FgBlue.Sprintf(envId)))
time.Sleep(5 * time.Second)
}

containers, _, err := client.ContainersApi.ListContainer(context.Background(), envId).Execute()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

var serviceIds []string
for _, containerName := range strings.Split(containerNames, ",") {
trimmedContainerName := strings.TrimSpace(containerName)
serviceIds = append(serviceIds, utils.FindByContainerName(containers.GetResults(), trimmedContainerName).Id)
}

_, err = utils.DeleteServices(client, envId, serviceIds, utils.ContainerType)

if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Deleting containers %s in progress..", pterm.FgBlue.Sprintf(containerNames)))
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return
}

containers, _, err := client.ContainersApi.ListContainer(context.Background(), envId).Execute()

if err != nil {
Expand Down Expand Up @@ -76,7 +133,6 @@ func init() {
containerDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
containerDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
containerDeleteCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name")
containerDeleteCmd.Flags().StringVarP(&containerNames, "containers", "", "", "Container Names (comma separated) (ex: --containers \"container1,container2\")")
containerDeleteCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch container status until it's ready or an error occurs")

_ = containerDeleteCmd.MarkFlagRequired("container")
}
65 changes: 61 additions & 4 deletions cmd/cronjob_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package cmd

import (
"fmt"
"github.com/pterm/pterm"
"os"
"strings"
"time"

"github.com/pterm/pterm"

"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var cronjobDeleteCmd = &cobra.Command{
Expand All @@ -22,6 +26,18 @@ var cronjobDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if cronjobName == "" && cronjobNames == "" {
utils.PrintlnError(fmt.Errorf("use neither --cronjob \"<cronjob name>\" nor --cronjobs \"<cronjob1 name>, <cronjob2 name>\""))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if cronjobName != "" && cronjobNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --cronjob and --cronjobs at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

Expand All @@ -31,6 +47,48 @@ var cronjobDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if cronjobNames != "" {
// wait until service is ready
for {
if utils.IsEnvironmentInATerminalState(envId, client) {
break
}

utils.Println(fmt.Sprintf("Waiting for environment %s to be ready..", pterm.FgBlue.Sprintf(envId)))
time.Sleep(5 * time.Second)
}

cronjobs, err := ListCronjobs(envId, client)

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

var serviceIds []string
for _, cronjobName := range strings.Split(cronjobNames, ",") {
trimmedCronjobName := strings.TrimSpace(cronjobName)
serviceIds = append(serviceIds, utils.FindByJobName(cronjobs, trimmedCronjobName).Id)
}

_, err = utils.DeleteServices(client, envId, serviceIds, utils.JobType)

if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Deleting cronjobs %s in progress..", pterm.FgBlue.Sprintf(cronjobNames)))
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return
}

cronjobs, err := ListCronjobs(envId, client)

if err != nil {
Expand Down Expand Up @@ -75,7 +133,6 @@ func init() {
cronjobDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
cronjobDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
cronjobDeleteCmd.Flags().StringVarP(&cronjobName, "cronjob", "n", "", "Cronjob Name")
cronjobDeleteCmd.Flags().StringVarP(&cronjobNames, "cronjobs", "", "", "Cronjob Names (comma separated) (ex: --cronjobs \"cron1,cron2\")")
cronjobDeleteCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch cronjob status until it's ready or an error occurs")

_ = cronjobDeleteCmd.MarkFlagRequired("cronjob")
}
63 changes: 60 additions & 3 deletions cmd/database_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
"fmt"
"os"
"strings"
"time"

"github.com/pterm/pterm"
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"

"github.com/qovery/qovery-cli/utils"
)

var databaseDeleteCmd = &cobra.Command{
Expand All @@ -23,6 +26,18 @@ var databaseDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if databaseName == "" && databaseNames == "" {
utils.PrintlnError(fmt.Errorf("use neither --database \"<database name>\" nor --databases \"<database1 name>, <database2 name>\""))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if databaseName != "" && databaseNames != "" {
utils.PrintlnError(fmt.Errorf("you can't use --database and --databases at the same time"))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

client := utils.GetQoveryClient(tokenType, token)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

Expand All @@ -32,6 +47,49 @@ var databaseDeleteCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if databaseNames != "" {
// wait until service is ready
for {
if utils.IsEnvironmentInATerminalState(envId, client) {
break
}

utils.Println(fmt.Sprintf("Waiting for environment %s to be ready..", pterm.FgBlue.Sprintf(envId)))
time.Sleep(5 * time.Second)
}

databases, _, err := client.DatabasesApi.ListDatabase(context.Background(), envId).Execute()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

var serviceIds []string
for _, databaseName := range strings.Split(databaseNames, ",") {
trimmedDatabaseName := strings.TrimSpace(databaseName)
serviceIds = append(serviceIds, utils.FindByDatabaseName(databases.GetResults(), trimmedDatabaseName).Id)
}

// stop multiple services
_, err = utils.DeleteServices(client, envId, serviceIds, utils.DatabaseType)

if watchFlag {
utils.WatchEnvironment(envId, "unused", client)
} else {
utils.Println(fmt.Sprintf("Deleting databases %s in progress..", pterm.FgBlue.Sprintf(databaseNames)))
}

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return
}

databases, _, err := client.DatabasesApi.ListDatabase(context.Background(), envId).Execute()

if err != nil {
Expand Down Expand Up @@ -76,7 +134,6 @@ func init() {
databaseDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
databaseDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
databaseDeleteCmd.Flags().StringVarP(&databaseName, "database", "n", "", "Database Name")
databaseDeleteCmd.Flags().StringVarP(&databaseNames, "databases", "", "", "Database Names (comma separated) Example: --databases \"db1,db2\"")
databaseDeleteCmd.Flags().BoolVarP(&watchFlag, "watch", "w", false, "Watch database status until it's ready or an error occurs")

_ = databaseDeleteCmd.MarkFlagRequired("database")
}
Loading

0 comments on commit 708970a

Please sign in to comment.