Skip to content

Commit

Permalink
feat(COR-916): Allow to define the GenerateCertificate option in a cu…
Browse files Browse the repository at this point in the history
…stom domain (#383)
  • Loading branch information
pggb25 committed Jun 24, 2024
1 parent 2e064e9 commit 772f17b
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 30 deletions.
4 changes: 4 additions & 0 deletions docs/data-sources/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ data "qovery_application" "my_application" {
<a id="nestedatt--custom_domains"></a>
### Nested Schema for `custom_domains`

Optional:

- `generate_certificate` (Boolean) Qovery will generate and manage the certificate for this domain.

Read-Only:

- `domain` (String) Your custom domain.
Expand Down
4 changes: 4 additions & 0 deletions docs/data-sources/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ data "qovery_container" "my_container" {
<a id="nestedatt--custom_domains"></a>
### Nested Schema for `custom_domains`

Optional:

- `generate_certificate` (Boolean) Qovery will generate and manage the certificate for this domain.

Read-Only:

- `domain` (String) Your custom domain.
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ Required:

- `domain` (String) Your custom domain.

Optional:

- `generate_certificate` (Boolean) Qovery will generate and manage the certificate for this domain.

Read-Only:

- `id` (String) Id of the custom domain.
Expand Down
4 changes: 4 additions & 0 deletions docs/resources/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ Required:

- `domain` (String) Your custom domain.

Optional:

- `generate_certificate` (Boolean) Qovery will generate and manage the certificate for this domain.

Read-Only:

- `id` (String) Id of the custom domain.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func (c containerQoveryAPI) Create(ctx context.Context, environmentID string, re
CreateContainerCustomDomain(ctx, newContainer.Id).
CustomDomainRequest(
qovery.CustomDomainRequest{
Domain: customDomain.Domain,
Domain: customDomain.Domain,
GenerateCertificate: customDomain.GenerateCertificate,
}).
Execute()
if err != nil || resp.StatusCode >= 400 {
Expand Down Expand Up @@ -150,7 +151,8 @@ func (c containerQoveryAPI) Update(ctx context.Context, containerID string, requ
EditContainerCustomDomain(ctx, containerID, customDomain.Id).
CustomDomainRequest(
qovery.CustomDomainRequest{
Domain: customDomain.Domain,
Domain: customDomain.Domain,
GenerateCertificate: customDomain.GenerateCertificate,
}).
Execute()
if err != nil || resp.StatusCode >= 400 {
Expand All @@ -162,7 +164,8 @@ func (c containerQoveryAPI) Update(ctx context.Context, containerID string, requ
CreateContainerCustomDomain(ctx, containerID).
CustomDomainRequest(
qovery.CustomDomainRequest{
Domain: customDomain.Domain,
Domain: customDomain.Domain,
GenerateCertificate: customDomain.GenerateCertificate,
}).
Execute()
if err != nil || resp.StatusCode >= 400 {
Expand Down
79 changes: 54 additions & 25 deletions qovery/custom_domain_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (
)

var customDomainAttrTypes = map[string]attr.Type{
"id": types.StringType,
"domain": types.StringType,
"validation_domain": types.StringType,
"status": types.StringType,
"id": types.StringType,
"domain": types.StringType,
"validation_domain": types.StringType,
"status": types.StringType,
"generate_certificate": types.BoolType,
}

type CustomDomainList []CustomDomain
Expand Down Expand Up @@ -71,27 +72,32 @@ func (domains CustomDomainList) diff(oldDomains CustomDomainList) client.CustomD
}

for _, d := range domains {
if !oldDomains.contains(d) {
do := oldDomains.find(ToString(d.Domain))
if do == nil {
diff.Create = append(diff.Create, d.toCreateRequest())
} else if do.GenerateCertificate != d.GenerateCertificate {
diff.Update = append(diff.Update, do.toUpdateRequest(d))
}
}

return diff
}

type CustomDomain struct {
Id types.String `tfsdk:"id"`
Domain types.String `tfsdk:"domain"`
ValidationDomain types.String `tfsdk:"validation_domain"`
Status types.String `tfsdk:"status"`
Id types.String `tfsdk:"id"`
Domain types.String `tfsdk:"domain"`
ValidationDomain types.String `tfsdk:"validation_domain"`
Status types.String `tfsdk:"status"`
GenerateCertificate types.Bool `tfsdk:"generate_certificate"`
}

func (d CustomDomain) toTerraformObject() types.Object {
var attributes = map[string]attr.Value{
"id": d.Id,
"domain": d.Domain,
"validation_domain": d.ValidationDomain,
"status": d.Status,
"id": d.Id,
"domain": d.Domain,
"validation_domain": d.ValidationDomain,
"status": d.Status,
"generate_certificate": d.GenerateCertificate,
}
terraformObjectValue, diagnostics := types.ObjectValue(customDomainAttrTypes, attributes)
if diagnostics.HasError() {
Expand All @@ -103,7 +109,8 @@ func (d CustomDomain) toTerraformObject() types.Object {
func (d CustomDomain) toCreateRequest() client.CustomDomainCreateRequest {
return client.CustomDomainCreateRequest{
CustomDomainRequest: qovery.CustomDomainRequest{
Domain: ToString(d.Domain),
Domain: ToString(d.Domain),
GenerateCertificate: ToBool(d.GenerateCertificate),
},
}
}
Expand All @@ -112,7 +119,8 @@ func (d CustomDomain) toUpdateRequest(new CustomDomain) client.CustomDomainUpdat
return client.CustomDomainUpdateRequest{
Id: ToString(d.Id),
CustomDomainRequest: qovery.CustomDomainRequest{
Domain: ToString(new.Domain),
Domain: ToString(new.Domain),
GenerateCertificate: ToBool(new.GenerateCertificate),
},
}
}
Expand All @@ -123,19 +131,39 @@ func (d CustomDomain) toDeleteRequest() client.CustomDomainDeleteRequest {
}
}

func fromCustomDomain(d *qovery.CustomDomain) CustomDomain {
func fromCustomDomain(plan *CustomDomain, d *qovery.CustomDomain) CustomDomain {
var generateCertificate *bool
if plan != nil && (plan.GenerateCertificate.IsNull() || plan.GenerateCertificate.IsUnknown()) {
// as GenerateCertificate is optional, terraform expect to receive null if GenerateCertificate is not defined in the plan
generateCertificate = nil
} else {
generateCertificate = &d.GenerateCertificate
}

return CustomDomain{
Id: FromString(d.Id),
Domain: FromString(d.Domain),
ValidationDomain: FromStringPointer(d.ValidationDomain),
Status: fromClientEnumPointer(d.Status),
Id: FromString(d.Id),
Domain: FromString(d.Domain),
ValidationDomain: FromStringPointer(d.ValidationDomain),
Status: fromClientEnumPointer(d.Status),
GenerateCertificate: FromBoolPointer(generateCertificate),
}
}

func findCustomDomainByDomain(initialState types.Set, domain string) *CustomDomain {
for _, elem := range initialState.Elements() {
customDomain := toCustomDomain(elem.(types.Object))
if customDomain.Domain.ValueString() == domain {
return &customDomain
}
}
return nil
}

func fromCustomDomainList(initialState types.Set, customDomains []*qovery.CustomDomain) CustomDomainList {
list := make([]CustomDomain, 0, len(customDomains))
for _, customDomain := range customDomains {
list = append(list, fromCustomDomain(customDomain))
found := findCustomDomainByDomain(initialState, customDomain.Domain)
list = append(list, fromCustomDomain(found, customDomain))
}

if len(list) == 0 && initialState.IsNull() {
Expand All @@ -146,10 +174,11 @@ func fromCustomDomainList(initialState types.Set, customDomains []*qovery.Custom

func toCustomDomain(v types.Object) CustomDomain {
return CustomDomain{
Id: v.Attributes()["id"].(types.String),
Domain: v.Attributes()["domain"].(types.String),
ValidationDomain: v.Attributes()["validation_domain"].(types.String),
Status: v.Attributes()["status"].(types.String),
Id: v.Attributes()["id"].(types.String),
Domain: v.Attributes()["domain"].(types.String),
ValidationDomain: v.Attributes()["validation_domain"].(types.String),
Status: v.Attributes()["status"].(types.String),
GenerateCertificate: v.Attributes()["generate_certificate"].(types.Bool),
}
}

Expand Down
4 changes: 4 additions & 0 deletions qovery/data_source_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ func (r applicationDataSource) Schema(_ context.Context, _ datasource.SchemaRequ
Description: "Your custom domain.",
Computed: true,
},
"generate_certificate": schema.BoolAttribute{
Description: "Qovery will generate and manage the certificate for this domain.",
Optional: true,
},
"validation_domain": schema.StringAttribute{
Description: "URL provided by Qovery. You must create a CNAME on your DNS provider using that URL.",
Computed: true,
Expand Down
4 changes: 4 additions & 0 deletions qovery/data_source_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ func (r containerDataSource) Schema(_ context.Context, _ datasource.SchemaReques
Description: "URL provided by Qovery. You must create a CNAME on your DNS provider using that URL.",
Computed: true,
},
"generate_certificate": schema.BoolAttribute{
Description: "Qovery will generate and manage the certificate for this domain.",
Optional: true,
},
"status": schema.StringAttribute{
Description: "Status of the custom domain.",
Computed: true,
Expand Down
5 changes: 4 additions & 1 deletion qovery/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package qovery
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
Expand Down Expand Up @@ -516,6 +515,10 @@ func (r applicationResource) Schema(_ context.Context, _ resource.SchemaRequest,
Description: "Your custom domain.",
Required: true,
},
"generate_certificate": schema.BoolAttribute{
Description: "Qovery will generate and manage the certificate for this domain.",
Optional: true,
},
"validation_domain": schema.StringAttribute{
Description: "URL provided by Qovery. You must create a CNAME on your DNS provider using that URL.",
Computed: true,
Expand Down
2 changes: 1 addition & 1 deletion qovery/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ func convertPortsToString(ports []servicePort) string {
func convertCustomDomainsToString(customDomains []string) string {
domains := make([]string, 0, len(customDomains))
for _, domain := range customDomains {
domains = append(domains, fmt.Sprintf(`{domain: "%s"}`, domain))
domains = append(domains, fmt.Sprintf(`{domain: "%s", generate_certificate: false}`, domain))
}
return fmt.Sprintf("[%s]", strings.Join(domains, ","))
}
Expand Down
4 changes: 4 additions & 0 deletions qovery/resource_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ func (r containerResource) Schema(_ context.Context, _ resource.SchemaRequest, r
Description: "Your custom domain.",
Required: true,
},
"generate_certificate": schema.BoolAttribute{
Description: "Qovery will generate and manage the certificate for this domain.",
Optional: true,
},
"validation_domain": schema.StringAttribute{
Description: "URL provided by Qovery. You must create a CNAME on your DNS provider using that URL.",
Computed: true,
Expand Down

0 comments on commit 772f17b

Please sign in to comment.