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

feat: Add resource ovh_vrack_ipv6 #838

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func Provider() *schema.Provider {
"ovh_vrack_dedicated_server": resourceVrackDedicatedServer(),
"ovh_vrack_dedicated_server_interface": resourceVrackDedicatedServerInterface(),
"ovh_vrack_ip": resourceVrackIp(),
"ovh_vrack_ipv6": resourceVrackIpV6(),
"ovh_vrack_iploadbalancing": resourceVrackIpLoadbalancing(),
},

Expand Down
123 changes: 123 additions & 0 deletions ovh/resource_vrack_ipv6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package ovh

import (
"fmt"
"net/url"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)

func resourceVrackIpV6() *schema.Resource {
return &schema.Resource{
Create: resourceVrackIpv6Create,
Read: resourceVrackIpv6Read,
Delete: resourceVrackIpv6Delete,
Importer: &schema.ResourceImporter{
State: resourceVrackIpv6ImportState,
},

Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The internal name of your vrack",
},
"block": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "IPv6 CIDR notation (e.g., 2001:41d0::/128)",
ValidateFunc: func(i interface{}, _ string) ([]string, []error) {
if err := helpers.ValidateIpBlock(i.(string)); err != nil {
return nil, []error{err}
}
return nil, nil
},
},
},
}
}

func resourceVrackIpv6ImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.Split(givenId, ",")
if len(splitId) != 2 {
return nil, fmt.Errorf("import ID is not SERVICE_NAME,IPv6-block formatted")
}
serviceName := splitId[0]
block := splitId[1]

d.SetId(fmt.Sprintf("vrack_%s-block_%s", serviceName, block))
d.Set("service_name", serviceName)
d.Set("block", block)

return []*schema.ResourceData{d}, nil
}

func resourceVrackIpv6Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

serviceName := d.Get("service_name").(string)

opts := (&VrackIpCreateOpts{}).FromResource(d)
task := VrackTask{}

endpoint := fmt.Sprintf("/vrack/%s/ipv6", url.PathEscape(serviceName))
if err := config.OVHClient.Post(endpoint, opts, &task); err != nil {
return fmt.Errorf("error calling POST %s with opts %v:\n\t %q", endpoint, opts, err)
}

if err := waitForVrackTask(&task, config.OVHClient); err != nil {
return fmt.Errorf("error waiting for vrack (%s) to attach ipv6 %v: %s", serviceName, opts, err)
}

d.SetId(fmt.Sprintf("vrack_%s-block_%s", serviceName, opts.Block))

return resourceVrackIpv6Read(d, meta)
}

func resourceVrackIpv6Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

serviceName := d.Get("service_name").(string)
block := d.Get("block").(string)

endpoint := fmt.Sprintf("/vrack/%s/ipv6/%s",
url.PathEscape(serviceName),
url.PathEscape(block),
)

if err := config.OVHClient.Get(endpoint, nil); err != nil {
return fmt.Errorf("failed to get vrack-ipv6 link: %w", err)
}

return nil
}

func resourceVrackIpv6Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

serviceName := d.Get("service_name").(string)
block := d.Get("block").(string)
task := VrackTask{}

endpoint := fmt.Sprintf("/vrack/%s/ipv6/%s",
url.PathEscape(serviceName),
url.PathEscape(block),
)

if err := config.OVHClient.Delete(endpoint, &task); err != nil {
return fmt.Errorf("error calling DELETE %s with %s/%s:\n\t %q", endpoint, serviceName, block, err)
}

if err := waitForVrackTask(&task, config.OVHClient); err != nil {
return fmt.Errorf("error waiting for vrack (%s) to detach ip (%s): %s", serviceName, block, err)
}

d.SetId("")

return nil
}
88 changes: 88 additions & 0 deletions ovh/resource_vrack_ipv6_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package ovh

import (
"fmt"
"log"
"net/url"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/ovh/go-ovh/ovh"
)

func init() {
resource.AddTestSweepers("ovh_vrack_ipv6", &resource.Sweeper{
Name: "ovh_vrack_ipv6",
F: testSweepVrackIPv6,
})
}

func testSweepVrackIPv6(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}

vrackId := os.Getenv("OVH_VRACK_SERVICE_TEST")
if vrackId == "" {
log.Print("[DEBUG] OVH_VRACK_SERVICE_TEST is not set. No vrack_ipv6 to sweep")
return nil
}

ipBlock := os.Getenv("OVH_IP_V6_BLOCK_TEST")
if ipBlock == "" {
log.Print("[DEBUG] OVH_CLOUD_PROJECT_SERVICE_TEST is not set. No vrack_ipv6 to sweep")
return nil
}

endpoint := fmt.Sprintf("/vrack/%s/ipv6/%s",
url.PathEscape(vrackId),
url.PathEscape(ipBlock),
)

if err := client.Get(endpoint, nil); err != nil {
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
return nil
}
return err
}

task := VrackTask{}
if err := client.Delete(endpoint, &task); err != nil {
return fmt.Errorf("Error calling DELETE %s with %s/%s:\n\t %q", endpoint, vrackId, ipBlock, err)
}

if err := waitForVrackTask(&task, client); err != nil {
return fmt.Errorf("Error waiting for vrack (%s) to detach ipv6 (%s): %s", vrackId, ipBlock, err)
}

return nil
}

func TestAccVrackIPv6_basic(t *testing.T) {
serviceName := os.Getenv("OVH_VRACK_SERVICE_TEST")
ipBlock := os.Getenv("OVH_IP_V6_BLOCK_TEST")

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckVRack(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "ovh_vrack_ipv6" "vrack-ipv6" {
service_name = "%s"
block = "%s"
}
`, serviceName, ipBlock),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("ovh_vrack_ipv6.vrack-ipv6", "service_name", serviceName),
resource.TestCheckResourceAttr("ovh_vrack_ipv6.vrack-ipv6", "block", ipBlock),
),
},
},
})
}
37 changes: 37 additions & 0 deletions website/docs/r/vrack_ipv6.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
subcategory : "vRack"
---

# ovh_vrack_ipv6

Attach an IPv6 block to a VRack.


## Example Usage

```hcl
resource "ovh_vrack_ipv6" "vrack_block" {
service_name = "<vRack service name>"
block = "<ipv6 block>"
}
```

## Argument Reference

The following arguments are supported:

* `service_name` - (Required) The internal name of your vrack
* `block` - (Required) Your IPv6 block.

## Attributes Reference

No additional attribute is exported.


## Import

Attachment of an IPv6 block and a VRack can be imported using the `service_name` (vRack identifier) and the `block` (IPv6 block), separated by "," E.g.,

```bash
$ terraform import ovh_vrack_ipv6.myattach "<service_name>,<block>"
```