From 4343ff9a28585cc10a0bd7e3ba98675766a518a3 Mon Sep 17 00:00:00 2001 From: Pierre Gerbelot Date: Fri, 22 Dec 2023 11:24:33 +0100 Subject: [PATCH] feat: add helm env create --- cmd/application_env_create.go | 7 ++- cmd/container_env_create.go | 7 ++- cmd/cronjob_env_create.go | 7 ++- cmd/helm_env_create.go | 92 ++++++++++++++++++++++++++++ cmd/lifecycle_env_create.go | 7 ++- utils/env_var.go | 109 ++++++---------------------------- 6 files changed, 127 insertions(+), 102 deletions(-) create mode 100644 cmd/helm_env_create.go diff --git a/cmd/application_env_create.go b/cmd/application_env_create.go index 274b2ebe..b46a7ef9 100644 --- a/cmd/application_env_create.go +++ b/cmd/application_env_create.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "github.com/qovery/qovery-client-go" "os" "github.com/pterm/pterm" @@ -24,7 +25,7 @@ var applicationEnvCreateCmd = &cobra.Command{ } client := utils.GetQoveryClient(tokenType, token) - _, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) if err != nil { utils.PrintlnError(err) @@ -50,7 +51,7 @@ var applicationEnvCreateCmd = &cobra.Command{ } if utils.IsSecret { - err = utils.CreateSecret(client, projectId, envId, application.Id, utils.Key, utils.Value, utils.ApplicationScope) + err = utils.CreateSecret(client, qovery.APIVARIABLESCOPEENUM_APPLICATION, application.Id, utils.Key, utils.Value) if err != nil { utils.PrintlnError(err) @@ -62,7 +63,7 @@ var applicationEnvCreateCmd = &cobra.Command{ return } - err = utils.CreateEnvironmentVariable(client, projectId, envId, application.Id, utils.Key, utils.Value, utils.ApplicationScope) + err = utils.CreateEnvironmentVariable(client, qovery.APIVARIABLESCOPEENUM_APPLICATION, application.Id, utils.Key, utils.Value) if err != nil { utils.PrintlnError(err) diff --git a/cmd/container_env_create.go b/cmd/container_env_create.go index d6dfb738..11470d97 100644 --- a/cmd/container_env_create.go +++ b/cmd/container_env_create.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "github.com/qovery/qovery-client-go" "os" "github.com/pterm/pterm" @@ -24,7 +25,7 @@ var containerEnvCreateCmd = &cobra.Command{ } client := utils.GetQoveryClient(tokenType, token) - _, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) if err != nil { utils.PrintlnError(err) @@ -50,7 +51,7 @@ var containerEnvCreateCmd = &cobra.Command{ } if utils.IsSecret { - err = utils.CreateSecret(client, projectId, envId, container.Id, utils.Key, utils.Value, utils.ContainerScope) + err = utils.CreateSecret(client, qovery.APIVARIABLESCOPEENUM_CONTAINER, container.Id, utils.Key, utils.Value) if err != nil { utils.PrintlnError(err) @@ -62,7 +63,7 @@ var containerEnvCreateCmd = &cobra.Command{ return } - err = utils.CreateEnvironmentVariable(client, projectId, envId, container.Id, utils.Key, utils.Value, utils.ContainerScope) + err = utils.CreateEnvironmentVariable(client, qovery.APIVARIABLESCOPEENUM_CONTAINER, container.Id, utils.Key, utils.Value) if err != nil { utils.PrintlnError(err) diff --git a/cmd/cronjob_env_create.go b/cmd/cronjob_env_create.go index 9e5bd334..ce912900 100644 --- a/cmd/cronjob_env_create.go +++ b/cmd/cronjob_env_create.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "github.com/qovery/qovery-client-go" "os" "github.com/pterm/pterm" @@ -24,7 +25,7 @@ var cronjobEnvCreateCmd = &cobra.Command{ } client := utils.GetQoveryClient(tokenType, token) - _, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) if err != nil { utils.PrintlnError(err) @@ -50,7 +51,7 @@ var cronjobEnvCreateCmd = &cobra.Command{ } if utils.IsSecret { - err = utils.CreateSecret(client, projectId, envId, cronjob.CronJobResponse.Id, utils.Key, utils.Value, utils.JobScope) + err = utils.CreateSecret(client, qovery.APIVARIABLESCOPEENUM_JOB, cronjob.CronJobResponse.Id, utils.Key, utils.Value) if err != nil { utils.PrintlnError(err) @@ -62,7 +63,7 @@ var cronjobEnvCreateCmd = &cobra.Command{ return } - err = utils.CreateEnvironmentVariable(client, projectId, envId, cronjob.CronJobResponse.Id, utils.Key, utils.Value, utils.JobScope) + err = utils.CreateEnvironmentVariable(client, qovery.APIVARIABLESCOPEENUM_JOB, cronjob.CronJobResponse.Id, utils.Key, utils.Value) if err != nil { utils.PrintlnError(err) diff --git a/cmd/helm_env_create.go b/cmd/helm_env_create.go new file mode 100644 index 00000000..446b3eda --- /dev/null +++ b/cmd/helm_env_create.go @@ -0,0 +1,92 @@ +package cmd + +import ( + "context" + "fmt" + "github.com/qovery/qovery-client-go" + "os" + + "github.com/pterm/pterm" + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var helmEnvCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create 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 + } + + if utils.IsSecret { + err = utils.CreateSecret(client, qovery.APIVARIABLESCOPEENUM_HELM, helm.Id, utils.Key, utils.Value) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("Secret %s has been created", pterm.FgBlue.Sprintf(utils.Key))) + return + } + + err = utils.CreateEnvironmentVariable(client, qovery.APIVARIABLESCOPEENUM_HELM, helm.Id, utils.Key, utils.Value) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("Environment variable %s has been created", pterm.FgBlue.Sprintf(utils.Key))) + }, +} + +func init() { + helmEnvCmd.AddCommand(helmEnvCreateCmd) + helmEnvCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + helmEnvCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + helmEnvCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + helmEnvCreateCmd.Flags().StringVarP(&helmName, "helm", "n", "", "helm Name") + helmEnvCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "Environment variable or secret key") + helmEnvCreateCmd.Flags().StringVarP(&utils.Value, "value", "v", "", "Environment variable or secret value") + helmEnvCreateCmd.Flags().StringVarP(&utils.HelmScope, "scope", "", "helm", "Scope of this env var ") + helmEnvCreateCmd.Flags().BoolVarP(&utils.IsSecret, "secret", "", false, "This environment variable is a secret") + + _ = helmEnvCreateCmd.MarkFlagRequired("key") + _ = helmEnvCreateCmd.MarkFlagRequired("value") + _ = helmEnvCreateCmd.MarkFlagRequired("helm") +} diff --git a/cmd/lifecycle_env_create.go b/cmd/lifecycle_env_create.go index 69aa63c0..f8f09142 100644 --- a/cmd/lifecycle_env_create.go +++ b/cmd/lifecycle_env_create.go @@ -3,6 +3,7 @@ package cmd import ( "context" "fmt" + "github.com/qovery/qovery-client-go" "os" "github.com/pterm/pterm" @@ -24,7 +25,7 @@ var lifecycleEnvCreateCmd = &cobra.Command{ } client := utils.GetQoveryClient(tokenType, token) - _, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + _, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client) if err != nil { utils.PrintlnError(err) @@ -50,7 +51,7 @@ var lifecycleEnvCreateCmd = &cobra.Command{ } if utils.IsSecret { - err = utils.CreateSecret(client, projectId, envId, lifecycle.LifecycleJobResponse.Id, utils.Key, utils.Value, utils.JobScope) + err = utils.CreateSecret(client, qovery.APIVARIABLESCOPEENUM_JOB, lifecycle.LifecycleJobResponse.Id, utils.Key, utils.Value) if err != nil { utils.PrintlnError(err) @@ -62,7 +63,7 @@ var lifecycleEnvCreateCmd = &cobra.Command{ return } - err = utils.CreateEnvironmentVariable(client, projectId, envId, lifecycle.LifecycleJobResponse.Id, utils.Key, utils.Value, utils.JobScope) + err = utils.CreateEnvironmentVariable(client, qovery.APIVARIABLESCOPEENUM_JOB, lifecycle.LifecycleJobResponse.Id, utils.Key, utils.Value) if err != nil { utils.PrintlnError(err) diff --git a/utils/env_var.go b/utils/env_var.go index 3eb2e89b..2c6f738c 100644 --- a/utils/env_var.go +++ b/utils/env_var.go @@ -18,6 +18,7 @@ var IsSecret bool var ApplicationScope string var JobScope string var ContainerScope string +var HelmScope string var Alias string var Key string var Value string @@ -179,114 +180,42 @@ func FromSecretToEnvVarLineOutput(secret qovery.Secret) EnvVarLineOutput { func CreateEnvironmentVariable( client *qovery.APIClient, - projectId string, - environmentId string, - serviceId string, + scope qovery.APIVariableScopeEnum, + parentId string, key string, value string, - scope string, ) error { - req := qovery.EnvironmentVariableRequest{ + variableRequest := qovery.VariableRequest{ Key: key, - Value: &value, + Value: value, MountPath: qovery.NullableString{}, + IsSecret: false, + VariableScope: scope, + VariableParentId: parentId, } - switch strings.ToUpper(scope) { - case "PROJECT": - _, _, err := client.ProjectEnvironmentVariableAPI.CreateProjectEnvironmentVariable( - context.Background(), - projectId, - ).EnvironmentVariableRequest(req).Execute() - - return err - case "ENVIRONMENT": - _, _, err := client.EnvironmentVariableAPI.CreateEnvironmentEnvironmentVariable( - context.Background(), - environmentId, - ).EnvironmentVariableRequest(req).Execute() - - return err - case "APPLICATION": - _, _, err := client.ApplicationEnvironmentVariableAPI.CreateApplicationEnvironmentVariable( - context.Background(), - serviceId, - ).EnvironmentVariableRequest(req).Execute() - - return err - case "JOB": - _, _, err := client.JobEnvironmentVariableAPI.CreateJobEnvironmentVariable( - context.Background(), - serviceId, - ).EnvironmentVariableRequest(req).Execute() - - return err - case "CONTAINER": - _, _, err := client.ContainerEnvironmentVariableAPI.CreateContainerEnvironmentVariable( - context.Background(), - serviceId, - ).EnvironmentVariableRequest(req).Execute() - - return err - } - - return errors.New("invalid scope") + _, _, err := client.VariableMainCallsAPI.CreateVariable(context.Background()).VariableRequest(variableRequest).Execute() + return err } func CreateSecret( client *qovery.APIClient, - projectId string, - environmentId string, - serviceId string, + scope qovery.APIVariableScopeEnum, + parentId string, key string, value string, - scope string, ) error { - req := qovery.SecretRequest{ + variableRequest := qovery.VariableRequest{ Key: key, - Value: &value, + Value: value, MountPath: qovery.NullableString{}, + IsSecret: true, + VariableScope: scope, + VariableParentId: parentId, } - switch strings.ToUpper(scope) { - case "PROJECT": - _, _, err := client.ProjectSecretAPI.CreateProjectSecret( - context.Background(), - projectId, - ).SecretRequest(req).Execute() - - return err - case "ENVIRONMENT": - _, _, err := client.EnvironmentSecretAPI.CreateEnvironmentSecret( - context.Background(), - environmentId, - ).SecretRequest(req).Execute() - - return err - case "APPLICATION": - _, _, err := client.ApplicationSecretAPI.CreateApplicationSecret( - context.Background(), - serviceId, - ).SecretRequest(req).Execute() - - return err - case "JOB": - _, _, err := client.JobSecretAPI.CreateJobSecret( - context.Background(), - serviceId, - ).SecretRequest(req).Execute() - - return err - case "CONTAINER": - _, _, err := client.ContainerSecretAPI.CreateContainerSecret( - context.Background(), - serviceId, - ).SecretRequest(req).Execute() - - return err - } - - return errors.New("invalid scope") + _, _, err := client.VariableMainCallsAPI.CreateVariable(context.Background()).VariableRequest(variableRequest).Execute() + return err } func FindEnvironmentVariableByKey(key string, envVars []qovery.VariableResponse) *qovery.VariableResponse {