Skip to content

Commit

Permalink
feat add helm create override
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Dec 22, 2023
1 parent bed182e commit 013e18a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 174 deletions.
3 changes: 2 additions & 1 deletion cmd/application_env_override_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var applicationEnvOverrideCreateCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

err = utils.CreateOverride(client, projectId, envId, application.Id, utils.ApplicationType, utils.Key, &utils.Value, utils.ApplicationScope)
err = utils.CreateOverride(client, projectId, envId, application.Id, utils.ApplicationType, utils.Key, utils.Value, utils.ApplicationScope)

if err != nil {
utils.PrintlnError(err)
Expand All @@ -73,4 +73,5 @@ func init() {

_ = applicationEnvOverrideCreateCmd.MarkFlagRequired("key")
_ = applicationEnvOverrideCreateCmd.MarkFlagRequired("application")
_ = applicationEnvOverrideCreateCmd.MarkFlagRequired("value")
}
3 changes: 2 additions & 1 deletion cmd/container_env_override_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var containerEnvOverrideCreateCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

err = utils.CreateOverride(client, projectId, envId, container.Id, utils.ContainerType, utils.Key, &utils.Value, utils.ContainerScope)
err = utils.CreateOverride(client, projectId, envId, container.Id, utils.ContainerType, utils.Key, utils.Value, utils.ContainerScope)

if err != nil {
utils.PrintlnError(err)
Expand All @@ -73,4 +73,5 @@ func init() {

_ = containerEnvOverrideCreateCmd.MarkFlagRequired("key")
_ = containerEnvOverrideCreateCmd.MarkFlagRequired("container")
_ = containerEnvOverrideCreateCmd.MarkFlagRequired("value")
}
3 changes: 2 additions & 1 deletion cmd/cronjob_env_override_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var cronjobEnvOverrideCreateCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

err = utils.CreateOverride(client, projectId, envId, cronjob.CronJobResponse.Id, utils.JobType, utils.Key, &utils.Value, utils.JobScope)
err = utils.CreateOverride(client, projectId, envId, cronjob.CronJobResponse.Id, utils.JobType, utils.Key, utils.Value, utils.JobScope)

if err != nil {
utils.PrintlnError(err)
Expand All @@ -73,4 +73,5 @@ func init() {

_ = cronjobEnvOverrideCreateCmd.MarkFlagRequired("key")
_ = cronjobEnvOverrideCreateCmd.MarkFlagRequired("cronjob")
_ = cronjobEnvOverrideCreateCmd.MarkFlagRequired("value")
}
24 changes: 24 additions & 0 deletions cmd/helm_env_override.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/qovery/qovery-cli/utils"
"github.com/spf13/cobra"
"os"
)

var helmEnvOverrideCmd = &cobra.Command{
Use: "override",
Short: "Manage helm environment variable and secret overrides",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)

if len(args) == 0 {
_ = cmd.Help()
os.Exit(0)
}
},
}

func init() {
helmEnvCmd.AddCommand(helmEnvOverrideCmd)
}
76 changes: 76 additions & 0 deletions cmd/helm_env_override_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
"context"
"fmt"
"os"

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

var helmEnvOverrideCreateCmd = &cobra.Command{
Use: "create",
Short: "Override 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)
_, projectId, 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.CreateOverride(client, projectId, envId, helm.Id, utils.HelmType, utils.Key, utils.Value, utils.HelmScope)

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

utils.Println(fmt.Sprintf("%s has been overidden", pterm.FgBlue.Sprintf(utils.Key)))
},
}

func init() {
helmEnvOverrideCmd.AddCommand(helmEnvOverrideCreateCmd)
helmEnvOverrideCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
helmEnvOverrideCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
helmEnvOverrideCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
helmEnvOverrideCreateCmd.Flags().StringVarP(&helmName, "helm", "n", "", "helm Name")
helmEnvOverrideCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "Environment variable or secret key")
helmEnvOverrideCreateCmd.Flags().StringVarP(&utils.Value, "value", "", "", "Environment variable or secret value")
helmEnvOverrideCreateCmd.Flags().StringVarP(&utils.HelmScope, "scope", "", "HELM", "Scope of this alias <PROJECT|ENVIRONMENT|HELM>")

_ = helmEnvOverrideCreateCmd.MarkFlagRequired("key")
_ = helmEnvOverrideCreateCmd.MarkFlagRequired("helm")
}
3 changes: 2 additions & 1 deletion cmd/lifecycle_env_override_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var lifecycleEnvOverrideCreateCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

err = utils.CreateOverride(client, projectId, envId, lifecycle.LifecycleJobResponse.Id, utils.JobType, utils.Key, &utils.Value, utils.JobScope)
err = utils.CreateOverride(client, projectId, envId, lifecycle.LifecycleJobResponse.Id, utils.JobType, utils.Key, utils.Value, utils.JobScope)

if err != nil {
utils.PrintlnError(err)
Expand All @@ -73,4 +73,5 @@ func init() {

_ = lifecycleEnvOverrideCreateCmd.MarkFlagRequired("key")
_ = lifecycleEnvOverrideCreateCmd.MarkFlagRequired("lifecycle")
_ = lifecycleEnvOverrideCreateCmd.MarkFlagRequired("value")
}
185 changes: 15 additions & 170 deletions utils/env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,6 @@ func FindEnvironmentVariableByKey(key string, envVars []qovery.VariableResponse)
return nil
}

func FindSecretByKey(key string, secrets []qovery.Secret) *qovery.Secret {
for _, secret := range secrets {
if secret.Key == key {
return &secret
}
}

return nil
}

func ListEnvironmentVariables(
client *qovery.APIClient,
serviceId string,
Expand Down Expand Up @@ -276,44 +266,6 @@ func getParentIdByScope(scope string, projectId string, environmentId string, se
return "", qovery.APIVARIABLESCOPEENUM_BUILT_IN, fmt.Errorf("scope %s not supported", scope)
}

func ListSecrets(
client *qovery.APIClient,
serviceId string,
serviceType ServiceType,
) ([]qovery.Secret, error) {
var res *qovery.SecretResponseList

switch serviceType {
case ApplicationType:
r, _, err := client.ApplicationSecretAPI.ListApplicationSecrets(context.Background(), serviceId).Execute()
if err != nil {
return nil, err
}

res = r
case ContainerType:
r, _, err := client.ContainerSecretAPI.ListContainerSecrets(context.Background(), serviceId).Execute()
if err != nil {
return nil, err
}

res = r
case JobType:
r, _, err := client.JobSecretAPI.ListJobSecrets(context.Background(), serviceId).Execute()
if err != nil {
return nil, err
}

res = r
}

if res == nil {
return nil, errors.New("invalid service type")
}

return res.Results, nil
}

func DeleteVariable(
client *qovery.APIClient,
serviceId string,
Expand Down Expand Up @@ -384,122 +336,19 @@ func CreateAlias(

func CreateEnvironmentVariableOverride(
client *qovery.APIClient,
projectId string,
environmentId string,
serviceId string,
parentEnvironmentVariableId string,
value *string,
scope string,
) error {
v := *qovery.NewValue()
if value != nil {
v.SetValue(*value)
}

switch strings.ToUpper(scope) {
case "PROJECT":
_, _, err := client.ProjectEnvironmentVariableAPI.CreateProjectEnvironmentVariableOverride(
context.Background(),
projectId,
parentEnvironmentVariableId,
).Value(v).Execute()

return err
case "ENVIRONMENT":
_, _, err := client.EnvironmentVariableAPI.CreateEnvironmentEnvironmentVariableOverride(
context.Background(),
environmentId,
parentEnvironmentVariableId,
).Value(v).Execute()

return err
case "APPLICATION":
_, _, err := client.ApplicationEnvironmentVariableAPI.CreateApplicationEnvironmentVariableOverride(
context.Background(),
serviceId,
parentEnvironmentVariableId,
).Value(v).Execute()

return err
case "JOB":
_, _, err := client.JobEnvironmentVariableAPI.CreateJobEnvironmentVariableOverride(
context.Background(),
serviceId,
parentEnvironmentVariableId,
).Value(v).Execute()

return err
case "CONTAINER":
_, _, err := client.ContainerEnvironmentVariableAPI.CreateContainerEnvironmentVariableOverride(
context.Background(),
serviceId,
parentEnvironmentVariableId,
).Value(v).Execute()

return err
}

return errors.New("invalid scope")
}

func CreateSecretOverride(
client *qovery.APIClient,
projectId string,
environmentId string,
serviceId string,
parentSecretId string,
value *string,
scope string,
overrideParentId string,
overrideScope qovery.APIVariableScopeEnum,
variableId string,
value string,
) error {
v := *qovery.NewValue()
if value != nil {
v.SetValue(*value)
}

switch strings.ToUpper(scope) {
case "PROJECT":
_, _, err := client.ProjectSecretAPI.CreateProjectSecretOverride(
context.Background(),
projectId,
parentSecretId,
).Value(v).Execute()

return err
case "ENVIRONMENT":
_, _, err := client.EnvironmentSecretAPI.CreateEnvironmentSecretOverride(
context.Background(),
environmentId,
parentSecretId,
).Value(v).Execute()

return err
case "APPLICATION":
_, _, err := client.ApplicationSecretAPI.CreateApplicationSecretOverride(
context.Background(),
serviceId,
parentSecretId,
).Value(v).Execute()

return err
case "JOB":
_, _, err := client.JobSecretAPI.CreateJobSecretOverride(
context.Background(),
serviceId,
parentSecretId,
).Value(v).Execute()

return err
case "CONTAINER":
_, _, err := client.ContainerSecretAPI.CreateContainerSecretOverride(
context.Background(),
serviceId,
parentSecretId,
).Value(v).Execute()

return err
variableOverrideRequest := qovery.VariableOverrideRequest{
Value: value,
OverrideScope: overrideScope,
OverrideParentId: overrideParentId,
}

return errors.New("invalid scope")
_, _, err := client.VariableMainCallsAPI.CreateVariableOverride(context.Background(), variableId).VariableOverrideRequest(variableOverrideRequest).Execute()
return err
}

func CreateOverride(
Expand All @@ -509,7 +358,7 @@ func CreateOverride(
serviceId string,
serviceType ServiceType,
key string,
value *string,
value string,
scope string,
) error {
envVars, err := ListEnvironmentVariables(client, serviceId, serviceType)
Expand All @@ -519,18 +368,14 @@ func CreateOverride(

envVar := FindEnvironmentVariableByKey(key, envVars)

if envVar != nil {
return CreateEnvironmentVariableOverride(client, projectId, environmentId, serviceId, envVar.Id, value, scope)
}

secrets, err := ListSecrets(client, serviceId, serviceType)
parentId, parentScope, err := getParentIdByScope(scope, projectId, environmentId, serviceId)
if err != nil {
return err
}

secret := FindSecretByKey(key, secrets)
if secret != nil {
return CreateSecretOverride(client, projectId, environmentId, serviceId, secret.Id, value, scope)
if envVar != nil {
// create override for environment variable
return CreateEnvironmentVariableOverride(client, parentId, parentScope, envVar.Id, value)
}

return fmt.Errorf("Environment variable or secret %s not found", pterm.FgRed.Sprintf(key))
Expand Down

0 comments on commit 013e18a

Please sign in to comment.