Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reserved ipv6 resource #1277

Merged
merged 14 commits into from
Dec 11, 2024
2 changes: 1 addition & 1 deletion digitalocean/droplet/resource_droplet.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,7 @@ func waitForDropletDestroy(ctx context.Context, d *schema.ResourceData, meta int

stateConf := &retry.StateChangeConf{
Pending: []string{"active", "off"},
Target: []string{"archived"},
Target: []string{"archive"},
Refresh: dropletStateRefreshFunc(ctx, d, "status", meta),
Timeout: 60 * time.Second,
Delay: 10 * time.Second,
Expand Down
4 changes: 4 additions & 0 deletions digitalocean/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/region"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/registry"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/reservedip"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/reservedipv6"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/size"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/snapshot"
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/spaces"
Expand Down Expand Up @@ -127,6 +128,7 @@ func Provider() *schema.Provider {
"digitalocean_region": region.DataSourceDigitalOceanRegion(),
"digitalocean_regions": region.DataSourceDigitalOceanRegions(),
"digitalocean_reserved_ip": reservedip.DataSourceDigitalOceanReservedIP(),
"digitalocean_reserved_ipv6": reservedipv6.DataSourceDigitalOceanReservedIPV6(),
"digitalocean_sizes": size.DataSourceDigitalOceanSizes(),
"digitalocean_spaces_bucket": spaces.DataSourceDigitalOceanSpacesBucket(),
"digitalocean_spaces_buckets": spaces.DataSourceDigitalOceanSpacesBuckets(),
Expand Down Expand Up @@ -177,6 +179,8 @@ func Provider() *schema.Provider {
"digitalocean_record": domain.ResourceDigitalOceanRecord(),
"digitalocean_reserved_ip": reservedip.ResourceDigitalOceanReservedIP(),
"digitalocean_reserved_ip_assignment": reservedip.ResourceDigitalOceanReservedIPAssignment(),
"digitalocean_reserved_ipv6": reservedipv6.ResourceDigitalOceanReservedIPV6(),
"digitalocean_reserved_ipv6_assignment": reservedipv6.ResourceDigitalOceanReservedIPV6Assignment(),
"digitalocean_spaces_bucket": spaces.ResourceDigitalOceanBucket(),
"digitalocean_spaces_bucket_cors_configuration": spaces.ResourceDigitalOceanBucketCorsConfiguration(),
"digitalocean_spaces_bucket_object": spaces.ResourceDigitalOceanSpacesBucketObject(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ resource "digitalocean_reserved_ip_assignment" "foobar" {

var testAccCheckDigitalOceanReservedIPAssignmentConfig_createBeforeDestroyReassign = `
resource "digitalocean_droplet" "foobar" {
image = "ubuntu-18-04-x64"
image = "ubuntu-22-04-x64"
name = "tf-acc-test-01"
region = "nyc3"
size = "s-1vcpu-1gb"
Expand Down
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 ipv6 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:` + ipv6Regex)

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:` + ipv6Regex)

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
}`
29 changes: 29 additions & 0 deletions digitalocean/reservedipv6/import_reserved_ipv6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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,
},
},
})
}
imaskm marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading