Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import support for aws_network_interface_sg_attachment resource #27785

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/27785.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note: enhancement
resource/aws_network_interface_sg_attachment: Add import support
```
44 changes: 44 additions & 0 deletions internal/service/ec2/vpc_network_interface_sg_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ec2
import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
Expand All @@ -17,6 +18,9 @@ func ResourceNetworkInterfaceSGAttachment() *schema.Resource {
Create: resourceNetworkInterfaceSGAttachmentCreate,
Read: resourceNetworkInterfaceSGAttachmentRead,
Delete: resourceNetworkInterfaceSGAttachmentDelete,
Importer: &schema.ResourceImporter{
State: resourceNetworkInterfaceSGAttachmentImport,
},

Schema: map[string]*schema.Schema{
"network_interface_id": {
Expand Down Expand Up @@ -161,3 +165,43 @@ func resourceNetworkInterfaceSGAttachmentDelete(d *schema.ResourceData, meta int

return nil
}

func resourceNetworkInterfaceSGAttachmentImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "_")
if len(parts) != 2 {
return []*schema.ResourceData{}, fmt.Errorf("Unexpected format for import: %s. Please use '<NetworkInterfaceId>_<SecurityGroupID>", d.Id())
}

networkInterfaceID := parts[0]
securityGroupID := parts[1]

log.Printf("[DEBUG] Importing network interface security group association, Interface: %s, Security Group: %s", networkInterfaceID, securityGroupID)

conn := meta.(*conns.AWSClient).EC2Conn

networkInterface, err := FindNetworkInterfaceByID(conn, networkInterfaceID)

if err != nil {
return nil, err
}

var associationID string

for _, attachedSecurityGroup := range networkInterface.Groups {
if aws.StringValue(attachedSecurityGroup.GroupId) == securityGroupID {
d.Set("security_group_id", securityGroupID)
associationID = securityGroupID + "_" + networkInterfaceID

break
}
}

if associationID == "" {
return nil, fmt.Errorf("Security Group %s is not attached to Network Interface %s", securityGroupID, networkInterfaceID)
}

d.SetId(associationID)
d.Set("network_interface_id", networkInterfaceID)

return []*schema.ResourceData{d}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func TestAccVPCNetworkInterfaceSgAttachment_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(resourceName, "security_group_id", securityGroupResourceName, "id"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateIdFunc: testAccVPCNetworkInterfaceSGAttachmentImportStateIdFunc(resourceName),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -305,3 +311,21 @@ resource "aws_network_interface_sg_attachment" "test" {
}
`, rName)
}

func testAccVPCNetworkInterfaceSGAttachmentImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]

if !ok {
return "", fmt.Errorf("not found: %s", resourceName)
}

var networkInterfaceID string
var securityGroupID string

networkInterfaceID = rs.Primary.Attributes["network_interface_id"]
securityGroupID = rs.Primary.Attributes["security_group_id"]

return fmt.Sprintf("%s_%s", networkInterfaceID, securityGroupID), nil
}
}
10 changes: 10 additions & 0 deletions website/docs/r/network_interface_sg_attachment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,13 @@ resource "aws_network_interface_sg_attachment" "sg_attachment" {
## Attributes Reference

No additional attributes are exported.

## Import

Network Interface Security Group attachments can be imported using the associated network interface ID and security group ID, separated by an underscore (`_`).

For example:

```
$ terraform import aws_network_interface_sg_attachment.sg_attachment eni-1234567890abcdef0_sg-1234567890abcdef0
```