mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
rearranged the tests
This commit is contained in:
parent
5eec8a531c
commit
cd0f9761da
@ -1,13 +1,11 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
|
||||
"github.com/mitchellh/goamz/ec2"
|
||||
)
|
||||
|
||||
func expandNetworkAclEntries(configured []interface{}) ([]ec2.NetworkAclEntry) {
|
||||
func expandNetworkAclEntries(configured []interface{}, entryType string) []ec2.NetworkAclEntry {
|
||||
entries := make([]ec2.NetworkAclEntry, 0, len(configured))
|
||||
|
||||
for _, eRaw := range configured {
|
||||
data := eRaw.(map[string]interface{})
|
||||
p := extractProtocolInteger(data["protocol"].(string))
|
||||
@ -17,7 +15,7 @@ func expandNetworkAclEntries(configured []interface{}) ([]ec2.NetworkAclEntry) {
|
||||
From: data["from_port"].(int),
|
||||
To: data["to_port"].(int),
|
||||
},
|
||||
Egress: false,
|
||||
Egress: (entryType == "egress"),
|
||||
RuleAction: data["action"].(string),
|
||||
RuleNumber: data["rule_no"].(int),
|
||||
CidrBlock: data["cidr_block"].(string),
|
||||
@ -52,15 +50,14 @@ func extractProtocolInteger(protocol string) int {
|
||||
|
||||
func extractProtocolString(protocol int) string {
|
||||
for key, value := range protocolIntegers() {
|
||||
if value == protocol{
|
||||
if value == protocol {
|
||||
return key
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
func protocolIntegers() map[string]int{
|
||||
func protocolIntegers() map[string]int {
|
||||
var protocolIntegers = make(map[string]int)
|
||||
protocolIntegers = map[string]int{
|
||||
"udp": 17,
|
||||
|
@ -26,7 +26,7 @@ func Test_expandNetworkAclEntryJoJo(t *testing.T) {
|
||||
"rule_no": 2,
|
||||
},
|
||||
}
|
||||
expanded := expandNetworkAclEntries(input)
|
||||
expanded := expandNetworkAclEntries(input, "egress")
|
||||
|
||||
expected := []ec2.NetworkAclEntry{
|
||||
ec2.NetworkAclEntry{
|
||||
@ -38,8 +38,8 @@ func Test_expandNetworkAclEntryJoJo(t *testing.T) {
|
||||
RuleAction: "deny",
|
||||
RuleNumber: 1,
|
||||
CidrBlock: "0.0.0.0/0",
|
||||
Egress: false,
|
||||
IcmpCode:ec2.IcmpCode{Code:0, Type:0},
|
||||
Egress: true,
|
||||
IcmpCode: ec2.IcmpCode{Code: 0, Type: 0},
|
||||
},
|
||||
ec2.NetworkAclEntry{
|
||||
Protocol: 6,
|
||||
@ -50,15 +50,15 @@ func Test_expandNetworkAclEntryJoJo(t *testing.T) {
|
||||
RuleAction: "deny",
|
||||
RuleNumber: 2,
|
||||
CidrBlock: "0.0.0.0/0",
|
||||
Egress: false,
|
||||
IcmpCode: ec2.IcmpCode{Code:0, Type:0},
|
||||
Egress: true,
|
||||
IcmpCode: ec2.IcmpCode{Code: 0, Type: 0},
|
||||
},
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(expanded, expected) {
|
||||
t.Fatalf(
|
||||
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
||||
expanded[0],
|
||||
expanded,
|
||||
expected)
|
||||
}
|
||||
|
||||
@ -109,11 +109,10 @@ func Test_flattenNetworkAclEntryJoJo(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
if !reflect.DeepEqual(flattened, expected) {
|
||||
t.Fatalf(
|
||||
"Got:\n\n%#v\n\nExpected:\n\n%#v\n",
|
||||
flattened,
|
||||
flattened[0],
|
||||
expected)
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,12 @@
|
||||
package aws
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"bytes"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/hashicorp/terraform/helper/hashcode"
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/mitchellh/goamz/ec2"
|
||||
)
|
||||
|
||||
@ -146,14 +146,13 @@ func resourceAwsNetworkAclRead(d *schema.ResourceData, meta interface{}) error {
|
||||
d.Set("vpc_id", networkAcl.VpcId)
|
||||
|
||||
for _, e := range networkAcl.EntrySet {
|
||||
if(e.Egress == true){
|
||||
if e.Egress == true {
|
||||
egressEntries = append(egressEntries, e)
|
||||
} else{
|
||||
} else {
|
||||
ingressEntries = append(ingressEntries, e)
|
||||
}
|
||||
}
|
||||
fmt.Printf("appending ingress entries %s", ingressEntries)
|
||||
|
||||
fmt.Printf("appending egress entries %s", egressEntries)
|
||||
|
||||
d.Set("ingress", ingressEntries)
|
||||
@ -162,33 +161,30 @@ func resourceAwsNetworkAclRead(d *schema.ResourceData, meta interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
func resourceAwsNetworkAclUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
|
||||
d.Partial(true)
|
||||
|
||||
if(d.HasChange("ingress")) {
|
||||
if d.HasChange("ingress") {
|
||||
err := updateNetworkAclEntries(d, "ingress", ec2conn)
|
||||
if(err != nil) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if(d.HasChange("egress")) {
|
||||
if d.HasChange("egress") {
|
||||
err := updateNetworkAclEntries(d, "egress", ec2conn)
|
||||
if(err != nil){
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
d.Partial(false)
|
||||
|
||||
return resourceAwsNetworkAclRead(d, meta)
|
||||
|
||||
}
|
||||
|
||||
func updateNetworkAclEntries(d *schema.ResourceData, entryType string, ec2conn *ec2.EC2) error{
|
||||
func updateNetworkAclEntries(d *schema.ResourceData, entryType string, ec2conn *ec2.EC2) error {
|
||||
|
||||
o, n := d.GetChange(entryType)
|
||||
fmt.Printf("Old : %s", o)
|
||||
@ -204,8 +200,8 @@ func updateNetworkAclEntries(d *schema.ResourceData, entryType string, ec2conn *
|
||||
os := o.(*schema.Set)
|
||||
ns := n.(*schema.Set)
|
||||
|
||||
toBeDeleted := expandNetworkAclEntries(os.Difference(ns).List())
|
||||
toBeCreated := expandNetworkAclEntries(ns.Difference(os).List())
|
||||
toBeDeleted := expandNetworkAclEntries(os.Difference(ns).List(), entryType)
|
||||
toBeCreated := expandNetworkAclEntries(ns.Difference(os).List(), entryType)
|
||||
fmt.Printf("to be created %s", toBeCreated)
|
||||
for _, remove := range toBeDeleted {
|
||||
// Revoke the old entry
|
||||
@ -230,7 +226,6 @@ func updateNetworkAclEntries(d *schema.ResourceData, entryType string, ec2conn *
|
||||
func resourceAwsNetworkAclDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
ec2conn := meta.(*AWSClient).ec2conn
|
||||
|
||||
|
||||
log.Printf("[INFO] Deleting Network Acl: %s", d.Id())
|
||||
if _, err := ec2conn.DeleteNetworkAcl(d.Id()); err != nil {
|
||||
ec2err, ok := err.(*ec2.Error)
|
||||
|
@ -11,79 +11,49 @@ import (
|
||||
// "github.com/hashicorp/terraform/helper/schema"
|
||||
)
|
||||
|
||||
const testAccAWSNetworkAclIngressConfig = `
|
||||
resource "aws_vpc" "foo" {
|
||||
cidr_block = "10.1.0.0/16"
|
||||
}
|
||||
resource "aws_subnet" "blob" {
|
||||
cidr_block = "10.1.1.0/24"
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
map_public_ip_on_launch = true
|
||||
}
|
||||
resource "aws_network_acl" "bar" {
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
ingress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 2
|
||||
action = "deny"
|
||||
cidr_block = "10.2.2.3/18"
|
||||
from_port = 0
|
||||
to_port = 22
|
||||
}
|
||||
func TestAccAWSNetworkAclsWithEgressAndIngressRulesSneha(t *testing.T) {
|
||||
var networkAcl ec2.NetworkAcl
|
||||
|
||||
ingress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 1
|
||||
action = "deny"
|
||||
cidr_block = "10.2.10.3/18"
|
||||
from_port = 443
|
||||
to_port = 443
|
||||
}
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testAccCheckAWSNetworkAclDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: testAccAWSNetworkAclEgressNIngressConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckAWSNetworkAclExists("aws_network_acl.bar", &networkAcl),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "ingress.0.protocol", "tcp"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "ingress.0.rule_no", "1"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "ingress.0.from_port", "80"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "ingress.0.to_port", "80"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "ingress.0.action", "allow"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "ingress.0.cidr_block", "10.3.10.3/18"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "egress.0.protocol", "tcp"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "egress.0.rule_no", "2"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "egress.0.from_port", "443"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "egress.0.to_port", "443"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "egress.0.cidr_block", "10.3.2.3/18"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.bar", "egress.0.action", "allow"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
`
|
||||
|
||||
|
||||
const testAccAWSNetworkAclEgressConfig = `
|
||||
resource "aws_vpc" "foo" {
|
||||
cidr_block = "10.2.0.0/16"
|
||||
}
|
||||
resource "aws_subnet" "blob" {
|
||||
cidr_block = "10.2.0.0/24"
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
map_public_ip_on_launch = true
|
||||
}
|
||||
resource "aws_network_acl" "bar" {
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
egress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 2
|
||||
action = "allow"
|
||||
cidr_block = "10.2.2.3/18"
|
||||
from_port = 443
|
||||
to_port = 443
|
||||
}
|
||||
|
||||
egress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 1
|
||||
action = "allow"
|
||||
cidr_block = "10.2.10.3/18"
|
||||
from_port = 80
|
||||
to_port = 80
|
||||
}
|
||||
|
||||
egress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 3
|
||||
action = "allow"
|
||||
cidr_block = "10.2.10.3/18"
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
func TestAccAWSNetworkAclsIngressSneha(t *testing.T) {
|
||||
func TestAccAWSNetworkAclsOnlyIngressRulesSneha(t *testing.T) {
|
||||
var networkAcl ec2.NetworkAcl
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
@ -94,15 +64,26 @@ func TestAccAWSNetworkAclsIngressSneha(t *testing.T) {
|
||||
resource.TestStep{
|
||||
Config: testAccAWSNetworkAclIngressConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckAWSNetworkAclExists("aws_network_acl.bar", &networkAcl),
|
||||
testAccCheckAWSNetworkAclExists("aws_network_acl.foos", &networkAcl),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.foos", "ingress.0.protocol", "tcp"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.foos", "ingress.0.rule_no", "2"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.foos", "ingress.0.from_port", "0"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.foos", "ingress.0.to_port", "22"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.foos", "ingress.0.action", "deny"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"aws_network_acl.foos", "ingress.0.cidr_block", "10.2.2.3/18"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
func TestAccAWSNetworkAclsEgressSneha(t *testing.T) {
|
||||
func TestAccAWSNetworkAclsOnlyEgressRulesSneha(t *testing.T) {
|
||||
var networkAcl ec2.NetworkAcl
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
@ -113,7 +94,7 @@ func TestAccAWSNetworkAclsEgressSneha(t *testing.T) {
|
||||
resource.TestStep{
|
||||
Config: testAccAWSNetworkAclEgressConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testAccCheckAWSNetworkAclExists("aws_network_acl.bar", &networkAcl),
|
||||
testAccCheckAWSNetworkAclExists("aws_network_acl.bond", &networkAcl),
|
||||
),
|
||||
},
|
||||
},
|
||||
@ -176,3 +157,96 @@ func testAccCheckAWSNetworkAclExists(n string, networkAcl *ec2.NetworkAcl) resou
|
||||
return fmt.Errorf("Network Acls not found")
|
||||
}
|
||||
}
|
||||
|
||||
const testAccAWSNetworkAclIngressConfig = `
|
||||
resource "aws_vpc" "foo" {
|
||||
cidr_block = "10.1.0.0/16"
|
||||
}
|
||||
resource "aws_subnet" "blob" {
|
||||
cidr_block = "10.1.1.0/24"
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
map_public_ip_on_launch = true
|
||||
}
|
||||
resource "aws_network_acl" "foos" {
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
ingress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 2
|
||||
action = "deny"
|
||||
cidr_block = "10.2.2.3/18"
|
||||
from_port = 0
|
||||
to_port = 22
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
const testAccAWSNetworkAclEgressConfig = `
|
||||
resource "aws_vpc" "foo" {
|
||||
cidr_block = "10.2.0.0/16"
|
||||
}
|
||||
resource "aws_subnet" "blob" {
|
||||
cidr_block = "10.2.0.0/24"
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
map_public_ip_on_launch = true
|
||||
}
|
||||
resource "aws_network_acl" "bond" {
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
egress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 2
|
||||
action = "allow"
|
||||
cidr_block = "10.2.2.3/18"
|
||||
from_port = 443
|
||||
to_port = 443
|
||||
}
|
||||
|
||||
egress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 1
|
||||
action = "allow"
|
||||
cidr_block = "10.2.10.3/18"
|
||||
from_port = 80
|
||||
to_port = 80
|
||||
}
|
||||
|
||||
egress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 3
|
||||
action = "allow"
|
||||
cidr_block = "10.2.10.3/18"
|
||||
from_port = 22
|
||||
to_port = 22
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
const testAccAWSNetworkAclEgressNIngressConfig = `
|
||||
resource "aws_vpc" "foo" {
|
||||
cidr_block = "10.3.0.0/16"
|
||||
}
|
||||
resource "aws_subnet" "blob" {
|
||||
cidr_block = "10.3.0.0/24"
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
map_public_ip_on_launch = true
|
||||
}
|
||||
resource "aws_network_acl" "bar" {
|
||||
vpc_id = "${aws_vpc.foo.id}"
|
||||
egress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 2
|
||||
action = "allow"
|
||||
cidr_block = "10.3.2.3/18"
|
||||
from_port = 443
|
||||
to_port = 443
|
||||
}
|
||||
|
||||
ingress = {
|
||||
protocol = "tcp"
|
||||
rule_no = 1
|
||||
action = "allow"
|
||||
cidr_block = "10.3.10.3/18"
|
||||
from_port = 80
|
||||
to_port = 80
|
||||
}
|
||||
}
|
||||
`
|
||||
|
Loading…
Reference in New Issue
Block a user