Skip to content

Commit

Permalink
Add support for glesys_server backupschedules
Browse files Browse the repository at this point in the history
```
backups_schedule {
  frequency = "daily"
  retention = 7
}

backups_schedule {
  frequency = "weekly"
  retention = 4
}
```

KVM backupschedule accTest
  • Loading branch information
norrland committed Oct 9, 2024
1 parent 61ebf7f commit 1bfc65d
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
40 changes: 40 additions & 0 deletions docs/resources/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOfN4dBsS2p1UX+DP6RicdxAYCCeRK8mzCldCS0W9A+5 [email protected]"
]
password = "hunter4!"
}
backups_schedule {
frequency = "daily"
retention = 7
}
backups_schedule {
frequency = "weekly"
retention = 4
}
}
# Advanced example using variables
# Set some variables
variable "datacenter" {
Expand Down Expand Up @@ -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.
Expand All @@ -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))

<a id="nestedblock--backups_schedule"></a>
### Nested Schema for `backups_schedule`

Required:

- `frequency` (String)
- `retention` (Number)


<a id="nestedblock--user"></a>
### Nested Schema for `user`
Expand Down
31 changes: 31 additions & 0 deletions examples/resources/glesys_server/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOfN4dBsS2p1UX+DP6RicdxAYCCeRK8mzCldCS0W9A+5 [email protected]"
]
password = "hunter4!"
}
backups_schedule {
frequency = "daily"
retention = 7
}
backups_schedule {
frequency = "weekly"
retention = 4
}
}
# Advanced example using variables
# Set some variables
variable "datacenter" {
Expand Down
62 changes: 62 additions & 0 deletions glesys/resource_glesys_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions glesys/resource_glesys_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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)
}

0 comments on commit 1bfc65d

Please sign in to comment.