Skip to content

Commit

Permalink
feat/r/dedicated_server_install_task: new resource
Browse files Browse the repository at this point in the history
  • Loading branch information
yann degat committed Jan 6, 2020
1 parent 5d0601e commit 770c364
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ovh/dedicated_server_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func waitForDedicatedServerTask(serviceName string, task *DedicatedServerTask, c
Pending: []string{"init", "todo", "doing"},
Target: []string{"done"},
Refresh: refreshFunc,
Timeout: 10 * time.Minute,
Timeout: 45 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
Expand Down
1 change: 1 addition & 0 deletions ovh/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func Provider() terraform.ResourceProvider {
"ovh_cloud_network_private_subnet": resourcePublicCloudPrivateNetworkSubnet(),
"ovh_cloud_user": resourcePublicCloudUser(),
"ovh_dedicated_server_update": resourceDedicatedServerUpdate(),
"ovh_dedicated_server_install_task": resourceDedicatedServerInstallTask(),
"ovh_dedicated_server_reboot_task": resourceDedicatedServerRebootTask(),
"ovh_domain_zone_record": resourceOvhDomainZoneRecord(),
"ovh_domain_zone_redirection": resourceOvhDomainZoneRedirection(),
Expand Down
136 changes: 127 additions & 9 deletions ovh/resource_ovh_dedicated_server_install_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,116 @@ import (
func resourceDedicatedServerInstallTask() *schema.Resource {
return &schema.Resource{
Create: resourceDedicatedServerInstallTaskCreate,
Update: resourceDedicatedServerInstallTaskUpdate,
Read: resourceDedicatedServerInstallTaskRead,
Delete: resourceDedicatedServerInstallTaskDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(45 * time.Minute),
},

Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The internal name of your dedicated server.",
},
"keepers": {
Type: schema.TypeList,
"partition_scheme_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Partition scheme name.",
},
"template_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Change this value to recreate an install task.",
Elem: &schema.Schema{
Type: schema.TypeString,
Description: "Template name",
},
"bootid_on_destroy": {
Type: schema.TypeInt,
Optional: true,
Description: "If set, reboot the server on the specified boot id during destroy phase",
},
"details": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"change_log": {
Type: schema.TypeString,
Optional: true,
Description: "Template change log details",
},
"custom_hostname": {
Type: schema.TypeString,
Optional: true,
Description: "Set up the server using the provided hostname instead of the default hostname",
},
"disk_group_id": {
Type: schema.TypeInt,
Optional: true,
Description: "",
},
"install_rtm": {
Type: schema.TypeBool,
Optional: true,
Description: "",
},
"install_sql_server": {
Type: schema.TypeBool,
Optional: true,
Description: "",
},
"language": {
Type: schema.TypeString,
Optional: true,
Description: "language",
},
"no_raid": {
Type: schema.TypeBool,
Optional: true,
Description: "",
},
"post_installation_script_link": {
Type: schema.TypeString,
Optional: true,
Description: "Indicate the URL where your postinstall customisation script is located",
},
"post_installation_script_return": {
Type: schema.TypeString,
Optional: true,
Description: "indicate the string returned by your postinstall customisation script on successful execution. Advice: your script should return a unique validation string in case of succes. A good example is 'loh1Xee7eo OK OK OK UGh8Ang1Gu'",
},
"reset_hw_raid": {
Type: schema.TypeBool,
Optional: true,
Description: "",
},
"soft_raid_devices": {
Type: schema.TypeInt,
Optional: true,
Description: "",
},
"ssh_key_name": {
Type: schema.TypeString,
Optional: true,
Description: "Name of the ssh key that should be installed. Password login will be disabled",
},
"use_spla": {
Type: schema.TypeBool,
Optional: true,
Description: "",
},
"use_distrib_kernel": {
Type: schema.TypeBool,
Optional: true,
Description: "Use the distribution's native kernel instead of the recommended OVH Kernel",
},
},
},
},

Expand Down Expand Up @@ -75,13 +168,13 @@ func resourceDedicatedServerInstallTaskCreate(d *schema.ResourceData, meta inter
serviceName := d.Get("service_name").(string)

endpoint := fmt.Sprintf(
"/dedicated/server/%s/reboot",
"/dedicated/server/%s/install/start",
url.PathEscape(serviceName),
)

opts := (&DedicatedServerInstallTaskCreateOpts{}).FromResource(d)
task := &DedicatedServerTask{}

if err := config.OVHClient.Post(endpoint, nil, task); err != nil {
if err := config.OVHClient.Post(endpoint, opts, task); err != nil {
return fmt.Errorf("Error calling POST %s:\n\t %q", endpoint, err)
}

Expand All @@ -101,7 +194,7 @@ func resourceDedicatedServerInstallTaskRead(d *schema.ResourceData, meta interfa
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return fmt.Errorf(
"Could not parse reboot task id %s,%s:\n\t %q",
"Could not parse install task id %s,%s:\n\t %q",
serviceName,
d.Id(),
err,
Expand Down Expand Up @@ -131,7 +224,32 @@ func resourceDedicatedServerInstallTaskRead(d *schema.ResourceData, meta interfa
return nil
}

func resourceDedicatedServerInstallTaskUpdate(d *schema.ResourceData, meta interface{}) error {
// nothing to do on update
return resourceDedicatedServerInstallTaskRead(d, meta)
}

func resourceDedicatedServerInstallTaskDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
bootId := getNilIntPointerFromData(d, "bootid_on_destroy")

if bootId != nil {
serviceName := d.Get("service_name").(string)
endpoint := fmt.Sprintf(
"/dedicated/server/%s/reboot",
url.PathEscape(serviceName),
)

task := &DedicatedServerTask{}
if err := config.OVHClient.Post(endpoint, nil, task); err != nil {
return fmt.Errorf("Error calling POST %s:\n\t %q", endpoint, err)
}

if err := waitForDedicatedServerTask(serviceName, task, config.OVHClient); err != nil {
return err
}
}

// we cant delete the task through the API, just forget about its Id
d.SetId("")
return nil
Expand Down
170 changes: 170 additions & 0 deletions ovh/resource_ovh_dedicated_server_install_task_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package ovh

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccDedicatedServerInstall_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckCredentials(t)
testAccPreCheckDedicatedServer(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDedicatedServerInstallConfig("basic"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ovh_dedicated_server_update.server", "state", "ok"),
resource.TestCheckResourceAttr(
"ovh_dedicated_server_update.server", "monitoring", "true"),
resource.TestCheckResourceAttr(
"ovh_dedicated_server_update.server", "state", "ok"),
resource.TestCheckResourceAttr(
"ovh_dedicated_server_install_task.server_install", "function", "reinstallServer"),
resource.TestCheckResourceAttr(
"ovh_dedicated_server_install_task.server_install", "status", "done"),
),
},
},
})
}

func TestAccDedicatedServerInstall_rebootondestroy(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckCredentials(t)
testAccPreCheckDedicatedServer(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDedicatedServerInstallConfig("rebootondestroy"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"ovh_dedicated_server_update.server", "state", "ok"),
resource.TestCheckResourceAttr(
"ovh_dedicated_server_update.server", "monitoring", "true"),
resource.TestCheckResourceAttr(
"ovh_dedicated_server_update.server", "state", "ok"),
resource.TestCheckResourceAttr(
"ovh_dedicated_server_install_task.server_install", "function", "reinstallServer"),
resource.TestCheckResourceAttr(
"ovh_dedicated_server_install_task.server_install", "status", "done"),
),
},
},
})
}

func testAccDedicatedServerInstallConfig(config string) string {
dedicated_server := os.Getenv("OVH_DEDICATED_SERVER")
testName := acctest.RandomWithPrefix(test_prefix)
sshKey := os.Getenv("OVH_SSH_KEY")
if sshKey == "" {
sshKey = "ssh-ed25519 AAAAC3NzaC1yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
}

if config == "rebootondestroy" {
return fmt.Sprintf(
testAccDedicatedServerInstallConfig_RebootOnDestroy,
dedicated_server,
testName,
sshKey,
testName,
)
}

return fmt.Sprintf(
testAccDedicatedServerInstallConfig_Basic,
dedicated_server,
testName,
sshKey,
testName,
)

}

const testAccDedicatedServerInstallConfig_Basic = `
data ovh_dedicated_server_boots "harddisk" {
service_name = "%s"
boot_type = "harddisk"
}
resource "ovh_me_ssh_key" "key" {
key_name = "%s"
key = "%s"
}
resource ovh_dedicated_server_update "server" {
service_name = data.ovh_dedicated_server_boots.harddisk.service_name
boot_id = data.ovh_dedicated_server_boots.harddisk.result[0]
monitoring = true
state = "ok"
}
resource "ovh_me_installation_template" "debian" {
base_template_name = "debian10_64"
template_name = "%s"
default_language = "en"
customization {
change_log = "v1"
custom_hostname = "mytest"
ssh_key_name = ovh_me_ssh_key.key.key_name
}
}
resource ovh_dedicated_server_install_task "server_install" {
service_name = data.ovh_dedicated_server_boots.harddisk.service_name
template_name = ovh_me_installation_template.debian.template_name
}
`

const testAccDedicatedServerInstallConfig_RebootOnDestroy = `
data ovh_dedicated_server_boots "harddisk" {
service_name = "%s"
boot_type = "harddisk"
}
data ovh_dedicated_server_boots "rescue" {
service_name = data.ovh_dedicated_server_boots.harddisk.service_name
boot_type = "rescue"
}
resource "ovh_me_ssh_key" "key" {
key_name = "%s"
key = "%s"
}
resource ovh_dedicated_server_update "server" {
service_name = data.ovh_dedicated_server_boots.harddisk.service_name
boot_id = data.ovh_dedicated_server_boots.harddisk.result[0]
monitoring = true
state = "ok"
}
resource "ovh_me_installation_template" "debian" {
base_template_name = "debian10_64"
template_name = "%s"
default_language = "en"
customization {
change_log = "v1"
custom_hostname = "mytest"
ssh_key_name = ovh_me_ssh_key.key.key_name
}
}
resource ovh_dedicated_server_install_task "server_install" {
service_name = data.ovh_dedicated_server_boots.harddisk.service_name
template_name = ovh_me_installation_template.debian.template_name
bootid_on_destroy = data.ovh_dedicated_server_boots.rescue.result[0]
}
`
Loading

0 comments on commit 770c364

Please sign in to comment.