From 3a95e04555e467c5c69d5fefd3877fbeec7e5402 Mon Sep 17 00:00:00 2001 From: Pierre Gerbelot Date: Tue, 3 Oct 2023 18:18:06 +0200 Subject: [PATCH] feat(COR-719): add generate_certificate to custom domain --- cmd/application_domain_create.go | 10 +++- cmd/application_domain_edit.go | 98 ++++++++++++++++++++++++++++++++ cmd/application_domain_list.go | 10 +++- cmd/container_domain_create.go | 9 ++- cmd/container_domain_edit.go | 98 ++++++++++++++++++++++++++++++++ cmd/container_domain_list.go | 10 +++- go.mod | 2 +- go.sum | 4 ++ 8 files changed, 234 insertions(+), 7 deletions(-) create mode 100644 cmd/application_domain_edit.go create mode 100644 cmd/container_domain_edit.go diff --git a/cmd/application_domain_create.go b/cmd/application_domain_create.go index af60f2fd..a328aa2a 100644 --- a/cmd/application_domain_create.go +++ b/cmd/application_domain_create.go @@ -5,12 +5,15 @@ import ( "fmt" "github.com/qovery/qovery-client-go" "os" + "strconv" "github.com/pterm/pterm" "github.com/qovery/qovery-cli/utils" "github.com/spf13/cobra" ) +var doNotGenerateCertificate bool + var applicationDomainCreateCmd = &cobra.Command{ Use: "create", Short: "Create application custom domain", @@ -65,11 +68,13 @@ var applicationDomainCreateCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + generateCertificate := !doNotGenerateCertificate req := qovery.CustomDomainRequest{ Domain: applicationCustomDomain, + GenerateCertificate: &generateCertificate, } - _, _, err = client.CustomDomainApi.CreateApplicationCustomDomain(context.Background(), application.Id).CustomDomainRequest(req).Execute() + createdDomain, _, err := client.CustomDomainApi.CreateApplicationCustomDomain(context.Background(), application.Id).CustomDomainRequest(req).Execute() if err != nil { utils.PrintlnError(err) @@ -77,7 +82,7 @@ var applicationDomainCreateCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } - utils.Println(fmt.Sprintf("Custom domain %s has been created", pterm.FgBlue.Sprintf(applicationCustomDomain))) + utils.Println(fmt.Sprintf("Custom domain %s has been created (generate certificate: %s)", pterm.FgBlue.Sprintf(createdDomain.Domain), pterm.FgBlue.Sprintf(strconv.FormatBool(*createdDomain.GenerateCertificate)))) }, } @@ -88,6 +93,7 @@ func init() { applicationDomainCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") applicationDomainCreateCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name") applicationDomainCreateCmd.Flags().StringVarP(&applicationCustomDomain, "domain", "", "", "Custom Domain ") + applicationDomainCreateCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate") _ = applicationDomainCreateCmd.MarkFlagRequired("application") _ = applicationDomainCreateCmd.MarkFlagRequired("domain") diff --git a/cmd/application_domain_edit.go b/cmd/application_domain_edit.go new file mode 100644 index 00000000..8b119cbe --- /dev/null +++ b/cmd/application_domain_edit.go @@ -0,0 +1,98 @@ +package cmd + +import ( + "context" + "fmt" + "github.com/qovery/qovery-client-go" + "os" + "strconv" + + "github.com/pterm/pterm" + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var applicationDomainEditCmd = &cobra.Command{ + Use: "edit", + Short: "Edit application custom domain", + 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 + } + + customDomains, _, err := client.CustomDomainApi.ListApplicationCustomDomain(context.Background(), application.Id).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + customDomain := utils.FindByCustomDomainName(customDomains.GetResults(), applicationCustomDomain) + if customDomain == nil { + utils.PrintlnError(fmt.Errorf("custom domain %s does not exist", applicationCustomDomain)) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + generateCertificate := !doNotGenerateCertificate + req := qovery.CustomDomainRequest{ + Domain: applicationCustomDomain, + GenerateCertificate: &generateCertificate, + } + + editedDomain, _, err := client.CustomDomainApi.EditCustomDomain(context.Background(), application.Id, customDomain.Id).CustomDomainRequest(req).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("Custom domain %s has been edited (generate certificate: %s)", pterm.FgBlue.Sprintf(editedDomain.Domain), pterm.FgBlue.Sprintf(strconv.FormatBool(*editedDomain.GenerateCertificate)))) + }, +} + +func init() { + applicationDomainCmd.AddCommand(applicationDomainEditCmd) + applicationDomainEditCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + applicationDomainEditCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + applicationDomainEditCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + applicationDomainEditCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name") + applicationDomainEditCmd.Flags().StringVarP(&applicationCustomDomain, "domain", "", "", "Custom Domain ") + applicationDomainEditCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate") + + _ = applicationDomainEditCmd.MarkFlagRequired("application") + _ = applicationDomainEditCmd.MarkFlagRequired("domain") +} diff --git a/cmd/application_domain_list.go b/cmd/application_domain_list.go index 101210f3..143d1b50 100644 --- a/cmd/application_domain_list.go +++ b/cmd/application_domain_list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/qovery/qovery-client-go" "os" + "strconv" "strings" "github.com/qovery/qovery-cli/utils" @@ -79,11 +80,17 @@ var applicationDomainListCmd = &cobra.Command{ for _, customDomain := range customDomains.GetResults() { customDomainsSet[customDomain.Domain] = true + generateCertificate := "N/A" + if customDomain.GenerateCertificate != nil { + generateCertificate = strconv.FormatBool(*customDomain.GenerateCertificate) + } + data = append(data, []string{ customDomain.Id, "CUSTOM_DOMAIN", customDomain.Domain, *customDomain.ValidationDomain, + generateCertificate, }) } @@ -96,12 +103,13 @@ var applicationDomainListCmd = &cobra.Command{ "BUILT_IN_DOMAIN", domain, "N/A", + "N/A", }) } } } - err = utils.PrintTable([]string{"Id", "Type", "Domain", "Validation Domain"}, data) + err = utils.PrintTable([]string{"Id", "Type", "Domain", "Validation Domain", "Generate Certificate"}, data) if err != nil { utils.PrintlnError(err) diff --git a/cmd/container_domain_create.go b/cmd/container_domain_create.go index a47e6a1c..40d747d5 100644 --- a/cmd/container_domain_create.go +++ b/cmd/container_domain_create.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "strconv" "github.com/qovery/qovery-client-go" @@ -12,6 +13,7 @@ import ( "github.com/spf13/cobra" ) + var containerDomainCreateCmd = &cobra.Command{ Use: "create", Short: "Create container custom domain", @@ -66,11 +68,13 @@ var containerDomainCreateCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + generateCertificate := !doNotGenerateCertificate req := qovery.CustomDomainRequest{ Domain: containerCustomDomain, + GenerateCertificate: &generateCertificate, } - _, _, err = client.ContainerCustomDomainApi.CreateContainerCustomDomain(context.Background(), container.Id).CustomDomainRequest(req).Execute() + createdDomain, _, err := client.ContainerCustomDomainApi.CreateContainerCustomDomain(context.Background(), container.Id).CustomDomainRequest(req).Execute() if err != nil { utils.PrintlnError(err) @@ -78,7 +82,7 @@ var containerDomainCreateCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } - utils.Println(fmt.Sprintf("Custom domain %s has been created", pterm.FgBlue.Sprintf(containerCustomDomain))) + utils.Println(fmt.Sprintf("Custom domain %s has been created (generate certificate: %s)", pterm.FgBlue.Sprintf(createdDomain.Domain), pterm.FgBlue.Sprintf(strconv.FormatBool(*createdDomain.GenerateCertificate)))) }, } @@ -89,6 +93,7 @@ func init() { containerDomainCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") containerDomainCreateCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name") containerDomainCreateCmd.Flags().StringVarP(&containerCustomDomain, "domain", "", "", "Custom Domain ") + containerDomainCreateCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate") _ = containerDomainCreateCmd.MarkFlagRequired("container") _ = containerDomainCreateCmd.MarkFlagRequired("domain") diff --git a/cmd/container_domain_edit.go b/cmd/container_domain_edit.go new file mode 100644 index 00000000..effa9e6a --- /dev/null +++ b/cmd/container_domain_edit.go @@ -0,0 +1,98 @@ +package cmd + +import ( + "context" + "fmt" + "github.com/qovery/qovery-client-go" + "os" + "strconv" + + "github.com/pterm/pterm" + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var containerDomainEditCmd = &cobra.Command{ + Use: "edit", + Short: "Edit container custom domain", + 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 + } + + customDomains, _, err := client.ContainerCustomDomainApi.ListContainerCustomDomain(context.Background(), container.Id).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + customDomain := utils.FindByCustomDomainName(customDomains.GetResults(), containerCustomDomain) + if customDomain == nil { + utils.PrintlnError(fmt.Errorf("custom domain %s does not exist", containerCustomDomain)) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + generateCertificate := !doNotGenerateCertificate + req := qovery.CustomDomainRequest{ + Domain: containerCustomDomain, + GenerateCertificate: &generateCertificate, + } + + editedDomain, _, err := client.ContainerCustomDomainApi.EditContainerCustomDomain(context.Background(), container.Id, customDomain.Id).CustomDomainRequest(req).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + utils.Println(fmt.Sprintf("Custom domain %s has been edited (generate certificate: %s)", pterm.FgBlue.Sprintf(editedDomain.Domain), pterm.FgBlue.Sprintf(strconv.FormatBool(*editedDomain.GenerateCertificate)))) + }, +} + +func init() { + containerDomainCmd.AddCommand(containerDomainEditCmd) + containerDomainEditCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + containerDomainEditCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + containerDomainEditCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + containerDomainEditCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name") + containerDomainEditCmd.Flags().StringVarP(&containerCustomDomain, "domain", "", "", "Custom Domain ") + containerDomainEditCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate") + + _ = containerDomainEditCmd.MarkFlagRequired("container") + _ = containerDomainEditCmd.MarkFlagRequired("domain") +} diff --git a/cmd/container_domain_list.go b/cmd/container_domain_list.go index cd9d552f..03548667 100644 --- a/cmd/container_domain_list.go +++ b/cmd/container_domain_list.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/qovery/qovery-client-go" "os" + "strconv" "strings" "github.com/qovery/qovery-cli/utils" @@ -66,11 +67,17 @@ var containerDomainListCmd = &cobra.Command{ for _, customDomain := range customDomains.GetResults() { customDomainsSet[customDomain.Domain] = true + generateCertificate := "N/A" + if customDomain.GenerateCertificate != nil { + generateCertificate = strconv.FormatBool(*customDomain.GenerateCertificate) + } + data = append(data, []string{ customDomain.Id, "CUSTOM_DOMAIN", customDomain.Domain, *customDomain.ValidationDomain, + generateCertificate, }) } @@ -96,12 +103,13 @@ var containerDomainListCmd = &cobra.Command{ "BUILT_IN_DOMAIN", domain, "N/A", + "N/A", }) } } } - err = utils.PrintTable([]string{"Id", "Type", "Domain", "Validation Domain"}, data) + err = utils.PrintTable([]string{"Id", "Type", "Domain", "Validation Domain", "Generate Certificate"}, data) if err != nil { utils.PrintlnError(err) diff --git a/go.mod b/go.mod index 6be65c7d..9b0860e7 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/posthog/posthog-go v0.0.0-20221221115252-24dfed35d71a github.com/pterm/pterm v0.12.55 - github.com/qovery/qovery-client-go v0.0.0-20231003140602-a877ab9bdb9d + github.com/qovery/qovery-client-go v0.0.0-20231003154456-09b5ae9eae15 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 4e92ad13..7e7738c6 100644 --- a/go.sum +++ b/go.sum @@ -291,6 +291,10 @@ github.com/qovery/qovery-client-go v0.0.0-20231003132450-36f1e524c6f8 h1:ZfXg+Bd github.com/qovery/qovery-client-go v0.0.0-20231003132450-36f1e524c6f8/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= github.com/qovery/qovery-client-go v0.0.0-20231003140602-a877ab9bdb9d h1:cUlEoJ2O9iAxb8aN8y9lmmZzcWb2edmXyW02/wF1qGM= github.com/qovery/qovery-client-go v0.0.0-20231003140602-a877ab9bdb9d/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= +github.com/qovery/qovery-client-go v0.0.0-20231003144739-772ce5bcc19e h1:o0TQ5QHRqGoONTMZL232RMZ7F54JOBlAMisyHHLG8vE= +github.com/qovery/qovery-client-go v0.0.0-20231003144739-772ce5bcc19e/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= +github.com/qovery/qovery-client-go v0.0.0-20231003154456-09b5ae9eae15 h1:pwdYIUSwOMXjYtuVErvuWN3IQYY1RhjYvGFKOt+y9CQ= +github.com/qovery/qovery-client-go v0.0.0-20231003154456-09b5ae9eae15/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=