forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_app_service_active_slot.go
112 lines (94 loc) · 3.45 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
package azurerm
import (
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/services/web/mgmt/2016-09-01/web"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func resourceArmAppServiceActiveSlot() *schema.Resource {
return &schema.Resource{
Create: resourceArmAppServiceActiveSlotCreate,
Read: resourceArmAppServiceActiveSlotRead,
Update: resourceArmAppServiceActiveSlotCreate,
Delete: resourceArmAppServiceActiveSlotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"resource_group_name": resourceGroupNameSchema(),
"app_service_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"app_service_slot_name": {
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceArmAppServiceActiveSlotCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).appServicesClient
ctx := meta.(*ArmClient).StopContext
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)
}
err = future.WaitForCompletion(ctx, client.Client)
if 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.(*ArmClient).appServicesClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["sites"]
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(d *schema.ResourceData, meta interface{}) error {
// There is nothing to delete so return nil
return nil
}