diff --git a/builtin/providers/opc/import_ip_address_association_test.go b/builtin/providers/opc/import_ip_address_association_test.go new file mode 100644 index 000000000..a68f6daf8 --- /dev/null +++ b/builtin/providers/opc/import_ip_address_association_test.go @@ -0,0 +1,29 @@ +package opc + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccOPCIPAddressAssociation_importBasic(t *testing.T) { + resourceName := "opc_compute_ip_address_association.test" + + ri := acctest.RandInt() + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckIPAddressAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccIPAddressAssociationBasic(ri), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/builtin/providers/opc/provider.go b/builtin/providers/opc/provider.go index ddde50d5a..8f52bd874 100644 --- a/builtin/providers/opc/provider.go +++ b/builtin/providers/opc/provider.go @@ -73,6 +73,7 @@ func Provider() terraform.ResourceProvider { "opc_compute_vnic_set": resourceOPCVNICSet(), "opc_compute_security_protocol": resourceOPCSecurityProtocol(), "opc_compute_ip_address_prefix_set": resourceOPCIPAddressPrefixSet(), + "opc_compute_ip_address_association": resourceOPCIPAddressAssociation(), }, ConfigureFunc: providerConfigure, diff --git a/builtin/providers/opc/resource_ip_address_association.go b/builtin/providers/opc/resource_ip_address_association.go new file mode 100644 index 000000000..de7dec4db --- /dev/null +++ b/builtin/providers/opc/resource_ip_address_association.go @@ -0,0 +1,156 @@ +package opc + +import ( + "fmt" + + "github.com/hashicorp/go-oracle-terraform/compute" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceOPCIPAddressAssociation() *schema.Resource { + return &schema.Resource{ + Create: resourceOPCIPAddressAssociationCreate, + Read: resourceOPCIPAddressAssociationRead, + Update: resourceOPCIPAddressAssociationUpdate, + Delete: resourceOPCIPAddressAssociationDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "ip_address_reservation": { + Type: schema.TypeString, + Optional: true, + }, + "vnic": { + Type: schema.TypeString, + Optional: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + }, + "tags": tagsOptionalSchema(), + "uri": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceOPCIPAddressAssociationCreate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*compute.Client).IPAddressAssociations() + + input := compute.CreateIPAddressAssociationInput{ + Name: d.Get("name").(string), + } + + if ipAddressReservation, ok := d.GetOk("ip_address_reservation"); ok { + input.IPAddressReservation = ipAddressReservation.(string) + } + + if vnic, ok := d.GetOk("vnic"); ok { + input.Vnic = vnic.(string) + } + + tags := getStringList(d, "tags") + if len(tags) != 0 { + input.Tags = tags + } + + if description, ok := d.GetOk("description"); ok { + input.Description = description.(string) + } + + info, err := client.CreateIPAddressAssociation(&input) + if err != nil { + return fmt.Errorf("Error creating IP Address Association: %s", err) + } + + d.SetId(info.Name) + return resourceOPCIPAddressAssociationRead(d, meta) +} + +func resourceOPCIPAddressAssociationRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*compute.Client).IPAddressAssociations() + name := d.Id() + + getInput := compute.GetIPAddressAssociationInput{ + name, + } + result, err := client.GetIPAddressAssociation(&getInput) + if err != nil { + // IP Address Association does not exist + if compute.WasNotFoundError(err) { + d.SetId("") + return nil + } + return fmt.Errorf("Error reading IP Address Association %s: %s", name, err) + } + if result == nil { + d.SetId("") + return fmt.Errorf("Error reading IP Address Association %s: %s", name, err) + } + + d.Set("name", result.Name) + d.Set("ip_address_reservation", result.IPAddressReservation) + d.Set("vnic", result.Vnic) + d.Set("description", result.Description) + d.Set("uri", result.Uri) + if err := setStringList(d, "tags", result.Tags); err != nil { + return err + } + return nil +} + +func resourceOPCIPAddressAssociationUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*compute.Client).IPAddressAssociations() + + input := compute.UpdateIPAddressAssociationInput{ + Name: d.Get("name").(string), + } + + if ipAddressReservation, ok := d.GetOk("ip_address_reservation"); ok { + input.IPAddressReservation = ipAddressReservation.(string) + } + + if vnic, ok := d.GetOk("vnic"); ok { + input.Vnic = vnic.(string) + } + + tags := getStringList(d, "tags") + if len(tags) != 0 { + input.Tags = tags + } + + if description, ok := d.GetOk("description"); ok { + input.Description = description.(string) + } + + info, err := client.UpdateIPAddressAssociation(&input) + if err != nil { + return fmt.Errorf("Error updating IP Address Association: %s", err) + } + + d.SetId(info.Name) + return resourceOPCIPAddressAssociationRead(d, meta) +} + +func resourceOPCIPAddressAssociationDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*compute.Client).IPAddressAssociations() + name := d.Id() + + input := compute.DeleteIPAddressAssociationInput{ + Name: name, + } + if err := client.DeleteIPAddressAssociation(&input); err != nil { + return fmt.Errorf("Error deleting IP Address Association: %s", err) + } + return nil +} diff --git a/builtin/providers/opc/resource_ip_address_association_test.go b/builtin/providers/opc/resource_ip_address_association_test.go new file mode 100644 index 000000000..a08abfbc1 --- /dev/null +++ b/builtin/providers/opc/resource_ip_address_association_test.go @@ -0,0 +1,155 @@ +package opc + +import ( + "fmt" + "testing" + + "github.com/hashicorp/go-oracle-terraform/compute" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccOPCIPAddressAssociation_Basic(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "opc_compute_ip_address_association.test" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckIPAddressAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccIPAddressAssociationBasic(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckIPAddressAssociationExists, + resource.TestCheckResourceAttr( + resourceName, "tags.#", "2"), + ), + }, + { + Config: testAccIPAddressAssociationBasic_Update(rInt), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + resourceName, "tags.#", "1"), + ), + }, + }, + }) +} + +func TestAccOPCIPAddressAssociation_Full(t *testing.T) { + rInt := acctest.RandInt() + resourceName := "opc_compute_ip_address_association.test" + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckIPAddressAssociationDestroy, + Steps: []resource.TestStep{ + { + Config: testAccIPAddressAssociationFull(rInt), + Check: resource.ComposeTestCheckFunc( + testAccCheckIPAddressAssociationExists, + resource.TestCheckResourceAttr( + resourceName, "vnic", fmt.Sprintf("test-vnic-data-%d", rInt)), + resource.TestCheckResourceAttr( + resourceName, "ip_address_reservation", fmt.Sprintf("testing-ip-address-association-%d", rInt)), + ), + }, + }, + }) +} + +func testAccCheckIPAddressAssociationExists(s *terraform.State) error { + client := testAccProvider.Meta().(*compute.Client).IPAddressAssociations() + + for _, rs := range s.RootModule().Resources { + if rs.Type != "opc_compute_ip_address_association" { + continue + } + + input := compute.GetIPAddressAssociationInput{ + Name: rs.Primary.Attributes["name"], + } + if _, err := client.GetIPAddressAssociation(&input); err != nil { + return fmt.Errorf("Error retrieving state of IP Address Association %s: %s", input.Name, err) + } + } + + return nil +} + +func testAccCheckIPAddressAssociationDestroy(s *terraform.State) error { + client := testAccProvider.Meta().(*compute.Client).IPAddressAssociations() + + for _, rs := range s.RootModule().Resources { + if rs.Type != "opc_compute_ip_address_association" { + continue + } + + input := compute.GetIPAddressAssociationInput{ + Name: rs.Primary.Attributes["name"], + } + if info, err := client.GetIPAddressAssociation(&input); err == nil { + return fmt.Errorf("IP Address Association %s still exists: %#v", input.Name, info) + } + } + + return nil +} + +func testAccIPAddressAssociationBasic(rInt int) string { + return fmt.Sprintf(` +resource "opc_compute_ip_address_association" "test" { + name = "testing-acc-%d" + description = "acctesting ip address association test %d" + tags = ["tag1", "tag2"] +}`, rInt, rInt) +} + +func testAccIPAddressAssociationBasic_Update(rInt int) string { + return fmt.Sprintf(` +resource "opc_compute_ip_address_association" "test" { + name = "testing-acc-%d" + description = "acctesting ip address association test updated %d" + tags = ["tag1"] +}`, rInt, rInt) +} + +func testAccIPAddressAssociationFull(rInt int) string { + return fmt.Sprintf(` +resource "opc_compute_ip_network" "foo" { + name = "testing-vnic-data-%d" + description = "testing-ip-address-association" + ip_address_prefix = "10.1.13.0/24" +} +resource "opc_compute_instance" "test" { + name = "test-%d" + label = "test" + shape = "oc3" + image_list = "/oracle/public/oel_6.7_apaas_16.4.5_1610211300" + networking_info { + index = 0 + ip_network = "${opc_compute_ip_network.foo.id}" + vnic = "test-vnic-data-%d" + shared_network = false + mac_address = "02:5a:cd:ec:2e:4c" + } +} +data "opc_compute_network_interface" "eth0" { + instance_name = "${opc_compute_instance.test.name}" + instance_id = "${opc_compute_instance.test.id}" + interface = "eth0" +} +resource "opc_compute_ip_address_reservation" "test" { + name = "testing-ip-address-association-%d" + description = "testing-desc-%d" + ip_address_pool = "public-ippool" +} +resource "opc_compute_ip_address_association" "test" { + name = "testing-acc-%d" + ip_address_reservation = "${opc_compute_ip_address_reservation.test.name}" + vnic = "${data.opc_compute_network_interface.eth0.vnic}" + description = "acctesting ip address association test %d" + tags = ["tag1", "tag2"] +}`, rInt, rInt, rInt, rInt, rInt, rInt, rInt) +} diff --git a/website/source/docs/providers/opc/r/opc_compute_ip_address_association.html.markdown b/website/source/docs/providers/opc/r/opc_compute_ip_address_association.html.markdown new file mode 100644 index 000000000..b531cf559 --- /dev/null +++ b/website/source/docs/providers/opc/r/opc_compute_ip_address_association.html.markdown @@ -0,0 +1,48 @@ +--- +layout: "opc" +page_title: "Oracle: opc_compute_ip_address_association" +sidebar_current: "docs-opc-resource-ip-address-association" +description: |- + Creates and manages an IP address association in an OPC identity domain. +--- + +# opc\_compute\_ip\_address\_association + +The ``opc_compute_ip_address_association`` resource creates and manages an IP address association between an IP address reservation and a virtual NIC in an OPC identity domain. + +## Example Usage + +``` +resource "opc_compute_ip_address_association" "default" { + name = "PrefixSet1" + ip_address_reservation = "${opc_compute_ip_address_reservation.default.name}" + vnic = "${data.opc_compute_vnic.default.name}" + tags = ["tags1", "tags2"] +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the ip address association. + +* `ip_address_reservation` - (Optional) The name of the NAT IP address reservation. + +* `vnic` - (Optional) The name of the virtual NIC associated with this NAT IP reservation. + +* `description` - (Optional) A description of the ip address association. + +* `tags` - (Optional) List of tags that may be applied to the ip address association. + +In addition to the above, the following variables are exported: + +* `uri` - (Computed) The Uniform Resource Identifier of the ip address association. + +## Import + +IP Address Associations can be imported using the `resource name`, e.g. + +``` +terraform import opc_compute_ip_address_association.default example +```