Skip to content

Commit

Permalink
Remove extra else
Browse files Browse the repository at this point in the history
  • Loading branch information
autotune committed Oct 5, 2023
1 parent 2749d3b commit 5c079b3
Showing 1 changed file with 87 additions and 101 deletions.
188 changes: 87 additions & 101 deletions internal/service/ec2/ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,60 +1041,53 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, meta in

// tags in root_block_device and ebs_block_device
// Initialize a map to store block device tags
blockDeviceTagsToCreate := map[string]map[string]interface{}{}

// Check if "root_block_device" data exists
if rootBlockDeviceData, ok := d.GetOk("root_block_device"); ok {
// Convert the data to a slice of interfaces
rootBlockDevices := rootBlockDeviceData.([]interface{})

// Iterate through each root block device
for _, rootBlockDevice := range rootBlockDevices {
// Convert the root block device data to a map
blockDeviceData := rootBlockDevice.(map[string]interface{})

// Check if the block device has tags and if they are non-empty
if blockDeviceTags, tagsExist := blockDeviceData["tags"].(map[string]interface{}); tagsExist && len(blockDeviceTags) > 0 {
// Calculate the root volume ID based on instance information
rootVolumeID := getRootVolumeId(instance)
for key, value := range tagValues {
blockDeviceTags[key] = value
}
// If the root volume ID is not empty, associate the tags with it
if rootVolumeID != "" {
blockDeviceTagsToCreate[rootVolumeID] = blockDeviceTags
}
} else {
// Calculate the root volume ID based on instance information
rootVolumeID := getRootVolumeId(instance)
for key, value := range tagValues {
blockDeviceTags[key] = value
}
// If the root volume ID is not empty, associate the tags with it
if rootVolumeID != "" {
blockDeviceTagsToCreate[rootVolumeID] = blockDeviceTags
if d.HasChange("root_block_device.0.tags") {
blockDeviceTagsToCreate := map[string]map[string]interface{}{}

// Check if "root_block_device" data exists
if rootBlockDeviceData, ok := d.GetOk("root_block_device"); ok {
// Convert the data to a slice of interfaces
rootBlockDevices := rootBlockDeviceData.([]interface{})

// Iterate through each root block device
for _, rootBlockDevice := range rootBlockDevices {
// Convert the root block device data to a map
blockDeviceData := rootBlockDevice.(map[string]interface{})

// Check if the block device has tags and if they are non-empty
if blockDeviceTags, tagsExist := blockDeviceData["tags"].(map[string]interface{}); tagsExist && len(blockDeviceTags) >= 0 {
// Calculate the root volume ID based on instance information
rootVolumeID := getRootVolumeId(instance)
for key, value := range tagValues {
blockDeviceTags[key] = value
}
// If the root volume ID is not empty, associate the tags with it
if rootVolumeID != "" {
blockDeviceTagsToCreate[rootVolumeID] = blockDeviceTags
}
}
}
}
}

if v, ok := d.GetOk("ebs_block_device"); ok {
vL := v.(*schema.Set).List()
for _, v := range vL {
bd := v.(map[string]interface{})
if blockDeviceTags, ok := bd["tags"].(map[string]interface{}); ok && len(blockDeviceTags) > 0 {
devName := bd["device_name"].(string)
if volumeId := getVolumeIdByDeviceName(instance, devName); volumeId != "" {
blockDeviceTagsToCreate[volumeId] = blockDeviceTags
if v, ok := d.GetOk("ebs_block_device"); ok {
vL := v.(*schema.Set).List()
for _, v := range vL {
bd := v.(map[string]interface{})
if blockDeviceTags, ok := bd["tags"].(map[string]interface{}); ok && len(blockDeviceTags) > 0 {
devName := bd["device_name"].(string)
if volumeId := getVolumeIdByDeviceName(instance, devName); volumeId != "" {
blockDeviceTagsToCreate[volumeId] = blockDeviceTags
}
}
}
}
}

for vol, blockDeviceTags := range blockDeviceTagsToCreate {
if err := createTags(ctx, conn, vol, Tags(tftags.New(ctx, blockDeviceTags))); err != nil {
log.Printf("[ERR] Error creating tags for EBS volume %s: %s", vol, err)
for vol, blockDeviceTags := range blockDeviceTagsToCreate {
if err := createTags(ctx, conn, vol, Tags(tftags.New(ctx, blockDeviceTags))); err != nil {
log.Printf("[ERR] Error creating tags for EBS volume %s: %s", vol, err)
}
}

Check failure on line 1090 in internal/service/ec2/ec2_instance.go

View workflow job for this annotation

GitHub Actions / 2 of 2

unnecessary trailing newline (whitespace)
}

// Update if we need to
Expand Down Expand Up @@ -1498,70 +1491,63 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, meta in
return sdkdiag.AppendErrorf(diags, "reading EC2 Instance (%s): %s", d.Id(), err)
}

tagSpecifications := getTagSpecificationsIn(ctx, ec2.ResourceTypeInstance)
if d.HasChange("root_block_device.0.tags") {

Check failure on line 1494 in internal/service/ec2/ec2_instance.go

View workflow job for this annotation

GitHub Actions / 2 of 2

unnecessary leading newline (whitespace)

// Declare the map outside the loops so values aren't overwritten
tagValues := make(map[string]interface{})
// Iterate over each tagSpecification
for i := 0; i < len(tagSpecifications); i++ {
// Iterate over each tag within the current tagSpecification
for j := 0; j < len(tagSpecifications[i].Tags); j++ {
// Extract the key and value from the current tag
key := aws.StringValue(tagSpecifications[i].Tags[j].Key)
value := aws.StringValue(tagSpecifications[i].Tags[j].Value)
// Add the key-value pair to the map
tagValues[key] = value //value
tagSpecifications := getTagSpecificationsIn(ctx, ec2.ResourceTypeInstance)

// Declare the map outside the loops so values aren't overwritten
tagValues := make(map[string]interface{})
// Iterate over each tagSpecification
for i := 0; i < len(tagSpecifications); i++ {
// Iterate over each tag within the current tagSpecification
for j := 0; j < len(tagSpecifications[i].Tags); j++ {
// Extract the key and value from the current tag
key := aws.StringValue(tagSpecifications[i].Tags[j].Key)
value := aws.StringValue(tagSpecifications[i].Tags[j].Value)
// Add the key-value pair to the map
tagValues[key] = value //value
}
}
}

// tags in root_block_device and ebs_block_device
// Initialize a map to store block device tags
blockDeviceTagsToCreate := map[string]map[string]interface{}{}

// Check if "root_block_device" data exists
if rootBlockDeviceData, ok := d.GetOk("root_block_device"); ok {
// Convert the data to a slice of interfaces
rootBlockDevices := rootBlockDeviceData.([]interface{})

// Iterate through each root block device
for _, rootBlockDevice := range rootBlockDevices {
// Convert the root block device data to a map
blockDeviceData := rootBlockDevice.(map[string]interface{})

// Check if the block device has tags and if they are non-empty
if blockDeviceTags, tagsExist := blockDeviceData["tags"].(map[string]interface{}); tagsExist && len(blockDeviceTags) > 0 {
// Calculate the root volume ID based on instance information
rootVolumeID := getRootVolumeId(instance)
for key, value := range tagValues {
blockDeviceTags[key] = value
}
// If the root volume ID is not empty, associate the tags with it
if rootVolumeID != "" {
blockDeviceTagsToCreate[rootVolumeID] = blockDeviceTags
}
} else {
// Calculate the root volume ID based on instance information
rootVolumeID := getRootVolumeId(instance)
for key, value := range tagValues {
blockDeviceTags[key] = value
}
// If the root volume ID is not empty, associate the tags with it
if rootVolumeID != "" {
blockDeviceTagsToCreate[rootVolumeID] = blockDeviceTags
// tags in root_block_device and ebs_block_device
// Initialize a map to store block device tags
blockDeviceTagsToCreate := map[string]map[string]interface{}{}

// Check if "root_block_device" data exists
if rootBlockDeviceData, ok := d.GetOk("root_block_device"); ok {
// Convert the data to a slice of interfaces
rootBlockDevices := rootBlockDeviceData.([]interface{})

// Iterate through each root block device
for _, rootBlockDevice := range rootBlockDevices {
// Convert the root block device data to a map
blockDeviceData := rootBlockDevice.(map[string]interface{})

// Check if the block device has tags and if they are non-empty
if blockDeviceTags, tagsExist := blockDeviceData["tags"].(map[string]interface{}); tagsExist && len(blockDeviceTags) >= 0 {
// Calculate the root volume ID based on instance information
rootVolumeID := getRootVolumeId(instance)
for key, value := range tagValues {
blockDeviceTags[key] = value
}
// If the root volume ID is not empty, associate the tags with it
if rootVolumeID != "" {
blockDeviceTagsToCreate[rootVolumeID] = blockDeviceTags
}
}
}
}
}
volumeIds, err := getInstanceVolumeIDs(ctx, conn, d.Id())
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating EC2 Instance (%s): %s", d.Id(), err)
}
volumeIds, err := getInstanceVolumeIDs(ctx, conn, d.Id())
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating EC2 Instance (%s): %s", d.Id(), err)
}

o, n := d.GetChange("root_block_device.0.tags")
o, n := d.GetChange("root_block_device.0.tags")

for _, volumeId := range volumeIds {
if err := updateTags(ctx, conn, volumeId, o, n); err != nil {
return sdkdiag.AppendErrorf(diags, "updating volume_tags (%s): %s", volumeId, err)
for _, volumeId := range volumeIds {
if err := updateTags(ctx, conn, volumeId, o, n); err != nil {
return sdkdiag.AppendErrorf(diags, "updating volume_tags (%s): %s", volumeId, err)
}
}
}

Expand Down

0 comments on commit 5c079b3

Please sign in to comment.