Skip to content

Commit

Permalink
feat: add helm env create
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Dec 22, 2023
1 parent b1c791b commit 4343ff9
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 102 deletions.
7 changes: 4 additions & 3 deletions cmd/application_env_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/qovery/qovery-client-go"
"os"

"github.com/pterm/pterm"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions cmd/container_env_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/qovery/qovery-client-go"
"os"

"github.com/pterm/pterm"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions cmd/cronjob_env_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/qovery/qovery-client-go"
"os"

"github.com/pterm/pterm"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
92 changes: 92 additions & 0 deletions cmd/helm_env_create.go
Original file line number Diff line number Diff line change
@@ -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 <PROJECT|ENVIRONMENT|helm>")
helmEnvCreateCmd.Flags().BoolVarP(&utils.IsSecret, "secret", "", false, "This environment variable is a secret")

_ = helmEnvCreateCmd.MarkFlagRequired("key")
_ = helmEnvCreateCmd.MarkFlagRequired("value")
_ = helmEnvCreateCmd.MarkFlagRequired("helm")
}
7 changes: 4 additions & 3 deletions cmd/lifecycle_env_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/qovery/qovery-client-go"
"os"

"github.com/pterm/pterm"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
109 changes: 19 additions & 90 deletions utils/env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4343ff9

Please sign in to comment.