Skip to content

Commit

Permalink
data.azurerm_virtual_network: add network_space property to match res…
Browse files Browse the repository at this point in the history
…ource (#3494)

Fixes #3373
Fixes #3384
  • Loading branch information
katbyte authored May 23, 2019
1 parent 14918ad commit 7ce4b69
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 113 deletions.
58 changes: 33 additions & 25 deletions azurerm/data_source_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ func dataSourceArmVirtualNetwork() *schema.Resource {
"resource_group_name": resourceGroupNameForDataSourceSchema(),

"address_spaces": {
Type: schema.TypeList,
Computed: true,
Deprecated: "This resource has been deprecated in favour of `address_space` to be more consistent with the `azurerm_virtual_network` resource",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"address_space": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Expand Down Expand Up @@ -69,51 +78,46 @@ func dataSourceArmVnetRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error making Read request on Virtual Network %q (resource group %q): %+v", name, resGroup, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("API returns a nil/empty id on Virtual Network %q (resource group %q): %+v", name, resGroup, err)
}
d.SetId(*resp.ID)

if props := resp.VirtualNetworkPropertiesFormat; props != nil {
addressSpaces := flattenVnetAddressPrefixes(props.AddressSpace.AddressPrefixes)
if err := d.Set("address_spaces", addressSpaces); err != nil {
return err
if as := props.AddressSpace; as != nil {
if err := d.Set("address_spaces", utils.FlattenStringSlice(as.AddressPrefixes)); err != nil { //todo remove in 2.0
return fmt.Errorf("error setting `address_spaces`: %v", err)
}
if err := d.Set("address_space", utils.FlattenStringSlice(as.AddressPrefixes)); err != nil {
return fmt.Errorf("error setting `address_space`: %v", err)
}
}

if options := props.DhcpOptions; options != nil {
dnsServers := flattenVnetAddressPrefixes(options.DNSServers)
if err := d.Set("dns_servers", dnsServers); err != nil {
return err
if err := d.Set("dns_servers", utils.FlattenStringSlice(options.DNSServers)); err != nil {
return fmt.Errorf("error setting `dns_servers`: %v", err)
}
}

subnets := flattenVnetSubnetsNames(props.Subnets)
if err := d.Set("subnets", subnets); err != nil {
return err
if err := d.Set("subnets", flattenVnetSubnetsNames(props.Subnets)); err != nil {
return fmt.Errorf("error setting `subnets`: %v", err)
}

vnetPeerings := flattenVnetPeerings(props.VirtualNetworkPeerings)
if err := d.Set("vnet_peerings", vnetPeerings); err != nil {
return err
if err := d.Set("vnet_peerings", flattenVnetPeerings(props.VirtualNetworkPeerings)); err != nil {
return fmt.Errorf("error setting `vnet_peerings`: %v", err)
}
}
return nil
}

func flattenVnetAddressPrefixes(input *[]string) []interface{} {
prefixes := make([]interface{}, 0)

if myprefixes := input; myprefixes != nil {
for _, prefix := range *myprefixes {
prefixes = append(prefixes, prefix)
}
}
return prefixes
}

func flattenVnetSubnetsNames(input *[]network.Subnet) []interface{} {
subnets := make([]interface{}, 0)

if mysubnets := input; mysubnets != nil {
for _, subnet := range *mysubnets {
subnets = append(subnets, *subnet.Name)
if v := subnet.Name; v != nil {
subnets = append(subnets, *v)
}
}
}
return subnets
Expand All @@ -124,12 +128,16 @@ func flattenVnetPeerings(input *[]network.VirtualNetworkPeering) map[string]inte

if peerings := input; peerings != nil {
for _, vnetpeering := range *peerings {
if vnetpeering.Name == nil || vnetpeering.RemoteVirtualNetwork == nil || vnetpeering.RemoteVirtualNetwork.ID == nil {
continue
}

key := *vnetpeering.Name
value := *vnetpeering.RemoteVirtualNetwork.ID

output[key] = value

}
}

return output
}
53 changes: 15 additions & 38 deletions azurerm/data_source_virtual_network_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package azurerm
import (
"fmt"

"bytes"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -94,7 +91,7 @@ func dataSourceArmVirtualNetworkGateway() *schema.Resource {
},

"root_certificate": {
Type: schema.TypeSet,
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -109,11 +106,10 @@ func dataSourceArmVirtualNetworkGateway() *schema.Resource {
},
},
},
Set: hashVirtualNetworkGatewayDataSourceRootCert,
},

"revoked_certificate": {
Type: schema.TypeSet,
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand All @@ -128,7 +124,6 @@ func dataSourceArmVirtualNetworkGateway() *schema.Resource {
},
},
},
Set: hashVirtualNetworkGatewayDataSourceRevokedCert,
},

"radius_server_address": {
Expand Down Expand Up @@ -287,44 +282,46 @@ func flattenArmVirtualNetworkGatewayDataSourceVpnClientConfig(cfg *network.VpnCl

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

addressSpace := make([]interface{}, 0)
if pool := cfg.VpnClientAddressPool; pool != nil {
if prefixes := pool.AddressPrefixes; prefixes != nil {
for _, addr := range *prefixes {
addressSpace = append(addressSpace, addr)
}
}
flat["address_space"] = utils.FlattenStringSlice(pool.AddressPrefixes)
} else {
flat["address_space"] = []interface{}{}
}
flat["address_space"] = addressSpace

rootCerts := make([]interface{}, 0)
if certs := cfg.VpnClientRootCertificates; certs != nil {
for _, cert := range *certs {
if cert.Name == nil || cert.VpnClientRootCertificatePropertiesFormat == nil || cert.VpnClientRootCertificatePropertiesFormat.PublicCertData == nil {
continue
}
v := map[string]interface{}{
"name": *cert.Name,
"public_cert_data": *cert.VpnClientRootCertificatePropertiesFormat.PublicCertData,
}
rootCerts = append(rootCerts, v)
}
}
flat["root_certificate"] = schema.NewSet(hashVirtualNetworkGatewayDataSourceRootCert, rootCerts)
flat["root_certificate"] = rootCerts

revokedCerts := make([]interface{}, 0)
if certs := cfg.VpnClientRevokedCertificates; certs != nil {
for _, cert := range *certs {
if cert.Name == nil || cert.VpnClientRevokedCertificatePropertiesFormat == nil || cert.VpnClientRevokedCertificatePropertiesFormat.Thumbprint == nil {
continue
}
v := map[string]interface{}{
"name": *cert.Name,
"thumbprint": *cert.VpnClientRevokedCertificatePropertiesFormat.Thumbprint,
}
revokedCerts = append(revokedCerts, v)
}
}
flat["revoked_certificate"] = schema.NewSet(hashVirtualNetworkGatewayDataSourceRevokedCert, revokedCerts)
flat["revoked_certificate"] = revokedCerts

vpnClientProtocols := &schema.Set{F: schema.HashString}
vpnClientProtocols := make([]interface{}, 0)
if vpnProtocols := cfg.VpnClientProtocols; vpnProtocols != nil {
for _, protocol := range *vpnProtocols {
vpnClientProtocols.Add(string(protocol))
vpnClientProtocols = append(vpnClientProtocols, string(protocol))
}
}
flat["vpn_client_protocols"] = vpnClientProtocols
Expand Down Expand Up @@ -361,23 +358,3 @@ func flattenArmVirtualNetworkGatewayDataSourceBgpSettings(settings *network.BgpS

return output
}

func hashVirtualNetworkGatewayDataSourceRootCert(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})

buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["public_cert_data"].(string)))

return hashcode.String(buf.String())
}

func hashVirtualNetworkGatewayDataSourceRevokedCert(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})

buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["thumbprint"].(string)))

return hashcode.String(buf.String())
}
4 changes: 2 additions & 2 deletions azurerm/helpers/azure/api_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func ExpandApiManagementOperationParameterContract(input []interface{}) *[]apima
Type: utils.String(paramType),
Required: utils.Bool(required),
DefaultValue: utils.String(defaultValue),
Values: utils.ExpandStringArray(valuesRaw),
Values: utils.ExpandStringSlice(valuesRaw),
}
outputs = append(outputs, output)
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func FlattenApiManagementOperationParameterContract(input *[]apimanagement.Param
output["default_value"] = *v.DefaultValue
}

output["values"] = schema.NewSet(schema.HashString, utils.FlattenStringArray(v.Values))
output["values"] = schema.NewSet(schema.HashString, utils.FlattenStringSlice(v.Values))

outputs = append(outputs, output)
}
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_api_management_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func resourceArmApiManagementPropertyCreateUpdate(d *schema.ResourceData, meta i
}

if tags, ok := d.GetOk("tags"); ok {
parameters.PropertyContractProperties.Tags = utils.ExpandStringArray(tags.([]interface{}))
parameters.PropertyContractProperties.Tags = utils.ExpandStringSlice(tags.([]interface{}))
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, name, parameters, ""); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_container_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ func expandContainerProbe(input interface{}) *containerinstance.ContainerProbe {
commands := probeConfig["exec"].([]interface{})
if len(commands) > 0 {
exec := containerinstance.ContainerExec{
Command: utils.ExpandStringArray(commands),
Command: utils.ExpandStringSlice(commands),
}
probe.Exec = &exec
}
Expand Down
4 changes: 2 additions & 2 deletions azurerm/resource_arm_eventgrid_event_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func resourceArmEventGridEventSubscriptionCreateUpdate(d *schema.ResourceData, m
Filter: expandEventGridEventSubscriptionFilter(d),
DeadLetterDestination: expandEventGridEventSubscriptionStorageBlobDeadLetterDestination(d),
RetryPolicy: expandEventGridEventSubscriptionRetryPolicy(d),
Labels: utils.ExpandStringArray(d.Get("labels").([]interface{})),
Labels: utils.ExpandStringSlice(d.Get("labels").([]interface{})),
EventDeliverySchema: eventgrid.EventDeliverySchema(d.Get("event_delivery_schema").(string)),
}

Expand Down Expand Up @@ -487,7 +487,7 @@ func expandEventGridEventSubscriptionFilter(d *schema.ResourceData) *eventgrid.E
filter := &eventgrid.EventSubscriptionFilter{}

if includedEvents, ok := d.GetOk("included_event_types"); ok {
filter.IncludedEventTypes = utils.ExpandStringArray(includedEvents.([]interface{}))
filter.IncludedEventTypes = utils.ExpandStringSlice(includedEvents.([]interface{}))
}

if subjectFilter, ok := d.GetOk("subject_filter"); ok {
Expand Down
6 changes: 3 additions & 3 deletions azurerm/resource_arm_firewall_application_rule_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ func expandArmFirewallApplicationRules(inputs []interface{}) ([]network.AzureFir
output := network.AzureFirewallApplicationRule{
Name: utils.String(ruleName),
Description: utils.String(ruleDescription),
SourceAddresses: utils.ExpandStringArray(ruleSourceAddresses),
FqdnTags: utils.ExpandStringArray(ruleFqdnTags),
TargetFqdns: utils.ExpandStringArray(ruleTargetFqdns),
SourceAddresses: utils.ExpandStringSlice(ruleSourceAddresses),
FqdnTags: utils.ExpandStringSlice(ruleFqdnTags),
TargetFqdns: utils.ExpandStringSlice(ruleTargetFqdns),
}

ruleProtocols := make([]network.AzureFirewallApplicationRuleProtocol, 0)
Expand Down
6 changes: 3 additions & 3 deletions azurerm/resource_arm_iothub.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func expandIoTHubRoutes(d *schema.ResourceData) *[]devices.RouteProperties {
Name: &name,
Source: source,
Condition: &condition,
EndpointNames: utils.ExpandStringArray(endpointNamesRaw),
EndpointNames: utils.ExpandStringSlice(endpointNamesRaw),
IsEnabled: &isEnabled,
})
}
Expand Down Expand Up @@ -710,7 +710,7 @@ func expandIoTHubFallbackRoute(d *schema.ResourceData) *devices.FallbackRoutePro
return &devices.FallbackRouteProperties{
Source: &source,
Condition: &condition,
EndpointNames: utils.ExpandStringArray(fallbackRouteMap["endpoint_names"].([]interface{})),
EndpointNames: utils.ExpandStringSlice(fallbackRouteMap["endpoint_names"].([]interface{})),
IsEnabled: &isEnabled,
}
}
Expand Down Expand Up @@ -905,7 +905,7 @@ func flattenIoTHubFallbackRoute(input *devices.RoutingProperties) []interface{}
output["source"] = *source
}

output["endpoint_names"] = utils.FlattenStringArray(route.EndpointNames)
output["endpoint_names"] = utils.FlattenStringSlice(route.EndpointNames)

return []interface{}{output}
}
Expand Down
14 changes: 7 additions & 7 deletions azurerm/resource_arm_key_vault_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func expandKeyVaultCertificatePolicy(d *schema.ResourceData) keyvault.Certificat
cert := v.(map[string]interface{})

ekus := cert["extended_key_usage"].([]interface{})
extendedKeyUsage := utils.ExpandStringArray(ekus)
extendedKeyUsage := utils.ExpandStringSlice(ekus)

keyUsage := make([]keyvault.KeyUsageType, 0)
keys := cert["key_usage"].([]interface{})
Expand All @@ -629,17 +629,17 @@ func expandKeyVaultCertificatePolicy(d *schema.ResourceData) keyvault.Certificat

emails := san["emails"].([]interface{})
if len(emails) > 0 {
subjectAlternativeNames.Emails = utils.ExpandStringArray(emails)
subjectAlternativeNames.Emails = utils.ExpandStringSlice(emails)
}

dnsNames := san["dns_names"].([]interface{})
if len(dnsNames) > 0 {
subjectAlternativeNames.DNSNames = utils.ExpandStringArray(dnsNames)
subjectAlternativeNames.DNSNames = utils.ExpandStringSlice(dnsNames)
}

upns := san["upns"].([]interface{})
if len(upns) > 0 {
subjectAlternativeNames.Upns = utils.ExpandStringArray(upns)
subjectAlternativeNames.Upns = utils.ExpandStringSlice(upns)
}
}
}
Expand Down Expand Up @@ -726,9 +726,9 @@ func flattenKeyVaultCertificatePolicy(input *keyvault.CertificatePolicy) []inter
if san := props.SubjectAlternativeNames; san != nil {
sanOutput := make(map[string]interface{})

sanOutput["emails"] = utils.FlattenStringArray(san.Emails)
sanOutput["dns_names"] = utils.FlattenStringArray(san.DNSNames)
sanOutput["upns"] = utils.FlattenStringArray(san.Upns)
sanOutput["emails"] = utils.FlattenStringSlice(san.Emails)
sanOutput["dns_names"] = utils.FlattenStringSlice(san.DNSNames)
sanOutput["upns"] = utils.FlattenStringSlice(san.Upns)

sanOutputs = append(sanOutputs, sanOutput)
}
Expand Down
4 changes: 2 additions & 2 deletions azurerm/resource_arm_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ func resourceArmKubernetesClusterCreateUpdate(d *schema.ResourceData, meta inter
rbacEnabled, azureADProfile := expandKubernetesClusterRoleBasedAccessControl(rbacRaw, tenantId)

apiServerAuthorizedIPRangesRaw := d.Get("api_server_authorized_ip_ranges").(*schema.Set).List()
apiServerAuthorizedIPRanges := utils.ExpandStringArray(apiServerAuthorizedIPRangesRaw)
apiServerAuthorizedIPRanges := utils.ExpandStringSlice(apiServerAuthorizedIPRangesRaw)

parameters := containerservice.ManagedCluster{
Name: &name,
Expand Down Expand Up @@ -669,7 +669,7 @@ func resourceArmKubernetesClusterRead(d *schema.ResourceData, meta interface{})
d.Set("kubernetes_version", props.KubernetesVersion)
d.Set("node_resource_group", props.NodeResourceGroup)

apiServerAuthorizedIPRanges := utils.FlattenStringArray(props.APIServerAuthorizedIPRanges)
apiServerAuthorizedIPRanges := utils.FlattenStringSlice(props.APIServerAuthorizedIPRanges)
if err := d.Set("api_server_authorized_ip_ranges", apiServerAuthorizedIPRanges); err != nil {
return fmt.Errorf("Error setting `api_server_authorized_ip_ranges`: %+v", err)
}
Expand Down
Loading

0 comments on commit 7ce4b69

Please sign in to comment.