-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1588 from okta/add-email-domain
add resource email domain
- Loading branch information
Showing
9 changed files
with
253 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
data "okta_brands" "test" { | ||
} | ||
|
||
resource "okta_email_domain" "test" { | ||
brand_id = tolist(data.okta_brands.test.brands)[0].id | ||
domain = "example.com" | ||
display_name = "test" | ||
user_name = "fff" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package okta | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/okta/okta-sdk-golang/v3/okta" | ||
) | ||
|
||
func resourceEmailDomain() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceEmailDomainCreate, | ||
ReadContext: resourceEmailDomainRead, | ||
UpdateContext: resourceEmailDomainUpdate, | ||
DeleteContext: resourceEmailDomainDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"brand_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Brand id", | ||
}, | ||
"domain": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Domain name", | ||
}, | ||
"display_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Display name", | ||
}, | ||
"user_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "User name", | ||
}, | ||
"validation_status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Status of the email domain. Values: NOT_STARTED, IN_PROGRESS, VERIFIED, COMPLETED", | ||
}, | ||
"dns_validation_records": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "TXT and cname records to be registered for the email Domain", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"fqdn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "DNS record name", | ||
}, | ||
"record_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Record type can be TXT or cname", | ||
}, | ||
"values": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Description: "DNS record values", | ||
}, | ||
"expiration": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "DNS TXT record expiration", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceEmailDomainCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
emailDomain, _, err := getOktaV3ClientFromMetadata(m).EmailDomainApi.CreateEmailDomain(ctx).EmailDomain(buildEmailDomain(d)).Execute() | ||
if err != nil { | ||
return diag.Errorf("failed to create email domain: %v", err) | ||
} | ||
d.SetId(emailDomain.GetId()) | ||
return resourceEmailDomainRead(ctx, d, m) | ||
} | ||
|
||
func resourceEmailDomainRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
emailDomain, resp, err := getOktaV3ClientFromMetadata(m).EmailDomainApi.GetEmailDomain(ctx, d.Id()).Execute() | ||
if err := v3suppressErrorOn404(resp, err); err != nil { | ||
return diag.Errorf("failed to get email domain: %v", err) | ||
} | ||
if emailDomain == nil || emailDomain.GetValidationStatus() == "DELETED" { | ||
d.SetId("") | ||
return nil | ||
} | ||
_ = d.Set("validation_status", emailDomain.GetValidationStatus()) | ||
_ = d.Set("domain", emailDomain.GetDomain()) | ||
_ = d.Set("display_name", emailDomain.GetDisplayName()) | ||
_ = d.Set("user_name", emailDomain.GetUserName()) | ||
dnsValidation := emailDomain.GetDnsValidationRecords() | ||
arr := make([]map[string]interface{}, len(dnsValidation)) | ||
for i := range dnsValidation { | ||
arr[i] = map[string]interface{}{ | ||
"fqdn": dnsValidation[i].GetFqdn(), | ||
"record_type": dnsValidation[i].GetRecordType(), | ||
"expiration": dnsValidation[i].GetExpiration(), | ||
} | ||
if len(dnsValidation[i].GetValues()) > 0 { | ||
arr[i]["value"] = dnsValidation[i].GetValues() | ||
} | ||
} | ||
err = setNonPrimitives(d, map[string]interface{}{"dns_validation_records": arr}) | ||
if err != nil { | ||
return diag.Errorf("failed to set DNS validation records: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func resourceEmailDomainUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
_, _, err := getOktaV3ClientFromMetadata(m).EmailDomainApi.ReplaceEmailDomain(ctx, d.Id()).UpdateEmailDomain(buildUpdateEmailDomain(d)).Execute() | ||
if err != nil { | ||
return diag.Errorf("failed to update email domain: %v", err) | ||
} | ||
return resourceEmailDomainRead(ctx, d, m) | ||
} | ||
|
||
func resourceEmailDomainDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
emailDomain, resp, err := getOktaV3ClientFromMetadata(m).EmailDomainApi.GetEmailDomain(ctx, d.Id()).Execute() | ||
if err := v3suppressErrorOn404(resp, err); err != nil { | ||
return diag.Errorf("failed to get email domain: %v", err) | ||
} | ||
if emailDomain == nil || emailDomain.GetValidationStatus() == "DELETED" { | ||
return nil | ||
} | ||
_, err = getOktaV3ClientFromMetadata(m).EmailDomainApi.DeleteEmailDomain(ctx, emailDomain.GetId()).Execute() | ||
if err := v3suppressErrorOn404(resp, err); err != nil { | ||
return diag.Errorf("failed to delete email domain: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func buildEmailDomain(d *schema.ResourceData) okta.EmailDomain { | ||
return okta.EmailDomain{ | ||
BrandId: d.Get("brand_id").(string), | ||
Domain: d.Get("domain").(string), | ||
DisplayName: d.Get("display_name").(string), | ||
UserName: d.Get("user_name").(string), | ||
} | ||
} | ||
|
||
func buildUpdateEmailDomain(d *schema.ResourceData) okta.UpdateEmailDomain { | ||
return okta.UpdateEmailDomain{ | ||
DisplayName: d.Get("display_name").(string), | ||
UserName: d.Get("user_name").(string), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package okta | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccOktaEmailDomain(t *testing.T) { | ||
mgr := newFixtureManager(emailDomain, t.Name()) | ||
config := mgr.GetFixtures("basic.tf", t) | ||
resourceName := fmt.Sprintf("%s.test", emailDomain) | ||
|
||
oktaResourceTest(t, resource.TestCase{ | ||
PreCheck: testAccPreCheck(t), | ||
ErrorCheck: testAccErrorChecks(t), | ||
ProviderFactories: testAccProvidersFactories, | ||
CheckDestroy: createCheckResourceDestroy(emailDomain, emailDomainExists), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
ensureResourceExists(resourceName, emailDomainExists), | ||
resource.TestCheckResourceAttrSet(resourceName, "brand_id"), | ||
resource.TestCheckResourceAttr(resourceName, "domain", "example.com"), | ||
resource.TestCheckResourceAttr(resourceName, "display_name", "test"), | ||
resource.TestCheckResourceAttr(resourceName, "user_name", "fff"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func emailDomainExists(id string) (bool, error) { | ||
client := oktaV3ClientForTest() | ||
emailDomain, resp, err := client.EmailDomainApi.GetEmailDomain(context.Background(), id).Execute() | ||
if err := v3suppressErrorOn404(resp, err); err != nil { | ||
return false, err | ||
} | ||
return emailDomain != nil && emailDomain.GetValidationStatus() != "DELETED", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package okta | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func resourceEmailDomainVerification() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceEmailDomainVerificationCreate, | ||
ReadContext: resourceFuncNoOp, | ||
DeleteContext: resourceFuncNoOp, | ||
Importer: nil, | ||
Schema: map[string]*schema.Schema{ | ||
"email_domain_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Email domain ID", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceEmailDomainVerificationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
_, _, err := getOktaV3ClientFromMetadata(m).EmailDomainApi.VerifyEmailDomain(ctx, d.Get("email_domain_id").(string)).Execute() | ||
if err != nil { | ||
return diag.Errorf("failed to verify email domain: %v", err) | ||
} | ||
d.SetId(d.Get("email_domain_id").(string)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters