Skip to content

Commit

Permalink
fix custom_domain registration (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
jubrad authored Dec 31, 2023
1 parent 19664bd commit 7df383a
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions provider/resource_frontegg_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"errors"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -51,6 +52,10 @@ type fronteggCustomDomain struct {
CustomDomain string `json:"customDomain"`
}

type fronteggCustomDomainVerification struct {
Verified bool `json:"verified"`
}

type fronteggAuth struct {
ID string `json:"id"`
AllowNotVerifiedUsersLogin bool `json:"allowNotVerifiedUsersLogin"`
Expand Down Expand Up @@ -1458,29 +1463,28 @@ func resourceFronteggWorkspaceUpdate(ctx context.Context, d *schema.ResourceData
in := fronteggCustomDomain{CustomDomain: domain.(string)}

customDomainErr := clientHolder.ApiClient.Post(ctx, fronteggCustomDomainURL, in, nil)
if customDomainErr != nil {
diag.FromErr(customDomainErr)
}

var err error
verified := false
if customDomainErr != nil && strings.Contains(customDomainErr.Error(), "CName not found") {
// Retry for up to a minute if the CName is not found, in case it
// was just installed and DNS is still propagating.
err = retry.RetryContext(ctx, time.Minute, func() *retry.RetryError {
if err := clientHolder.ApiClient.Post(ctx, fronteggCustomDomainVerifyURL, verified, nil); err != nil || !verified {
if strings.Contains(err.Error(), "CName not found") || !verified {
return retry.RetryableError(err)
} else {
return retry.NonRetryableError(err)
}
var out fronteggCustomDomainVerification
err = retry.RetryContext(ctx, time.Minute, func() *retry.RetryError {
if err := clientHolder.ApiClient.Post(ctx, fronteggCustomDomainVerifyURL, nil, &out); err != nil {
if strings.Contains(err.Error(), "CName not found") {
return retry.RetryableError(err)
} else {
return retry.NonRetryableError(err)
}
}
if !out.Verified {
return retry.RetryableError(errors.New("domain not yet verified"))
} else {
return nil
})
}

if verified {
err = clientHolder.ApiClient.Post(ctx, fronteggCustomDomainURL, in, nil)
}
}
})

if !verified || err != nil {
if err != nil {
return diag.FromErr(err)
}
}
Expand Down

0 comments on commit 7df383a

Please sign in to comment.