forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_management_email_template_resource.go
190 lines (161 loc) · 6.96 KB
/
api_management_email_template_resource.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package apimanagement
import (
"fmt"
"log"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2020-12-01/apimanagement"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/tf"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/clients"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/apimanagement/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/apimanagement/schemaz"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/pluginsdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tf/validation"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/timeouts"
"github.com/kevinklinger/terraform-provider-azurerm/v2/utils"
)
func resourceApiManagementEmailTemplate() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceApiManagementEmailTemplateCreateUpdate,
Read: resourceApiManagementEmailTemplateRead,
Update: resourceApiManagementEmailTemplateCreateUpdate,
Delete: resourceApiManagementEmailTemplateDelete,
// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),
Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*pluginsdk.Schema{
"resource_group_name": azure.SchemaResourceGroupName(),
"api_management_name": schemaz.SchemaApiManagementName(),
// There is an open issue for the capitalization of the template names: https://github.com/Azure/azure-rest-api-specs/issues/13341
"template_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
strings.Title(string(apimanagement.AccountClosedDeveloper)),
strings.Title(string(apimanagement.ApplicationApprovedNotificationMessage)),
strings.Title(string(apimanagement.ConfirmSignUpIdentityDefault)),
strings.Title(string(apimanagement.EmailChangeIdentityDefault)),
strings.Title(string(apimanagement.InviteUserNotificationMessage)),
strings.Title(string(apimanagement.NewCommentNotificationMessage)),
strings.Title(string(apimanagement.NewDeveloperNotificationMessage)),
strings.Title(string(apimanagement.NewIssueNotificationMessage)),
strings.Title(string(apimanagement.PasswordResetByAdminNotificationMessage)),
strings.Title(string(apimanagement.PasswordResetIdentityDefault)),
strings.Title(string(apimanagement.PurchaseDeveloperNotificationMessage)),
strings.Title(string(apimanagement.QuotaLimitApproachingDeveloperNotificationMessage)),
strings.Title(string(apimanagement.RejectDeveloperNotificationMessage)),
strings.Title(string(apimanagement.RequestDeveloperNotificationMessage)),
}, false),
},
"body": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"subject": {
Type: pluginsdk.TypeString,
Required: true,
},
// Computed:
"title": {
Type: pluginsdk.TypeString,
Computed: true,
},
"description": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}
func resourceApiManagementEmailTemplateCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.EmailTemplateClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
defer cancel()
resourceGroup := d.Get("resource_group_name").(string)
serviceName := d.Get("api_management_name").(string)
templateName := apimanagement.TemplateName(d.Get("template_name").(string))
id := parse.NewEmailTemplateID(subscriptionId, resourceGroup, serviceName, d.Get("template_name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, templateName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing %s: %s", id, err)
}
}
// in case the template has been edited (is not default anymore) this errors and the resource should be imported manually into the state (terraform import).
if !utils.ResponseWasNotFound(existing.Response) && (existing.IsDefault != nil && !*existing.IsDefault) {
return tf.ImportAsExistsError("azurerm_api_management_email_template", id.ID())
}
}
subject := d.Get("subject").(string)
body := d.Get("body").(string)
emailTemplateUpdateParameters := apimanagement.EmailTemplateUpdateParameters{
EmailTemplateUpdateParameterProperties: &apimanagement.EmailTemplateUpdateParameterProperties{
Subject: utils.String(subject),
Body: utils.String(body),
},
}
if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, templateName, emailTemplateUpdateParameters, ""); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}
d.SetId(id.ID())
return resourceApiManagementEmailTemplateRead(d, meta)
}
func resourceApiManagementEmailTemplateRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.EmailTemplateClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.EmailTemplateID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.ServiceName
templateName := apimanagement.TemplateName(id.TemplateName)
resp, err := client.Get(ctx, resourceGroup, serviceName, templateName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] %s does not exist - removing from state!", *id)
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)
d.Set("template_name", templateName)
if properties := resp.EmailTemplateContractProperties; properties != nil {
d.Set("title", properties.Title)
d.Set("description", properties.Description)
d.Set("subject", properties.Subject)
d.Set("body", properties.Body)
}
return nil
}
func resourceApiManagementEmailTemplateDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).ApiManagement.EmailTemplateClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := parse.EmailTemplateID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.ServiceName
templateName := apimanagement.TemplateName(id.TemplateName)
if resp, err := client.Delete(ctx, resourceGroup, serviceName, templateName, ""); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("deleting %s: %s", *id, err)
}
}
return nil
}