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 Dec 18, 2019
1 parent ac6321b commit 89a8f3a
Show file tree
Hide file tree
Showing 5 changed files with 241 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
105 changes: 96 additions & 9 deletions ovh/resource_ovh_dedicated_server_install_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,107 @@ func resourceDedicatedServerInstallTask() *schema.Resource {
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",
},
"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 +162,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 +188,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
91 changes: 91 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,91 @@
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(),
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() 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"
}

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
}
`
52 changes: 52 additions & 0 deletions ovh/types_dedicated_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,55 @@ type DedicatedServerTask struct {
DoneDate time.Time `json:"doneDate"`
StartDate time.Time `json:"startDate"`
}

type DedicatedServerInstallTaskCreateOpts struct {
TemplateName string `json:"templateName"`
PartitionSchemeName *string `json:"partitionSchemeName,omitempty"`
Details *DedicatedServerInstallTaskDetails `json:"details"`
}

func (opts *DedicatedServerInstallTaskCreateOpts) FromResource(d *schema.ResourceData) *DedicatedServerInstallTaskCreateOpts {
opts.TemplateName = d.Get("template_name").(string)
opts.PartitionSchemeName = getNilStringPointerFromData(d, "partition_scheme_name")

details := d.Get("details").([]interface{})
if details != nil && len(details) == 1 {
opts.Details = (&DedicatedServerInstallTaskDetails{}).FromResource(d, "details.0")

}
return opts
}

type DedicatedServerInstallTaskDetails struct {
CustomHostname *string `json:"customHostname,omitempty"`
DiskGroupId *int64 `json:"diskGroupId,omitempty"`
InstallRTM *bool `json:"installRTM,omitempty"`
InstallSqlServer *bool `json:"installSqlServer,omitempty"`
Language *string `json:"language,omitempty"`
NoRaid *bool `json:"noRaid,omitempty"`
PostInstallationScriptLink *string `json:"postInstallationScriptLink,omitempty"`
PostInstallationScriptReturn *string `json:"postInstallationScriptReturn,omitempty"`
ResetHwRaid *bool `json:"resetHwRaid,omitempty"`
SoftRaidDevices *int64 `json:"softRaidDevices,omitempty"`
SshKeyName *string `json:"sshKeyName,omitempty"`
UseDistribKernel *bool `json:"useDistribKernel,omitempty"`
UseSpla *bool `json:"useSpla,omitempty"`
}

func (opts *DedicatedServerInstallTaskDetails) FromResource(d *schema.ResourceData, parent string) *DedicatedServerInstallTaskDetails {
opts.CustomHostname = getNilStringPointerFromData(d, fmt.Sprintf("%s.custom_hostname", parent))
opts.DiskGroupId = getNilInt64PointerFromData(d, fmt.Sprintf("%s.disk_group_id", parent))
opts.InstallRTM = getNilBoolPointerFromData(d, fmt.Sprintf("%s.install_rtm", parent))
opts.InstallSqlServer = getNilBoolPointerFromData(d, fmt.Sprintf("%s.install_sql_server", parent))
opts.Language = getNilStringPointerFromData(d, fmt.Sprintf("%s.language", parent))
opts.NoRaid = getNilBoolPointerFromData(d, fmt.Sprintf("%s.no_raid", parent))
opts.PostInstallationScriptLink = getNilStringPointerFromData(d, fmt.Sprintf("%s.post_installation_script_link", parent))
opts.PostInstallationScriptReturn = getNilStringPointerFromData(d, fmt.Sprintf("%s.post_installation_script_return", parent))
opts.ResetHwRaid = getNilBoolPointerFromData(d, fmt.Sprintf("%s.reset_hw_raid", parent))
opts.SoftRaidDevices = getNilInt64PointerFromData(d, fmt.Sprintf("%s.soft_raid_devices", parent))
opts.SshKeyName = getNilStringPointerFromData(d, fmt.Sprintf("%s.ssh_key_name", parent))
opts.UseDistribKernel = getNilBoolPointerFromData(d, fmt.Sprintf("%s.use_distrib_kernel", parent))
opts.UseSpla = getNilBoolPointerFromData(d, fmt.Sprintf("%s.use_spla", parent))

return opts
}

0 comments on commit 89a8f3a

Please sign in to comment.