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_vrackservices #839

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 @@ -282,6 +282,7 @@ func Provider() *schema.Provider {
"ovh_vrack_dedicated_server_interface": resourceVrackDedicatedServerInterface(),
"ovh_vrack_ip": resourceVrackIp(),
"ovh_vrack_iploadbalancing": resourceVrackIpLoadbalancing(),
"ovh_vrack_vrackservices": resourceVrackVrackServices(),
},

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

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

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

func resourceVrackVrackServices() *schema.Resource {
return &schema.Resource{
Create: resourceVrackVrackServicesCreate,
Read: resourceVrackVrackServicesRead,
Delete: resourceVrackVrackServicesDelete,
Importer: &schema.ResourceImporter{
State: resourceVrackVrackServicesImportState,
},

Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The internal name of your vrack",
},
"vrack_services": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "vrackServices service name",
},
},
}
}

func resourceVrackVrackServicesImportState(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 serviceName/vrackServicesName formatted")
}
serviceName := splitId[0]
vrackServices := splitId[1]

d.SetId(fmt.Sprintf("vrack_%s-vrackServices_%s", serviceName, vrackServices))
d.Set("service_name", serviceName)
d.Set("vrack_services", vrackServices)

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

func resourceVrackVrackServicesCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
vrackServices := d.Get("vrack_services").(string)

opts := &VrackVrackServicesCreateOpts{
VrackServices: vrackServices,
}
task := VrackTask{}

endpoint := fmt.Sprintf("/vrack/%s/vrackServices", 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 vrackServices %v: %s", serviceName, opts, err)
}

d.SetId(fmt.Sprintf("vrack_%s-vrackServices_%s", serviceName, vrackServices))

return resourceVrackVrackServicesRead(d, meta)
}

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

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

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

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

return nil
}

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

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

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

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

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

d.SetId("")

return nil
}
88 changes: 88 additions & 0 deletions ovh/resource_vrack_vrackservices_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_vrackservices", &resource.Sweeper{
Name: "ovh_vrack_vrackservices",
F: testSweepVrackVrackServices,
})
}

func testSweepVrackVrackServices(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_vrackservices to sweep")
return nil
}

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

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

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, vrackServices, err)
}

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

return nil
}

func TestAccVrackVrackServices_basic(t *testing.T) {
serviceName := os.Getenv("OVH_VRACK_SERVICE_TEST")
vrackServices := os.Getenv("OVH_VRACKSERVICES_SERVICE_TEST")

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckVRack(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "ovh_vrack_vrackservices" "vrack-vrackServices" {
service_name = "%s"
vrack_services = "%s"
}
`, serviceName, vrackServices),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("ovh_vrack_vrackservices.vrack-vrackServices", "service_name", serviceName),
resource.TestCheckResourceAttr("ovh_vrack_vrackservices.vrack-vrackServices", "vrack_services", vrackServices),
),
},
},
})
}
4 changes: 4 additions & 0 deletions ovh/types_vrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ type VrackDedicatedServerCreateOpts struct {
DedicatedServer string `json:"dedicatedServer"`
}

type VrackVrackServicesCreateOpts struct {
VrackServices string `json:"vrackServices"`
}

func (opts *VrackDedicatedServerCreateOpts) FromResource(d *schema.ResourceData) *VrackDedicatedServerCreateOpts {
opts.DedicatedServer = d.Get("server_id").(string)
return opts
Expand Down
35 changes: 35 additions & 0 deletions website/docs/r/vrack_vrackservices.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
subcategory : "vRack"
---

# ovh_vrack_vrackservices

Attach a vrackServices to the vrack.

## Example Usage

```hcl
resource "ovh_vrack_vrackservices" "vrack_vrackservices" {
service_name = "<vRack service name>"
vrack_services = "<vrackServices service name>"
}
```

## Argument Reference

The following arguments are supported:

* `service_name` - (Required) The internal name of your vrack
* `vrack_services` - (Required) Your vrackServices service name.

## Attributes Reference

No additional attribute is exported.

## Import

Attachment of a vrackServices and a vRack can be imported using the `service_name` (vRack identifier) and the `vrack_services` (vrackServices service name), separated by "/" E.g.,

```bash
$ terraform import ovh_vrack_vrackservices.myattach "<service_name>/<vrackServices service name>"
```