forked from hashicorp/terraform-provider-azurerm
/
resource_arm_security_center_contact.go
149 lines (119 loc) · 4.21 KB
/
resource_arm_security_center_contact.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package azurerm
import (
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/services/preview/security/mgmt/2017-08-01-preview/security"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
//seems you can only set one contact:
// Invalid security contact name was provided - only 'defaultX' is allowed where X is an index
// Invalid security contact name 'default0' was provided. Expected 'default1'
// Message="Invalid security contact name 'default2' was provided. Expected 'default1'"
const securityCenterContactName = "default1"
func resourceArmSecurityCenterContact() *schema.Resource {
return &schema.Resource{
Create: resourceArmSecurityCenterContactCreateUpdate,
Read: resourceArmSecurityCenterContactRead,
Update: resourceArmSecurityCenterContactCreateUpdate,
Delete: resourceArmSecurityCenterContactDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"email": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"phone": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"alert_notifications": {
Type: schema.TypeBool,
Required: true,
},
"alerts_to_admins": {
Type: schema.TypeBool,
Required: true,
},
},
}
}
func resourceArmSecurityCenterContactCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).securityCenterContactsClient
ctx := meta.(*ArmClient).StopContext
name := securityCenterContactName
contact := security.Contact{
ContactProperties: &security.ContactProperties{
Email: utils.String(d.Get("email").(string)),
Phone: utils.String(d.Get("phone").(string)),
},
}
if alertNotifications := d.Get("alert_notifications").(bool); alertNotifications {
contact.AlertNotifications = security.On
} else {
contact.AlertNotifications = security.Off
}
if alertNotifications := d.Get("alerts_to_admins").(bool); alertNotifications {
contact.AlertsToAdmins = security.AlertsToAdminsOn
} else {
contact.AlertsToAdmins = security.AlertsToAdminsOff
}
if d.IsNewResource() {
if _, err := client.Create(ctx, name, contact); err != nil {
return fmt.Errorf("Error creating Security Center Contact: %+v", err)
}
resp, err := client.Get(ctx, name)
if err != nil {
return fmt.Errorf("Error reading Security Center Contact: %+v", err)
}
if resp.ID == nil {
return fmt.Errorf("Security Center Contact ID is nil")
}
d.SetId(*resp.ID)
} else {
if _, err := client.Update(ctx, name, contact); err != nil {
return fmt.Errorf("Error updating Security Center Contact: %+v", err)
}
}
return resourceArmSecurityCenterContactRead(d, meta)
}
func resourceArmSecurityCenterContactRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).securityCenterContactsClient
ctx := meta.(*ArmClient).StopContext
name := securityCenterContactName
resp, err := client.Get(ctx, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Security Center Subscription Contact was not found: %v", err)
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Security Center Contact: %+v", err)
}
if properties := resp.ContactProperties; properties != nil {
d.Set("email", properties.Email)
d.Set("phone", properties.Phone)
d.Set("alert_notifications", properties.AlertNotifications == security.On)
d.Set("alerts_to_admins", properties.AlertsToAdmins == security.AlertsToAdminsOn)
}
return nil
}
func resourceArmSecurityCenterContactDelete(_ *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).securityCenterContactsClient
ctx := meta.(*ArmClient).StopContext
name := securityCenterContactName
resp, err := client.Delete(ctx, name)
if err != nil {
if utils.ResponseWasNotFound(resp) {
log.Printf("[DEBUG] Security Center Subscription Contact was not found: %v", err)
return nil
}
return fmt.Errorf("Error deleting Security Center Contact: %+v", err)
}
return nil
}