-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
data_source_dev_test_virtual_network.go
189 lines (158 loc) · 5.12 KB
/
data_source_dev_test_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package azurerm
import (
"fmt"
"time"
"github.com/Azure/azure-sdk-for-go/services/devtestlabs/mgmt/2016-05-15/dtl"
"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 dataSourceArmDevTestVirtualNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDevTestVnetRead,
Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"lab_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.DevTestLabName(),
},
"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),
"unique_identifier": {
Type: schema.TypeString,
Computed: true,
},
"allowed_subnets": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allow_public_ip": {
Type: schema.TypeString,
Computed: true,
},
"lab_subnet_name": {
Type: schema.TypeString,
Computed: true,
},
"resource_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"subnet_overrides": {
Type: schema.TypeList,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"lab_subnet_name": {
Type: schema.TypeString,
Computed: true,
},
"resource_id": {
Type: schema.TypeString,
Computed: true,
},
"use_in_vm_creation_permission": {
Type: schema.TypeString,
Computed: true,
},
"use_public_ip_address_permission": {
Type: schema.TypeString,
Computed: true,
},
"virtual_network_pool_name": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceArmDevTestVnetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).DevTestLabs.VirtualNetworksClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
defer cancel()
resGroup := d.Get("resource_group_name").(string)
labName := d.Get("lab_name").(string)
name := d.Get("name").(string)
resp, err := client.Get(ctx, resGroup, labName, name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
return fmt.Errorf("Error: Virtual Network %q in Dev Test Lab %q (Resource Group %q) was not found", name, labName, resGroup)
}
return fmt.Errorf("Error making Read request on Virtual Network %q in Dev Test Lab %q (Resource Group %q): %+v", name, labName, resGroup, err)
}
if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("API returns a nil/empty id on Virtual Network %q in Dev Test Lab %q (Resource Group %q): %+v", name, labName, resGroup, err)
}
d.SetId(*resp.ID)
if props := resp.VirtualNetworkProperties; props != nil {
if as := props.AllowedSubnets; as != nil {
if err := d.Set("allowed_subnets", flattenDevTestVirtualNetworkAllowedSubnets(as)); err != nil {
return fmt.Errorf("error setting `allowed_subnets`: %v", err)
}
}
if so := props.SubnetOverrides; so != nil {
if err := d.Set("subnet_overrides", flattenDevTestVirtualNetworkSubnetOverrides(so)); err != nil {
return fmt.Errorf("error setting `subnet_overrides`: %v", err)
}
}
d.Set("unique_identifier", props.UniqueIdentifier)
}
return nil
}
func flattenDevTestVirtualNetworkAllowedSubnets(input *[]dtl.Subnet) []interface{} {
result := make([]interface{}, 0)
if input == nil {
return result
}
for _, v := range *input {
allowedSubnet := make(map[string]interface{})
allowedSubnet["allow_public_ip"] = string(v.AllowPublicIP)
if resourceID := v.ResourceID; resourceID != nil {
allowedSubnet["resource_id"] = *resourceID
}
if labSubnetName := v.LabSubnetName; labSubnetName != nil {
allowedSubnet["lab_subnet_name"] = *labSubnetName
}
result = append(result, allowedSubnet)
}
return result
}
func flattenDevTestVirtualNetworkSubnetOverrides(input *[]dtl.SubnetOverride) []interface{} {
result := make([]interface{}, 0)
if input == nil {
return result
}
for _, v := range *input {
subnetOverride := make(map[string]interface{})
if v.LabSubnetName != nil {
subnetOverride["lab_subnet_name"] = *v.LabSubnetName
}
if v.ResourceID != nil {
subnetOverride["resource_id"] = *v.ResourceID
}
subnetOverride["use_public_ip_address_permission"] = string(v.UsePublicIPAddressPermission)
subnetOverride["use_in_vm_creation_permission"] = string(v.UseInVMCreationPermission)
if v.VirtualNetworkPoolName != nil {
subnetOverride["virtual_network_pool_name"] = *v.VirtualNetworkPoolName
}
result = append(result, subnetOverride)
}
return result
}