Skip to content

Commit

Permalink
feat(COR-803): helm define custom domains (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 authored Jan 2, 2024
1 parent 831a226 commit 89db244
Show file tree
Hide file tree
Showing 8 changed files with 475 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var chartGitCommitId string
var charGitCommitBranch string
var valuesOverrideCommitId string
var valuesOverrideCommitBranch string
var helmCustomDomain string

var helmCmd = &cobra.Command{
Use: "helm",
Expand Down
100 changes: 100 additions & 0 deletions cmd/helm_container_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cmd

import (
"context"
"fmt"
"os"
"strconv"

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

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


var helmDomainCreateCmd = &cobra.Command{
Use: "create",
Short: "Create helm 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
}

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
}

customDomains, _, err := client.CustomDomainAPI.ListHelmCustomDomain(context.Background(), helm.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(), helmCustomDomain)
if customDomain != nil {
utils.PrintlnError(fmt.Errorf("custom domain %s already exists", helmCustomDomain))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

generateCertificate := !doNotGenerateCertificate
req := qovery.CustomDomainRequest{
Domain: helmCustomDomain,
GenerateCertificate: generateCertificate,
}

createdDomain, _, err := client.CustomDomainAPI.CreateHelmCustomDomain(context.Background(), helm.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 (generate certificate: %s)", pterm.FgBlue.Sprintf(createdDomain.Domain), pterm.FgBlue.Sprintf(strconv.FormatBool(createdDomain.GenerateCertificate))))
},
}

func init() {
helmDomainCmd.AddCommand(helmDomainCreateCmd)
helmDomainCreateCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
helmDomainCreateCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
helmDomainCreateCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
helmDomainCreateCmd.Flags().StringVarP(&helmName, "helm", "n", "", "helm Name")
helmDomainCreateCmd.Flags().StringVarP(&helmCustomDomain, "domain", "", "", "Custom Domain <subdomain.domain.tld>")
helmDomainCreateCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate")

_ = helmDomainCreateCmd.MarkFlagRequired("helm")
_ = helmDomainCreateCmd.MarkFlagRequired("domain")
}
24 changes: 24 additions & 0 deletions cmd/helm_domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

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

var helmDomainCmd = &cobra.Command{
Use: "domain",
Short: "Manage helm domains",
Run: func(cmd *cobra.Command, args []string) {
utils.Capture(cmd)

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

func init() {
helmCmd.AddCommand(helmDomainCmd)
}
98 changes: 98 additions & 0 deletions cmd/helm_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 helmDomainEditCmd = &cobra.Command{
Use: "edit",
Short: "Edit helm 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
}

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
}

customDomains, _, err := client.CustomDomainAPI.ListHelmCustomDomain(context.Background(), helm.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(), helmCustomDomain)
if customDomain == nil {
utils.PrintlnError(fmt.Errorf("custom domain %s does not exist", helmCustomDomain))
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

generateCertificate := !doNotGenerateCertificate
req := qovery.CustomDomainRequest{
Domain: helmCustomDomain,
GenerateCertificate: generateCertificate,
}

editedDomain, _, err := client.HelmCustomDomainAPI.EditHelmCustomDomain(context.Background(), helm.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() {
helmDomainCmd.AddCommand(helmDomainEditCmd)
helmDomainEditCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
helmDomainEditCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
helmDomainEditCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
helmDomainEditCmd.Flags().StringVarP(&helmName, "helm", "n", "", "helm Name")
helmDomainEditCmd.Flags().StringVarP(&helmCustomDomain, "domain", "", "", "Custom Domain <subdomain.domain.tld>")
helmDomainEditCmd.Flags().BoolVarP(&doNotGenerateCertificate, "do-not-generate-certificate", "", false, "Do Not Generate Certificate")

_ = helmDomainEditCmd.MarkFlagRequired("helm")
_ = helmDomainEditCmd.MarkFlagRequired("domain")
}
160 changes: 160 additions & 0 deletions cmd/helm_domain_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"github.com/qovery/qovery-client-go"
"os"
"strconv"
"strings"

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

var helmDomainListCmd = &cobra.Command{
Use: "list",
Short: "List helm domains",
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
}

customDomains, _, err := client.CustomDomainAPI.ListHelmCustomDomain(context.Background(), helm.Id).Execute()

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

customDomainsSet := make(map[string]bool)
var data [][]string

for _, customDomain := range customDomains.GetResults() {
customDomainsSet[customDomain.Domain] = true

data = append(data, []string{
customDomain.Id,
"CUSTOM_DOMAIN",
customDomain.Domain,
*customDomain.ValidationDomain,
strconv.FormatBool(customDomain.GenerateCertificate),
})
}

links, _, err := client.HelmMainCallsAPI.ListHelmLinks(context.Background(), helm.Id).Execute()

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

if jsonFlag {
utils.Println(gethelmDomainJsonOutput(links.GetResults(), customDomains.GetResults()))
return
}

for _, link := range links.GetResults() {
if link.Url != nil {
domain := strings.ReplaceAll(*link.Url, "https://", "")
if !customDomainsSet[domain] {
data = append(data, []string{
"N/A",
"BUILT_IN_DOMAIN",
domain,
"N/A",
"N/A",
})
}
}
}

err = utils.PrintTable([]string{"Id", "Type", "Domain", "Validation Domain", "Generate Certificate"}, data)

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

func gethelmDomainJsonOutput(links []qovery.Link, domains []qovery.CustomDomain) string {
var results []interface{}

for _, link := range links {
if link.Url != nil {
results = append(results, map[string]interface{}{
"id": nil,
"type": "BUILT_IN_DOMAIN",
"domain": strings.ReplaceAll(*link.Url, "https://", ""),
"validation_domain": nil,
})
}
}

for _, domain := range domains {
results = append(results, map[string]interface{}{
"id": domain.Id,
"type": "CUSTOM_DOMAIN",
"domain": domain.Domain,
"validation_domain": *domain.ValidationDomain,
})
}

j, err := json.Marshal(results)

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

return string(j)
}

func init() {
helmDomainCmd.AddCommand(helmDomainListCmd)
helmDomainListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
helmDomainListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
helmDomainListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
helmDomainListCmd.Flags().StringVarP(&helmName, "helm", "n", "", "helm Name")
helmDomainListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output")

_ = helmDomainListCmd.MarkFlagRequired("helm")
}
Loading

0 comments on commit 89db244

Please sign in to comment.