Skip to content

Commit

Permalink
Merge pull request #39427 from hashicorp/b-vpc-dhcp-options-panic
Browse files Browse the repository at this point in the history
vpc/dhcp_options: Fix panic
  • Loading branch information
YakDriver committed Sep 20, 2024
2 parents 77cd6ed + ce7b708 commit 14e2fd8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changelog/39427.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_vpc_dhcp_options: Fix a bug causing a panic crash when an option is absent
```
48 changes: 27 additions & 21 deletions internal/service/ec2/vpc_dhcp_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,33 @@ func newDHCPOptionsMap(tfToApi map[string]string) *dhcpOptionsMap {

// dhcpConfigurationsToResourceData sets Terraform ResourceData from a list of AWS API DHCP configurations.
func (m *dhcpOptionsMap) dhcpConfigurationsToResourceData(dhcpConfigurations []awstypes.DhcpConfiguration, d *schema.ResourceData) error {
for v := range m.tfToApi {
d.Set(v, nil)
// Clear existing values
for tfName := range m.tfToApi {
d.Set(tfName, nil)
}

for _, dhcpConfiguration := range dhcpConfigurations {
apiName := aws.ToString(dhcpConfiguration.Key)
if tfName, ok := m.apiToTf[apiName]; ok {
switch v := d.Get(tfName).(type) {
case string:
d.Set(tfName, dhcpConfiguration.Values[0].Value)
case []interface{}:
var values []*string
for _, v := range dhcpConfiguration.Values {
values = append(values, v.Value)
tfName, ok := m.apiToTf[apiName]
if !ok {
return fmt.Errorf("unsupported DHCP option: %s", apiName)
}

currentValue := d.Get(tfName)

switch currentValue.(type) {
case string:
d.Set(tfName, dhcpConfiguration.Values[0].Value)
case []interface{}:
var values []string
for _, v := range dhcpConfiguration.Values {
if v.Value != nil {
values = append(values, aws.ToString(v.Value))
}
d.Set(tfName, values)
default:
return fmt.Errorf("Attribute (%s) is of unsupported type: %T", tfName, v)
}
} else {
return fmt.Errorf("Unsupported DHCP option: %s", apiName)
d.Set(tfName, values)
default:
return fmt.Errorf("attribute (%s) is of unsupported type: %T", tfName, currentValue)
}
}

Expand All @@ -287,7 +293,8 @@ func (m *dhcpOptionsMap) resourceDataToDHCPConfigurations(d *schema.ResourceData
var output []awstypes.NewDhcpConfiguration

for tfName, apiName := range m.tfToApi {
switch v := d.Get(tfName).(type) {
value := d.Get(tfName)
switch v := value.(type) {
case string:
if v != "" {
output = append(output, awstypes.NewDhcpConfiguration{
Expand All @@ -297,10 +304,9 @@ func (m *dhcpOptionsMap) resourceDataToDHCPConfigurations(d *schema.ResourceData
}
case []interface{}:
var values []string
for _, v := range v {
v := v.(string)
if v != "" {
values = append(values, v)
for _, item := range v {
if str, ok := item.(string); ok && str != "" {
values = append(values, str)
}
}
if len(values) > 0 {
Expand All @@ -310,7 +316,7 @@ func (m *dhcpOptionsMap) resourceDataToDHCPConfigurations(d *schema.ResourceData
})
}
default:
return nil, fmt.Errorf("Attribute (%s) is of unsupported type: %T", tfName, v)
return nil, fmt.Errorf("attribute (%s) is of unsupported type: %T", tfName, value)
}
}

Expand Down

0 comments on commit 14e2fd8

Please sign in to comment.