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_ovhcloudconnect #875

Merged
merged 1 commit into from
Mar 4, 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_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
NewOkmsServiceKeyResource,
NewOkmsServiceKeyJwkResource,
NewVpsResource,
NewVrackOvhcloudconnectResource,
}
}

Expand Down
8 changes: 8 additions & 0 deletions ovh/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ func testAccPreCheckKubernetesVRack(t *testing.T) {
checkEnvOrSkip(t, "OVH_VRACK_SERVICE_TEST")
}

// Checks that the environment variables needed for the /vrack/{service}/ovhCloudConnect/{ovhCloudConnect} acceptance tests
// are set.
func testAccPreCheckOCCVRack(t *testing.T) {
testAccPreCheckCredentials(t)
checkEnvOrSkip(t, "OVH_VRACK_SERVICE_TEST")
checkEnvOrSkip(t, "OVH_VRACK_OVH_CLOUD_CONNECT")
}

// Checks that the environment variables needed for the /ipLoadbalacing acceptance tests
// are set.
func testAccPreCheckIpLoadbalancing(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion ovh/resource_vrack_ipv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestAccVrackIPv6_basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckVRack(t)
testAccPreCheckOCCVRack(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
Expand Down
148 changes: 148 additions & 0 deletions ovh/resource_vrack_ovhcloudconnect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package ovh

import (
"context"
"fmt"
"net/url"

"github.com/hashicorp/terraform-plugin-framework/resource"
)

var _ resource.ResourceWithConfigure = (*vrackOvhcloudconnectResource)(nil)

func NewVrackOvhcloudconnectResource() resource.Resource {
return &vrackOvhcloudconnectResource{}
}

type vrackOvhcloudconnectResource struct {
config *Config
}

func (r *vrackOvhcloudconnectResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_vrack_ovhcloudconnect"
}

func (d *vrackOvhcloudconnectResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

config, ok := req.ProviderData.(*Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

d.config = config
}

func (d *vrackOvhcloudconnectResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = VrackOvhcloudconnectResourceSchema(ctx)
}

func (r *vrackOvhcloudconnectResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data, responseData VrackOvhcloudconnectModel
var task VrackTask

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

endpoint := "/vrack/" + url.PathEscape(data.ServiceName.ValueString()) + "/ovhCloudConnect"
if err := r.config.OVHClient.Post(endpoint, data.ToCreate(), &task); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Post %s", endpoint),
err.Error(),
)
return
}

if err := waitForVrackTask(&task, r.config.OVHClient); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("error waiting for vrack (%s) to attach OCC %v: %s", task.ServiceName, data.OvhCloudConnect.String(), err),
err.Error(),
)
return
}

endpoint = "/vrack/" + url.PathEscape(data.ServiceName.ValueString()) + "/ovhCloudConnect/" + url.PathEscape(data.OvhCloudConnect.ValueString())

if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Get %s", endpoint),
err.Error(),
)
return
}

data.MergeWith(&responseData)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *vrackOvhcloudconnectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data, responseData VrackOvhcloudconnectModel

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

endpoint := "/vrack/" + url.PathEscape(data.ServiceName.ValueString()) + "/ovhCloudConnect/" + url.PathEscape(data.OvhCloudConnect.ValueString())

if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Get %s", endpoint),
err.Error(),
)
return
}

data.MergeWith(&responseData)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *vrackOvhcloudconnectResource) Update(ctx context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError("not implemented", "update func should never be called")
}

func (r *vrackOvhcloudconnectResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data VrackOvhcloudconnectModel
var task VrackTask

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if resp.Diagnostics.HasError() {
return
}

// Delete API call logic
endpoint := "/vrack/" + url.PathEscape(data.ServiceName.ValueString()) + "/ovhCloudConnect/" + url.PathEscape(data.OvhCloudConnect.ValueString())
if err := r.config.OVHClient.Delete(endpoint, &task); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Error calling Delete %s", endpoint),
err.Error(),
)
}

if err := waitForVrackTask(&task, r.config.OVHClient); err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("error waiting for vrack (%s) to detach OCC %v: %s", task.ServiceName, data.OvhCloudConnect.String(), err),
err.Error(),
)
return
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
85 changes: 85 additions & 0 deletions ovh/resource_vrack_ovhcloudconnect_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions ovh/resource_vrack_ovhcloudconnect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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_ovhcloudconnect", &resource.Sweeper{
Name: "ovh_vrack_ovhcloudconnect",
F: testSweepVrackOCC,
})
}

func testSweepVrackOCC(region string) error {
client, err := sharedClientForRegion(region)
if err != nil {
return fmt.Errorf("error getting client: %s", err)
}
serviceName := os.Getenv("OVH_VRACK_SERVICE_TEST")
if serviceName == "" {
log.Print("[DEBUG] OVH_VRACK_SERVICE_TEST is not set. No vrack_ovhcloudconnect to sweep")
}

occ := os.Getenv("OVH_OVH_CLOUD_CONNECT_TEST")
if occ == "" {
log.Print("[DEBUG] OVH_OVH_CLOUD_CONNECT_TEST is not set. No vrack_ovhcloudconnect to sweep")
}

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

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, nil); err != nil {
return fmt.Errorf("Error calling DELETE %s with %s/%s:\n\t %q", endpoint, serviceName, occ, err)
}
if err := waitForVrackTask(&task, client); err != nil {
return fmt.Errorf("Error waiting for vrack (%s) to detach occ (%s): %s", serviceName, occ, err)
}

return nil
}

func TestAccVrackOCC_basic(t *testing.T) {
serviceName := os.Getenv("OVH_VRACK_SERVICE_TEST")
occ := os.Getenv("OVH_OVH_CLOUD_CONNECT_TEST")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheckCredentials(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "ovh_vrack_ovhcloudconnect" "vrack-occ" {
service_name = "%s"
ovh_cloud_connect = "%s"
}
`, serviceName, occ),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("ovh_vrack_ovhcloudconnect.vrack-occ", "service_name", serviceName),
resource.TestCheckResourceAttr("ovh_vrack_ovhcloudconnect.vrack-occ", "ovh_cloud_connect", occ),
),
},
},
})
}
35 changes: 35 additions & 0 deletions website/docs/r/vrack_ovhcloudconnect.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
subcategory : "vRack"
---

# ovh_vrack_ovhcloudconnect

Attach an OVH Cloud Connect to the vrack.

## Example Usage

```hcl
resource "ovh_vrack_ovhcloudconnect" "vrack_ovhcloudconnect" {
service_name = "<vRack service name>"
ovh_cloud_connect = "<OVH Cloud Connect service name>"
}
```

## Argument Reference

The following arguments are supported:

* `service_name` - (Required) The internal name of your vrack
* `ovh_cloud_connect` - (Required) Your OVH Cloud Connect service name.

## Attributes Reference

No additional attribute is exported.

## Import

Attachment of an OVH Cloud Connect and a vRack can be imported using the `service_name` (vRack identifier) and the `ovh_cloud_connect` (OVH Cloud Connect service name), separated by "/" E.g.,

```bash
$ terraform import ovh_vrack_ovhcloudconnect.myattach "<service_name>/<OVH Cloud Connect service name>"
```