Skip to content

Commit cc18e4d

Browse files
pmcatomineystack72
authored andcommitted
provider/azurerm: add traffic manager resources (hashicorp#7826)
* provider/azurerm: vendor arm/trafficmanager package * provider/azurerm: add azurerm_traffic_manager_profile resource * provider/azurerm: add azurerm_traffic_manager_endpoint resource * provider/azurerm: document traffic manager resources * provider/azurerm: use short type argument for traffic manager endpoint The resource now takes the short type for example azureEndpoints instead of the long form Microsoft.Network/TrafficManagerProfiles/azureEndpoints. ``` TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMTrafficManagerEndpoint -timeout 120m === RUN TestAccAzureRMTrafficManagerEndpoint_basic --- PASS: TestAccAzureRMTrafficManagerEndpoint_basic (179.72s) === RUN TestAccAzureRMTrafficManagerEndpoint_basicDisableExternal --- PASS: TestAccAzureRMTrafficManagerEndpoint_basicDisableExternal (171.36s) === RUN TestAccAzureRMTrafficManagerEndpoint_updateWeight --- PASS: TestAccAzureRMTrafficManagerEndpoint_updateWeight (167.24s) === RUN TestAccAzureRMTrafficManagerEndpoint_updatePriority --- PASS: TestAccAzureRMTrafficManagerEndpoint_updatePriority (192.91s) === RUN TestAccAzureRMTrafficManagerEndpoint_nestedEndpoints --- PASS: TestAccAzureRMTrafficManagerEndpoint_nestedEndpoints (111.18s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 822.534s ``` * provider/azurerm: remove unnecesary dereferences in traffic manager resources ``` TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMTrafficManager -timeout 120m === RUN TestAccAzureRMTrafficManagerEndpoint_basic --- PASS: TestAccAzureRMTrafficManagerEndpoint_basic (176.08s) === RUN TestAccAzureRMTrafficManagerEndpoint_basicDisableExternal --- PASS: TestAccAzureRMTrafficManagerEndpoint_basicDisableExternal (172.28s) === RUN TestAccAzureRMTrafficManagerEndpoint_updateWeight --- PASS: TestAccAzureRMTrafficManagerEndpoint_updateWeight (148.97s) === RUN TestAccAzureRMTrafficManagerEndpoint_updatePriority --- PASS: TestAccAzureRMTrafficManagerEndpoint_updatePriority (101.18s) === RUN TestAccAzureRMTrafficManagerEndpoint_nestedEndpoints --- PASS: TestAccAzureRMTrafficManagerEndpoint_nestedEndpoints (88.33s) === RUN TestAccAzureRMTrafficManagerProfile_weighted --- PASS: TestAccAzureRMTrafficManagerProfile_weighted (80.92s) === RUN TestAccAzureRMTrafficManagerProfile_performance --- PASS: TestAccAzureRMTrafficManagerProfile_performance (82.98s) === RUN TestAccAzureRMTrafficManagerProfile_priority --- PASS: TestAccAzureRMTrafficManagerProfile_priority (81.07s) === RUN TestAccAzureRMTrafficManagerProfile_withTags --- PASS: TestAccAzureRMTrafficManagerProfile_withTags (102.50s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 1034.458s ```
1 parent 802f014 commit cc18e4d

15 files changed

Lines changed: 2671 additions & 0 deletions

builtin/providers/azurerm/config.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
1313
"github.com/Azure/azure-sdk-for-go/arm/scheduler"
1414
"github.com/Azure/azure-sdk-for-go/arm/storage"
15+
"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
1516
mainStorage "github.com/Azure/azure-sdk-for-go/storage"
1617
"github.com/Azure/go-autorest/autorest"
1718
"github.com/Azure/go-autorest/autorest/azure"
@@ -61,6 +62,9 @@ type ArmClient struct {
6162
storageUsageClient storage.UsageOperationsClient
6263

6364
deploymentsClient resources.DeploymentsClient
65+
66+
trafficManagerProfilesClient trafficmanager.ProfilesClient
67+
trafficManagerEndpointsClient trafficmanager.EndpointsClient
6468
}
6569

6670
func withRequestLogging() autorest.SendDecorator {
@@ -325,6 +329,18 @@ func (c *Config) getArmClient() (*ArmClient, error) {
325329
dc.Sender = autorest.CreateSender(withRequestLogging())
326330
client.deploymentsClient = dc
327331

332+
tmpc := trafficmanager.NewProfilesClient(c.SubscriptionID)
333+
setUserAgent(&tmpc.Client)
334+
tmpc.Authorizer = spt
335+
tmpc.Sender = autorest.CreateSender(withRequestLogging())
336+
client.trafficManagerProfilesClient = tmpc
337+
338+
tmec := trafficmanager.NewEndpointsClient(c.SubscriptionID)
339+
setUserAgent(&tmec.Client)
340+
tmec.Authorizer = spt
341+
tmec.Sender = autorest.CreateSender(withRequestLogging())
342+
client.trafficManagerEndpointsClient = tmec
343+
328344
return &client, nil
329345
}
330346

builtin/providers/azurerm/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func Provider() terraform.ResourceProvider {
6363
"azurerm_storage_table": resourceArmStorageTable(),
6464
"azurerm_subnet": resourceArmSubnet(),
6565
"azurerm_template_deployment": resourceArmTemplateDeployment(),
66+
"azurerm_traffic_manager_endpoint": resourceArmTrafficManagerEndpoint(),
67+
"azurerm_traffic_manager_profile": resourceArmTrafficManagerProfile(),
6668
"azurerm_virtual_machine": resourceArmVirtualMachine(),
6769
"azurerm_virtual_machine_scale_set": resourceArmVirtualMachineScaleSet(),
6870
"azurerm_virtual_network": resourceArmVirtualNetwork(),
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
8+
"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
9+
"github.com/hashicorp/terraform/helper/schema"
10+
)
11+
12+
func resourceArmTrafficManagerEndpoint() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceArmTrafficManagerEndpointCreate,
15+
Read: resourceArmTrafficManagerEndpointRead,
16+
Update: resourceArmTrafficManagerEndpointCreate,
17+
Delete: resourceArmTrafficManagerEndpointDelete,
18+
Importer: &schema.ResourceImporter{
19+
State: schema.ImportStatePassthrough,
20+
},
21+
22+
Schema: map[string]*schema.Schema{
23+
"name": {
24+
Type: schema.TypeString,
25+
Required: true,
26+
ForceNew: true,
27+
},
28+
29+
"type": {
30+
Type: schema.TypeString,
31+
Required: true,
32+
ForceNew: true,
33+
ValidateFunc: validateAzureRMTrafficManagerEndpointType,
34+
},
35+
36+
"profile_name": {
37+
Type: schema.TypeString,
38+
Required: true,
39+
ForceNew: true,
40+
},
41+
42+
"target": {
43+
Type: schema.TypeString,
44+
Optional: true,
45+
// when targeting an Azure resource the FQDN of that resource will be set as the target
46+
Computed: true,
47+
},
48+
49+
"target_resource_id": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
},
53+
54+
"endpoint_status": {
55+
Type: schema.TypeString,
56+
Optional: true,
57+
Computed: true,
58+
},
59+
60+
"weight": {
61+
Type: schema.TypeInt,
62+
Optional: true,
63+
Computed: true,
64+
ValidateFunc: validateAzureRMTrafficManagerEndpointWeight,
65+
},
66+
67+
"priority": {
68+
Type: schema.TypeInt,
69+
Optional: true,
70+
Computed: true,
71+
ValidateFunc: validateAzureRMTrafficManagerEndpointPriority,
72+
},
73+
74+
"endpoint_location": {
75+
Type: schema.TypeString,
76+
Optional: true,
77+
// when targeting an Azure resource the location of that resource will be set on the endpoint
78+
Computed: true,
79+
StateFunc: azureRMNormalizeLocation,
80+
},
81+
82+
"min_child_endpoints": {
83+
Type: schema.TypeInt,
84+
Optional: true,
85+
},
86+
87+
"resource_group_name": {
88+
Type: schema.TypeString,
89+
Required: true,
90+
ForceNew: true,
91+
},
92+
},
93+
}
94+
}
95+
96+
func resourceArmTrafficManagerEndpointCreate(d *schema.ResourceData, meta interface{}) error {
97+
client := meta.(*ArmClient).trafficManagerEndpointsClient
98+
99+
log.Printf("[INFO] preparing arguments for ARM TrafficManager Endpoint creation.")
100+
101+
name := d.Get("name").(string)
102+
endpointType := d.Get("type").(string)
103+
fullEndpointType := fmt.Sprintf("Microsoft.Network/TrafficManagerProfiles/%s", endpointType)
104+
profileName := d.Get("profile_name").(string)
105+
resGroup := d.Get("resource_group_name").(string)
106+
107+
params := trafficmanager.Endpoint{
108+
Name: &name,
109+
Type: &fullEndpointType,
110+
Properties: getArmTrafficManagerEndpointProperties(d),
111+
}
112+
113+
_, err := client.CreateOrUpdate(resGroup, profileName, endpointType, name, params)
114+
if err != nil {
115+
return err
116+
}
117+
118+
read, err := client.Get(resGroup, profileName, endpointType, name)
119+
if err != nil {
120+
return err
121+
}
122+
if read.ID == nil {
123+
return fmt.Errorf("Cannot read TrafficManager endpoint %s (resource group %s) ID", name, resGroup)
124+
}
125+
126+
d.SetId(*read.ID)
127+
128+
return resourceArmTrafficManagerEndpointRead(d, meta)
129+
}
130+
131+
func resourceArmTrafficManagerEndpointRead(d *schema.ResourceData, meta interface{}) error {
132+
client := meta.(*ArmClient).trafficManagerEndpointsClient
133+
134+
id, err := parseAzureResourceID(d.Id())
135+
if err != nil {
136+
return err
137+
}
138+
resGroup := id.ResourceGroup
139+
endpointType := d.Get("type").(string)
140+
profileName := id.Path["trafficManagerProfiles"]
141+
142+
// endpoint name is keyed by endpoint type in ARM ID
143+
name := id.Path[endpointType]
144+
145+
resp, err := client.Get(resGroup, profileName, endpointType, name)
146+
if resp.StatusCode == http.StatusNotFound {
147+
d.SetId("")
148+
return nil
149+
}
150+
if err != nil {
151+
return fmt.Errorf("Error making Read request on TrafficManager Endpoint %s: %s", name, err)
152+
}
153+
endpoint := *resp.Properties
154+
155+
d.Set("name", resp.Name)
156+
d.Set("endpoint_status", endpoint.EndpointStatus)
157+
d.Set("target_resource_id", endpoint.TargetResourceID)
158+
d.Set("target", endpoint.Target)
159+
d.Set("weight", endpoint.Weight)
160+
d.Set("priority", endpoint.Priority)
161+
d.Set("endpoint_location", endpoint.EndpointLocation)
162+
d.Set("endpoint_monitor_status", endpoint.EndpointMonitorStatus)
163+
d.Set("min_child_endpoints", endpoint.MinChildEndpoints)
164+
165+
return nil
166+
}
167+
168+
func resourceArmTrafficManagerEndpointDelete(d *schema.ResourceData, meta interface{}) error {
169+
client := meta.(*ArmClient).trafficManagerEndpointsClient
170+
171+
id, err := parseAzureResourceID(d.Id())
172+
if err != nil {
173+
return err
174+
}
175+
resGroup := id.ResourceGroup
176+
endpointType := d.Get("type").(string)
177+
profileName := id.Path["trafficManagerProfiles"]
178+
179+
// endpoint name is keyed by endpoint type in ARM ID
180+
name := id.Path[endpointType]
181+
182+
_, err = client.Delete(resGroup, profileName, endpointType, name)
183+
184+
return err
185+
}
186+
187+
func getArmTrafficManagerEndpointProperties(d *schema.ResourceData) *trafficmanager.EndpointProperties {
188+
var endpointProps trafficmanager.EndpointProperties
189+
190+
if targetResID := d.Get("target_resource_id").(string); targetResID != "" {
191+
endpointProps.TargetResourceID = &targetResID
192+
}
193+
194+
if target := d.Get("target").(string); target != "" {
195+
endpointProps.Target = &target
196+
}
197+
198+
if status := d.Get("endpoint_status").(string); status != "" {
199+
endpointProps.EndpointStatus = &status
200+
}
201+
202+
if weight := d.Get("weight").(int); weight != 0 {
203+
w64 := int64(weight)
204+
endpointProps.Weight = &w64
205+
}
206+
207+
if priority := d.Get("priority").(int); priority != 0 {
208+
p64 := int64(priority)
209+
endpointProps.Priority = &p64
210+
}
211+
212+
if location := d.Get("endpoint_location").(string); location != "" {
213+
endpointProps.EndpointLocation = &location
214+
}
215+
216+
if minChildEndpoints := d.Get("min_child_endpoints").(int); minChildEndpoints != 0 {
217+
mci64 := int64(minChildEndpoints)
218+
endpointProps.MinChildEndpoints = &mci64
219+
}
220+
221+
return &endpointProps
222+
}
223+
224+
func validateAzureRMTrafficManagerEndpointType(i interface{}, k string) (s []string, errors []error) {
225+
valid := map[string]struct{}{
226+
"azureEndpoints": struct{}{},
227+
"externalEndpoints": struct{}{},
228+
"nestedEndpoints": struct{}{},
229+
}
230+
231+
if _, ok := valid[i.(string)]; !ok {
232+
errors = append(errors, fmt.Errorf("endpoint type invalid, got %s", i.(string)))
233+
}
234+
return
235+
}
236+
237+
func validateAzureRMTrafficManagerEndpointWeight(i interface{}, k string) (s []string, errors []error) {
238+
w := i.(int)
239+
if w < 1 || w > 1000 {
240+
errors = append(errors, fmt.Errorf("endpoint weight must be between 1-1000 inclusive"))
241+
}
242+
return
243+
}
244+
245+
func validateAzureRMTrafficManagerEndpointPriority(i interface{}, k string) (s []string, errors []error) {
246+
p := i.(int)
247+
if p < 1 || p > 1000 {
248+
errors = append(errors, fmt.Errorf("endpoint priority must be between 1-1000 inclusive"))
249+
}
250+
return
251+
}

0 commit comments

Comments
 (0)