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

Added Virtual Network Integration to the API Management service #2582

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 69 additions & 5 deletions azurerm/resource_arm_api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,31 @@ func resourceArmApiManagementService() *schema.Resource {
},
},

"virtual_network_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(apimanagement.VirtualNetworkTypeInternal),
string(apimanagement.VirtualNetworkTypeExternal),
string(apimanagement.VirtualNetworkTypeNone),
}, false),
},

"virtual_network_configuration": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},
},
},
},

"tags": tagsSchema(),

"gateway_url": {
Expand Down Expand Up @@ -325,14 +350,19 @@ func resourceArmApiManagementServiceCreateUpdate(d *schema.ResourceData, meta in
certificates := expandAzureRmApiManagementCertificates(d)
hostnameConfigurations := expandAzureRmApiManagementHostnameConfigurations(d)

virtualNetworkType := apimanagement.VirtualNetworkType(d.Get("virtual_network_type").(string))
virtualNetworkConfiguration := expandAzureRmApiManagementVirtualNetworkConfiguration(d)

properties := apimanagement.ServiceResource{
Location: utils.String(location),
ServiceProperties: &apimanagement.ServiceProperties{
PublisherName: utils.String(publisherName),
PublisherEmail: utils.String(publisherEmail),
CustomProperties: customProperties,
Certificates: certificates,
HostnameConfigurations: hostnameConfigurations,
PublisherName: utils.String(publisherName),
PublisherEmail: utils.String(publisherEmail),
CustomProperties: customProperties,
Certificates: certificates,
HostnameConfigurations: hostnameConfigurations,
VirtualNetworkType: virtualNetworkType,
VirtualNetworkConfiguration: virtualNetworkConfiguration,
},
Tags: expandTags(tags),
Sku: sku,
Expand Down Expand Up @@ -428,6 +458,12 @@ func resourceArmApiManagementServiceRead(d *schema.ResourceData, meta interface{
return fmt.Errorf("Error setting `hostname_configuration`: %+v", err)
}

d.Set("virtual_network_type", props.VirtualNetworkType)

if err := d.Set("virtual_network_configuration", flattenApiManagementVirtualNetworkConfiguration(props.VirtualNetworkConfiguration)); err != nil {
return fmt.Errorf("Error setting `virtual_network_configuration`: %+v", err)
}

if err := d.Set("additional_location", flattenApiManagementAdditionalLocations(props.AdditionalLocations)); err != nil {
return fmt.Errorf("Error setting `additional_location`: %+v", err)
}
Expand Down Expand Up @@ -740,6 +776,34 @@ func flattenApiManagementServiceSku(input *apimanagement.ServiceSkuProperties) [
return []interface{}{sku}
}

func expandAzureRmApiManagementVirtualNetworkConfiguration(d *schema.ResourceData) *apimanagement.VirtualNetworkConfiguration {
vs := d.Get("virtual_network_configuration").([]interface{})
// guaranteed by MinItems in the schema
v := vs[0].(map[string]interface{})

subnetid := v["subnet_id"].(string)

vnetConfig := &apimanagement.VirtualNetworkConfiguration{
SubnetResourceID: utils.String(subnetid),
}

return vnetConfig
}

func flattenApiManagementVirtualNetworkConfiguration(input *apimanagement.VirtualNetworkConfiguration) []interface{} {
if input == nil {
return []interface{}{}
}

vnetConfig := make(map[string]interface{})

if id := input.SubnetResourceID; id != nil {
vnetConfig["subnet_id"] = *id
}

return []interface{}{vnetConfig}
}

func expandApiManagementCustomProperties(d *schema.ResourceData) map[string]*string {
vs := d.Get("security").([]interface{})

Expand Down
36 changes: 34 additions & 2 deletions azurerm/resource_arm_api_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ resource "azurerm_api_management" "test" {
}

func testAccAzureRMApiManagement_complete(rInt int, location string, altLocation string) string {
template := testAccAzureRMApiManagement_template(rInt, location)
return fmt.Sprintf(`
%s

resource "azurerm_resource_group" "test1" {
name = "acctestRG-%d"
location = "%s"
Expand Down Expand Up @@ -309,7 +312,13 @@ resource "azurerm_api_management" "test" {
certificate = "${base64encode(file("testdata/api_management_portal_test.pfx"))}"
certificate_password = "terraform"
}
}
}

virtual_network_type = "External"

virtual_network_configuration {
subnet_id = "${azurerm_subnet.test.id}"
}

sku {
name = "Premium"
Expand All @@ -323,5 +332,28 @@ resource "azurerm_api_management" "test" {
location = "${azurerm_resource_group.test1.location}"
resource_group_name = "${azurerm_resource_group.test1.name}"
}
`, rInt, location, rInt, altLocation, rInt)
`, template, rInt, location, rInt, altLocation, rInt)
}

func testAccAzureRMApiManagement_template(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_virtual_network" "test" {
name = "acctest-vnet-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
address_space = ["10.0.0.0/16"]
location = "${azurerm_resource_group.test.location}"
}

resource "azurerm_subnet" "test" {
name = "subnet-%d"
resource_group_name = "${azurerm_resource_group.test.name}"
virtual_network_name = "${azurerm_virtual_network.test.name}"
address_prefix = "10.0.0.0/24"
}
`, rInt, location, rInt, rInt)
}
10 changes: 10 additions & 0 deletions website/docs/r/api_management.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ The following arguments are supported:

* `security` - (Optional) A `security` block as defined below.

* `virtual_network_type` - (Optional) Specifies the Virtual Network Integration for the API Management Service. Possible values include: `None`, `External`, and `Internal`.

* `virtual_network_configuration` - (Optional) A `virtual_network_configuration` block as defined below.

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

---
Expand Down Expand Up @@ -120,6 +124,12 @@ A `security` block supports the following:

---

A `virtual_network_configuration` block supports the following:

* `subnet_id` - (Required) Specifies the resource id of the subnet to integrate the API management service with.

---

A `sku` block supports the following:

* `name` - (Required) Specifies the Pricing Tier for the API Management Service. Possible values include: `Developer`, `Basic`, `Standard` and `Premium`.
Expand Down