providers/aws: import network ACLs

This commit is contained in:
Mitchell Hashimoto 2016-05-13 13:01:05 -07:00
parent 2d5745328b
commit f6b77a6c02
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,95 @@
package aws
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
// Network ACLs import their rules and associations
func resourceAwsNetworkAclImportState(
d *schema.ResourceData,
meta interface{}) ([]*schema.ResourceData, error) {
conn := meta.(*AWSClient).ec2conn
// First query the resource itself
resp, err := conn.DescribeNetworkAcls(&ec2.DescribeNetworkAclsInput{
NetworkAclIds: []*string{aws.String(d.Id())},
})
if err != nil {
return nil, err
}
if resp == nil || len(resp.NetworkAcls) < 1 || resp.NetworkAcls[0] == nil {
return nil, fmt.Errorf("network ACL %s is not found", d.Id())
}
acl := resp.NetworkAcls[0]
// Start building our results
results := make([]*schema.ResourceData, 1,
2+len(acl.Associations)+len(acl.Entries))
results[0] = d
/*
{
// Construct the entries
subResource := resourceAwsNetworkAclRule()
for _, entry := range acl.Entries {
// Minimal data for route
d := subResource.Data(nil)
d.SetType("aws_network_acl_rule")
d.Set("network_acl_id", acl.NetworkAclId)
d.Set("rule_number", entry.RuleNumber)
d.Set("egress", entry.Egress)
d.Set("protocol", entry.Protocol)
d.SetId(networkAclIdRuleNumberEgressHash(
d.Get("network_acl_id").(string),
d.Get("rule_number").(int),
d.Get("egress").(bool),
d.Get("protocol").(string)))
results = append(results, d)
}
}
{
// Construct the associations
subResource := resourceAwsRouteTableAssociation()
for _, assoc := range table.Associations {
if *assoc.Main {
// Ignore
continue
}
// Minimal data for route
d := subResource.Data(nil)
d.SetType("aws_route_table_association")
d.Set("route_table_id", assoc.RouteTableId)
d.SetId(*assoc.RouteTableAssociationId)
results = append(results, d)
}
}
{
// Construct the main associations. We could do this above but
// I keep this as a separate section since it is a separate resource.
subResource := resourceAwsMainRouteTableAssociation()
for _, assoc := range table.Associations {
if !*assoc.Main {
// Ignore
continue
}
// Minimal data for route
d := subResource.Data(nil)
d.SetType("aws_main_route_table_association")
d.Set("route_table_id", id)
d.Set("vpc_id", table.VpcId)
d.SetId(*assoc.RouteTableAssociationId)
results = append(results, d)
}
}
*/
return results, nil
}

View File

@ -0,0 +1,37 @@
package aws
import (
"testing"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccAWSNetworkAcl_importBasic(t *testing.T) {
/*
checkFn := func(s []*terraform.InstanceState) error {
// Expect 2: acl, 2 rules
if len(s) != 3 {
return fmt.Errorf("bad states: %#v", s)
}
return nil
}
*/
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSNetworkAclDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSNetworkAclEgressNIngressConfig,
},
resource.TestStep{
ResourceName: "aws_network_acl.bar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

View File

@ -23,6 +23,9 @@ func resourceAwsNetworkAcl() *schema.Resource {
Read: resourceAwsNetworkAclRead,
Delete: resourceAwsNetworkAclDelete,
Update: resourceAwsNetworkAclUpdate,
Importer: &schema.ResourceImporter{
State: resourceAwsNetworkAclImportState,
},
Schema: map[string]*schema.Schema{
"vpc_id": &schema.Schema{