From 1bfc65dda60c7d00821e618f76f4f5568142a3e0 Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Wed, 18 Oct 2023 14:49:10 +0200 Subject: [PATCH] Add support for glesys_server backupschedules ``` backups_schedule { frequency = "daily" retention = 7 } backups_schedule { frequency = "weekly" retention = 4 } ``` KVM backupschedule accTest --- docs/resources/server.md | 40 +++++++++++++ examples/resources/glesys_server/resource.tf | 31 ++++++++++ glesys/resource_glesys_server.go | 62 ++++++++++++++++++++ glesys/resource_glesys_server_test.go | 49 ++++++++++++++++ 4 files changed, 182 insertions(+) diff --git a/docs/resources/server.md b/docs/resources/server.md index 63b3f60..29b56b7 100644 --- a/docs/resources/server.md +++ b/docs/resources/server.md @@ -45,6 +45,37 @@ resource "glesys_server" "kvm" { } } +# KVM with backups_schedule + +resource "glesys_server" "kvm" { + datacenter = "Stockholm" + memory = 1024 + storage = 20 + cpu = 1 + bandwidth = 100 + + hostname = "www1" + + platform = "KVM" + template = "debian-11" + + user { + username = "alice" + publickeys = [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl alice@example.com", + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOfN4dBsS2p1UX+DP6RicdxAYCCeRK8mzCldCS0W9A+5 alice@ws.example.com" + ] + password = "hunter4!" + } + backups_schedule { + frequency = "daily" + retention = 7 + } + backups_schedule { + frequency = "weekly" + retention = 4 + } +} # Advanced example using variables # Set some variables variable "datacenter" { @@ -154,6 +185,7 @@ Examples can be found in the [GleSYS API - Cloud config documentation](https://g ### Optional +- `backups_schedule` (Block Set) KVM Server backup schedule definition. (see [below for nested schema](#nestedblock--backups_schedule)) - `campaigncode` (String) Campaigncode used during creation for possible discount - `cloudconfig` (String) Cloudconfig used to provision server using a provided cloud-config mustache template. - `cloudconfigparams` (Map of String) Cloudconfigparams is used to provide additional parameters to the template in `cloudconfig` using a map. This can be set using a Terraform Local Value. @@ -174,6 +206,14 @@ Examples can be found in the [GleSYS API - Cloud config documentation](https://g - `isrunning` (Boolean) Server running state - `network_adapters` (List of Object) Network adapters associated with the server. `glesys_networkadapter` (see [below for nested schema](#nestedatt--network_adapters)) + +### Nested Schema for `backups_schedule` + +Required: + +- `frequency` (String) +- `retention` (Number) + ### Nested Schema for `user` diff --git a/examples/resources/glesys_server/resource.tf b/examples/resources/glesys_server/resource.tf index 4507a47..97bc52d 100644 --- a/examples/resources/glesys_server/resource.tf +++ b/examples/resources/glesys_server/resource.tf @@ -34,6 +34,37 @@ resource "glesys_server" "kvm" { } } +# KVM with backups_schedule + +resource "glesys_server" "kvm" { + datacenter = "Stockholm" + memory = 1024 + storage = 20 + cpu = 1 + bandwidth = 100 + + hostname = "www1" + + platform = "KVM" + template = "debian-11" + + user { + username = "alice" + publickeys = [ + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl alice@example.com", + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOfN4dBsS2p1UX+DP6RicdxAYCCeRK8mzCldCS0W9A+5 alice@ws.example.com" + ] + password = "hunter4!" + } + backups_schedule { + frequency = "daily" + retention = 7 + } + backups_schedule { + frequency = "weekly" + retention = 4 + } +} # Advanced example using variables # Set some variables variable "datacenter" { diff --git a/glesys/resource_glesys_server.go b/glesys/resource_glesys_server.go index d45a8ce..e92ca39 100644 --- a/glesys/resource_glesys_server.go +++ b/glesys/resource_glesys_server.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" ) func resourceGlesysServer() *schema.Resource { @@ -194,6 +195,25 @@ func resourceGlesysServer() *schema.Resource { }, }, }, + + "backups_schedule": { + Type: schema.TypeSet, + Optional: true, + Description: "KVM Server backup schedule definition.", + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "frequency": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringInSlice([]string{"daily", "weekly"}, false), + }, + "retention": { + Type: schema.TypeInt, + Required: true, + }, + }, + }, + }, }, } } @@ -222,6 +242,22 @@ func expandUsers(config []interface{}) ([]glesys.User, error) { return users, nil } +func expandBackupSchedules(config []interface{}) ([]glesys.ServerBackupSchedule, error) { + schedules := make([]glesys.ServerBackupSchedule, 0, len(config)) + + for _, rawSchedule := range config { + schedule := rawSchedule.(map[string]interface{}) + + s := glesys.ServerBackupSchedule{ + Frequency: schedule["frequency"].(string), + Numberofimagestokeep: schedule["retention"].(int), + } + + schedules = append(schedules, s) + } + return schedules, nil +} + func buildServerParamStruct(d *schema.ResourceData) *glesys.CreateServerParams { opts := glesys.CreateServerParams{ Bandwidth: d.Get("bandwidth").(int), @@ -252,6 +288,12 @@ func resourceGlesysServerCreate(ctx context.Context, d *schema.ResourceData, m i // Setup server parameters srv := buildServerParamStruct(d) + backupsList, err := expandBackupSchedules(d.Get("backups_schedule").(*schema.Set).List()) + if err != nil { + return diag.Errorf("Error when expanding backup schedules: %v", err) + } + srv.Backup = backupsList + // Setup users for server creation usersList, err := expandUsers(d.Get("user").(*schema.Set).List()) if err != nil { @@ -329,6 +371,19 @@ func resourceGlesysServerRead(ctx context.Context, d *schema.ResourceData, m int } d.Set("extra_disks", diskIDs) + var backupSchedules []map[string]interface{} + for _, bs := range srv.Backup.Schedules { + schedule := map[string]interface{}{ + "frequency": bs.Frequency, + "retention": bs.Numberofimagestokeep, + } + backupSchedules = append(backupSchedules, schedule) + } + + if err := d.Set("backups_schedule", backupSchedules); err != nil { + return diag.Errorf("unable to set backups_schedule, read value %v", err) + } + var adapters []map[string]interface{} netAdapters, _ := client.Servers.NetworkAdapters(ctx, d.Id()) for _, v := range *netAdapters { @@ -374,6 +429,13 @@ func resourceGlesysServerUpdate(ctx context.Context, d *schema.ResourceData, m i if d.HasChange("storage") { opts.Storage = d.Get("storage").(int) } + if d.HasChange("backups_schedule") { + backupsList, err := expandBackupSchedules(d.Get("backups_schedule").(*schema.Set).List()) + if err != nil { + diag.Errorf("Error updating backups_schedule: %s", err) + } + opts.Backup = backupsList + } _, err := client.Servers.Edit(ctx, d.Id(), opts) if err != nil { return diag.Errorf("Error updating instance: %s", err) diff --git a/glesys/resource_glesys_server_test.go b/glesys/resource_glesys_server_test.go index 9f64eee..43aa850 100644 --- a/glesys/resource_glesys_server_test.go +++ b/glesys/resource_glesys_server_test.go @@ -76,6 +76,30 @@ func TestAccServerVMware_basic(t *testing.T) { }) } +func TestAccServerKVM_BackupSchedule(t *testing.T) { + rName := acctest.RandomWithPrefix("tf-srv-kvm") + + name := "glesys_server.test" + resource.UnitTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testGlesysProviders, + Steps: []resource.TestStep{ + { + Config: testAccGlesysServerBase_KVM_BackupSchedule(rName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(name, "hostname", rName), + resource.TestCheckResourceAttr(name, "datacenter", "Falkenberg"), + resource.TestCheckResourceAttr(name, "platform", "KVM"), + resource.TestCheckResourceAttr(name, "backups_schedule.0.frequency", "daily"), + resource.TestCheckResourceAttr(name, "backups_schedule.0.retention", "3"), + resource.TestCheckResourceAttrSet(name, "ipv4_address"), + resource.TestCheckResourceAttrSet(name, "ipv6_address"), + ), + }, + }, + }) +} + func testAccGlesysServerBase_VMware(name string) string { return fmt.Sprintf(` resource "glesys_server" "test" { @@ -95,3 +119,28 @@ func testAccGlesysServerBase_VMware(name string) string { } } `, name) } + +func testAccGlesysServerBase_KVM_BackupSchedule(name string) string { + return fmt.Sprintf(` + resource "glesys_server" "test" { + hostname = "%s" + datacenter = "Falkenberg" + platform = "KVM" + bandwidth = 100 + cpu = 1 + memory = 1024 + storage = 10 + template = "Debian 12 (Bookworm)" + + user { + username = "acctestuser" + publickeys = ["ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINOCh8br7CwZDMGmINyJgBip943QXgkf7XdXrDMJf5Dl acctestuser@example-host"] + password = "hunter123!" + } + + backups_schedule { + frequency = "daily" + retention = 3 + } + } `, name) +}