forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_web_app_resource.go
695 lines (579 loc) · 24.3 KB
/
linux_web_app_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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
package appservice
import (
"context"
"fmt"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2021-02-01/web"
"github.com/kevinklinger/terraform-provider-azurerm/v2/helpers/azure"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/location"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/sdk"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/appservice/helpers"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/appservice/parse"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/services/appservice/validate"
"github.com/kevinklinger/terraform-provider-azurerm/v2/internal/tags"
"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/utils"
)
type LinuxWebAppResource struct{}
type LinuxWebAppModel struct {
Name string `tfschema:"name"`
ResourceGroup string `tfschema:"resource_group_name"`
Location string `tfschema:"location"`
ServicePlanId string `tfschema:"service_plan_id"`
AppSettings map[string]string `tfschema:"app_settings"`
AuthSettings []helpers.AuthSettings `tfschema:"auth_settings"`
Backup []helpers.Backup `tfschema:"backup"`
ClientAffinityEnabled bool `tfschema:"client_affinity_enabled"`
ClientCertEnabled bool `tfschema:"client_cert_enabled"`
ClientCertMode string `tfschema:"client_cert_mode"`
Enabled bool `tfschema:"enabled"`
HttpsOnly bool `tfschema:"https_only"`
Identity []helpers.Identity `tfschema:"identity"`
LogsConfig []helpers.LogsConfig `tfschema:"logs"`
MetaData map[string]string `tfschema:"app_metadata"`
SiteConfig []helpers.SiteConfigLinux `tfschema:"site_config"`
StorageAccounts []helpers.StorageAccount `tfschema:"storage_account"`
ConnectionStrings []helpers.ConnectionString `tfschema:"connection_string"`
Tags map[string]string `tfschema:"tags"`
CustomDomainVerificationId string `tfschema:"custom_domain_verification_id"`
DefaultHostname string `tfschema:"default_hostname"`
Kind string `tfschema:"kind"`
OutboundIPAddresses string `tfschema:"outbound_ip_addresses"`
OutboundIPAddressList []string `tfschema:"outbound_ip_address_list"`
PossibleOutboundIPAddresses string `tfschema:"possible_outbound_ip_addresses"`
PossibleOutboundIPAddressList []string `tfschema:"possible_outbound_ip_address_list"`
SiteCredentials []helpers.SiteCredential `tfschema:"site_credential"`
}
var _ sdk.ResourceWithUpdate = LinuxWebAppResource{}
var _ sdk.ResourceWithCustomImporter = LinuxWebAppResource{}
func (r LinuxWebAppResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.WebAppName,
},
"resource_group_name": azure.SchemaResourceGroupName(),
"location": location.Schema(),
"service_plan_id": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ServicePlanID,
},
// Optional
"app_settings": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"auth_settings": helpers.AuthSettingsSchema(),
"backup": helpers.BackupSchema(),
"client_affinity_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
"client_cert_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
"client_cert_mode": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "Required",
ValidateFunc: validation.StringInSlice([]string{
string(web.ClientCertModeOptional),
string(web.ClientCertModeRequired),
}, false),
},
"connection_string": helpers.ConnectionStringSchema(),
"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"https_only": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
"identity": helpers.IdentitySchema(),
"logs": helpers.LogsConfigSchema(),
"site_config": helpers.SiteConfigSchemaLinux(),
"storage_account": helpers.StorageAccountSchema(),
"tags": tags.Schema(),
}
}
// TODO - Feature: Deployments (Preview)?
// TODO - Feature: App Insights? - Part of site_settings map
func (r LinuxWebAppResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"custom_domain_verification_id": {
Type: pluginsdk.TypeString,
Computed: true,
Sensitive: true,
},
"default_hostname": {
Type: pluginsdk.TypeString,
Computed: true,
},
"kind": {
Type: pluginsdk.TypeString,
Computed: true,
},
"app_metadata": {
Type: pluginsdk.TypeMap,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"outbound_ip_addresses": {
Type: pluginsdk.TypeString,
Computed: true,
},
"outbound_ip_address_list": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"possible_outbound_ip_addresses": {
Type: pluginsdk.TypeString,
Computed: true,
},
"possible_outbound_ip_address_list": {
Type: pluginsdk.TypeList,
Computed: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"site_credential": helpers.SiteCredentialSchema(),
}
}
func (r LinuxWebAppResource) ModelObject() interface{} {
return &LinuxWebAppModel{}
}
func (r LinuxWebAppResource) ResourceType() string {
return "azurerm_linux_web_app"
}
func (r LinuxWebAppResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
var webApp LinuxWebAppModel
if err := metadata.Decode(&webApp); err != nil {
return err
}
client := metadata.Client.AppService.WebAppsClient
aseClient := metadata.Client.AppService.AppServiceEnvironmentClient
servicePlanClient := metadata.Client.AppService.ServicePlanClient
subscriptionId := metadata.Client.Account.SubscriptionId
id := parse.NewWebAppID(subscriptionId, webApp.ResourceGroup, webApp.Name)
existing, err := client.Get(ctx, id.ResourceGroup, id.SiteName)
if err != nil && !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Linux %s: %+v", id, err)
}
if !utils.ResponseWasNotFound(existing.Response) {
return metadata.ResourceRequiresImport(r.ResourceType(), id)
}
availabilityRequest := web.ResourceNameAvailabilityRequest{
Name: utils.String(webApp.Name),
Type: web.CheckNameResourceTypesMicrosoftWebsites,
}
servicePlanId, err := parse.ServicePlanID(webApp.ServicePlanId)
if err != nil {
return err
}
servicePlan, err := servicePlanClient.Get(ctx, servicePlanId.ResourceGroup, servicePlanId.ServerfarmName)
if err != nil {
return fmt.Errorf("reading %s: %+v", servicePlanId, err)
}
if ase := servicePlan.HostingEnvironmentProfile; ase != nil {
// Attempt to check the ASE for the appropriate suffix for the name availability request.
// This varies between internal and external ASE Types, and potentially has other names in other clouds
// We use the "internal" as the fallback here, if we can read the ASE, we'll get the full one
nameSuffix := "appserviceenvironment.net"
if ase.ID != nil {
aseId, err := parse.AppServiceEnvironmentID(*ase.ID)
nameSuffix = fmt.Sprintf("%s.%s", aseId.HostingEnvironmentName, nameSuffix)
if err != nil {
metadata.Logger.Warnf("could not parse App Service Environment ID determine FQDN for name availability check, defaulting to `%s.%s.appserviceenvironment.net`", webApp.Name, servicePlanId)
} else {
existingASE, err := aseClient.Get(ctx, aseId.ResourceGroup, aseId.HostingEnvironmentName)
if err != nil {
metadata.Logger.Warnf("could not read App Service Environment to determine FQDN for name availability check, defaulting to `%s.%s.appserviceenvironment.net`", webApp.Name, servicePlanId)
} else if props := existingASE.AppServiceEnvironment; props != nil && props.DNSSuffix != nil && *props.DNSSuffix != "" {
nameSuffix = *props.DNSSuffix
}
}
}
availabilityRequest.Name = utils.String(fmt.Sprintf("%s.%s", webApp.Name, nameSuffix))
availabilityRequest.IsFqdn = utils.Bool(true)
}
checkName, err := client.CheckNameAvailability(ctx, availabilityRequest)
if err != nil {
return fmt.Errorf("checking name availability for Linux %s: %+v", id, err)
}
if !*checkName.NameAvailable {
return fmt.Errorf("the Site Name %q failed the availability check: %+v", id.SiteName, *checkName.Message)
}
siteConfig, err := helpers.ExpandSiteConfigLinux(webApp.SiteConfig, nil, metadata)
if err != nil {
return err
}
siteEnvelope := web.Site{
Location: utils.String(webApp.Location),
Tags: tags.FromTypedObject(webApp.Tags),
SiteProperties: &web.SiteProperties{
ServerFarmID: utils.String(webApp.ServicePlanId),
Enabled: utils.Bool(webApp.Enabled),
HTTPSOnly: utils.Bool(webApp.HttpsOnly),
SiteConfig: siteConfig,
ClientAffinityEnabled: utils.Bool(webApp.ClientAffinityEnabled),
ClientCertEnabled: utils.Bool(webApp.ClientCertEnabled),
ClientCertMode: web.ClientCertMode(webApp.ClientCertMode),
},
}
if identity := helpers.ExpandIdentity(webApp.Identity); identity != nil {
siteEnvelope.Identity = identity
}
future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SiteName, siteEnvelope)
if err != nil {
return fmt.Errorf("creating Linux %s: %+v", id, err)
}
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation of Linux %s: %+v", id, err)
}
metadata.SetID(id)
appSettings := helpers.ExpandAppSettings(webApp.AppSettings)
if appSettings.Properties != nil {
if _, err := client.UpdateApplicationSettings(ctx, id.ResourceGroup, id.SiteName, *appSettings); err != nil {
return fmt.Errorf("setting App Settings for Linux %s: %+v", id, err)
}
}
auth := helpers.ExpandAuthSettings(webApp.AuthSettings)
if auth.SiteAuthSettingsProperties != nil {
if _, err := client.UpdateAuthSettings(ctx, id.ResourceGroup, id.SiteName, *auth); err != nil {
return fmt.Errorf("setting Authorisation Settings for Linux %s: %+v", id, err)
}
}
if metadata.ResourceData.HasChange("logs") {
logsConfig := helpers.ExpandLogsConfig(webApp.LogsConfig)
if logsConfig.SiteLogsConfigProperties != nil {
if _, err := client.UpdateDiagnosticLogsConfig(ctx, id.ResourceGroup, id.SiteName, *logsConfig); err != nil {
return fmt.Errorf("setting Diagnostic Logs Configuration for Linux %s: %+v", id, err)
}
}
}
backupConfig := helpers.ExpandBackupConfig(webApp.Backup)
if backupConfig.BackupRequestProperties != nil {
if _, err := client.UpdateBackupConfiguration(ctx, id.ResourceGroup, id.SiteName, *backupConfig); err != nil {
return fmt.Errorf("adding Backup Settings for Linux %s: %+v", id, err)
}
}
storageConfig := helpers.ExpandStorageConfig(webApp.StorageAccounts)
if storageConfig.Properties != nil {
if _, err := client.UpdateAzureStorageAccounts(ctx, id.ResourceGroup, id.SiteName, *storageConfig); err != nil {
if err != nil {
return fmt.Errorf("setting Storage Accounts for Linux %s: %+v", id, err)
}
}
}
connectionStrings := helpers.ExpandConnectionStrings(webApp.ConnectionStrings)
if connectionStrings.Properties != nil {
if _, err := client.UpdateConnectionStrings(ctx, id.ResourceGroup, id.SiteName, *connectionStrings); err != nil {
return fmt.Errorf("setting Connection Strings for Linux %s: %+v", id, err)
}
}
return nil
},
}
}
func (r LinuxWebAppResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.WebAppsClient
id, err := parse.WebAppID(metadata.ResourceData.Id())
if err != nil {
return err
}
webApp, err := client.Get(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
if utils.ResponseWasNotFound(webApp.Response) {
return metadata.MarkAsGone(id)
}
return fmt.Errorf("reading Linux %s: %+v", id, err)
}
if webApp.SiteProperties == nil {
return fmt.Errorf("reading properties of Linux %s", id)
}
// Despite being part of the defined `Get` response model, site_config is always nil so we get it explicitly
webAppSiteConfig, err := client.GetConfiguration(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("reading Site Config for Linux %s: %+v", id, err)
}
auth, err := client.GetAuthSettings(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("reading Auth Settings for Linux %s: %+v", id, err)
}
backup, err := client.GetBackupConfiguration(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
if !utils.ResponseWasNotFound(backup.Response) {
return fmt.Errorf("reading Backup Settings for Linux %s: %+v", id, err)
}
}
logsConfig, err := client.GetDiagnosticLogsConfiguration(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("reading Diagnostic Logs information for Linux %s: %+v", id, err)
}
appSettings, err := client.ListApplicationSettings(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("reading App Settings for Linux %s: %+v", id, err)
}
storageAccounts, err := client.ListAzureStorageAccounts(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("reading Storage Account information for Linux %s: %+v", id, err)
}
connectionStrings, err := client.ListConnectionStrings(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("reading Connection String information for Linux %s: %+v", id, err)
}
siteCredentialsFuture, err := client.ListPublishingCredentials(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("listing Site Publishing Credential information for Linux %s: %+v", id, err)
}
if err := siteCredentialsFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for Site Publishing Credential information for Linux %s: %+v", id, err)
}
siteCredentials, err := siteCredentialsFuture.Result(*client)
if err != nil {
return fmt.Errorf("reading Site Publishing Credential information for Linux %s: %+v", id, err)
}
state := LinuxWebAppModel{
Name: id.SiteName,
ResourceGroup: id.ResourceGroup,
Location: location.NormalizeNilable(webApp.Location),
AppSettings: helpers.FlattenAppSettings(appSettings),
Tags: tags.ToTypedObject(webApp.Tags),
}
webAppProps := webApp.SiteProperties
if v := webAppProps.ServerFarmID; v != nil {
state.ServicePlanId = *v
}
if v := webAppProps.ClientAffinityEnabled; v != nil {
state.ClientAffinityEnabled = *v
}
if v := webAppProps.ClientCertEnabled; v != nil {
state.ClientCertEnabled = *v
}
if webAppProps.ClientCertMode != "" {
state.ClientCertMode = string(webAppProps.ClientCertMode)
}
if v := webAppProps.Enabled; v != nil {
state.Enabled = *v
}
if v := webAppProps.HTTPSOnly; v != nil {
state.HttpsOnly = *v
}
if v := webAppProps.CustomDomainVerificationID; v != nil {
state.CustomDomainVerificationId = *v
}
if v := webAppProps.DefaultHostName; v != nil {
state.DefaultHostname = *v
}
if v := webApp.Kind; v != nil {
state.Kind = *v
}
if v := webAppProps.OutboundIPAddresses; v != nil {
state.OutboundIPAddresses = *v
state.OutboundIPAddressList = strings.Split(*v, ",")
}
if v := webAppProps.PossibleOutboundIPAddresses; v != nil {
state.PossibleOutboundIPAddresses = *v
state.PossibleOutboundIPAddressList = strings.Split(*v, ",")
}
state.AuthSettings = helpers.FlattenAuthSettings(auth)
state.Backup = helpers.FlattenBackupConfig(backup)
if identity := helpers.FlattenIdentity(webApp.Identity); identity != nil {
state.Identity = identity
}
state.LogsConfig = helpers.FlattenLogsConfig(logsConfig)
state.SiteConfig = helpers.FlattenSiteConfigLinux(webAppSiteConfig.SiteConfig)
state.StorageAccounts = helpers.FlattenStorageAccounts(storageAccounts)
state.ConnectionStrings = helpers.FlattenConnectionStrings(connectionStrings)
state.SiteCredentials = helpers.FlattenSiteCredentials(siteCredentials)
return metadata.Encode(&state)
},
}
}
func (r LinuxWebAppResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.WebAppsClient
id, err := parse.WebAppID(metadata.ResourceData.Id())
if err != nil {
return err
}
metadata.Logger.Infof("deleting %s", *id)
deleteMetrics := true
deleteEmptyServerFarm := false
if _, err := client.Delete(ctx, id.ResourceGroup, id.SiteName, &deleteMetrics, &deleteEmptyServerFarm); err != nil {
return fmt.Errorf("deleting Linux %s: %+v", id, err)
}
return nil
},
}
}
func (r LinuxWebAppResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return validate.WebAppID
}
func (r LinuxWebAppResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.WebAppsClient
id, err := parse.WebAppID(metadata.ResourceData.Id())
if err != nil {
return err
}
// TODO - Need locking here for source control meta resource?
var state LinuxWebAppModel
if err := metadata.Decode(&state); err != nil {
return fmt.Errorf("decoding: %+v", err)
}
existing, err := client.Get(ctx, id.ResourceGroup, id.SiteName)
if err != nil {
return fmt.Errorf("reading Linux %s: %v", id, err)
}
if metadata.ResourceData.HasChange("service_plan_id") {
existing.SiteProperties.ServerFarmID = utils.String(state.ServicePlanId)
}
if metadata.ResourceData.HasChange("enabled") {
existing.SiteProperties.Enabled = utils.Bool(state.Enabled)
}
if metadata.ResourceData.HasChange("https_only") {
existing.SiteProperties.HTTPSOnly = utils.Bool(state.HttpsOnly)
}
if metadata.ResourceData.HasChange("client_affinity_enabled") {
existing.SiteProperties.ClientAffinityEnabled = utils.Bool(state.ClientAffinityEnabled)
}
if metadata.ResourceData.HasChange("client_cert_enabled") {
existing.SiteProperties.ClientCertEnabled = utils.Bool(state.ClientCertEnabled)
}
if metadata.ResourceData.HasChange("client_cert_mode") {
existing.SiteProperties.ClientCertMode = web.ClientCertMode(state.ClientCertMode)
}
if metadata.ResourceData.HasChange("identity") {
existing.Identity = helpers.ExpandIdentity(state.Identity)
}
if metadata.ResourceData.HasChange("tags") {
existing.Tags = tags.FromTypedObject(state.Tags)
}
if metadata.ResourceData.HasChange("site_config") {
siteConfig, err := helpers.ExpandSiteConfigLinux(state.SiteConfig, existing.SiteConfig, metadata)
if err != nil {
return fmt.Errorf("expanding Site Config for Linux %s: %+v", id, err)
}
existing.SiteConfig = siteConfig
}
updateFuture, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.SiteName, existing)
if err != nil {
return fmt.Errorf("updating Linux %s: %+v", id, err)
}
if err := updateFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting to update %s: %+v", id, err)
}
// (@jackofallops) - App Settings can clobber logs configuration so must be updated before we send any Log updates
if metadata.ResourceData.HasChange("app_settings") {
appSettingsUpdate := helpers.ExpandAppSettings(state.AppSettings)
if _, err := client.UpdateApplicationSettings(ctx, id.ResourceGroup, id.SiteName, *appSettingsUpdate); err != nil {
return fmt.Errorf("updating App Settings for Linux %s: %+v", id, err)
}
}
if metadata.ResourceData.HasChange("connection_string") {
connectionStringUpdate := helpers.ExpandConnectionStrings(state.ConnectionStrings)
if connectionStringUpdate.Properties == nil {
connectionStringUpdate.Properties = map[string]*web.ConnStringValueTypePair{}
}
if _, err := client.UpdateConnectionStrings(ctx, id.ResourceGroup, id.SiteName, *connectionStringUpdate); err != nil {
return fmt.Errorf("updating Connection Strings for Linux %s: %+v", id, err)
}
}
if metadata.ResourceData.HasChange("auth_settings") {
authUpdate := helpers.ExpandAuthSettings(state.AuthSettings)
if _, err := client.UpdateAuthSettings(ctx, id.ResourceGroup, id.SiteName, *authUpdate); err != nil {
return fmt.Errorf("updating Auth Settings for Linux %s: %+v", id, err)
}
}
if metadata.ResourceData.HasChange("backup") {
backupUpdate := helpers.ExpandBackupConfig(state.Backup)
if backupUpdate.BackupRequestProperties == nil {
if _, err := client.DeleteBackupConfiguration(ctx, id.ResourceGroup, id.SiteName); err != nil {
return fmt.Errorf("removing Backup Settings for Linux %s: %+v", id, err)
}
} else {
if _, err := client.UpdateBackupConfiguration(ctx, id.ResourceGroup, id.SiteName, *backupUpdate); err != nil {
return fmt.Errorf("updating Backup Settings for Linux %s: %+v", id, err)
}
}
}
if metadata.ResourceData.HasChange("logs") {
logsUpdate := helpers.ExpandLogsConfig(state.LogsConfig)
if logsUpdate.SiteLogsConfigProperties == nil {
logsUpdate = helpers.DisabledLogsConfig() // The API is update only, so we need to send an update with everything switched of when a user removes the "logs" block
}
if _, err := client.UpdateDiagnosticLogsConfig(ctx, id.ResourceGroup, id.SiteName, *logsUpdate); err != nil {
return fmt.Errorf("updating Logs Config for Linux %s: %+v", id, err)
}
}
if metadata.ResourceData.HasChange("storage_account") {
storageAccountUpdate := helpers.ExpandStorageConfig(state.StorageAccounts)
if _, err := client.UpdateAzureStorageAccounts(ctx, id.ResourceGroup, id.SiteName, *storageAccountUpdate); err != nil {
return fmt.Errorf("updating Storage Accounts for Linux %s: %+v", id, err)
}
}
return nil
},
}
}
func (r LinuxWebAppResource) CustomImporter() sdk.ResourceRunFunc {
return func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.AppService.WebAppsClient
servicePlanClient := metadata.Client.AppService.ServicePlanClient
id, err := parse.WebAppID(metadata.ResourceData.Id())
if err != nil {
return err
}
site, err := client.Get(ctx, id.ResourceGroup, id.SiteName)
if err != nil || site.SiteProperties == nil {
return fmt.Errorf("reading Linux %s: %+v", id, err)
}
props := site.SiteProperties
if props.ServerFarmID == nil {
return fmt.Errorf("determining Service Plan ID for Linux %s: %+v", id, err)
}
servicePlanId, err := parse.ServicePlanID(*props.ServerFarmID)
if err != nil {
return err
}
sp, err := servicePlanClient.Get(ctx, servicePlanId.ResourceGroup, servicePlanId.ServerfarmName)
if err != nil || sp.Kind == nil {
return fmt.Errorf("reading Service Plan for Linux %s: %+v", id, err)
}
if !strings.Contains(*sp.Kind, "linux") && !strings.Contains(*sp.Kind, "Linux") {
return fmt.Errorf("specified Service Plan is not a Linux plan")
}
return nil
}
}