/
resource_okta_security_notification_emails.go
124 lines (116 loc) · 5.85 KB
/
resource_okta_security_notification_emails.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package okta
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/terraform-provider-okta/sdk"
)
func resourceSecurityNotificationEmails() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSecurityNotificationEmailsCreate,
ReadContext: resourceSecurityNotificationEmailsRead,
UpdateContext: resourceSecurityNotificationEmailsUpdate,
DeleteContext: resourceSecurityNotificationEmailsDelete,
Importer: &schema.ResourceImporter{StateContext: schema.ImportStatePassthroughContext},
Description: `Manages Security Notification Emails
This resource allows you to configure Security Notification Emails.
~> **WARNING:** This resource is available only when using a SSWS API token in the provider config, it is incompatible with OAuth 2.0 authentication.
~> **WARNING:** This resource makes use of an internal/private Okta API endpoint that could change without notice rendering this resource inoperable.`,
Schema: map[string]*schema.Schema{
"send_email_for_new_device_enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Notifies end users about new sign-on activity. Default is `true`.",
Default: true,
},
"send_email_for_factor_enrollment_enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Notifies end users of any activity on their account related to MFA factor enrollment. Default is `true`.",
Default: true,
},
"send_email_for_factor_reset_enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Notifies end users that one or more factors have been reset for their account. Default is `true`.",
Default: true,
},
"send_email_for_password_changed_enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Notifies end users that the password for their account has changed. Default is `true`.",
Default: true,
},
"report_suspicious_activity_enabled": {
Type: schema.TypeBool,
Optional: true,
Description: "Notifies end users about suspicious or unrecognized activity from their account. Default is `true`.",
Default: true,
},
},
}
}
func resourceSecurityNotificationEmailsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*Config)
client := c.oktaSDKClientV2.GetConfig().HttpClient
emails, err := getAPISupplementFromMetadata(m).UpdateSecurityNotificationEmails(ctx, buildSecurityNotificationEmails(d), c.orgName, c.domain, c.apiToken, client)
if err != nil {
return diag.Errorf("failed to update security notification emails: %v", err)
}
d.SetId("security_notification_emails")
_ = d.Set("send_email_for_new_device_enabled", emails.SendEmailForNewDeviceEnabled)
_ = d.Set("send_email_for_factor_enrollment_enabled", emails.SendEmailForFactorEnrollmentEnabled)
_ = d.Set("send_email_for_factor_reset_enabled", emails.SendEmailForFactorResetEnabled)
_ = d.Set("send_email_for_password_changed_enabled", emails.SendEmailForPasswordChangedEnabled)
_ = d.Set("report_suspicious_activity_enabled", emails.ReportSuspiciousActivityEnabled)
return nil
}
func resourceSecurityNotificationEmailsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*Config)
client := c.oktaSDKClientV2.GetConfig().HttpClient
emails, err := getAPISupplementFromMetadata(m).GetSecurityNotificationEmails(ctx, c.orgName, c.domain, c.apiToken, client)
if err != nil {
return diag.Errorf("failed to get security notification emails: %v", err)
}
d.SetId("security_notification_emails")
_ = d.Set("send_email_for_new_device_enabled", emails.SendEmailForNewDeviceEnabled)
_ = d.Set("send_email_for_factor_enrollment_enabled", emails.SendEmailForFactorEnrollmentEnabled)
_ = d.Set("send_email_for_factor_reset_enabled", emails.SendEmailForFactorResetEnabled)
_ = d.Set("send_email_for_password_changed_enabled", emails.SendEmailForPasswordChangedEnabled)
_ = d.Set("report_suspicious_activity_enabled", emails.ReportSuspiciousActivityEnabled)
return nil
}
func resourceSecurityNotificationEmailsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*Config)
client := c.oktaSDKClientV2.GetConfig().HttpClient
_, err := getAPISupplementFromMetadata(m).UpdateSecurityNotificationEmails(ctx, buildSecurityNotificationEmails(d), c.orgName, c.domain, c.apiToken, client)
if err != nil {
return diag.Errorf("failed to update security notification emails: %v", err)
}
return nil
}
func resourceSecurityNotificationEmailsDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*Config)
client := c.oktaSDKClientV2.GetConfig().HttpClient
emails := sdk.SecurityNotificationEmails{
SendEmailForNewDeviceEnabled: true,
SendEmailForFactorEnrollmentEnabled: true,
SendEmailForFactorResetEnabled: true,
SendEmailForPasswordChangedEnabled: true,
ReportSuspiciousActivityEnabled: true,
}
_, err := getAPISupplementFromMetadata(m).UpdateSecurityNotificationEmails(ctx, emails, c.orgName, c.domain, c.apiToken, client)
if err != nil {
return diag.Errorf("failed to set default security notification emails: %v", err)
}
return nil
}
func buildSecurityNotificationEmails(d *schema.ResourceData) sdk.SecurityNotificationEmails {
return sdk.SecurityNotificationEmails{
SendEmailForNewDeviceEnabled: d.Get("send_email_for_new_device_enabled").(bool),
SendEmailForFactorEnrollmentEnabled: d.Get("send_email_for_factor_enrollment_enabled").(bool),
SendEmailForFactorResetEnabled: d.Get("send_email_for_factor_reset_enabled").(bool),
SendEmailForPasswordChangedEnabled: d.Get("send_email_for_password_changed_enabled").(bool),
ReportSuspiciousActivityEnabled: d.Get("report_suspicious_activity_enabled").(bool),
}
}