Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new okta_org_support and okta_org_configuration resources #749

Merged
merged 2 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/okta_org_configuration/standard.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "okta_org_configuration" "test" {
company_name = "Hashicorp CI Terraform Provider Okta"
website = "https://terraform.io"
}
6 changes: 6 additions & 0 deletions examples/okta_org_configuration/standard_updated.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "okta_org_configuration" "test" {
company_name = "Hashicorp CI Terraform Provider Okta Updated"
website = "https://terraform.com"
phone_number = "replace_with_uuid"

}
3 changes: 3 additions & 0 deletions examples/okta_org_support/extended.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "okta_org_support" "test" {
extend_by = 1
}
1 change: 1 addition & 0 deletions examples/okta_org_support/standard.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resource "okta_org_support" "test" {}
6 changes: 5 additions & 1 deletion okta/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ const (
idpSocial = "okta_idp_social"
inlineHook = "okta_inline_hook"
networkZone = "okta_network_zone"
orgConfiguration = "okta_org_configuration"
orgSupport = "okta_org_support"
policy = "okta_policy"
policyMfa = "okta_policy_mfa"
policyMfaDefault = "okta_policy_mfa_default"
Expand All @@ -89,8 +91,8 @@ const (
policyRuleSignOn = "okta_policy_rule_signon"
policySignOn = "okta_policy_signon"
profileMapping = "okta_profile_mapping"
securityNotificationEmails = "okta_security_notification_emails"
roleSubscription = "okta_role_subscription"
securityNotificationEmails = "okta_security_notification_emails"
templateEmail = "okta_template_email"
templateSms = "okta_template_sms"
threatInsightSettings = "okta_threat_insight_settings"
Expand Down Expand Up @@ -260,6 +262,8 @@ func Provider() *schema.Provider {
idpSocial: resourceIdpSocial(),
inlineHook: resourceInlineHook(),
networkZone: resourceNetworkZone(),
orgConfiguration: resourceOrgConfiguration(),
orgSupport: resourceOrgSupport(),
policyMfa: resourcePolicyMfa(),
policyMfaDefault: resourcePolicyMfaDefault(),
policyPassword: resourcePolicyPassword(),
Expand Down
283 changes: 283 additions & 0 deletions okta/resource_okta_org_configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
package okta

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/okta-sdk-golang/v2/okta"
)

func resourceOrgConfiguration() *schema.Resource {
return &schema.Resource{
CreateContext: resourceOrgSettingsCreate,
ReadContext: resourceOrgSettingsRead,
UpdateContext: resourceOrgSettingsUpdate,
DeleteContext: resourceOrgSettingsDelete,
Importer: &schema.ResourceImporter{StateContext: schema.ImportStatePassthroughContext},
Schema: map[string]*schema.Schema{
"company_name": {
Type: schema.TypeString,
Required: true,
Description: "Name of org",
},
"website": {
Type: schema.TypeString,
Optional: true,
Description: "The org's website",
},
"phone_number": {
Type: schema.TypeString,
Optional: true,
Description: "Support help phone of org",
},
"end_user_support_help_url": {
Type: schema.TypeString,
Optional: true,
Description: "Support link of org",
},
"support_phone_number": {
Type: schema.TypeString,
Optional: true,
Description: "Support help phone of org",
},
"address_1": {
Type: schema.TypeString,
Optional: true,
Description: "Primary address of org",
},
"address_2": {
Type: schema.TypeString,
Optional: true,
Description: "Secondary address of org",
},
"city": {
Type: schema.TypeString,
Optional: true,
Description: "City of org",
},
"state": {
Type: schema.TypeString,
Optional: true,
Description: "State of org",
},
"country": {
Type: schema.TypeString,
Optional: true,
Description: "Country of org",
},
"postal_code": {
Type: schema.TypeString,
Optional: true,
Description: "Postal code of org",
},
"expires_at": {
Type: schema.TypeString,
Computed: true,
Description: "Expiration of org",
},
"subdomain": {
Type: schema.TypeString,
Computed: true,
Description: "Subdomain of org",
},
"logo": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: logoValid(),
Description: "Local path to logo of the org.",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return new == ""
},
StateFunc: func(val interface{}) string {
logoPath := val.(string)
if logoPath == "" {
return logoPath
}
return fmt.Sprintf("%s (%s)", logoPath, computeFileHash(logoPath))
},
},
"billing_contact_user": {
Type: schema.TypeString,
Optional: true,
Description: "User ID representing the billing contact",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return new == ""
},
},
"technical_contact_user": {
Type: schema.TypeString,
Optional: true,
Description: "User ID representing the technical contact",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return new == ""
},
},
"opt_out_communication_emails": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicates whether the org's users receive Okta Communication emails",
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return new == ""
},
},
},
}
}

func resourceOrgSettingsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
settings, _, err := getOktaClientFromMetadata(m).OrgSetting.PartialUpdateOrgSetting(ctx, buildOrgSettings(d))
if err != nil {
return diag.Errorf("failed to update org settings: %v", err)
}
d.SetId(settings.Id)
logo, ok := d.GetOk("logo")
if ok {
_, err := getSupplementFromMetadata(m).UploadOrgLogo(ctx, logo.(string))
if err != nil {
return diag.Errorf("failed to upload org logo: %v", err)
}
}
err = updateCommunicationSettings(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
err = updateContactUsers(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
return resourceOrgSettingsRead(ctx, d, m)
}

func resourceOrgSettingsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
settings, _, err := getOktaClientFromMetadata(m).OrgSetting.GetOrgSettings(ctx)
if err != nil {
return diag.Errorf("failed to get org settings: %v", err)
}
setOrgSettings(d, settings)
comm, _, err := getOktaClientFromMetadata(m).OrgSetting.GetOktaCommunicationSettings(ctx)
if err != nil {
return diag.Errorf("failed to get org communication settings: %v", err)
}
_ = d.Set("opt_out_communication_emails", comm.OptOutEmailUsers)
billingContact, _, err := getOktaClientFromMetadata(m).OrgSetting.GetOrgContactUser(ctx, "BILLING")
if err != nil {
return diag.Errorf("failed to get billing contact user: %v", err)
}
_ = d.Set("billing_contact_user", billingContact.UserId)
technicalContact, _, err := getOktaClientFromMetadata(m).OrgSetting.GetOrgContactUser(ctx, "TECHNICAL")
if err != nil {
return diag.Errorf("failed to get technical contact user: %v", err)
}
_ = d.Set("technical_contact_user", technicalContact.UserId)
return nil
}

func resourceOrgSettingsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
_, _, err := getOktaClientFromMetadata(m).OrgSetting.UpdateOrgSetting(ctx, buildOrgSettings(d))
if err != nil {
return diag.Errorf("failed to update org settings: %v", err)
}
logo, ok := d.GetOk("logo")
if ok {
_, err := getSupplementFromMetadata(m).UploadOrgLogo(ctx, logo.(string))
if err != nil {
return diag.Errorf("failed to upload org logo: %v", err)
}
}
err = updateCommunicationSettings(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
err = updateContactUsers(ctx, d, m)
if err != nil {
return diag.FromErr(err)
}
return resourceOrgSettingsRead(ctx, d, m)
}

func resourceOrgSettingsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return nil
}

func updateCommunicationSettings(ctx context.Context, d *schema.ResourceData, m interface{}) error {
comm, _, err := getOktaClientFromMetadata(m).OrgSetting.GetOktaCommunicationSettings(ctx)
if err != nil {
return fmt.Errorf("failed to get org communication settings: %v", err)
}
o, ok := d.GetOk("opt_out_communication_emails")
if ok && *comm.OptOutEmailUsers != o.(bool) {
if o.(bool) {
comm, _, err = getOktaClientFromMetadata(m).OrgSetting.OptOutUsersFromOktaCommunicationEmails(ctx)
} else {
comm, _, err = getOktaClientFromMetadata(m).OrgSetting.OptInUsersToOktaCommunicationEmails(ctx)
}
if err != nil {
return fmt.Errorf("failed to update org communication settings: %v", err)
}
}
return nil
}

func updateContactUsers(ctx context.Context, d *schema.ResourceData, m interface{}) error {
billingContact, _, err := getOktaClientFromMetadata(m).OrgSetting.GetOrgContactUser(ctx, "BILLING")
if err != nil {
return fmt.Errorf("failed to get billing contact user: %v", err)
}
billing, ok := d.GetOk("billing_contact_user")
if ok && billingContact.UserId != billing.(string) {
_, _, err := getOktaClientFromMetadata(m).OrgSetting.UpdateOrgContactUser(ctx,
"BILLING", okta.UserIdString{UserId: billing.(string)})
if err != nil {
return fmt.Errorf("failed to update billing contact user: %v", err)
}
}
technicalContact, _, err := getOktaClientFromMetadata(m).OrgSetting.GetOrgContactUser(ctx, "TECHNICAL")
if err != nil {
return fmt.Errorf("failed to get technical contact user: %v", err)
}
technical, ok := d.GetOk("technical_contact_user")
if ok && technicalContact.UserId != technical.(string) {
_, _, err := getOktaClientFromMetadata(m).OrgSetting.UpdateOrgContactUser(ctx,
"TECHNICAL", okta.UserIdString{UserId: technical.(string)})
if err != nil {
return fmt.Errorf("failed to update technical contact user: %v", err)
}
}
return nil
}

func setOrgSettings(d *schema.ResourceData, settings *okta.OrgSetting) {
_ = d.Set("address_1", settings.Address1)
_ = d.Set("address_2", settings.Address2)
_ = d.Set("city", settings.City)
_ = d.Set("company_name", settings.CompanyName)
_ = d.Set("country", settings.Country)
_ = d.Set("end_user_support_help_url", settings.EndUserSupportHelpURL)
_ = d.Set("phone_number", settings.PhoneNumber)
_ = d.Set("postal_code", settings.PostalCode)
_ = d.Set("state", settings.State)
_ = d.Set("support_phone_number", settings.SupportPhoneNumber)
_ = d.Set("website", settings.Website)
_ = d.Set("subdomain", settings.Subdomain)
if settings.ExpiresAt != nil {
_ = d.Set("expires_at", settings.ExpiresAt.String())
}
}

func buildOrgSettings(d *schema.ResourceData) okta.OrgSetting {
return okta.OrgSetting{
Address1: d.Get("address_1").(string),
Address2: d.Get("address_2").(string),
City: d.Get("city").(string),
CompanyName: d.Get("company_name").(string),
Country: d.Get("country").(string),
EndUserSupportHelpURL: d.Get("end_user_support_help_url").(string),
PhoneNumber: d.Get("phone_number").(string),
PostalCode: d.Get("postal_code").(string),
State: d.Get("state").(string),
SupportPhoneNumber: d.Get("support_phone_number").(string),
Website: d.Get("website").(string),
}
}
44 changes: 44 additions & 0 deletions okta/resource_okta_org_configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package okta

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccOktaOrgConfiguration(t *testing.T) {
ri := acctest.RandInt()
resourceName := fmt.Sprintf("%s.test", orgConfiguration)
mgr := newFixtureManager(orgConfiguration)
config := mgr.GetFixtures("standard.tf", ri, t)
updatedConfig := mgr.GetFixtures("standard_updated.tf", ri, t)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProvidersFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "company_name", "Hashicorp CI Terraform Provider Okta"),
resource.TestCheckResourceAttr(resourceName, "website", "https://terraform.io"),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "company_name", "Hashicorp CI Terraform Provider Okta Updated"),
resource.TestCheckResourceAttr(resourceName, "website", "https://terraform.com"),
resource.TestCheckResourceAttr(resourceName, "phone_number", strconv.Itoa(ri)),
),
},
{
Config: config,
},
},
})
}
Loading