Skip to content

Commit

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

err = utils.CreateAlias(client, projectId, envId, application.Id, utils.ApplicationType, utils.Key, utils.Alias, utils.ApplicationScope)

err = utils.CreateAlias(client, projectId, envId, application.Id, utils.ApplicationType, utils.Key, utils.Alias, utils.ApplicationScope)

if err != nil {
utils.PrintlnError(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/container_env_alias_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var containerEnvAliasCreateCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

err = utils.CreateAlias(client, projectId, envId, container.Id, utils.ContainerType, utils.Key, utils.Alias, utils.ContainerScope)
err = utils.CreateAlias(client, projectId, envId, container.Id, utils.ContainerType, utils.Key, utils.Alias, utils.ContainerScope)

if err != nil {
utils.PrintlnError(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cronjob_env_alias_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var cronjobEnvAliasCreateCmd = &cobra.Command{
}

client := utils.GetQoveryClient(tokenType, token)
_, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

if err != nil {
utils.PrintlnError(err)
Expand Down
24 changes: 24 additions & 0 deletions cmd/helm_env_alias.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 helmEnvAliasCmd = &cobra.Command{
Use: "alias",
Short: "Manage helm environment variable and secret aliases",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)

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

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

import (
"context"
"fmt"
"os"

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

var helmEnvAliasCreateCmd = &cobra.Command{
Use: "create",
Short: "Create helm environment variable or secret alias",
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.CreateAlias(client, projectId, envId, helm.Id, utils.HelmType, utils.Key, utils.Alias, 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("Alias %s has been created", pterm.FgBlue.Sprintf(utils.Alias)))
},
}

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

_ = helmEnvAliasCreateCmd.MarkFlagRequired("key")
_ = helmEnvAliasCreateCmd.MarkFlagRequired("alias")
_ = helmEnvAliasCreateCmd.MarkFlagRequired("helm")
}
2 changes: 1 addition & 1 deletion cmd/lifecycle_env_alias_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var lifecycleEnvAliasCreateCmd = &cobra.Command{
}

client := utils.GetQoveryClient(tokenType, token)
_, projectId, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)
_, _, envId, err := getOrganizationProjectEnvironmentContextResourcesIds(client)

if err != nil {
utils.PrintlnError(err)
Expand Down
95 changes: 35 additions & 60 deletions utils/env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,30 @@ func ServiceTypeToScope(serviceType ServiceType) (qovery.APIVariableScopeEnum, e
return qovery.APIVARIABLESCOPEENUM_HELM, nil
}

return qovery.APIVARIABLESCOPEENUM_BUILT_IN, fmt.Errorf("%s not supported", serviceType)
return qovery.APIVARIABLESCOPEENUM_BUILT_IN, fmt.Errorf("service type %s not supported", serviceType)
}

func getParentIdByScope(scope string, projectId string, environmentId string, serviceId string) (string, qovery.APIVariableScopeEnum, error) {
switch scope {
case "PROJECT":
return projectId, qovery.APIVARIABLESCOPEENUM_PROJECT, nil
case "ENVIRONMENT":
return environmentId, qovery.APIVARIABLESCOPEENUM_ENVIRONMENT, nil
case "APPLICATION":return serviceId, qovery.APIVARIABLESCOPEENUM_APPLICATION, nil
case "CONTAINER":
return serviceId, qovery.APIVARIABLESCOPEENUM_CONTAINER, nil
case "JOB":
return serviceId, qovery.APIVARIABLESCOPEENUM_JOB, nil
case "HELM":
return serviceId, qovery.APIVARIABLESCOPEENUM_HELM, nil
}

return "", qovery.APIVARIABLESCOPEENUM_BUILT_IN, fmt.Errorf("scope %s not supported", scope)
}




func ListSecrets(
client *qovery.APIClient,
serviceId string,
Expand Down Expand Up @@ -337,59 +358,19 @@ func DeleteVariable(

func CreateEnvironmentVariableAlias(
client *qovery.APIClient,
projectId string,
environmentId string,
serviceId string,
parentEnvironmentVariableId string,
aliasParentId string,
aliasScope qovery.APIVariableScopeEnum,
variableId string,
alias string,
scope string,
) error {
key := *qovery.NewKey(alias)

switch strings.ToUpper(scope) {
case "PROJECT":
_, _, err := client.ProjectEnvironmentVariableAPI.CreateProjectEnvironmentVariableAlias(
context.Background(),
projectId,
parentEnvironmentVariableId,
).Key(key).Execute()

return err
case "ENVIRONMENT":
_, _, err := client.EnvironmentVariableAPI.CreateEnvironmentEnvironmentVariableAlias(
context.Background(),
environmentId,
parentEnvironmentVariableId,
).Key(key).Execute()

return err
case "APPLICATION":
_, _, err := client.ApplicationEnvironmentVariableAPI.CreateApplicationEnvironmentVariableAlias(
context.Background(),
serviceId,
parentEnvironmentVariableId,
).Key(key).Execute()

return err
case "JOB":
_, _, err := client.JobEnvironmentVariableAPI.CreateJobEnvironmentVariableAlias(
context.Background(),
serviceId,
parentEnvironmentVariableId,
).Key(key).Execute()

return err
case "CONTAINER":
_, _, err := client.ContainerEnvironmentVariableAPI.CreateContainerEnvironmentVariableAlias(
context.Background(),
serviceId,
parentEnvironmentVariableId,
).Key(key).Execute()

return err
variableAliasRequest := qovery.VariableAliasRequest{
Key: alias,
AliasScope: aliasScope,
AliasParentId: aliasParentId,
}

return errors.New("invalid scope")
_, _, err := client.VariableMainCallsAPI.CreateVariableAlias(context.Background(), variableId).VariableAliasRequest(variableAliasRequest).Execute()
return err
}

func CreateSecretAlias(
Expand Down Expand Up @@ -466,20 +447,14 @@ func CreateAlias(

envVar := FindEnvironmentVariableByKey(key, envVars)

if envVar != nil {
// create alias for environment variable
return CreateEnvironmentVariableAlias(client, projectId, environmentId, serviceId, envVar.Id, alias, 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 {
// create alias for secret
return CreateSecretAlias(client, projectId, environmentId, serviceId, secret.Id, alias, scope)
if envVar != nil {
// create alias for environment variable
return CreateEnvironmentVariableAlias(client, parentId, parentScope, envVar.Id, alias)
}

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

0 comments on commit f05c06a

Please sign in to comment.