forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_virtual_network.go
135 lines (110 loc) · 3.27 KB
/
data_source_virtual_network.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package azurerm
import (
"fmt"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
"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"
)
func dataSourceArmVirtualNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmVnetRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"resource_group_name": resourceGroupNameForDataSourceSchema(),
"address_spaces": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"dns_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"subnets": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"vnet_peerings": {
Type: schema.TypeMap,
Computed: true,
},
},
}
}
func dataSourceArmVnetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).vnetClient
ctx := meta.(*ArmClient).StopContext
resGroup := d.Get("resource_group_name").(string)
name := d.Get("name").(string)
resp, err := client.Get(ctx, resGroup, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Virtual Network %q (Resource Group %q) was not found", name, resGroup)
}
return fmt.Errorf("Error making Read request 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 options := props.DhcpOptions; options != nil {
dnsServers := flattenVnetAddressPrefixes(options.DNSServers)
if err := d.Set("dns_servers", dnsServers); err != nil {
return err
}
}
subnets := flattenVnetSubnetsNames(props.Subnets)
if err := d.Set("subnets", subnets); err != nil {
return err
}
vnetPeerings := flattenVnetPeerings(props.VirtualNetworkPeerings)
if err := d.Set("vnet_peerings", vnetPeerings); err != nil {
return 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)
}
}
return subnets
}
func flattenVnetPeerings(input *[]network.VirtualNetworkPeering) map[string]interface{} {
output := make(map[string]interface{})
if peerings := input; peerings != nil {
for _, vnetpeering := range *peerings {
key := *vnetpeering.Name
value := *vnetpeering.RemoteVirtualNetwork.ID
output[key] = value
}
}
return output
}