Skip to content

Commit

Permalink
added reserved ipv6 resource
Browse files Browse the repository at this point in the history
  • Loading branch information
imaskm committed Nov 26, 2024
1 parent d91733b commit 3970caa
Show file tree
Hide file tree
Showing 7 changed files with 1,011 additions and 0 deletions.
46 changes: 46 additions & 0 deletions digitalocean/reservedipv6/datasource_reserved_ipv6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package reservedipv6

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func DataSourceDigitalOceanReservedIPV6() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDigitalOceanReservedIPV6Read,
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Required: true,
Description: "reserved ipv6 address",
ValidateFunc: validation.NoZeroValues,
},
// computed attributes
"urn": {
Type: schema.TypeString,
Computed: true,
Description: "the uniform resource name for the reserved ipv6",
},
"region_slug": {
Type: schema.TypeString,
Computed: true,
Description: "the regionSlug that the reserved ipv6 is reserved to",
},
"droplet_id": {
Type: schema.TypeInt,
Computed: true,
Description: "the droplet id that the reserved ip has been assigned to.",
},
},
}
}

func dataSourceDigitalOceanReservedIPV6Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
ipAddress := d.Get("ip").(string)
d.SetId(ipAddress)

return resourceDigitalOceanReservedIPV6Read(ctx, d, meta)
}
110 changes: 110 additions & 0 deletions digitalocean/reservedipv6/datasource_reserved_ipv6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package reservedipv6_test

import (
"context"
"fmt"
"regexp"
"testing"

"github.com/digitalocean/godo"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/acceptance"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccDataSourceDigitalOceanReservedIPV6_Basic(t *testing.T) {
var reservedIPv6 godo.ReservedIPV6

expectedURNRegex, _ := regexp.Compile(`do:reservedipv6:/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i`)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckDataSourceDigitalOceanReservedIPConfig_Basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceDigitalOceanReservedIPV6Exists("data.digitalocean_reserved_ipv6.foobar", &reservedIPv6),
resource.TestCheckResourceAttrSet(
"data.digitalocean_reserved_ipv6.foobar", "ip"),
resource.TestCheckResourceAttr(
"data.digitalocean_reserved_ipv6.foobar", "region_slug", "nyc3"),
resource.TestMatchResourceAttr("data.digitalocean_reserved_ipv6.foobar", "urn", expectedURNRegex),
),
},
},
})
}

func TestAccDataSourceDigitalOceanReservedIPV6_FindsReservedIP(t *testing.T) {
var reservedIPv6 godo.ReservedIPV6

expectedURNRegex, _ := regexp.Compile(`do:reservedipv6:/^[0-9A-F]{8}-[0-9A-F]{4}-[4][0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i`)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccCheckDataSourceDigitalOceanReservedIPConfig_FindsFloatingIP,
Check: resource.ComposeTestCheckFunc(
testAccCheckDataSourceDigitalOceanReservedIPV6Exists("data.digitalocean_reserved_ipv6.foobar", &reservedIPv6),
resource.TestCheckResourceAttrSet(
"data.digitalocean_reserved_ipv6.foobar", "ip"),
resource.TestCheckResourceAttr(
"data.digitalocean_reserved_ipv6.foobar", "region_slug", "nyc3"),
resource.TestMatchResourceAttr("data.digitalocean_reserved_ipv6.foobar", "urn", expectedURNRegex),
),
},
},
})
}

func testAccCheckDataSourceDigitalOceanReservedIPV6Exists(n string, reservedIPv6 *godo.ReservedIPV6) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]

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

if rs.Primary.ID == "" {
return fmt.Errorf("No reserved IPv6 ID is set")
}

client := acceptance.TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

foundReservedIP, _, err := client.ReservedIPV6s.Get(context.Background(), rs.Primary.ID)

if err != nil {
return err
}

if foundReservedIP.IP != rs.Primary.ID {
return fmt.Errorf("reserved IPv6 not found")
}

*reservedIPv6 = *foundReservedIP

return nil
}
}

const testAccCheckDataSourceDigitalOceanReservedIPConfig_FindsFloatingIP = `
resource "digitalocean_reserved_ipv6" "foo" {
region_slug = "nyc3"
}
data "digitalocean_reserved_ipv6" "foobar" {
ip = digitalocean_reserved_ipv6.foo.ip
}`

const testAccCheckDataSourceDigitalOceanReservedIPConfig_Basic = `
resource "digitalocean_reserved_ipv6" "foo" {
region_slug = "nyc3"
}
data "digitalocean_reserved_ipv6" "foobar" {
ip = digitalocean_reserved_ipv6.foo.ip
}`
51 changes: 51 additions & 0 deletions digitalocean/reservedipv6/import_reserved_ipv6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package reservedipv6_test

import (
"testing"

"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/acceptance"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDigitalOceanReservedIPV6_importBasicRegion(t *testing.T) {
resourceName := "digitalocean_reserved_ipv6.foobar"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanReservedIPV6Destroy,
Steps: []resource.TestStep{
{
Config: testAccCheckDigitalOceanReservedIPV6Config_regionSlug,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccDigitalOceanReservedIPV6_importBasicDroplet(t *testing.T) {
resourceName := "digitalocean_reserved_ipv6.foobar"
name := acceptance.RandomTestName()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanReservedIPV6Destroy,
Steps: []resource.TestStep{
{
Config: testAccCheckDigitalOceanReservedIPV6Config_droplet(name),
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Loading

0 comments on commit 3970caa

Please sign in to comment.