From eb3c877ecb2a7f3e43bb530b219125b6c49e2bc7 Mon Sep 17 00:00:00 2001 From: yann degat Date: Fri, 13 Dec 2019 16:19:01 +0000 Subject: [PATCH] feat/r/dedicated_server_install_task: new resource --- ovh/dedicated_server_task.go | 2 +- ovh/provider.go | 1 + ...ource_ovh_dedicated_server_install_task.go | 105 ++++++++++++++++-- ..._ovh_dedicated_server_install_task_test.go | 91 +++++++++++++++ ovh/types_dedicated_server.go | 53 +++++++++ 5 files changed, 242 insertions(+), 10 deletions(-) create mode 100644 ovh/resource_ovh_dedicated_server_install_task_test.go diff --git a/ovh/dedicated_server_task.go b/ovh/dedicated_server_task.go index 0c6ad2508..386c8962c 100644 --- a/ovh/dedicated_server_task.go +++ b/ovh/dedicated_server_task.go @@ -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, } diff --git a/ovh/provider.go b/ovh/provider.go index be1afc638..735c1b86d 100644 --- a/ovh/provider.go +++ b/ovh/provider.go @@ -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(), diff --git a/ovh/resource_ovh_dedicated_server_install_task.go b/ovh/resource_ovh_dedicated_server_install_task.go index d0b83e9b7..d23a5c960 100644 --- a/ovh/resource_ovh_dedicated_server_install_task.go +++ b/ovh/resource_ovh_dedicated_server_install_task.go @@ -18,6 +18,10 @@ 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, @@ -25,13 +29,96 @@ func resourceDedicatedServerInstallTask() *schema.Resource { 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", + }, + }, }, }, @@ -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) } @@ -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, diff --git a/ovh/resource_ovh_dedicated_server_install_task_test.go b/ovh/resource_ovh_dedicated_server_install_task_test.go new file mode 100644 index 000000000..8f85f2363 --- /dev/null +++ b/ovh/resource_ovh_dedicated_server_install_task_test.go @@ -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 +} +` diff --git a/ovh/types_dedicated_server.go b/ovh/types_dedicated_server.go index a6bc6c4b5..b3d3bc3a0 100644 --- a/ovh/types_dedicated_server.go +++ b/ovh/types_dedicated_server.go @@ -101,3 +101,56 @@ 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 = getNilStringPointer(d.Get("partition_scheme_name")) + + details := d.Get("details").([]interface{}) + if details != nil && len(details) == 1 { + opts.Details = (&DedicatedServerInstallTaskDetails{}).FromMap( + details[0].(map[string]interface{}), + ) + } + 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) FromMap(data map[string]interface{}) *DedicatedServerInstallTaskDetails { + opts.CustomHostname = getNilStringPointer(data["custom_hostname"]) + opts.DiskGroupId = getNilInt64Pointer(data["disk_group_id"]) + opts.InstallRTM = getNilBoolPointer(data["install_rtm"]) + opts.InstallSqlServer = getNilBoolPointer(data["install_sql_server"]) + opts.Language = getNilStringPointer(data["language"]) + opts.NoRaid = getNilBoolPointer(data["no_raid"]) + opts.PostInstallationScriptLink = getNilStringPointer(data["post_installation_script_link"]) + opts.PostInstallationScriptReturn = getNilStringPointer(data["post_installation_script_return"]) + opts.ResetHwRaid = getNilBoolPointer(data["reset_hw_raid"]) + opts.SoftRaidDevices = getNilInt64Pointer(data["soft_raid_devices"]) + opts.SshKeyName = getNilStringPointer(data["ssh_key_name"]) + opts.UseDistribKernel = getNilBoolPointer(data["use_distrib_kernel"]) + opts.UseSpla = getNilBoolPointer(data["use_spla"]) + + return opts +}