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_subnet - add support for Service Endpoints #786

Merged
merged 5 commits into from
Feb 6, 2018
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
47 changes: 47 additions & 0 deletions azurerm/resource_arm_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
},
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
44 changes: 44 additions & 0 deletions azurerm/resource_arm_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions website/docs/r/subnet.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down