github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_ip_network_test.go (about) 1 package opc 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/hashicorp/go-oracle-terraform/compute" 9 "github.com/hashicorp/terraform/helper/acctest" 10 "github.com/hashicorp/terraform/helper/resource" 11 ) 12 13 func TestAccOPCIPNetwork_Basic(t *testing.T) { 14 rInt := acctest.RandInt() 15 resName := "opc_compute_ip_network.test" 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: opcResourceCheck(resName, testAccOPCCheckIPNetworkDestroyed), 21 Steps: []resource.TestStep{ 22 { 23 Config: testAccOPCIPNetworkConfig_Basic(rInt), 24 Check: resource.ComposeTestCheckFunc( 25 opcResourceCheck(resName, testAccOPCCheckIPNetworkExists), 26 resource.TestCheckResourceAttr(resName, "ip_address_prefix", "10.0.12.0/24"), 27 resource.TestCheckResourceAttr(resName, "public_napt_enabled", "false"), 28 resource.TestCheckResourceAttr(resName, "description", fmt.Sprintf("testing-desc-%d", rInt)), 29 resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("testing-ip-network-%d", rInt)), 30 resource.TestMatchResourceAttr(resName, "uri", regexp.MustCompile("testing-ip-network")), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccOPCIPNetwork_Update(t *testing.T) { 38 rInt := acctest.RandInt() 39 resName := "opc_compute_ip_network.test" 40 41 resource.Test(t, resource.TestCase{ 42 PreCheck: func() { testAccPreCheck(t) }, 43 Providers: testAccProviders, 44 CheckDestroy: opcResourceCheck(resName, testAccOPCCheckIPNetworkDestroyed), 45 Steps: []resource.TestStep{ 46 { 47 Config: testAccOPCIPNetworkConfig_Basic(rInt), 48 Check: resource.ComposeTestCheckFunc( 49 opcResourceCheck(resName, testAccOPCCheckIPNetworkExists), 50 resource.TestCheckResourceAttr(resName, "ip_address_prefix", "10.0.12.0/24"), 51 resource.TestCheckResourceAttr(resName, "public_napt_enabled", "false"), 52 resource.TestCheckResourceAttr(resName, "description", fmt.Sprintf("testing-desc-%d", rInt)), 53 resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("testing-ip-network-%d", rInt)), 54 resource.TestMatchResourceAttr(resName, "uri", regexp.MustCompile("testing-ip-network")), 55 ), 56 }, 57 { 58 Config: testAccOPCIPNetworkConfig_BasicUpdate(rInt), 59 Check: resource.ComposeTestCheckFunc( 60 opcResourceCheck(resName, testAccOPCCheckIPNetworkExists), 61 resource.TestCheckResourceAttr(resName, "ip_address_prefix", "10.0.12.0/24"), 62 resource.TestCheckResourceAttr(resName, "public_napt_enabled", "true"), 63 resource.TestCheckResourceAttr(resName, "description", fmt.Sprintf("testing-desc-%d", rInt)), 64 resource.TestCheckResourceAttr(resName, "name", fmt.Sprintf("testing-ip-network-%d", rInt)), 65 ), 66 }, 67 }, 68 }) 69 } 70 71 func testAccOPCIPNetworkConfig_Basic(rInt int) string { 72 return fmt.Sprintf(` 73 resource "opc_compute_ip_network" "test" { 74 name = "testing-ip-network-%d" 75 description = "testing-desc-%d" 76 ip_address_prefix = "10.0.12.0/24" 77 }`, rInt, rInt) 78 } 79 80 func testAccOPCIPNetworkConfig_BasicUpdate(rInt int) string { 81 return fmt.Sprintf(` 82 resource "opc_compute_ip_network" "test" { 83 name = "testing-ip-network-%d" 84 description = "testing-desc-%d" 85 ip_address_prefix = "10.0.12.0/24" 86 public_napt_enabled = true 87 }`, rInt, rInt) 88 } 89 90 func testAccOPCCheckIPNetworkExists(state *OPCResourceState) error { 91 name := state.Attributes["name"] 92 93 input := &compute.GetIPNetworkInput{ 94 Name: name, 95 } 96 97 if _, err := state.Client.IPNetworks().GetIPNetwork(input); err != nil { 98 return fmt.Errorf("Error retrieving state of IP Network '%s': %v", name, err) 99 } 100 101 return nil 102 } 103 104 func testAccOPCCheckIPNetworkDestroyed(state *OPCResourceState) error { 105 name := state.Attributes["name"] 106 107 input := &compute.GetIPNetworkInput{ 108 Name: name, 109 } 110 111 if info, _ := state.Client.IPNetworks().GetIPNetwork(input); info != nil { 112 return fmt.Errorf("IP Network '%s' still exists: %+v", name, info) 113 } 114 return nil 115 }