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_application_gateway - Support for Managed Identity (resolves #3640) #3648

Merged
merged 7 commits into from
Jul 24, 2019
95 changes: 95 additions & 0 deletions azurerm/resource_arm_application_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,40 @@ func resourceArmApplicationGateway() *schema.Resource {
ForceNew: true,
},

"identity": {
fxgsell marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
fxgsell marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validation.StringInSlice([]string{
string(network.ResourceIdentityTypeSystemAssigned),
string(network.ResourceIdentityTypeUserAssigned),
string(network.ResourceIdentityTypeSystemAssignedUserAssigned),
}, false),
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"identity_ids": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
},
},
},
},

// Required
"backend_address_pool": {
Type: schema.TypeList,
Expand Down Expand Up @@ -1352,6 +1386,10 @@ func resourceArmApplicationGatewayCreateUpdate(d *schema.ResourceData, meta inte
},
}

if _, ok := d.GetOk("identity"); ok {
gateway.Identity = expandAzureRmApplicationGatewayIdentity(d)
}

for _, backendHttpSettings := range *backendHTTPSettingsCollection {
if props := backendHttpSettings.ApplicationGatewayBackendHTTPSettingsPropertiesFormat; props != nil {
if props.HostName == nil || props.PickHostNameFromBackendAddress == nil {
Expand Down Expand Up @@ -1457,6 +1495,11 @@ func resourceArmApplicationGatewayRead(d *schema.ResourceData, meta interface{})
}
d.Set("zones", applicationGateway.Zones)

identity := flattenRmApplicationGatewayIdentity(applicationGateway.Identity)
if err := d.Set("identity", identity); err != nil {
return err
}

if props := applicationGateway.ApplicationGatewayPropertiesFormat; props != nil {
flattenedCerts := flattenApplicationGatewayAuthenticationCertificates(props.AuthenticationCertificates, d)
if setErr := d.Set("authentication_certificate", flattenedCerts); setErr != nil {
Expand Down Expand Up @@ -1587,6 +1630,58 @@ func resourceArmApplicationGatewayDelete(d *schema.ResourceData, meta interface{
return nil
}

func expandAzureRmApplicationGatewayIdentity(d *schema.ResourceData) *network.ManagedServiceIdentity {
v := d.Get("identity")
identities := v.([]interface{})
identity := identities[0].(map[string]interface{})
identityType := network.ResourceIdentityType(identity["type"].(string))

identityIds := make(map[string]*network.ManagedServiceIdentityUserAssignedIdentitiesValue)
for _, id := range identity["identity_ids"].([]interface{}) {
identityIds[id.(string)] = &network.ManagedServiceIdentityUserAssignedIdentitiesValue{}
}

appGatewayIdentity := network.ManagedServiceIdentity{
Type: identityType,
}

if identityType == network.ResourceIdentityTypeUserAssigned || identityType == network.ResourceIdentityTypeSystemAssignedUserAssigned {
appGatewayIdentity.UserAssignedIdentities = identityIds
}

return &appGatewayIdentity
}

func flattenRmApplicationGatewayIdentity(identity *network.ManagedServiceIdentity) []interface{} {
if identity == nil {
return make([]interface{}, 0)
}

result := make(map[string]interface{})
result["type"] = string(identity.Type)
if identity.PrincipalID != nil {
result["principal_id"] = *identity.PrincipalID
}

identityIds := make([]string, 0)
if identity.UserAssignedIdentities != nil {
/*
"userAssignedIdentities": {
"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/tomdevidentity/providers/Microsoft.ManagedIdentity/userAssignedIdentities/tom123": {
"principalId": "00000000-0000-0000-0000-000000000000",
"clientId": "00000000-0000-0000-0000-000000000000"
}
}
*/
for key := range identity.UserAssignedIdentities {
identityIds = append(identityIds, key)
}
}
result["identity_ids"] = identityIds

return []interface{}{result}
}

func expandApplicationGatewayAuthenticationCertificates(d *schema.ResourceData) *[]network.ApplicationGatewayAuthenticationCertificate {
vs := d.Get("authentication_certificate").([]interface{})
results := make([]network.ApplicationGatewayAuthenticationCertificate, 0)
Expand Down
106 changes: 106 additions & 0 deletions azurerm/resource_arm_application_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
Expand Down Expand Up @@ -947,6 +948,28 @@ func TestAccAzureRMApplicationGateway_gatewayIP(t *testing.T) {
},
})
}
func TestAccAzureRMApplicationGateway_UserAssignedIdentity(t *testing.T) {
resourceName := "azurerm_application_gateway.test"
ri := tf.AccRandTimeInt()
rs := acctest.RandString(14)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMApplicationGatewayDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApplicationGateway_UserDefinedIdentity(ri, testLocation(), rs),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApplicationGatewayExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "identity.0.type", "UserAssigned"),
resource.TestCheckResourceAttr(resourceName, "identity.0.identity_ids.#", "1"),
resource.TestCheckResourceAttr(resourceName, "identity.0.principal_id", ""),
),
},
},
})
}

func testCheckAzureRMApplicationGatewayExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -1076,6 +1099,89 @@ resource "azurerm_application_gateway" "test" {
`, template, rInt)
}

func testAccAzureRMApplicationGateway_UserDefinedIdentity(rInt int, location string, rString string) string {
template := testAccAzureRMApplicationGateway_template(rInt, location)
return fmt.Sprintf(`
%s

# since these variables are re-used - a locals block makes this more maintainable
locals {
backend_address_pool_name = "${azurerm_virtual_network.test.name}-beap"
frontend_port_name = "${azurerm_virtual_network.test.name}-feport"
frontend_ip_configuration_name = "${azurerm_virtual_network.test.name}-feip"
http_setting_name = "${azurerm_virtual_network.test.name}-be-htst"
listener_name = "${azurerm_virtual_network.test.name}-httplstn"
request_routing_rule_name = "${azurerm_virtual_network.test.name}-rqrt"
}

resource "azurerm_user_assigned_identity" "test" {
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"

name = "acctest%s"
}

resource "azurerm_application_gateway" "test" {
name = "acctestag-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
location = "${azurerm_resource_group.test.location}"

sku {
name = "Standard_Small"
tier = "Standard"
capacity = 2
}

identity {
type = "UserAssigned"
identity_ids = ["${azurerm_user_assigned_identity.test.id}"]
}

gateway_ip_configuration {
name = "my-gateway-ip-configuration"
subnet_id = "${azurerm_subnet.test.id}"
}

frontend_port {
name = "${local.frontend_port_name}"
port = 80
}

frontend_ip_configuration {
name = "${local.frontend_ip_configuration_name}"
public_ip_address_id = "${azurerm_public_ip.test.id}"
}

backend_address_pool {
name = "${local.backend_address_pool_name}"
}

backend_http_settings {
name = "${local.http_setting_name}"
cookie_based_affinity = "Disabled"
port = 80
protocol = "Http"
request_timeout = 1
}

http_listener {
name = "${local.listener_name}"
frontend_ip_configuration_name = "${local.frontend_ip_configuration_name}"
frontend_port_name = "${local.frontend_port_name}"
protocol = "Http"
}

request_routing_rule {
name = "${local.request_routing_rule_name}"
rule_type = "Basic"
http_listener_name = "${local.listener_name}"
backend_address_pool_name = "${local.backend_address_pool_name}"
backend_http_settings_name = "${local.http_setting_name}"
}
}
`, template, rString, rInt)
}

func testAccAzureRMApplicationGateway_zones(rInt int, location string) string {
template := testAccAzureRMApplicationGateway_template(rInt, location)
return fmt.Sprintf(`
Expand Down