diff --git a/cmd/application_env_delete.go b/cmd/application_env_delete.go index 0f4cff7e..b7bfcef8 100644 --- a/cmd/application_env_delete.go +++ b/cmd/application_env_delete.go @@ -24,7 +24,7 @@ var applicationEnvDeleteCmd = &cobra.Command{ } client := utils.GetQoveryClient(tokenType, token) - _, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) if err != nil { utils.PrintlnError(err) @@ -49,7 +49,7 @@ var applicationEnvDeleteCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } - err = utils.DeleteByKey(client, projectId, envId, application.Id, utils.ApplicationType, utils.Key) + err = utils.DeleteVariable(client, application.Id, utils.ApplicationType, utils.Key) if err != nil { utils.PrintlnError(err) diff --git a/cmd/container_env_delete.go b/cmd/container_env_delete.go index 6d05aca7..9f0b4476 100644 --- a/cmd/container_env_delete.go +++ b/cmd/container_env_delete.go @@ -24,7 +24,7 @@ var containerEnvDeleteCmd = &cobra.Command{ } client := utils.GetQoveryClient(tokenType, token) - _, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) if err != nil { utils.PrintlnError(err) @@ -49,7 +49,7 @@ var containerEnvDeleteCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } - err = utils.DeleteByKey(client, projectId, envId, container.Id, utils.ContainerType, utils.Key) + err = utils.DeleteVariable(client, container.Id, utils.ContainerType, utils.Key) if err != nil { utils.PrintlnError(err) diff --git a/cmd/cronjob_env_delete.go b/cmd/cronjob_env_delete.go index 268e049a..ff8826b7 100644 --- a/cmd/cronjob_env_delete.go +++ b/cmd/cronjob_env_delete.go @@ -24,7 +24,7 @@ var cronjobEnvDeleteCmd = &cobra.Command{ } client := utils.GetQoveryClient(tokenType, token) - _, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) if err != nil { utils.PrintlnError(err) @@ -49,7 +49,7 @@ var cronjobEnvDeleteCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } - err = utils.DeleteByKey(client, projectId, envId, cronjob.CronJobResponse.Id, utils.JobType, utils.Key) + err = utils.DeleteVariable(client, cronjob.CronJobResponse.Id, utils.JobType, utils.Key) if err != nil { utils.PrintlnError(err) diff --git a/cmd/helm_env_delete.go b/cmd/helm_env_delete.go new file mode 100644 index 00000000..6cf77126 --- /dev/null +++ b/cmd/helm_env_delete.go @@ -0,0 +1,74 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var helmEnvDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete helm environment variable or secret", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + tokenType, token, err := utils.GetAccessToken() + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + client := utils.GetQoveryClient(tokenType, token) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + helms, _, err := client.HelmsAPI.ListHelms(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + helm := utils.FindByHelmName(helms.GetResults(), helmName) + + if helm == nil { + utils.PrintlnError(fmt.Errorf("helm %s not found", helmName)) + utils.PrintlnInfo("You can list all helms with: qovery helm list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.DeleteVariable(client, helm.Id, utils.HelmType, utils.Key) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("Variable %s has been deleted", pterm.FgBlue.Sprintf(utils.Key))) + }, +} + +func init() { + helmEnvCmd.AddCommand(helmEnvDeleteCmd) + helmEnvDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + helmEnvDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + helmEnvDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + helmEnvDeleteCmd.Flags().StringVarP(&helmName, "helm", "n", "", "helm Name") + helmEnvDeleteCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "Environment variable or secret key") + + _ = helmEnvDeleteCmd.MarkFlagRequired("key") + _ = helmEnvDeleteCmd.MarkFlagRequired("helm") +} diff --git a/cmd/lifecycle_env_delete.go b/cmd/lifecycle_env_delete.go index 092d186e..42429766 100644 --- a/cmd/lifecycle_env_delete.go +++ b/cmd/lifecycle_env_delete.go @@ -24,7 +24,7 @@ var lifecycleEnvDeleteCmd = &cobra.Command{ } client := utils.GetQoveryClient(tokenType, token) - _, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) if err != nil { utils.PrintlnError(err) @@ -49,7 +49,7 @@ var lifecycleEnvDeleteCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } - err = utils.DeleteByKey(client, projectId, envId, lifecycle.LifecycleJobResponse.Id, utils.JobType, utils.Key) + err = utils.DeleteVariable(client, lifecycle.LifecycleJobResponse.Id, utils.JobType, utils.Key) if err != nil { utils.PrintlnError(err) diff --git a/utils/env_var.go b/utils/env_var.go index 2c6f738c..e47dd62c 100644 --- a/utils/env_var.go +++ b/utils/env_var.go @@ -313,155 +313,25 @@ func ListSecrets( return res.Results, nil } -func DeleteEnvironmentVariableByKey( +func DeleteVariable( client *qovery.APIClient, - projectId string, - environmentId string, serviceId string, serviceType ServiceType, key string, ) error { + envVars, err := ListEnvironmentVariables(client, serviceId, serviceType) if err != nil { return err } envVar := FindEnvironmentVariableByKey(key, envVars) - if envVar == nil { return fmt.Errorf("environment variable %s not found", pterm.FgRed.Sprintf(key)) } - switch string(envVar.Scope) { - case "PROJECT": - _, err := client.ProjectEnvironmentVariableAPI.DeleteProjectEnvironmentVariable( - context.Background(), - projectId, - envVar.Id, - ).Execute() - - return err - case "ENVIRONMENT": - _, err := client.EnvironmentVariableAPI.DeleteEnvironmentEnvironmentVariable( - context.Background(), - environmentId, - envVar.Id, - ).Execute() - - return err - case "APPLICATION": - _, err := client.ApplicationEnvironmentVariableAPI.DeleteApplicationEnvironmentVariable( - context.Background(), - serviceId, - envVar.Id, - ).Execute() - - return err - case "JOB": - _, err := client.JobEnvironmentVariableAPI.DeleteJobEnvironmentVariable( - context.Background(), - serviceId, - envVar.Id, - ).Execute() - - return err - case "CONTAINER": - _, err := client.ContainerEnvironmentVariableAPI.DeleteContainerEnvironmentVariable( - context.Background(), - serviceId, - envVar.Id, - ).Execute() - - return err - } - - return errors.New("invalid scope") -} - -func DeleteSecretByKey( - client *qovery.APIClient, - projectId string, - environmentId string, - serviceId string, - serviceType ServiceType, - key string, -) error { - secrets, err := ListSecrets(client, serviceId, serviceType) - if err != nil { - return err - } - - secret := FindSecretByKey(key, secrets) - - if secret == nil { - return fmt.Errorf("secret %s not found", pterm.FgRed.Sprintf(key)) - } - - switch string(secret.Scope) { - case "PROJECT": - _, err := client.ProjectSecretAPI.DeleteProjectSecret( - context.Background(), - projectId, - secret.Id, - ).Execute() - - return err - case "ENVIRONMENT": - _, err := client.EnvironmentVariableAPI.DeleteEnvironmentEnvironmentVariable( - context.Background(), - environmentId, - secret.Id, - ).Execute() - - return err - case "APPLICATION": - _, err := client.ApplicationSecretAPI.DeleteApplicationSecret( - context.Background(), - serviceId, - secret.Id, - ).Execute() - - return err - case "JOB": - _, err := client.JobSecretAPI.DeleteJobSecret( - context.Background(), - serviceId, - secret.Id, - ).Execute() - - return err - case "CONTAINER": - _, err := client.ContainerSecretAPI.DeleteContainerSecret( - context.Background(), - serviceId, - secret.Id, - ).Execute() - - return err - } - - return errors.New("invalid scope") -} - -func DeleteByKey( - client *qovery.APIClient, - projectId string, - environmentId string, - serviceId string, - serviceType ServiceType, - key string, -) error { - err := DeleteEnvironmentVariableByKey(client, projectId, environmentId, serviceId, serviceType, key) - if err == nil { - return nil - } - - err = DeleteSecretByKey(client, projectId, environmentId, serviceId, serviceType, key) - if err == nil { - return nil - } - - return fmt.Errorf("environment variable or secret %s not found", pterm.FgRed.Sprintf(key)) + _, err = client.VariableMainCallsAPI.DeleteVariable(context.Background(), envVar.Id).Execute() + return err } func CreateEnvironmentVariableAlias(