From 6cbb0f034844b8f28cfa2a7b057d8d41f4cf2edf Mon Sep 17 00:00:00 2001 From: lecs <33053602+larsve@users.noreply.github.com> Date: Fri, 24 Sep 2021 12:54:04 +0200 Subject: [PATCH] Fixes KVM template mismatch --- glesys/resource_glesys_server.go | 11 ++++++- glesys/resource_glesys_server_test.go | 42 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 glesys/resource_glesys_server_test.go diff --git a/glesys/resource_glesys_server.go b/glesys/resource_glesys_server.go index 6f01165..d3632cc 100644 --- a/glesys/resource_glesys_server.go +++ b/glesys/resource_glesys_server.go @@ -175,6 +175,15 @@ func resourceGlesysServerCreate(d *schema.ResourceData, m interface{}) error { return resourceGlesysServerRead(d, m) } +func getTemplate(original string, srv *glesys.ServerDetails) string { + for _, tag := range srv.InitialTemplate.CurrentTags { + if tag == original { + return original + } + } + return srv.Template +} + func resourceGlesysServerRead(d *schema.ResourceData, m interface{}) error { client := m.(*glesys.Client) @@ -202,7 +211,7 @@ func resourceGlesysServerRead(d *schema.ResourceData, m interface{}) error { d.Set("memory", srv.Memory) d.Set("platform", srv.Platform) d.Set("storage", srv.Storage) - d.Set("template", srv.Template) + d.Set("template", getTemplate(d.Get("template").(string), srv)) d.SetConnInfo(map[string]string{ "type": "ssh", diff --git a/glesys/resource_glesys_server_test.go b/glesys/resource_glesys_server_test.go new file mode 100644 index 0000000..b0574a6 --- /dev/null +++ b/glesys/resource_glesys_server_test.go @@ -0,0 +1,42 @@ +package glesys + +import ( + "testing" + + "github.com/glesys/glesys-go/v2" +) + +func Test_getTemplate(t *testing.T) { + srv := &glesys.ServerDetails{} + for _, tt := range []struct { + name string + tfTemplate string + readTemplate string + readTags []string + want string + }{ + { + name: "KVM_instance", + tfTemplate: "ubuntu-20-04", + readTemplate: "Ubuntu 20.04 LTS (Focal Fossa)", + readTags: []string{"ubuntu", "ubuntu-lts", "ubuntu-20-04"}, + want: "ubuntu-20-04", + }, + { + name: "VMware_instance", + tfTemplate: "Ubuntu 20.04 LTS 64-bit", + readTemplate: "Ubuntu 20.04 LTS 64-bit", + readTags: []string{}, + want: "Ubuntu 20.04 LTS 64-bit", + }, + } { + t.Run(tt.name, func(t *testing.T) { + srv.Template = tt.readTemplate + srv.InitialTemplate.Name = tt.readTemplate + srv.InitialTemplate.CurrentTags = tt.readTags + if got := getTemplate(tt.tfTemplate, srv); got != tt.want { + t.Errorf("got: %v, want %v", got, tt.want) + } + }) + } +}