-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
resource_arm_app_service_active_slot.go
127 lines (107 loc) · 4.24 KB
/
resource_arm_app_service_active_slot.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
package web
import (
"fmt"
"log"
"time"
"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2018-02-01/web"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmAppServiceActiveSlot() *schema.Resource {
return &schema.Resource{
Create: resourceArmAppServiceActiveSlotCreateUpdate,
Read: resourceArmAppServiceActiveSlotRead,
Update: resourceArmAppServiceActiveSlotCreateUpdate,
Delete: resourceArmAppServiceActiveSlotDelete,
Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := ParseAppServiceID(id)
return err
}),
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
"resource_group_name": azure.SchemaResourceGroupName(),
"app_service_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"app_service_slot_name": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceArmAppServiceActiveSlotCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Web.AppServicesClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()
appServiceName := d.Get("app_service_name").(string)
resGroup := d.Get("resource_group_name").(string)
targetSlot := d.Get("app_service_slot_name").(string)
preserveVnet := true
resp, err := client.Get(ctx, resGroup, appServiceName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("[DEBUG] App Service %q (resource group %q) was not found.", appServiceName, resGroup)
}
return fmt.Errorf("Error making Read request on AzureRM App Service %q: %+v", appServiceName, err)
}
_, err = client.Get(ctx, resGroup, targetSlot)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("[DEBUG] App Service Target Active Slot %q/%q (resource group %q) was not found.", appServiceName, targetSlot, resGroup)
}
return fmt.Errorf("Error making Read request on AzureRM App Service Slot %q/%q: %+v", appServiceName, targetSlot, err)
}
cmsSlotEntity := web.CsmSlotEntity{
TargetSlot: &targetSlot,
PreserveVnet: &preserveVnet,
}
future, err := client.SwapSlotWithProduction(ctx, resGroup, appServiceName, cmsSlotEntity)
if err != nil {
return fmt.Errorf("Error swapping App Service Slot %q/%q: %+v", appServiceName, targetSlot, err)
}
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error swapping App Service Slot %q/%q: %+v", appServiceName, targetSlot, err)
}
d.SetId(*resp.ID)
return resourceArmAppServiceActiveSlotRead(d, meta)
}
func resourceArmAppServiceActiveSlotRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Web.AppServicesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()
id, err := ParseAppServiceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Name
resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] App Service %q (resource group %q) was not found - removing from state", name, resGroup)
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on AzureRM App Service %q: %+v", name, err)
}
d.Set("app_service_name", resp.Name)
d.Set("resource_group_name", resp.ResourceGroup)
d.Set("app_service_slot_name", resp.SiteProperties.SlotSwapStatus.SourceSlotName)
return nil
}
func resourceArmAppServiceActiveSlotDelete(_ *schema.ResourceData, _ interface{}) error {
// There is nothing to delete so return nil
return nil
}