Skip to content

Commit

Permalink
Fixes KVM template mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
larsve authored and norrland committed Oct 14, 2021
1 parent 829e9f8 commit 6cbb0f0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
11 changes: 10 additions & 1 deletion glesys/resource_glesys_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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",
Expand Down
42 changes: 42 additions & 0 deletions glesys/resource_glesys_server_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 6cbb0f0

Please sign in to comment.