-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
data_source_subnet.go
127 lines (103 loc) · 3.34 KB
/
data_source_subnet.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
package azurerm
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
func dataSourceArmSubnet() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmSubnetRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"virtual_network_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"address_prefix": {
Type: schema.TypeString,
Computed: true,
},
"network_security_group_id": {
Type: schema.TypeString,
Computed: true,
},
"route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"ip_configurations": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"service_endpoints": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"enforce_private_link_service_network_policies": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}
func dataSourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Network.SubnetsClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
name := d.Get("name").(string)
virtualNetworkName := d.Get("virtual_network_name").(string)
resourceGroup := d.Get("resource_group_name").(string)
resp, err := client.Get(ctx, resourceGroup, virtualNetworkName, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Subnet %q (Virtual Network %q / Resource Group %q) was not found", name, virtualNetworkName, resourceGroup)
}
return fmt.Errorf("Error making Read request on Azure Subnet %q: %+v", name, err)
}
d.SetId(*resp.ID)
d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("virtual_network_name", virtualNetworkName)
if props := resp.SubnetPropertiesFormat; props != nil {
d.Set("address_prefix", props.AddressPrefix)
if p := props.PrivateLinkServiceNetworkPolicies; p != nil {
d.Set("enforce_private_link_service_network_policies", strings.EqualFold("Disabled", *p))
}
if props.NetworkSecurityGroup != nil {
d.Set("network_security_group_id", props.NetworkSecurityGroup.ID)
} else {
d.Set("network_security_group_id", "")
}
if props.RouteTable != nil {
d.Set("route_table_id", props.RouteTable.ID)
} else {
d.Set("route_table_id", "")
}
if err := d.Set("ip_configurations", flattenSubnetIPConfigurations(props.IPConfigurations)); err != nil {
return err
}
if err := d.Set("service_endpoints", flattenSubnetServiceEndpoints(props.ServiceEndpoints)); err != nil {
return err
}
}
return nil
}