Skip to content

Commit

Permalink
azurerm_traffic_manager_profile - support for new field `max_return…
Browse files Browse the repository at this point in the history
…` and support for `traffic_routing_method` to be `MultiValue` (#9487)

This PR adds a new optional field to the `azurerm_traffic_manager_profile` resource, which is `max_return`, which allows profiles with the routing methods `MultiValue` to be set. Without this field, resources like this that attempt to be created are always rejected by the Azure API because the `MaxReturn` value isn't set, so it's impossible to create them with Terraform.

Not coincidentally, this type of routing method was also omitted from the acceptance tests (probably because they wouldn't have passed if `MultiValue` was included). Thus I've done my best to add the missing tests there too, but did run into some trouble running the acceptance tests, so please let me know if there's anything I need to improve there!
  • Loading branch information
brandon-dd authored Dec 15, 2020
1 parent 32b9dfd commit 9ed4f1a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ func resourceArmTrafficManagerProfile() *schema.Resource {
Computed: true,
},

"max_return": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 8),
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -212,10 +218,19 @@ func resourceArmTrafficManagerProfileCreate(d *schema.ResourceData, meta interfa
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if maxReturn, ok := d.GetOk("max_return"); ok {
profile.MaxReturn = utils.Int64(int64(maxReturn.(int)))
}

if status, ok := d.GetOk("profile_status"); ok {
profile.ProfileStatus = trafficmanager.ProfileStatus(status.(string))
}

if profile.ProfileProperties.TrafficRoutingMethod == trafficmanager.MultiValue &&
profile.ProfileProperties.MaxReturn == nil {
return fmt.Errorf("`max_return` must be specified when `traffic_routing_method` is set to `MultiValue`")
}

if *profile.ProfileProperties.MonitorConfig.IntervalInSeconds == int64(10) &&
*profile.ProfileProperties.MonitorConfig.TimeoutInSeconds == int64(10) {
return fmt.Errorf("`timeout_in_seconds` must be between `5` and `9` when `interval_in_seconds` is set to `10`")
Expand Down Expand Up @@ -254,6 +269,7 @@ func resourceArmTrafficManagerProfileRead(d *schema.ResourceData, meta interface
if profile := resp.ProfileProperties; profile != nil {
d.Set("profile_status", profile.ProfileStatus)
d.Set("traffic_routing_method", profile.TrafficRoutingMethod)
d.Set("max_return", profile.MaxReturn)

d.Set("dns_config", flattenAzureRMTrafficManagerProfileDNSConfig(profile.DNSConfig))
d.Set("monitor_config", flattenAzureRMTrafficManagerProfileMonitorConfig(profile.MonitorConfig))
Expand Down Expand Up @@ -291,6 +307,12 @@ func resourceArmTrafficManagerProfileUpdate(d *schema.ResourceData, meta interfa
update.ProfileProperties.TrafficRoutingMethod = trafficmanager.TrafficRoutingMethod(d.Get("traffic_routing_method").(string))
}

if d.HasChange("max_return") {
if maxReturn, ok := d.GetOk("max_return"); ok {
update.MaxReturn = utils.Int64(int64(maxReturn.(int)))
}
}

if d.HasChange("dns_config") {
update.ProfileProperties.DNSConfig = expandArmTrafficManagerDNSConfig(d)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ func TestAccAzureRMTrafficManagerProfile_cycleMethod(t *testing.T) {
),
},
data.ImportStep(),
{
Config: r.multiValue(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("traffic_routing_method").HasValue("MultiValue"),
check.That(data.ResourceName).Key("fqdn").HasValue(fmt.Sprintf("acctest-tmp-%d.trafficmanager.net", data.RandomInteger)),
),
},
data.ImportStep(),
})
}

Expand All @@ -173,6 +182,18 @@ func TestAccAzureRMTrafficManagerProfile_fastEndpointFailoverSettingsError(t *te
})
}

func TestAccAzureRMTrafficManagerProfile_fastMaxReturnSettingError(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_traffic_manager_profile", "test")
r := TrafficManagerProfileResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.maxReturnError(data),
ExpectError: regexp.MustCompile("`max_return` must be specified when `traffic_routing_method` is set to `MultiValue`"),
},
})
}

func TestAccAzureRMTrafficManagerProfile_updateTTL(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_traffic_manager_profile", "test")
r := TrafficManagerProfileResource{}
Expand Down Expand Up @@ -247,6 +268,31 @@ resource "azurerm_traffic_manager_profile" "test" {
`, template, data.RandomInteger, method, data.RandomInteger)
}

func (r TrafficManagerProfileResource) multiValue(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s
resource "azurerm_traffic_manager_profile" "test" {
name = "acctest-TMP-%d"
resource_group_name = azurerm_resource_group.test.name
traffic_routing_method = "MultiValue"
max_return = 8
dns_config {
relative_name = "acctest-tmp-%d"
ttl = 30
}
monitor_config {
protocol = "https"
port = 443
path = "/"
}
}
`, template, data.RandomInteger, data.RandomInteger)
}

func (r TrafficManagerProfileResource) requiresImport(data acceptance.TestData) string {
template := r.basic(data, "Geographic")
return fmt.Sprintf(`
Expand Down Expand Up @@ -483,6 +529,33 @@ resource "azurerm_traffic_manager_profile" "test" {
`, template, data.RandomInteger, data.RandomInteger)
}

func (r TrafficManagerProfileResource) maxReturnError(data acceptance.TestData) string {
template := r.template(data)
return fmt.Sprintf(`
%s
resource "azurerm_traffic_manager_profile" "test" {
name = "acctest-TMP-%d"
resource_group_name = azurerm_resource_group.test.name
traffic_routing_method = "MultiValue"
dns_config {
relative_name = "acctest-tmp-%d"
ttl = 30
}
monitor_config {
protocol = "https"
port = 443
path = "/"
interval_in_seconds = 10
timeout_in_seconds = 8
tolerated_number_of_failures = 3
}
}
`, template, data.RandomInteger, data.RandomInteger)
}

func (r TrafficManagerProfileResource) withTTL(data acceptance.TestData, method string, ttl int) string {
template := r.template(data)
return fmt.Sprintf(`
Expand Down
4 changes: 4 additions & 0 deletions website/docs/r/traffic_manager_profile.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ The following arguments are supported:

* `monitor_config` - (Required) This block specifies the Endpoint monitoring configuration for the Profile, it supports the fields documented below.

* `max_return` - (Optional) The amount of endpoints to return for DNS queries to this Profile. Possible values range from `1` to `8`.

~> **NOTE**: `max_return` must be set when the `traffic_routing_method` is `MultiValue`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

The `dns_config` block supports:
Expand Down

0 comments on commit 9ed4f1a

Please sign in to comment.