diff --git a/cmd/application_external_secret.go b/cmd/application_external_secret.go new file mode 100644 index 00000000..922a35bf --- /dev/null +++ b/cmd/application_external_secret.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var applicationExternalSecretCmd = &cobra.Command{ + Use: "external-secret", + Short: "Manage application external secrets", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, +} + +func init() { + applicationCmd.AddCommand(applicationExternalSecretCmd) +} diff --git a/cmd/application_external_secret_create.go b/cmd/application_external_secret_create.go new file mode 100644 index 00000000..a85cdb90 --- /dev/null +++ b/cmd/application_external_secret_create.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var applicationExternalSecretCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create application external 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 + } + + applications, _, err := client.ApplicationsAPI.ListApplication(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + application := utils.FindByApplicationName(applications.GetResults(), applicationName) + + if application == nil { + utils.PrintlnError(fmt.Errorf("application %s not found", applicationName)) + utils.PrintlnInfo("You can list all applications with: qovery application list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.CreateServiceExternalSecret(client, projectId, envId, application.Id, utils.ApplicationScope, utils.Key, utils.Reference, utils.SecretManagerAccessId, utils.MountPath) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been created", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + applicationExternalSecretCmd.AddCommand(applicationExternalSecretCreateCmd) + applicationExternalSecretCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + applicationExternalSecretCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + applicationExternalSecretCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + applicationExternalSecretCreateCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name") + applicationExternalSecretCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + applicationExternalSecretCreateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "Reference to the secret in the secrets provider") + applicationExternalSecretCreateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "Secret manager access ID") + applicationExternalSecretCreateCmd.Flags().StringVarP(&utils.ApplicationScope, "scope", "", "APPLICATION", "Scope of this external secret ") + applicationExternalSecretCreateCmd.Flags().StringVarP(&utils.MountPath, "mount-path", "", "", "Path where the secret will be mounted as a file") + + _ = applicationExternalSecretCreateCmd.MarkFlagRequired("key") + _ = applicationExternalSecretCreateCmd.MarkFlagRequired("reference") + _ = applicationExternalSecretCreateCmd.MarkFlagRequired("secret-manager-access-id") + _ = applicationExternalSecretCreateCmd.MarkFlagRequired("application") +} diff --git a/cmd/application_external_secret_delete.go b/cmd/application_external_secret_delete.go new file mode 100644 index 00000000..cb919519 --- /dev/null +++ b/cmd/application_external_secret_delete.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var applicationExternalSecretDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete application external 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 + } + + applications, _, err := client.ApplicationsAPI.ListApplication(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + application := utils.FindByApplicationName(applications.GetResults(), applicationName) + + if application == nil { + utils.PrintlnError(fmt.Errorf("application %s not found", applicationName)) + utils.PrintlnInfo("You can list all applications with: qovery application list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.DeleteServiceVariable(client, application.Id, utils.ApplicationType, 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("External secret %s has been deleted", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + applicationExternalSecretCmd.AddCommand(applicationExternalSecretDeleteCmd) + applicationExternalSecretDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + applicationExternalSecretDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + applicationExternalSecretDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + applicationExternalSecretDeleteCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name") + applicationExternalSecretDeleteCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + + _ = applicationExternalSecretDeleteCmd.MarkFlagRequired("key") + _ = applicationExternalSecretDeleteCmd.MarkFlagRequired("application") +} diff --git a/cmd/application_external_secret_update.go b/cmd/application_external_secret_update.go new file mode 100644 index 00000000..7ac4c7f2 --- /dev/null +++ b/cmd/application_external_secret_update.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var applicationExternalSecretUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update application external 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 + } + + applications, _, err := client.ApplicationsAPI.ListApplication(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + application := utils.FindByApplicationName(applications.GetResults(), applicationName) + + if application == nil { + utils.PrintlnError(fmt.Errorf("application %s not found", applicationName)) + utils.PrintlnInfo("You can list all applications with: qovery application list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.UpdateServiceExternalSecret(client, utils.Key, utils.Reference, utils.SecretManagerAccessId, application.Id, utils.ApplicationType) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been updated", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + applicationExternalSecretCmd.AddCommand(applicationExternalSecretUpdateCmd) + applicationExternalSecretUpdateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + applicationExternalSecretUpdateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + applicationExternalSecretUpdateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + applicationExternalSecretUpdateCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name") + applicationExternalSecretUpdateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + applicationExternalSecretUpdateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "New reference to the secret in the secrets provider") + applicationExternalSecretUpdateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "New secret manager access ID") + + _ = applicationExternalSecretUpdateCmd.MarkFlagRequired("key") + _ = applicationExternalSecretUpdateCmd.MarkFlagRequired("application") +} diff --git a/cmd/container_external_secret.go b/cmd/container_external_secret.go new file mode 100644 index 00000000..dd349284 --- /dev/null +++ b/cmd/container_external_secret.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var containerExternalSecretCmd = &cobra.Command{ + Use: "external-secret", + Short: "Manage container external secrets", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, +} + +func init() { + containerCmd.AddCommand(containerExternalSecretCmd) +} diff --git a/cmd/container_external_secret_create.go b/cmd/container_external_secret_create.go new file mode 100644 index 00000000..ab99361f --- /dev/null +++ b/cmd/container_external_secret_create.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var containerExternalSecretCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create container external 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 + } + + containers, _, err := client.ContainersAPI.ListContainer(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + container := utils.FindByContainerName(containers.GetResults(), containerName) + + if container == nil { + utils.PrintlnError(fmt.Errorf("container %s not found", containerName)) + utils.PrintlnInfo("You can list all containers with: qovery container list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.CreateServiceExternalSecret(client, projectId, envId, container.Id, utils.ContainerScope, utils.Key, utils.Reference, utils.SecretManagerAccessId, utils.MountPath) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been created", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + containerExternalSecretCmd.AddCommand(containerExternalSecretCreateCmd) + containerExternalSecretCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + containerExternalSecretCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + containerExternalSecretCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + containerExternalSecretCreateCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name") + containerExternalSecretCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + containerExternalSecretCreateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "Reference to the secret in the secrets provider") + containerExternalSecretCreateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "Secret manager access ID") + containerExternalSecretCreateCmd.Flags().StringVarP(&utils.ContainerScope, "scope", "", "CONTAINER", "Scope of this external secret ") + containerExternalSecretCreateCmd.Flags().StringVarP(&utils.MountPath, "mount-path", "", "", "Path where the secret will be mounted as a file") + + _ = containerExternalSecretCreateCmd.MarkFlagRequired("key") + _ = containerExternalSecretCreateCmd.MarkFlagRequired("reference") + _ = containerExternalSecretCreateCmd.MarkFlagRequired("secret-manager-access-id") + _ = containerExternalSecretCreateCmd.MarkFlagRequired("container") +} diff --git a/cmd/container_external_secret_delete.go b/cmd/container_external_secret_delete.go new file mode 100644 index 00000000..31f9f690 --- /dev/null +++ b/cmd/container_external_secret_delete.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var containerExternalSecretDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete container external 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 + } + + containers, _, err := client.ContainersAPI.ListContainer(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + container := utils.FindByContainerName(containers.GetResults(), containerName) + + if container == nil { + utils.PrintlnError(fmt.Errorf("container %s not found", containerName)) + utils.PrintlnInfo("You can list all containers with: qovery container list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.DeleteServiceVariable(client, container.Id, utils.ContainerType, 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("External secret %s has been deleted", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + containerExternalSecretCmd.AddCommand(containerExternalSecretDeleteCmd) + containerExternalSecretDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + containerExternalSecretDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + containerExternalSecretDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + containerExternalSecretDeleteCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name") + containerExternalSecretDeleteCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + + _ = containerExternalSecretDeleteCmd.MarkFlagRequired("key") + _ = containerExternalSecretDeleteCmd.MarkFlagRequired("container") +} diff --git a/cmd/container_external_secret_update.go b/cmd/container_external_secret_update.go new file mode 100644 index 00000000..b7a10462 --- /dev/null +++ b/cmd/container_external_secret_update.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var containerExternalSecretUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update container external 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 + } + + containers, _, err := client.ContainersAPI.ListContainer(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + container := utils.FindByContainerName(containers.GetResults(), containerName) + + if container == nil { + utils.PrintlnError(fmt.Errorf("container %s not found", containerName)) + utils.PrintlnInfo("You can list all containers with: qovery container list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.UpdateServiceExternalSecret(client, utils.Key, utils.Reference, utils.SecretManagerAccessId, container.Id, utils.ContainerType) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been updated", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + containerExternalSecretCmd.AddCommand(containerExternalSecretUpdateCmd) + containerExternalSecretUpdateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + containerExternalSecretUpdateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + containerExternalSecretUpdateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + containerExternalSecretUpdateCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name") + containerExternalSecretUpdateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + containerExternalSecretUpdateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "New reference to the secret in the secrets provider") + containerExternalSecretUpdateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "New secret manager access ID") + + _ = containerExternalSecretUpdateCmd.MarkFlagRequired("key") + _ = containerExternalSecretUpdateCmd.MarkFlagRequired("container") +} diff --git a/cmd/cronjob_external_secret.go b/cmd/cronjob_external_secret.go new file mode 100644 index 00000000..424f04ba --- /dev/null +++ b/cmd/cronjob_external_secret.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var cronjobExternalSecretCmd = &cobra.Command{ + Use: "external-secret", + Short: "Manage cronjob external secrets", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, +} + +func init() { + cronjobCmd.AddCommand(cronjobExternalSecretCmd) +} diff --git a/cmd/cronjob_external_secret_create.go b/cmd/cronjob_external_secret_create.go new file mode 100644 index 00000000..aee75781 --- /dev/null +++ b/cmd/cronjob_external_secret_create.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var cronjobExternalSecretCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create cronjob external 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 + } + + cronjobs, _, err := client.JobsAPI.ListJobs(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + cronjob := utils.FindByJobName(cronjobs.GetResults(), cronjobName) + + if cronjob == nil || cronjob.CronJobResponse == nil { + utils.PrintlnError(fmt.Errorf("cronjob %s not found", cronjobName)) + utils.PrintlnInfo("You can list all cronjobs with: qovery cronjob list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.CreateServiceExternalSecret(client, projectId, envId, cronjob.CronJobResponse.Id, utils.JobScope, utils.Key, utils.Reference, utils.SecretManagerAccessId, utils.MountPath) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been created", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + cronjobExternalSecretCmd.AddCommand(cronjobExternalSecretCreateCmd) + cronjobExternalSecretCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + cronjobExternalSecretCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + cronjobExternalSecretCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + cronjobExternalSecretCreateCmd.Flags().StringVarP(&cronjobName, "cronjob", "n", "", "Cronjob Name") + cronjobExternalSecretCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + cronjobExternalSecretCreateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "Reference to the secret in the secrets provider") + cronjobExternalSecretCreateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "Secret manager access ID") + cronjobExternalSecretCreateCmd.Flags().StringVarP(&utils.JobScope, "scope", "", "JOB", "Scope of this external secret ") + cronjobExternalSecretCreateCmd.Flags().StringVarP(&utils.MountPath, "mount-path", "", "", "Path where the secret will be mounted as a file") + + _ = cronjobExternalSecretCreateCmd.MarkFlagRequired("key") + _ = cronjobExternalSecretCreateCmd.MarkFlagRequired("reference") + _ = cronjobExternalSecretCreateCmd.MarkFlagRequired("secret-manager-access-id") + _ = cronjobExternalSecretCreateCmd.MarkFlagRequired("cronjob") +} diff --git a/cmd/cronjob_external_secret_delete.go b/cmd/cronjob_external_secret_delete.go new file mode 100644 index 00000000..372a585b --- /dev/null +++ b/cmd/cronjob_external_secret_delete.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var cronjobExternalSecretDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete cronjob external 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 + } + + cronjobs, _, err := client.JobsAPI.ListJobs(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + cronjob := utils.FindByJobName(cronjobs.GetResults(), cronjobName) + + if cronjob == nil || cronjob.CronJobResponse == nil { + utils.PrintlnError(fmt.Errorf("cronjob %s not found", cronjobName)) + utils.PrintlnInfo("You can list all cronjobs with: qovery cronjob list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.DeleteServiceVariable(client, cronjob.CronJobResponse.Id, utils.JobType, 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("External secret %s has been deleted", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + cronjobExternalSecretCmd.AddCommand(cronjobExternalSecretDeleteCmd) + cronjobExternalSecretDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + cronjobExternalSecretDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + cronjobExternalSecretDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + cronjobExternalSecretDeleteCmd.Flags().StringVarP(&cronjobName, "cronjob", "n", "", "Cronjob Name") + cronjobExternalSecretDeleteCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + + _ = cronjobExternalSecretDeleteCmd.MarkFlagRequired("key") + _ = cronjobExternalSecretDeleteCmd.MarkFlagRequired("cronjob") +} diff --git a/cmd/cronjob_external_secret_update.go b/cmd/cronjob_external_secret_update.go new file mode 100644 index 00000000..4bbe93bd --- /dev/null +++ b/cmd/cronjob_external_secret_update.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var cronjobExternalSecretUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update cronjob external 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 + } + + cronjobs, _, err := client.JobsAPI.ListJobs(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + cronjob := utils.FindByJobName(cronjobs.GetResults(), cronjobName) + + if cronjob == nil || cronjob.CronJobResponse == nil { + utils.PrintlnError(fmt.Errorf("cronjob %s not found", cronjobName)) + utils.PrintlnInfo("You can list all cronjobs with: qovery cronjob list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.UpdateServiceExternalSecret(client, utils.Key, utils.Reference, utils.SecretManagerAccessId, cronjob.CronJobResponse.Id, utils.JobType) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been updated", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + cronjobExternalSecretCmd.AddCommand(cronjobExternalSecretUpdateCmd) + cronjobExternalSecretUpdateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + cronjobExternalSecretUpdateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + cronjobExternalSecretUpdateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + cronjobExternalSecretUpdateCmd.Flags().StringVarP(&cronjobName, "cronjob", "n", "", "Cronjob Name") + cronjobExternalSecretUpdateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + cronjobExternalSecretUpdateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "New reference to the secret in the secrets provider") + cronjobExternalSecretUpdateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "New secret manager access ID") + + _ = cronjobExternalSecretUpdateCmd.MarkFlagRequired("key") + _ = cronjobExternalSecretUpdateCmd.MarkFlagRequired("cronjob") +} diff --git a/cmd/environment_external_secret.go b/cmd/environment_external_secret.go new file mode 100644 index 00000000..26c24df9 --- /dev/null +++ b/cmd/environment_external_secret.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var environmentExternalSecretCmd = &cobra.Command{ + Use: "external-secret", + Short: "Manage environment external secrets", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, +} + +func init() { + environmentCmd.AddCommand(environmentExternalSecretCmd) +} diff --git a/cmd/environment_external_secret_create.go b/cmd/environment_external_secret_create.go new file mode 100644 index 00000000..0fc3d997 --- /dev/null +++ b/cmd/environment_external_secret_create.go @@ -0,0 +1,73 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var environmentExternalSecretCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create environment external secret", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + tokenType, token, err := utils.GetAccessToken() + checkError(err) + + client := utils.GetQoveryClient(tokenType, token) + + organizationId, _, _, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + checkError(err) + + projects, _, err := client.ProjectsAPI.ListProject(context.Background(), organizationId).Execute() + checkError(err) + + project := utils.FindByProjectName(projects.GetResults(), projectName) + if project == nil { + utils.PrintlnError(fmt.Errorf("project %s not found", projectName)) + utils.PrintlnInfo("You can list all projects with: qovery project list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + environments, _, err := client.EnvironmentsAPI.ListEnvironment(context.Background(), project.Id).Execute() + checkError(err) + + environment := utils.FindByEnvironmentName(environments.GetResults(), environmentName) + if environment == nil { + utils.PrintlnError(fmt.Errorf("environment %s not found", environmentName)) + utils.PrintlnInfo("You can list all environments with: qovery environment list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.CreateServiceExternalSecret(client, project.Id, environment.Id, "", utils.EnvironmentScope, utils.Key, utils.Reference, utils.SecretManagerAccessId, utils.MountPath) + checkError(err) + + utils.Println(fmt.Sprintf("External secret %s has been created", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + environmentExternalSecretCmd.AddCommand(environmentExternalSecretCreateCmd) + environmentExternalSecretCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + environmentExternalSecretCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + environmentExternalSecretCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + environmentExternalSecretCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + environmentExternalSecretCreateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "Reference to the secret in the secrets provider") + environmentExternalSecretCreateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "Secret manager access ID") + environmentExternalSecretCreateCmd.Flags().StringVarP(&utils.EnvironmentScope, "scope", "", "ENVIRONMENT", "Scope of this external secret ") + environmentExternalSecretCreateCmd.Flags().StringVarP(&utils.MountPath, "mount-path", "", "", "Path where the secret will be mounted as a file") + + _ = environmentExternalSecretCreateCmd.MarkFlagRequired("project") + _ = environmentExternalSecretCreateCmd.MarkFlagRequired("environment") + _ = environmentExternalSecretCreateCmd.MarkFlagRequired("key") + _ = environmentExternalSecretCreateCmd.MarkFlagRequired("reference") + _ = environmentExternalSecretCreateCmd.MarkFlagRequired("secret-manager-access-id") +} diff --git a/cmd/environment_external_secret_delete.go b/cmd/environment_external_secret_delete.go new file mode 100644 index 00000000..2162bec1 --- /dev/null +++ b/cmd/environment_external_secret_delete.go @@ -0,0 +1,67 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var environmentExternalSecretDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete environment external secret", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + tokenType, token, err := utils.GetAccessToken() + checkError(err) + + client := utils.GetQoveryClient(tokenType, token) + + organizationId, _, _, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + checkError(err) + + projects, _, err := client.ProjectsAPI.ListProject(context.Background(), organizationId).Execute() + checkError(err) + + project := utils.FindByProjectName(projects.GetResults(), projectName) + if project == nil { + utils.PrintlnError(fmt.Errorf("project %s not found", projectName)) + utils.PrintlnInfo("You can list all projects with: qovery project list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + environments, _, err := client.EnvironmentsAPI.ListEnvironment(context.Background(), project.Id).Execute() + checkError(err) + + environment := utils.FindByEnvironmentName(environments.GetResults(), environmentName) + if environment == nil { + utils.PrintlnError(fmt.Errorf("environment %s not found", environmentName)) + utils.PrintlnInfo("You can list all environments with: qovery environment list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.DeleteEnvironmentVar(client, environment.Id, utils.Key) + checkError(err) + + utils.Println(fmt.Sprintf("External secret %s has been deleted", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + environmentExternalSecretCmd.AddCommand(environmentExternalSecretDeleteCmd) + environmentExternalSecretDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + environmentExternalSecretDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + environmentExternalSecretDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + environmentExternalSecretDeleteCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + + _ = environmentExternalSecretDeleteCmd.MarkFlagRequired("project") + _ = environmentExternalSecretDeleteCmd.MarkFlagRequired("environment") + _ = environmentExternalSecretDeleteCmd.MarkFlagRequired("key") +} diff --git a/cmd/environment_external_secret_update.go b/cmd/environment_external_secret_update.go new file mode 100644 index 00000000..d1676308 --- /dev/null +++ b/cmd/environment_external_secret_update.go @@ -0,0 +1,69 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var environmentExternalSecretUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update environment external secret", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + tokenType, token, err := utils.GetAccessToken() + checkError(err) + + client := utils.GetQoveryClient(tokenType, token) + + organizationId, _, _, err := getOrganizationProjectEnvironmentContextResourcesIds(client) + checkError(err) + + projects, _, err := client.ProjectsAPI.ListProject(context.Background(), organizationId).Execute() + checkError(err) + + project := utils.FindByProjectName(projects.GetResults(), projectName) + if project == nil { + utils.PrintlnError(fmt.Errorf("project %s not found", projectName)) + utils.PrintlnInfo("You can list all projects with: qovery project list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + environments, _, err := client.EnvironmentsAPI.ListEnvironment(context.Background(), project.Id).Execute() + checkError(err) + + environment := utils.FindByEnvironmentName(environments.GetResults(), environmentName) + if environment == nil { + utils.PrintlnError(fmt.Errorf("environment %s not found", environmentName)) + utils.PrintlnInfo("You can list all environments with: qovery environment list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.UpdateEnvironmentExternalSecret(client, environment.Id, utils.Key, utils.Reference, utils.SecretManagerAccessId) + checkError(err) + + utils.Println(fmt.Sprintf("External secret %s has been updated", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + environmentExternalSecretCmd.AddCommand(environmentExternalSecretUpdateCmd) + environmentExternalSecretUpdateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + environmentExternalSecretUpdateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + environmentExternalSecretUpdateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + environmentExternalSecretUpdateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + environmentExternalSecretUpdateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "New reference to the secret in the secrets provider") + environmentExternalSecretUpdateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "New secret manager access ID") + + _ = environmentExternalSecretUpdateCmd.MarkFlagRequired("project") + _ = environmentExternalSecretUpdateCmd.MarkFlagRequired("environment") + _ = environmentExternalSecretUpdateCmd.MarkFlagRequired("key") +} diff --git a/cmd/helm_external_secret.go b/cmd/helm_external_secret.go new file mode 100644 index 00000000..7e64ec45 --- /dev/null +++ b/cmd/helm_external_secret.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var helmExternalSecretCmd = &cobra.Command{ + Use: "external-secret", + Short: "Manage helm external secrets", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, +} + +func init() { + helmCmd.AddCommand(helmExternalSecretCmd) +} diff --git a/cmd/helm_external_secret_create.go b/cmd/helm_external_secret_create.go new file mode 100644 index 00000000..a9d91a25 --- /dev/null +++ b/cmd/helm_external_secret_create.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var helmExternalSecretCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create helm external 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.CreateServiceExternalSecret(client, projectId, envId, helm.Id, utils.HelmScope, utils.Key, utils.Reference, utils.SecretManagerAccessId, utils.MountPath) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been created", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + helmExternalSecretCmd.AddCommand(helmExternalSecretCreateCmd) + helmExternalSecretCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + helmExternalSecretCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + helmExternalSecretCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + helmExternalSecretCreateCmd.Flags().StringVarP(&helmName, "helm", "n", "", "Helm Name") + helmExternalSecretCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + helmExternalSecretCreateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "Reference to the secret in the secrets provider") + helmExternalSecretCreateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "Secret manager access ID") + helmExternalSecretCreateCmd.Flags().StringVarP(&utils.HelmScope, "scope", "", "HELM", "Scope of this external secret ") + helmExternalSecretCreateCmd.Flags().StringVarP(&utils.MountPath, "mount-path", "", "", "Path where the secret will be mounted as a file") + + _ = helmExternalSecretCreateCmd.MarkFlagRequired("key") + _ = helmExternalSecretCreateCmd.MarkFlagRequired("reference") + _ = helmExternalSecretCreateCmd.MarkFlagRequired("secret-manager-access-id") + _ = helmExternalSecretCreateCmd.MarkFlagRequired("helm") +} diff --git a/cmd/helm_external_secret_delete.go b/cmd/helm_external_secret_delete.go new file mode 100644 index 00000000..af3f4d6a --- /dev/null +++ b/cmd/helm_external_secret_delete.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var helmExternalSecretDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete helm external 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.DeleteServiceVariable(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("External secret %s has been deleted", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + helmExternalSecretCmd.AddCommand(helmExternalSecretDeleteCmd) + helmExternalSecretDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + helmExternalSecretDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + helmExternalSecretDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + helmExternalSecretDeleteCmd.Flags().StringVarP(&helmName, "helm", "n", "", "Helm Name") + helmExternalSecretDeleteCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + + _ = helmExternalSecretDeleteCmd.MarkFlagRequired("key") + _ = helmExternalSecretDeleteCmd.MarkFlagRequired("helm") +} diff --git a/cmd/helm_external_secret_update.go b/cmd/helm_external_secret_update.go new file mode 100644 index 00000000..4cbaea4c --- /dev/null +++ b/cmd/helm_external_secret_update.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var helmExternalSecretUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update helm external 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.UpdateServiceExternalSecret(client, utils.Key, utils.Reference, utils.SecretManagerAccessId, helm.Id, utils.HelmType) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been updated", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + helmExternalSecretCmd.AddCommand(helmExternalSecretUpdateCmd) + helmExternalSecretUpdateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + helmExternalSecretUpdateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + helmExternalSecretUpdateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + helmExternalSecretUpdateCmd.Flags().StringVarP(&helmName, "helm", "n", "", "Helm Name") + helmExternalSecretUpdateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + helmExternalSecretUpdateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "New reference to the secret in the secrets provider") + helmExternalSecretUpdateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "New secret manager access ID") + + _ = helmExternalSecretUpdateCmd.MarkFlagRequired("key") + _ = helmExternalSecretUpdateCmd.MarkFlagRequired("helm") +} diff --git a/cmd/lifecycle_external_secret.go b/cmd/lifecycle_external_secret.go new file mode 100644 index 00000000..0a90598a --- /dev/null +++ b/cmd/lifecycle_external_secret.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var lifecycleExternalSecretCmd = &cobra.Command{ + Use: "external-secret", + Short: "Manage lifecycle external secrets", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, +} + +func init() { + lifecycleCmd.AddCommand(lifecycleExternalSecretCmd) +} diff --git a/cmd/lifecycle_external_secret_create.go b/cmd/lifecycle_external_secret_create.go new file mode 100644 index 00000000..e7e89f83 --- /dev/null +++ b/cmd/lifecycle_external_secret_create.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var lifecycleExternalSecretCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create lifecycle external 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 + } + + lifecycles, _, err := client.JobsAPI.ListJobs(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + lifecycle := utils.FindByJobName(lifecycles.GetResults(), lifecycleName) + + if lifecycle == nil || lifecycle.LifecycleJobResponse == nil { + utils.PrintlnError(fmt.Errorf("lifecycle %s not found", lifecycleName)) + utils.PrintlnInfo("You can list all lifecycles with: qovery lifecycle list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.CreateServiceExternalSecret(client, projectId, envId, lifecycle.LifecycleJobResponse.Id, utils.JobScope, utils.Key, utils.Reference, utils.SecretManagerAccessId, utils.MountPath) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been created", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + lifecycleExternalSecretCmd.AddCommand(lifecycleExternalSecretCreateCmd) + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&lifecycleName, "lifecycle", "n", "", "Lifecycle Name") + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "Reference to the secret in the secrets provider") + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "Secret manager access ID") + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&utils.JobScope, "scope", "", "JOB", "Scope of this external secret ") + lifecycleExternalSecretCreateCmd.Flags().StringVarP(&utils.MountPath, "mount-path", "", "", "Path where the secret will be mounted as a file") + + _ = lifecycleExternalSecretCreateCmd.MarkFlagRequired("key") + _ = lifecycleExternalSecretCreateCmd.MarkFlagRequired("reference") + _ = lifecycleExternalSecretCreateCmd.MarkFlagRequired("secret-manager-access-id") + _ = lifecycleExternalSecretCreateCmd.MarkFlagRequired("lifecycle") +} diff --git a/cmd/lifecycle_external_secret_delete.go b/cmd/lifecycle_external_secret_delete.go new file mode 100644 index 00000000..3581c62c --- /dev/null +++ b/cmd/lifecycle_external_secret_delete.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var lifecycleExternalSecretDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete lifecycle external 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 + } + + lifecycles, _, err := client.JobsAPI.ListJobs(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + lifecycle := utils.FindByJobName(lifecycles.GetResults(), lifecycleName) + + if lifecycle == nil || lifecycle.LifecycleJobResponse == nil { + utils.PrintlnError(fmt.Errorf("lifecycle %s not found", lifecycleName)) + utils.PrintlnInfo("You can list all lifecycles with: qovery lifecycle list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.DeleteServiceVariable(client, lifecycle.LifecycleJobResponse.Id, utils.JobType, 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("External secret %s has been deleted", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + lifecycleExternalSecretCmd.AddCommand(lifecycleExternalSecretDeleteCmd) + lifecycleExternalSecretDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + lifecycleExternalSecretDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + lifecycleExternalSecretDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + lifecycleExternalSecretDeleteCmd.Flags().StringVarP(&lifecycleName, "lifecycle", "n", "", "Lifecycle Name") + lifecycleExternalSecretDeleteCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + + _ = lifecycleExternalSecretDeleteCmd.MarkFlagRequired("key") + _ = lifecycleExternalSecretDeleteCmd.MarkFlagRequired("lifecycle") +} diff --git a/cmd/lifecycle_external_secret_update.go b/cmd/lifecycle_external_secret_update.go new file mode 100644 index 00000000..17fc2d52 --- /dev/null +++ b/cmd/lifecycle_external_secret_update.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var lifecycleExternalSecretUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update lifecycle external 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 + } + + lifecycles, _, err := client.JobsAPI.ListJobs(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + lifecycle := utils.FindByJobName(lifecycles.GetResults(), lifecycleName) + + if lifecycle == nil || lifecycle.LifecycleJobResponse == nil { + utils.PrintlnError(fmt.Errorf("lifecycle %s not found", lifecycleName)) + utils.PrintlnInfo("You can list all lifecycles with: qovery lifecycle list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.UpdateServiceExternalSecret(client, utils.Key, utils.Reference, utils.SecretManagerAccessId, lifecycle.LifecycleJobResponse.Id, utils.JobType) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been updated", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + lifecycleExternalSecretCmd.AddCommand(lifecycleExternalSecretUpdateCmd) + lifecycleExternalSecretUpdateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + lifecycleExternalSecretUpdateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + lifecycleExternalSecretUpdateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + lifecycleExternalSecretUpdateCmd.Flags().StringVarP(&lifecycleName, "lifecycle", "n", "", "Lifecycle Name") + lifecycleExternalSecretUpdateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + lifecycleExternalSecretUpdateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "New reference to the secret in the secrets provider") + lifecycleExternalSecretUpdateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "New secret manager access ID") + + _ = lifecycleExternalSecretUpdateCmd.MarkFlagRequired("key") + _ = lifecycleExternalSecretUpdateCmd.MarkFlagRequired("lifecycle") +} diff --git a/cmd/terraform_external_secret.go b/cmd/terraform_external_secret.go new file mode 100644 index 00000000..4f60dd55 --- /dev/null +++ b/cmd/terraform_external_secret.go @@ -0,0 +1,25 @@ +package cmd + +import ( + "os" + + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var terraformExternalSecretCmd = &cobra.Command{ + Use: "external-secret", + Short: "Manage terraform external secrets", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + if len(args) == 0 { + _ = cmd.Help() + os.Exit(0) + } + }, +} + +func init() { + terraformCmd.AddCommand(terraformExternalSecretCmd) +} diff --git a/cmd/terraform_external_secret_create.go b/cmd/terraform_external_secret_create.go new file mode 100644 index 00000000..744c0d90 --- /dev/null +++ b/cmd/terraform_external_secret_create.go @@ -0,0 +1,81 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var terraformExternalSecretCreateCmd = &cobra.Command{ + Use: "create", + Short: "Create terraform external 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 + } + + terraforms, _, err := client.TerraformsAPI.ListTerraforms(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + terraform := utils.FindByTerraformName(terraforms.GetResults(), terraformName) + + if terraform == nil { + utils.PrintlnError(fmt.Errorf("terraform %s not found", terraformName)) + utils.PrintlnInfo("You can list all terraforms with: qovery terraform list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.CreateServiceExternalSecret(client, projectId, envId, terraform.Id, utils.TerraformScope, utils.Key, utils.Reference, utils.SecretManagerAccessId, utils.MountPath) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been created", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + terraformExternalSecretCmd.AddCommand(terraformExternalSecretCreateCmd) + terraformExternalSecretCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + terraformExternalSecretCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + terraformExternalSecretCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + terraformExternalSecretCreateCmd.Flags().StringVarP(&terraformName, "terraform", "n", "", "Terraform Name") + terraformExternalSecretCreateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + terraformExternalSecretCreateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "Reference to the secret in the secrets provider") + terraformExternalSecretCreateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "Secret manager access ID") + terraformExternalSecretCreateCmd.Flags().StringVarP(&utils.TerraformScope, "scope", "", "TERRAFORM", "Scope of this external secret ") + terraformExternalSecretCreateCmd.Flags().StringVarP(&utils.MountPath, "mount-path", "", "", "Path where the secret will be mounted as a file") + + _ = terraformExternalSecretCreateCmd.MarkFlagRequired("key") + _ = terraformExternalSecretCreateCmd.MarkFlagRequired("reference") + _ = terraformExternalSecretCreateCmd.MarkFlagRequired("secret-manager-access-id") + _ = terraformExternalSecretCreateCmd.MarkFlagRequired("terraform") +} diff --git a/cmd/terraform_external_secret_delete.go b/cmd/terraform_external_secret_delete.go new file mode 100644 index 00000000..da0426a3 --- /dev/null +++ b/cmd/terraform_external_secret_delete.go @@ -0,0 +1,75 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var terraformExternalSecretDeleteCmd = &cobra.Command{ + Use: "delete", + Short: "Delete terraform external 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 + } + + terraforms, _, err := client.TerraformsAPI.ListTerraforms(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + terraform := utils.FindByTerraformName(terraforms.GetResults(), terraformName) + + if terraform == nil { + utils.PrintlnError(fmt.Errorf("terraform %s not found", terraformName)) + utils.PrintlnInfo("You can list all terraforms with: qovery terraform list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.DeleteServiceVariable(client, terraform.Id, utils.TerraformType, 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("External secret %s has been deleted", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + terraformExternalSecretCmd.AddCommand(terraformExternalSecretDeleteCmd) + terraformExternalSecretDeleteCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + terraformExternalSecretDeleteCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + terraformExternalSecretDeleteCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + terraformExternalSecretDeleteCmd.Flags().StringVarP(&terraformName, "terraform", "n", "", "Terraform Name") + terraformExternalSecretDeleteCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + + _ = terraformExternalSecretDeleteCmd.MarkFlagRequired("key") + _ = terraformExternalSecretDeleteCmd.MarkFlagRequired("terraform") +} diff --git a/cmd/terraform_external_secret_update.go b/cmd/terraform_external_secret_update.go new file mode 100644 index 00000000..2256d50b --- /dev/null +++ b/cmd/terraform_external_secret_update.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/pterm/pterm" + "github.com/spf13/cobra" + + "github.com/qovery/qovery-cli/utils" +) + +var terraformExternalSecretUpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update terraform external 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 + } + + terraforms, _, err := client.TerraformsAPI.ListTerraforms(context.Background(), envId).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + terraform := utils.FindByTerraformName(terraforms.GetResults(), terraformName) + + if terraform == nil { + utils.PrintlnError(fmt.Errorf("terraform %s not found", terraformName)) + utils.PrintlnInfo("You can list all terraforms with: qovery terraform list") + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + err = utils.UpdateServiceExternalSecret(client, utils.Key, utils.Reference, utils.SecretManagerAccessId, terraform.Id, utils.TerraformType) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("External secret %s has been updated", pterm.FgBlue.Sprintf("%s", utils.Key))) + }, +} + +func init() { + terraformExternalSecretCmd.AddCommand(terraformExternalSecretUpdateCmd) + terraformExternalSecretUpdateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + terraformExternalSecretUpdateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + terraformExternalSecretUpdateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + terraformExternalSecretUpdateCmd.Flags().StringVarP(&terraformName, "terraform", "n", "", "Terraform Name") + terraformExternalSecretUpdateCmd.Flags().StringVarP(&utils.Key, "key", "k", "", "External secret key") + terraformExternalSecretUpdateCmd.Flags().StringVarP(&utils.Reference, "reference", "r", "", "New reference to the secret in the secrets provider") + terraformExternalSecretUpdateCmd.Flags().StringVarP(&utils.SecretManagerAccessId, "secret-manager-access-id", "", "", "New secret manager access ID") + + _ = terraformExternalSecretUpdateCmd.MarkFlagRequired("key") + _ = terraformExternalSecretUpdateCmd.MarkFlagRequired("terraform") +} diff --git a/go.mod b/go.mod index c512badb..2e9bc3d6 100644 --- a/go.mod +++ b/go.mod @@ -24,7 +24,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/posthog/posthog-go v1.12.5 github.com/pterm/pterm v0.12.83 - github.com/qovery/qovery-client-go v0.0.0-20260610095547-986d768ca7f9 + github.com/qovery/qovery-client-go v0.0.0-20260610153209-3c28b05bfe2b github.com/sirupsen/logrus v1.9.4 github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.0.10 diff --git a/go.sum b/go.sum index 0702a9f1..0d8bb89f 100644 --- a/go.sum +++ b/go.sum @@ -189,8 +189,10 @@ github.com/posthog/posthog-go v1.12.5 h1:l/x3mpqisXJ0sTOyyRutsTQAgiWYuJT1uhN4cQr github.com/posthog/posthog-go v1.12.5/go.mod h1:xsVOW9YImilUcazwPNEq4PJDqEZf2KeCS758zXjwkPg= github.com/pterm/pterm v0.12.83 h1:ie+YmGmA727VuhxBlyGr74Ks+7McV6kT99IB8EU80aA= github.com/pterm/pterm v0.12.83/go.mod h1:xlgc6bFWyJIMtmLJvGim+L7jhSReilOlOnodeIYe4Tk= -github.com/qovery/qovery-client-go v0.0.0-20260610095547-986d768ca7f9 h1:vYYPlj1RNfR/8RXTE8ATWsguR25mVxktq3HNIUqPhyA= -github.com/qovery/qovery-client-go v0.0.0-20260610095547-986d768ca7f9/go.mod h1:mcXeQtxR4AIGIBaWLhy52S16UwL8/1fcDywDuSK1BZ4= +github.com/qovery/qovery-client-go v0.0.0-20260609072636-f548ebe903f2 h1:R6lG3dFH9/N7k7Hz+MMMCY1y3c0N9rmY6NAAjy1dkos= +github.com/qovery/qovery-client-go v0.0.0-20260609072636-f548ebe903f2/go.mod h1:mcXeQtxR4AIGIBaWLhy52S16UwL8/1fcDywDuSK1BZ4= +github.com/qovery/qovery-client-go v0.0.0-20260610153209-3c28b05bfe2b h1:BEeLs9mqTI93TyzHPfAhhrn1aIyXVZzNwkQOI6bN3yc= +github.com/qovery/qovery-client-go v0.0.0-20260610153209-3c28b05bfe2b/go.mod h1:mcXeQtxR4AIGIBaWLhy52S16UwL8/1fcDywDuSK1BZ4= github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ= github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= diff --git a/utils/env_var.go b/utils/env_var.go index 78a94a79..4acc271f 100644 --- a/utils/env_var.go +++ b/utils/env_var.go @@ -25,6 +25,10 @@ var EnvironmentScope string var Alias string var Key string var Value string +var SecretManagerAccessId string +var Reference string +var MountPath string +var TerraformScope string type EnvVarLines struct { lines map[string][]EnvVarLineOutput @@ -201,6 +205,101 @@ func CreateServiceVariable( return err } +func CreateServiceExternalSecret( + client *qovery.APIClient, + projectId string, + environmentId string, + serviceId string, + scope string, + key string, + reference string, + secretManagerAccessId string, + mountPath string, +) error { + parentId, parentScope, err := getParentIdByScope(scope, projectId, environmentId, serviceId) + if err != nil { + return err + } + + variableRequest := qovery.VariableRequest{ + Key: key, + Value: reference, + IsSecret: false, + VariableScope: parentScope, + VariableParentId: parentId, + } + variableRequest.SetSecretManagerAccessId(secretManagerAccessId) + if mountPath != "" { + variableRequest.SetMountPath(mountPath) + } + + _, _, err = client.VariableMainCallsAPI.CreateVariable(context.Background()).VariableRequest(variableRequest).Execute() + return err +} + +func UpdateServiceExternalSecret( + client *qovery.APIClient, + key string, + reference string, + secretManagerAccessId string, + serviceId string, + serviceType ServiceType, +) error { + envVars, err := ListServiceVariables(client, serviceId, serviceType) + if err != nil { + return err + } + + envVar := FindEnvironmentVariableByKey(key, envVars) + if envVar == nil { + return fmt.Errorf("external secret %s not found", pterm.FgRed.Sprintf("%s", key)) + } + + editRequest := qovery.VariableEditRequest{ + Key: key, + } + if reference != "" { + editRequest.SetValue(reference) + } + if secretManagerAccessId != "" { + editRequest.SetSecretManagerAccessId(secretManagerAccessId) + } + + _, _, err = client.VariableMainCallsAPI.EditVariable(context.Background(), envVar.Id).VariableEditRequest(editRequest).Execute() + return err +} + +func UpdateEnvironmentExternalSecret( + client *qovery.APIClient, + environmentId string, + key string, + reference string, + secretManagerAccessId string, +) error { + envVars, err := ListEnvironmentVariables(client, environmentId) + if err != nil { + return err + } + + envVar := FindEnvironmentVariableByKey(key, envVars) + if envVar == nil { + return fmt.Errorf("external secret %s not found", pterm.FgRed.Sprintf("%s", key)) + } + + editRequest := qovery.VariableEditRequest{ + Key: key, + } + if reference != "" { + editRequest.SetValue(reference) + } + if secretManagerAccessId != "" { + editRequest.SetSecretManagerAccessId(secretManagerAccessId) + } + + _, _, err = client.VariableMainCallsAPI.EditVariable(context.Background(), envVar.Id).VariableEditRequest(editRequest).Execute() + return err +} + func CreateEnvironmentVariable( client *qovery.APIClient, projectId string, @@ -411,6 +510,8 @@ func ServiceTypeToScope(serviceType ServiceType) (qovery.APIVariableScopeEnum, e return qovery.APIVARIABLESCOPEENUM_JOB, nil case HelmType: return qovery.APIVARIABLESCOPEENUM_HELM, nil + case TerraformType: + return qovery.APIVARIABLESCOPEENUM_TERRAFORM, nil } return qovery.APIVARIABLESCOPEENUM_BUILT_IN, fmt.Errorf("the service type %s is not supported", serviceType) @@ -430,6 +531,8 @@ func getParentIdByScope(scope string, projectId string, environmentId string, se return serviceId, qovery.APIVARIABLESCOPEENUM_JOB, nil case "HELM": return serviceId, qovery.APIVARIABLESCOPEENUM_HELM, nil + case "TERRAFORM": + return serviceId, qovery.APIVARIABLESCOPEENUM_TERRAFORM, nil } return "", qovery.APIVARIABLESCOPEENUM_BUILT_IN, fmt.Errorf("scope %s not supported", scope)