diff --git a/azurerm/resource_arm_subnet.go b/azurerm/resource_arm_subnet.go index ec5b3b73d6de..ae144bd5364f 100644 --- a/azurerm/resource_arm_subnet.go +++ b/azurerm/resource_arm_subnet.go @@ -58,6 +58,12 @@ func resourceArmSubnet() *schema.Resource { Elem: &schema.Schema{Type: schema.TypeString}, Set: schema.HashString, }, + + "service_endpoints": { + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, } } @@ -110,6 +116,13 @@ func resourceArmSubnetCreate(d *schema.ResourceData, meta interface{}) error { defer azureRMUnlockByName(routeTableName, routeTableResourceName) } + serviceEndpoints, serviceEndpointsErr := expandAzureRmServiceEndpoints(d) + if serviceEndpointsErr != nil { + return fmt.Errorf("Error Building list of Service Endpoints: %+v", serviceEndpointsErr) + } + + properties.ServiceEndpoints = &serviceEndpoints + subnet := network.Subnet{ Name: &name, SubnetPropertiesFormat: &properties, @@ -179,6 +192,11 @@ func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error { if err := d.Set("ip_configurations", ips); err != nil { return err } + + serviceEndpoints := flattenSubnetServiceEndpoints(props.ServiceEndpoints) + if err := d.Set("service_endpoints", serviceEndpoints); err != nil { + return err + } } return nil @@ -237,6 +255,35 @@ func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error { return nil } +func expandAzureRmServiceEndpoints(d *schema.ResourceData) ([]network.ServiceEndpointPropertiesFormat, error) { + serviceEndpoints := d.Get("service_endpoints").([]interface{}) + enpoints := make([]network.ServiceEndpointPropertiesFormat, 0) + + for _, serviceEndpointsRaw := range serviceEndpoints { + data := serviceEndpointsRaw.(string) + + endpoint := network.ServiceEndpointPropertiesFormat{ + Service: &data, + } + + enpoints = append(enpoints, endpoint) + } + + return enpoints, nil +} + +func flattenSubnetServiceEndpoints(serviceEndpoints *[]network.ServiceEndpointPropertiesFormat) []string { + endpoints := make([]string, 0) + + if serviceEndpoints != nil { + for _, endpoint := range *serviceEndpoints { + endpoints = append(endpoints, *endpoint.Service) + } + } + + return endpoints +} + func flattenSubnetIPConfigurations(ipConfigurations *[]network.IPConfiguration) []string { ips := make([]string, 0) diff --git a/azurerm/resource_arm_subnet_test.go b/azurerm/resource_arm_subnet_test.go index 86c0f546ba7a..0ca60630f81b 100644 --- a/azurerm/resource_arm_subnet_test.go +++ b/azurerm/resource_arm_subnet_test.go @@ -325,6 +325,26 @@ func testCheckAzureRMSubnetDestroy(s *terraform.State) error { return nil } +func TestAccAzureRMSubnet_serviceEndpoints(t *testing.T) { + + ri := acctest.RandInt() + config := testAccAzureRMSubnet_serviceEndpoints(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMSubnetDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMSubnetExists("azurerm_subnet.test"), + ), + }, + }, + }) +} + func testAccAzureRMSubnet_basic(rInt int, location string) string { return fmt.Sprintf(` resource "azurerm_resource_group" "test" { @@ -667,3 +687,27 @@ resource "azurerm_subnet" "test" { } `, rInt, location, rInt, rInt, rInt, rInt) } + +func testAccAzureRMSubnet_serviceEndpoints(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%d" + location = "%s" +} + +resource "azurerm_virtual_network" "test" { + name = "acctestvirtnet%d" + address_space = ["10.0.0.0/16"] + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" +} + +resource "azurerm_subnet" "test" { + name = "acctestsubnet%d" + resource_group_name = "${azurerm_resource_group.test.name}" + virtual_network_name = "${azurerm_virtual_network.test.name}" + address_prefix = "10.0.2.0/24" + service_endpoints = ["Microsoft.Sql","Microsoft.Storage"] +} +`, rInt, location, rInt, rInt) +} diff --git a/website/docs/r/subnet.html.markdown b/website/docs/r/subnet.html.markdown index 37bfb93cf089..b7bcbf05d6a5 100644 --- a/website/docs/r/subnet.html.markdown +++ b/website/docs/r/subnet.html.markdown @@ -54,6 +54,8 @@ The following arguments are supported: * `route_table_id` - (Optional) The ID of the Route Table to associate with the subnet. +* `service_endpoints` - (Optional) The list of Service endpoints to associate with the subnet. Possible values include: `Microsoft.Storage`, `Microsoft.Sql`. + ## Attributes Reference The following attributes are exported: