Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 25d30b7

Browse files
authoredMar 4, 2025
Merge pull request #875 from Elyfler/add-vrack-occ
feat: Add resource ovh_vrack_ovhcloudconnect
2 parents 4bb8507 + 0cf01a5 commit 25d30b7

7 files changed

+359
-1
lines changed
 

‎ovh/provider_new.go

+1
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ func (p *OvhProvider) Resources(_ context.Context) []func() resource.Resource {
252252
NewOkmsServiceKeyResource,
253253
NewOkmsServiceKeyJwkResource,
254254
NewVpsResource,
255+
NewVrackOvhcloudconnectResource,
255256
}
256257
}
257258

‎ovh/provider_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ func testAccPreCheckKubernetesVRack(t *testing.T) {
335335
checkEnvOrSkip(t, "OVH_VRACK_SERVICE_TEST")
336336
}
337337

338+
// Checks that the environment variables needed for the /vrack/{service}/ovhCloudConnect/{ovhCloudConnect} acceptance tests
339+
// are set.
340+
func testAccPreCheckOCCVRack(t *testing.T) {
341+
testAccPreCheckCredentials(t)
342+
checkEnvOrSkip(t, "OVH_VRACK_SERVICE_TEST")
343+
checkEnvOrSkip(t, "OVH_VRACK_OVH_CLOUD_CONNECT")
344+
}
345+
338346
// Checks that the environment variables needed for the /ipLoadbalacing acceptance tests
339347
// are set.
340348
func testAccPreCheckIpLoadbalancing(t *testing.T) {

‎ovh/resource_vrack_ipv6_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestAccVrackIPv6_basic(t *testing.T) {
6767

6868
resource.Test(t, resource.TestCase{
6969
PreCheck: func() {
70-
testAccPreCheckVRack(t)
70+
testAccPreCheckOCCVRack(t)
7171
},
7272
Providers: testAccProviders,
7373
Steps: []resource.TestStep{

‎ovh/resource_vrack_ovhcloudconnect.go

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package ovh
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/url"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/resource"
9+
)
10+
11+
var _ resource.ResourceWithConfigure = (*vrackOvhcloudconnectResource)(nil)
12+
13+
func NewVrackOvhcloudconnectResource() resource.Resource {
14+
return &vrackOvhcloudconnectResource{}
15+
}
16+
17+
type vrackOvhcloudconnectResource struct {
18+
config *Config
19+
}
20+
21+
func (r *vrackOvhcloudconnectResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
22+
resp.TypeName = req.ProviderTypeName + "_vrack_ovhcloudconnect"
23+
}
24+
25+
func (d *vrackOvhcloudconnectResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
26+
if req.ProviderData == nil {
27+
return
28+
}
29+
30+
config, ok := req.ProviderData.(*Config)
31+
if !ok {
32+
resp.Diagnostics.AddError(
33+
"Unexpected Resource Configure Type",
34+
fmt.Sprintf("Expected *Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
35+
)
36+
return
37+
}
38+
39+
d.config = config
40+
}
41+
42+
func (d *vrackOvhcloudconnectResource) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
43+
resp.Schema = VrackOvhcloudconnectResourceSchema(ctx)
44+
}
45+
46+
func (r *vrackOvhcloudconnectResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
47+
var data, responseData VrackOvhcloudconnectModel
48+
var task VrackTask
49+
50+
// Read Terraform plan data into the model
51+
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
52+
if resp.Diagnostics.HasError() {
53+
return
54+
}
55+
56+
endpoint := "/vrack/" + url.PathEscape(data.ServiceName.ValueString()) + "/ovhCloudConnect"
57+
if err := r.config.OVHClient.Post(endpoint, data.ToCreate(), &task); err != nil {
58+
resp.Diagnostics.AddError(
59+
fmt.Sprintf("Error calling Post %s", endpoint),
60+
err.Error(),
61+
)
62+
return
63+
}
64+
65+
if err := waitForVrackTask(&task, r.config.OVHClient); err != nil {
66+
resp.Diagnostics.AddError(
67+
fmt.Sprintf("error waiting for vrack (%s) to attach OCC %v: %s", task.ServiceName, data.OvhCloudConnect.String(), err),
68+
err.Error(),
69+
)
70+
return
71+
}
72+
73+
endpoint = "/vrack/" + url.PathEscape(data.ServiceName.ValueString()) + "/ovhCloudConnect/" + url.PathEscape(data.OvhCloudConnect.ValueString())
74+
75+
if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
76+
resp.Diagnostics.AddError(
77+
fmt.Sprintf("Error calling Get %s", endpoint),
78+
err.Error(),
79+
)
80+
return
81+
}
82+
83+
data.MergeWith(&responseData)
84+
85+
// Save updated data into Terraform state
86+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
87+
}
88+
89+
func (r *vrackOvhcloudconnectResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
90+
var data, responseData VrackOvhcloudconnectModel
91+
92+
// Read Terraform prior state data into the model
93+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
94+
if resp.Diagnostics.HasError() {
95+
return
96+
}
97+
98+
endpoint := "/vrack/" + url.PathEscape(data.ServiceName.ValueString()) + "/ovhCloudConnect/" + url.PathEscape(data.OvhCloudConnect.ValueString())
99+
100+
if err := r.config.OVHClient.Get(endpoint, &responseData); err != nil {
101+
resp.Diagnostics.AddError(
102+
fmt.Sprintf("Error calling Get %s", endpoint),
103+
err.Error(),
104+
)
105+
return
106+
}
107+
108+
data.MergeWith(&responseData)
109+
110+
// Save updated data into Terraform state
111+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
112+
}
113+
114+
func (r *vrackOvhcloudconnectResource) Update(ctx context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
115+
resp.Diagnostics.AddError("not implemented", "update func should never be called")
116+
}
117+
118+
func (r *vrackOvhcloudconnectResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
119+
var data VrackOvhcloudconnectModel
120+
var task VrackTask
121+
122+
// Read Terraform prior state data into the model
123+
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
124+
125+
if resp.Diagnostics.HasError() {
126+
return
127+
}
128+
129+
// Delete API call logic
130+
endpoint := "/vrack/" + url.PathEscape(data.ServiceName.ValueString()) + "/ovhCloudConnect/" + url.PathEscape(data.OvhCloudConnect.ValueString())
131+
if err := r.config.OVHClient.Delete(endpoint, &task); err != nil {
132+
resp.Diagnostics.AddError(
133+
fmt.Sprintf("Error calling Delete %s", endpoint),
134+
err.Error(),
135+
)
136+
}
137+
138+
if err := waitForVrackTask(&task, r.config.OVHClient); err != nil {
139+
resp.Diagnostics.AddError(
140+
fmt.Sprintf("error waiting for vrack (%s) to detach OCC %v: %s", task.ServiceName, data.OvhCloudConnect.String(), err),
141+
err.Error(),
142+
)
143+
return
144+
}
145+
146+
// Save updated data into Terraform state
147+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
148+
}

‎ovh/resource_vrack_ovhcloudconnect_gen.go

+85
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package ovh
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/url"
7+
"os"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
"github.com/ovh/go-ovh/ovh"
12+
)
13+
14+
func init() {
15+
resource.AddTestSweepers("ovh_vrack_ovhcloudconnect", &resource.Sweeper{
16+
Name: "ovh_vrack_ovhcloudconnect",
17+
F: testSweepVrackOCC,
18+
})
19+
}
20+
21+
func testSweepVrackOCC(region string) error {
22+
client, err := sharedClientForRegion(region)
23+
if err != nil {
24+
return fmt.Errorf("error getting client: %s", err)
25+
}
26+
serviceName := os.Getenv("OVH_VRACK_SERVICE_TEST")
27+
if serviceName == "" {
28+
log.Print("[DEBUG] OVH_VRACK_SERVICE_TEST is not set. No vrack_ovhcloudconnect to sweep")
29+
}
30+
31+
occ := os.Getenv("OVH_OVH_CLOUD_CONNECT_TEST")
32+
if occ == "" {
33+
log.Print("[DEBUG] OVH_OVH_CLOUD_CONNECT_TEST is not set. No vrack_ovhcloudconnect to sweep")
34+
}
35+
36+
endpoint := fmt.Sprintf("/vrack/%s/ovhCloudConnect/%s",
37+
url.PathEscape(serviceName),
38+
url.PathEscape(occ),
39+
)
40+
41+
if err := client.Get(endpoint, nil); err != nil {
42+
if errOvh, ok := err.(*ovh.APIError); ok && errOvh.Code == 404 {
43+
return nil
44+
}
45+
return err
46+
}
47+
48+
task := VrackTask{}
49+
if err := client.Delete(endpoint, nil); err != nil {
50+
return fmt.Errorf("Error calling DELETE %s with %s/%s:\n\t %q", endpoint, serviceName, occ, err)
51+
}
52+
if err := waitForVrackTask(&task, client); err != nil {
53+
return fmt.Errorf("Error waiting for vrack (%s) to detach occ (%s): %s", serviceName, occ, err)
54+
}
55+
56+
return nil
57+
}
58+
59+
func TestAccVrackOCC_basic(t *testing.T) {
60+
serviceName := os.Getenv("OVH_VRACK_SERVICE_TEST")
61+
occ := os.Getenv("OVH_OVH_CLOUD_CONNECT_TEST")
62+
63+
resource.Test(t, resource.TestCase{
64+
PreCheck: func() { testAccPreCheckCredentials(t) },
65+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
66+
Steps: []resource.TestStep{
67+
{
68+
Config: fmt.Sprintf(`
69+
resource "ovh_vrack_ovhcloudconnect" "vrack-occ" {
70+
service_name = "%s"
71+
ovh_cloud_connect = "%s"
72+
}
73+
`, serviceName, occ),
74+
Check: resource.ComposeTestCheckFunc(
75+
resource.TestCheckResourceAttr("ovh_vrack_ovhcloudconnect.vrack-occ", "service_name", serviceName),
76+
resource.TestCheckResourceAttr("ovh_vrack_ovhcloudconnect.vrack-occ", "ovh_cloud_connect", occ),
77+
),
78+
},
79+
},
80+
})
81+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
subcategory : "vRack"
3+
---
4+
5+
# ovh_vrack_ovhcloudconnect
6+
7+
Attach an OVH Cloud Connect to the vrack.
8+
9+
## Example Usage
10+
11+
```hcl
12+
resource "ovh_vrack_ovhcloudconnect" "vrack_ovhcloudconnect" {
13+
service_name = "<vRack service name>"
14+
ovh_cloud_connect = "<OVH Cloud Connect service name>"
15+
}
16+
```
17+
18+
## Argument Reference
19+
20+
The following arguments are supported:
21+
22+
* `service_name` - (Required) The internal name of your vrack
23+
* `ovh_cloud_connect` - (Required) Your OVH Cloud Connect service name.
24+
25+
## Attributes Reference
26+
27+
No additional attribute is exported.
28+
29+
## Import
30+
31+
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.,
32+
33+
```bash
34+
$ terraform import ovh_vrack_ovhcloudconnect.myattach "<service_name>/<OVH Cloud Connect service name>"
35+
```

0 commit comments

Comments
 (0)
Please sign in to comment.