forked from openshift/terraform-provider-azurerm
/
data_source_public_ip.go
126 lines (100 loc) · 2.95 KB
/
data_source_public_ip.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
package azurerm
import (
"fmt"
"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 dataSourceArmPublicIP() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmPublicIPRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
},
"location": locationForDataSourceSchema(),
"resource_group_name": resourceGroupNameForDataSourceSchema(),
"sku": {
Type: schema.TypeString,
Computed: true,
},
"allocation_method": {
Type: schema.TypeString,
Computed: true,
},
"ip_version": {
Type: schema.TypeString,
Computed: true,
},
"domain_name_label": {
Type: schema.TypeString,
Computed: true,
},
"idle_timeout_in_minutes": {
Type: schema.TypeInt,
Computed: true,
},
"fqdn": {
Type: schema.TypeString,
Computed: true,
},
"reverse_fqdn": {
Type: schema.TypeString,
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Computed: true,
},
"zones": zonesSchemaComputed(),
"tags": tagsSchema(),
},
}
}
func dataSourceArmPublicIPRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).publicIPClient
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: Public IP %q (Resource Group %q) was not found", name, resGroup)
}
return fmt.Errorf("Error making Read request on Azure public ip %s: %s", name, err)
}
d.SetId(*resp.ID)
d.Set("zones", resp.Zones)
//ensure values are at least set to "", d.Set() is a noop on a nil
//there must be a better way...
d.Set("location", "")
d.Set("sku", "")
d.Set("fqdn", "")
d.Set("reverse_fqdn", "")
d.Set("domain_name_label", "")
d.Set("allocation_method", "")
d.Set("ip_address", "")
d.Set("ip_version", "")
d.Set("idle_timeout_in_minutes", 0)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}
if sku := resp.Sku; sku != nil {
d.Set("sku", string(sku.Name))
}
if props := resp.PublicIPAddressPropertiesFormat; props != nil {
if dnsSettings := props.DNSSettings; dnsSettings != nil {
d.Set("fqdn", dnsSettings.Fqdn)
d.Set("reverse_fqdn", dnsSettings.ReverseFqdn)
d.Set("domain_name_label", dnsSettings.DomainNameLabel)
}
d.Set("allocation_method", string(props.PublicIPAllocationMethod))
d.Set("ip_address", props.IPAddress)
d.Set("ip_version", string(props.PublicIPAddressVersion))
d.Set("idle_timeout_in_minutes", props.IdleTimeoutInMinutes)
}
flattenAndSetTags(d, resp.Tags)
return nil
}