Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_traffic_manager_profile - add traffic view support #10005

Merged
merged 3 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/trafficmanager/mgmt/2018-04-01/trafficmanager"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
Expand Down Expand Up @@ -126,6 +127,11 @@ func dataSourceArmTrafficManagerProfile() *schema.Resource {
Computed: true,
},

"traffic_view_enabled": {
Type: schema.TypeBool,
Optional: true,
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -155,6 +161,7 @@ func dataSourceArmTrafficManagerProfileRead(d *schema.ResourceData, meta interfa

d.Set("dns_config", flattenAzureRMTrafficManagerProfileDNSConfig(profile.DNSConfig))
d.Set("monitor_config", flattenAzureRMTrafficManagerProfileMonitorConfig(profile.MonitorConfig))
d.Set("traffic_view_enabled", profile.TrafficViewEnrollmentStatus == trafficmanager.TrafficViewEnrollmentStatusEnabled)

// fqdn is actually inside DNSConfig, inlined for simpler reference
if dns := profile.DNSConfig; dns != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ func resourceArmTrafficManagerProfile() *schema.Resource {
ValidateFunc: validation.IntBetween(1, 8),
},

"traffic_view_enabled": {
Type: schema.TypeBool,
Optional: true,
},

"tags": tags.Schema(),
},
}
Expand Down Expand Up @@ -226,6 +231,10 @@ func resourceArmTrafficManagerProfileCreate(d *schema.ResourceData, meta interfa
profile.ProfileStatus = trafficmanager.ProfileStatus(status.(string))
}

if trafficViewStatus, ok := d.GetOk("traffic_view_enabled"); ok {
profile.TrafficViewEnrollmentStatus = expandArmTrafficManagerTrafficView(trafficViewStatus.(bool))
}

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`")
Expand Down Expand Up @@ -273,6 +282,7 @@ func resourceArmTrafficManagerProfileRead(d *schema.ResourceData, meta interface

d.Set("dns_config", flattenAzureRMTrafficManagerProfileDNSConfig(profile.DNSConfig))
d.Set("monitor_config", flattenAzureRMTrafficManagerProfileMonitorConfig(profile.MonitorConfig))
d.Set("traffic_view_enabled", profile.TrafficViewEnrollmentStatus == trafficmanager.TrafficViewEnrollmentStatusEnabled)

// fqdn is actually inside DNSConfig, inlined for simpler reference
if dns := profile.DNSConfig; dns != nil {
Expand Down Expand Up @@ -321,6 +331,12 @@ func resourceArmTrafficManagerProfileUpdate(d *schema.ResourceData, meta interfa
update.ProfileProperties.MonitorConfig = expandArmTrafficManagerMonitorConfig(d)
}

if d.HasChange("traffic_view_enabled") {
if trafficViewStatus, ok := d.GetOk("traffic_view_enabled"); ok {
update.ProfileProperties.TrafficViewEnrollmentStatus = expandArmTrafficManagerTrafficView(trafficViewStatus.(bool))
}
}

if _, err := client.Update(ctx, id.ResourceGroup, id.Name, update); err != nil {
return fmt.Errorf("updating Traffic Manager Profile %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
Expand Down Expand Up @@ -433,6 +449,13 @@ func expandArmTrafficManagerDNSConfig(d *schema.ResourceData) *trafficmanager.DN
}
}

func expandArmTrafficManagerTrafficView(s bool) trafficmanager.TrafficViewEnrollmentStatus {
if s {
return trafficmanager.TrafficViewEnrollmentStatusEnabled
}
return trafficmanager.TrafficViewEnrollmentStatusDisabled
}

func flattenAzureRMTrafficManagerProfileDNSConfig(dns *trafficmanager.DNSConfig) []interface{} {
result := make(map[string]interface{})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,29 @@ func TestAccAzureRMTrafficManagerProfile_fastMaxReturnSettingError(t *testing.T)
})
}

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

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.withTrafficView(data, false),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("traffic_view_enabled").HasValue("false"),
),
},
data.ImportStep(),
{
Config: r.withTrafficView(data, true),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("traffic_view_enabled").HasValue("true"),
),
},
})
}

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

func (r TrafficManagerProfileResource) withTrafficView(data acceptance.TestData, enabled bool) 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 = "Geographic"

dns_config {
relative_name = "acctest-tmp-%d"
ttl = 30
}

monitor_config {
protocol = "https"
port = 443
path = "/"
}
traffic_view_enabled = %t
}
`, template, data.RandomInteger, data.RandomInteger, enabled)
}

func (r TrafficManagerProfileResource) withTTL(data acceptance.TestData, method string, ttl int) string {
template := r.template(data)
return fmt.Sprintf(`
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/traffic_manager_profile.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ output "traffic_routing_method" {

* `traffic_routing_method` - Specifies the algorithm used to route traffic.

* `traffic_view_enabled` - Indicates whether Traffic View is enabled for the Traffic Manager profile.

* `dns_config` - This block specifies the DNS configuration of the Profile.

* `monitor_config` - This block specifies the Endpoint monitoring configuration for the Profile.
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/traffic_manager_profile.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ The following arguments are supported:
* `Subnet` - Traffic is routed based on a mapping of sets of end-user IP address ranges to a specific Endpoint within a Traffic Manager profile.
* `Weighted` - Traffic is spread across Endpoints proportional to their `weight` value.

* `traffic_view_enabled` - (Optional) Indicates whether Traffic View is enabled for the Traffic Manager profile.

* `dns_config` - (Required) This block specifies the DNS configuration of the Profile, it supports the fields documented below.

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