Skip to content

Commit

Permalink
feat(COR-719): add generate_certificate to custom domain
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Oct 4, 2023
1 parent e0f590f commit 3a95e04
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 7 deletions.
10 changes: 8 additions & 2 deletions cmd/application_domain_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -65,19 +68,21 @@ 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)
os.Exit(1)
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))))
},
}

Expand All @@ -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 <subdomain.domain.tld>")
applicationDomainCreateCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate")

_ = applicationDomainCreateCmd.MarkFlagRequired("application")
_ = applicationDomainCreateCmd.MarkFlagRequired("domain")
Expand Down
98 changes: 98 additions & 0 deletions cmd/application_domain_edit.go
Original file line number Diff line number Diff line change
@@ -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 <subdomain.domain.tld>")
applicationDomainEditCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate")

_ = applicationDomainEditCmd.MarkFlagRequired("application")
_ = applicationDomainEditCmd.MarkFlagRequired("domain")
}
10 changes: 9 additions & 1 deletion cmd/application_domain_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/qovery/qovery-client-go"
"os"
"strconv"
"strings"

"github.com/qovery/qovery-cli/utils"
Expand Down Expand Up @@ -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,
})
}

Expand All @@ -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)
Expand Down
9 changes: 7 additions & 2 deletions cmd/container_domain_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strconv"

"github.com/qovery/qovery-client-go"

Expand All @@ -12,6 +13,7 @@ import (
"github.com/spf13/cobra"
)


var containerDomainCreateCmd = &cobra.Command{
Use: "create",
Short: "Create container custom domain",
Expand Down Expand Up @@ -66,19 +68,21 @@ 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)
os.Exit(1)
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))))
},
}

Expand All @@ -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 <subdomain.domain.tld>")
containerDomainCreateCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate")

_ = containerDomainCreateCmd.MarkFlagRequired("container")
_ = containerDomainCreateCmd.MarkFlagRequired("domain")
Expand Down
98 changes: 98 additions & 0 deletions cmd/container_domain_edit.go
Original file line number Diff line number Diff line change
@@ -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 <subdomain.domain.tld>")
containerDomainEditCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate")

_ = containerDomainEditCmd.MarkFlagRequired("container")
_ = containerDomainEditCmd.MarkFlagRequired("domain")
}
10 changes: 9 additions & 1 deletion cmd/container_domain_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/qovery/qovery-client-go"
"os"
"strconv"
"strings"

"github.com/qovery/qovery-cli/utils"
Expand Down Expand Up @@ -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,
})
}

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 3a95e04

Please sign in to comment.