forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_availability_set.go
140 lines (116 loc) · 3.49 KB
/
resource_arm_availability_set.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
package azurerm
import (
"fmt"
"log"
"net/http"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmAvailabilitySet() *schema.Resource {
return &schema.Resource{
Create: resourceArmAvailabilitySetCreate,
Read: resourceArmAvailabilitySetRead,
Update: resourceArmAvailabilitySetCreate,
Delete: resourceArmAvailabilitySetDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: azureRMNormalizeLocation,
},
"platform_update_domain_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 5,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value > 20 {
errors = append(errors, fmt.Errorf(
"Maximum value for `platform_update_domain_count` is 20"))
}
return
},
},
"platform_fault_domain_count": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 3,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value > 3 {
errors = append(errors, fmt.Errorf(
"Maximum value for `platform_fault_domain_count` is 3", k))
}
return
},
},
"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceArmAvailabilitySetCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
availSetClient := client.availSetClient
log.Printf("[INFO] preparing arguments for Azure ARM Availability Set creation.")
name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
updateDomainCount := d.Get("platform_update_domain_count").(int)
faultDomainCount := d.Get("platform_fault_domain_count").(int)
availSet := compute.AvailabilitySet{
Name: &name,
Location: &location,
Properties: &compute.AvailabilitySetProperties{
PlatformFaultDomainCount: &faultDomainCount,
PlatformUpdateDomainCount: &updateDomainCount,
},
}
resp, err := availSetClient.CreateOrUpdate(resGroup, name, availSet)
if err != nil {
return err
}
d.SetId(*resp.ID)
return resourceArmAvailabilitySetRead(d, meta)
}
func resourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) error {
availSetClient := meta.(*ArmClient).availSetClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["availabilitySets"]
resp, err := availSetClient.Get(resGroup, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure Availability Set %s: %s", name, err)
}
availSet := *resp.Properties
d.Set("platform_update_domain_count", availSet.PlatformUpdateDomainCount)
d.Set("platform_fault_domain_count", availSet.PlatformFaultDomainCount)
return nil
}
func resourceArmAvailabilitySetDelete(d *schema.ResourceData, meta interface{}) error {
availSetClient := meta.(*ArmClient).availSetClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["availabilitySets"]
_, err = availSetClient.Delete(resGroup, name)
return err
}