From 83dd9a979f8931175bfd51c714fc26d0eebc67be Mon Sep 17 00:00:00 2001 From: Alexis Autret Date: Wed, 3 Apr 2024 16:45:26 +0200 Subject: [PATCH] add userMetadata option for dedicated server resource (& update dedicated_server_install_test with debian12 template and some doc typo update) --- ovh/resource_dedicated_server_install_task.go | 20 +++ ...urce_dedicated_server_install_task_test.go | 104 +++++++++++++++- ovh/types_dedicated_server.go | 18 ++- ...edicated_server_install_task.html.markdown | 117 +++++++++++++++++- 4 files changed, 253 insertions(+), 6 deletions(-) diff --git a/ovh/resource_dedicated_server_install_task.go b/ovh/resource_dedicated_server_install_task.go index 1243011cf..cc176958d 100644 --- a/ovh/resource_dedicated_server_install_task.go +++ b/ovh/resource_dedicated_server_install_task.go @@ -120,6 +120,26 @@ func resourceDedicatedServerInstallTask() *schema.Resource { }, }, }, + "user_metadata": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + MaxItems: 128, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "key": { + Type: schema.TypeString, + Description: "The key for the user_metadata", + Optional: true, + }, + "value": { + Type: schema.TypeString, + Description: "The value for the user_metadata", + Optional: true, + }, + }, + }, + }, //Computed "comment": { diff --git a/ovh/resource_dedicated_server_install_task_test.go b/ovh/resource_dedicated_server_install_task_test.go index a27bf5aa5..8d3bbd9fc 100644 --- a/ovh/resource_dedicated_server_install_task_test.go +++ b/ovh/resource_dedicated_server_install_task_test.go @@ -75,6 +75,39 @@ func TestAccDedicatedServerInstall_rebootondestroy(t *testing.T) { }) } +func TestAccDedicatedServerInstall_usermetadata(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheckCredentials(t) + testAccPreCheckDedicatedServer(t) + }, + Providers: testAccProviders, + ExternalProviders: map[string]resource.ExternalProvider{ + "time": { + VersionConstraint: "0.10.0", + Source: "hashicorp/time", + }, + }, + Steps: []resource.TestStep{ + { + Config: testAccDedicatedServerInstallConfig("usermetadata"), + 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) @@ -93,6 +126,18 @@ func testAccDedicatedServerInstallConfig(config string) string { ) } + if config == "usermetadata" { + return fmt.Sprintf( + testAccDedicatedServerInstallConfig_Usermetadata, + dedicated_server, + testName, + sshKey, + testName, + sshKey, + sshKey, + ) + } + return fmt.Sprintf( testAccDedicatedServerInstallConfig_Basic, dedicated_server, @@ -169,7 +214,7 @@ resource ovh_dedicated_server_update "server" { } resource "ovh_me_installation_template" "debian" { - base_template_name = "debian10_64" + base_template_name = "debian12_64" template_name = "%s" default_language = "en" @@ -192,3 +237,60 @@ resource ovh_dedicated_server_install_task "server_install" { depends_on = [time_sleep.wait_for_ssh_key_sync] } ` +const testAccDedicatedServerInstallConfig_Usermetadata = ` +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 + monitoring = true + state = "ok" +} + +resource "ovh_me_installation_template" "byolinux" { + base_template_name = "byolinux_64" + template_name = "%s" + default_language = "en" +} + +resource ovh_dedicated_server_install_task "server_install" { + service_name = data.ovh_dedicated_server_boots.harddisk.service_name + template_name = ovh_me_installation_template.byolinux.template_name + user_metadata { + key = "imageURL" + value = "https://github.com/ashmonger/akution_test/releases/download/0.6-fixCache/deb11k6.qcow2" + } + user_metadata { + key = "imageType" + value = "qcow2" + } + user_metadata { + key = "httpHeaders0Key" + value = "Authorization" + } + user_metadata { + key = "httpHeaders0Value" + value = "Basic bG9naW46cGFzc3dvcmQ=" + } + user_metadata { + key = "imageCheckSum" + value = "047122c9ff4d2a69512212104b06c678f5a9cdb22b75467353613ff87ccd03b57b38967e56d810e61366f9d22d6bd39ac0addf4e00a4c6445112a2416af8f225" + } + user_metadata { + key = "imageCheckSumType" + value = "sha512" + } + user_metadata { + key = "configDriveUserData" + value = "#cloud-config\nssh_authorized_keys:\n - %s\n\nusers:\n - name: aautret\n sudo: ALL=(ALL) NOPASSWD:ALL\n groups: users, sudo\n shell: /bin/bash\n lock_passwd: false\n ssh_authorized_keys:\n - %s\ndisable_root: false\npackages:\n - vim\n - tree\nfinal_message: The system is finally up, after $UPTIME seconds\n" + } +} +` diff --git a/ovh/types_dedicated_server.go b/ovh/types_dedicated_server.go index cdd3cd171..5ddfc973f 100644 --- a/ovh/types_dedicated_server.go +++ b/ovh/types_dedicated_server.go @@ -111,6 +111,7 @@ type DedicatedServerInstallTaskCreateOpts struct { TemplateName string `json:"templateName"` PartitionSchemeName *string `json:"partitionSchemeName,omitempty"` Details *DedicatedServerInstallTaskDetails `json:"details"` + UserMetadata *[]map[string]string `json:"userMetadata"` } func (opts *DedicatedServerInstallTaskCreateOpts) FromResource(d *schema.ResourceData) *DedicatedServerInstallTaskCreateOpts { @@ -118,10 +119,25 @@ func (opts *DedicatedServerInstallTaskCreateOpts) FromResource(d *schema.Resourc opts.PartitionSchemeName = helpers.GetNilStringPointerFromData(d, "partition_scheme_name") details := d.Get("details").([]interface{}) - if details != nil && len(details) == 1 { + if len(details) == 1 { opts.Details = (&DedicatedServerInstallTaskDetails{}).FromResource(d, "details.0") + } + userMetadata := d.Get("user_metadata").([]interface{}) + var userMetadatas []map[string]string + if len(userMetadata) >= 1 { + for _, metadata := range userMetadata { + m := metadata.(map[string]interface{}) + metadatum := make(map[string]string) + key := m["key"].(string) + metadatum["key"] = key + value := m["value"].(string) + metadatum["value"] = value + userMetadatas = append(userMetadatas, metadatum) + } + opts.UserMetadata = &userMetadatas } + return opts } diff --git a/website/docs/r/dedicated_server_install_task.html.markdown b/website/docs/r/dedicated_server_install_task.html.markdown index 5c70bb65b..ea0ce1e50 100644 --- a/website/docs/r/dedicated_server_install_task.html.markdown +++ b/website/docs/r/dedicated_server_install_task.html.markdown @@ -13,6 +13,7 @@ after a while. ## Example Usage +Using a custom template based on an OVHCloud template ```hcl data ovh_dedicated_server_boots "rescue" { service_name = "nsxxxxxxx.ip-xx-xx-xx.eu" @@ -44,6 +45,96 @@ resource "ovh_dedicated_server_install_task" "server_install" { } } ``` +Using a BringYourOwnLinux (BYOL) template (with userMetadata) +```hcl +data "ovh_dedicated_server" "server" { + service_name = "nsxxxxxxx.ip-xx-xx-xx.eu" +} + +resource "ovh_me_ssh_key" "key" { + key_name = "mykey" + key = "ssh-ed25519 AAAAC3..." +} + +data ovh_dedicated_server_boots "rescue" { + service_name = "nsxxxxxxx.ip-xx-xx-xx.eu" + boot_type = "rescue" +} + +resource "ovh_me_installation_template" "mytemplate" { + base_template_name = "byolinux_64" + template_name = "mybyol_test" +} + +resource "ovh_dedicated_server_install_task" "server_install" { + service_name = data.ovh_dedicated_server.server.service_name + template_name = ovh_me_installation_template.mytemplate.template_name + bootid_on_destroy = data.ovh_dedicated_server_boots.rescue.result[0] + user_metadata { + key = "imageURL" + value = "https://myimage.qcow2" + } + user_metadata { + key = "imageType" + value = "qcow2" + } + user_metadata { + key = "httpHeaders0Key" + value = "Authorization" + } + user_metadata { + key = "httpHeaders0Value" + value = "Basic bG9naW46xxxxxxx=" + } + user_metadata { + key = "imageChecksumType" + value = "sha512" + } + user_metadata { + key = "language" + value = "en" + } + user_metadata { + key = "imageCheckSum" + value = "047122c9ff4d2a69512212104b06c678f5a9cdb22b75467353613ff87ccd03b57b38967e56d810e61366f9d22d6bd39ac0addf4e00a4c6445112a2416af8f225" + } + user_metadata { + key = "configDriveUserData" + value = "#cloud-config\nssh_authorized_keys:\n - ${data.ovh_me_ssh_key.mykey.key}\n\nusers:\n - name: patient0\n sudo: ALL=(ALL) NOPASSWD:ALL\n groups: users, sudo\n shell: /bin/bash\n lock_passwd: false\n ssh_authorized_keys:\n - ${data.ovh_me_ssh_key.mykey.key}\ndisable_root: false\npackages:\n - vim\n - tree\nfinal_message: The system is finally up, after $UPTIME seconds\n" + } + details { + custom_hostname = "mytest" + } +} +``` +Using a Microsoft Windows server OVHcloud template with a specific language +hcl``` +data "ovh_dedicated_server" "server" { + service_name = "nsxxxxxxx.ip-xx-xx-xx.eu" +} + +data ovh_dedicated_server_boots "rescue" { + service_name = "nsxxxxxxx.ip-xx-xx-xx.eu" + boot_type = "rescue" +} + +resource "ovh_dedicated_server_install_task" "server_install" { + service_name = data.ovh_dedicated_server.server.service_name + template_name = "win2019-std_64" + bootid_on_destroy = data.ovh_dedicated_server_boots.rescue.result[0] + user_metadata { + key = "language" + value ="fr-fr" + } + user_metadata { + key = "useSpla" + value = "true" + } + details { + custom_hostname = "mytest" + } +} +``` ## Argument Reference @@ -54,19 +145,37 @@ The following arguments are supported: * `template_name` - (Required) Template name. * `bootid_on_destroy` - If set, reboot the server on the specified boot id during destroy phase. * `details` - see `details` block below. +* `user_metadata` - see `user_metadata` block below. + The `details` block supports: * `custom_hostname` - Set up the server using the provided hostname instead of the default hostname. * `disk_group_id` - Disk group id. -* `install_sql_server` - set to true to install sql server (Windows template only). -* `language` - language. -* `no_raid` - set to true to disable RAID. +* `install_sql_server` - Set to true to install sql server (Windows template only). +* `language` - Language. +* `no_raid` - Set to true to disable RAID. * `post_installation_script_link` - Indicate the URL where your postinstall customisation script is located. * `post_installation_script_return` - 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'. * `soft_raid_devices` - soft raid devices. * `ssh_key_name` - Name of the ssh key that should be installed. Password login will be disabled. -* `use_spla` - set to true to use SPLA. +* `use_spla` - Set to true to use SPLA. + +The `user_metadata` block supports : +(but is not limited to ! : [see documentation](https://help.ovhcloud.com/csm/world-documentation-bare-metal-cloud-dedicated-servers-managing-servers?id=kb_browse_cat&kb_id=203c4f65551974502d4c6e78b7421996&kb_category=97feff9459b0a510f078155c0c16be9b)) + +* `imageUrl` - Your Linux image URL. +* `imageType` - Your Linux image type (qcow2, raw). +* `imageCheckSum` - Your image's checksum. +* `imageCheckSumType` - Your image's checksum type. +* `httpHeadersNKey` - Your image's HTTP headers key (where N is a integer). +* `httpHeadersNValue` - Your image's HTTP headers value (where N is a integer). +* `configDriveUserData` - Your user config drive user data. +* `configDriveMetadata0Key` - Your user config drive user metadata key(where N is a integer). +* `configDriveMetadata0Value` - Your user config drive user metadata value (where N is a integer). +* `language` - Language. +* `useSpla` - Set to true to use SPLA. + ## Attributes Reference