forked from hashicorp/terraform-provider-aws
/
data_source_aws_vpc_dhcp_options.go
131 lines (113 loc) · 3.49 KB
/
data_source_aws_vpc_dhcp_options.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
package aws
import (
"errors"
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceAwsVpcDhcpOptions() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsVpcDhcpOptionsRead,
Schema: map[string]*schema.Schema{
"dhcp_options_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"domain_name": {
Type: schema.TypeString,
Computed: true,
},
"domain_name_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"filter": ec2CustomFiltersSchema(),
"netbios_name_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"netbios_node_type": {
Type: schema.TypeString,
Computed: true,
},
"ntp_servers": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tagsSchemaComputed(),
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAwsVpcDhcpOptionsRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
input := &ec2.DescribeDhcpOptionsInput{}
if v, ok := d.GetOk("dhcp_options_id"); ok && v.(string) != "" {
input.DhcpOptionsIds = []*string{aws.String(v.(string))}
}
input.Filters = append(input.Filters, buildEC2CustomFilterList(
d.Get("filter").(*schema.Set),
)...)
if len(input.Filters) == 0 {
// Don't send an empty filters list; the EC2 API won't accept it.
input.Filters = nil
}
log.Printf("[DEBUG] Reading EC2 DHCP Options: %s", input)
output, err := conn.DescribeDhcpOptions(input)
if err != nil {
if isNoSuchDhcpOptionIDErr(err) {
return errors.New("No matching EC2 DHCP Options found")
}
return fmt.Errorf("error reading EC2 DHCP Options: %s", err)
}
if len(output.DhcpOptions) == 0 {
return errors.New("No matching EC2 DHCP Options found")
}
if len(output.DhcpOptions) > 1 {
return errors.New("Multiple matching EC2 DHCP Options found")
}
dhcpOptionID := aws.StringValue(output.DhcpOptions[0].DhcpOptionsId)
d.SetId(dhcpOptionID)
d.Set("dhcp_options_id", dhcpOptionID)
dhcpConfigurations := output.DhcpOptions[0].DhcpConfigurations
for _, dhcpConfiguration := range dhcpConfigurations {
key := aws.StringValue(dhcpConfiguration.Key)
tfKey := strings.Replace(key, "-", "_", -1)
if len(dhcpConfiguration.Values) == 0 {
continue
}
switch key {
case "domain-name":
d.Set(tfKey, aws.StringValue(dhcpConfiguration.Values[0].Value))
case "domain-name-servers":
if err := d.Set(tfKey, flattenEc2AttributeValues(dhcpConfiguration.Values)); err != nil {
return fmt.Errorf("error setting %s: %s", tfKey, err)
}
case "netbios-name-servers":
if err := d.Set(tfKey, flattenEc2AttributeValues(dhcpConfiguration.Values)); err != nil {
return fmt.Errorf("error setting %s: %s", tfKey, err)
}
case "netbios-node-type":
d.Set(tfKey, aws.StringValue(dhcpConfiguration.Values[0].Value))
case "ntp-servers":
if err := d.Set(tfKey, flattenEc2AttributeValues(dhcpConfiguration.Values)); err != nil {
return fmt.Errorf("error setting %s: %s", tfKey, err)
}
}
}
if err := d.Set("tags", d.Set("tags", tagsToMap(output.DhcpOptions[0].Tags))); err != nil {
return fmt.Errorf("error setting tags: %s", err)
}
d.Set("owner_id", output.DhcpOptions[0].OwnerId)
return nil
}