Skip to content

Commit

Permalink
add userMetadata option for dedicated server resource
Browse files Browse the repository at this point in the history
(& update dedicated_server_install_test with debian12 template
and some doc typo update)
  • Loading branch information
bigbigbang committed Apr 17, 2024
1 parent ccda5dc commit 83dd9a9
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 6 deletions.
20 changes: 20 additions & 0 deletions ovh/resource_dedicated_server_install_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
104 changes: 103 additions & 1 deletion ovh/resource_dedicated_server_install_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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"
Expand All @@ -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"
}
}
`
18 changes: 17 additions & 1 deletion ovh/types_dedicated_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,33 @@ 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 {
opts.TemplateName = d.Get("template_name").(string)
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
}

Expand Down
117 changes: 113 additions & 4 deletions website/docs/r/dedicated_server_install_task.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 83dd9a9

Please sign in to comment.