Skip to content

Commit

Permalink
feat: add helm env delete
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Dec 22, 2023
1 parent 4343ff9 commit 7facc16
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 142 deletions.
4 changes: 2 additions & 2 deletions cmd/application_env_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/container_env_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cronjob_env_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions cmd/helm_env_delete.go
Original file line number Diff line number Diff line change
@@ -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")
}
4 changes: 2 additions & 2 deletions cmd/lifecycle_env_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
138 changes: 4 additions & 134 deletions utils/env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7facc16

Please sign in to comment.