Skip to content

Commit

Permalink
F #300: improve host updates and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
treywelsh committed Nov 29, 2022
1 parent f1b8b09 commit 016e2a1
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
19 changes: 19 additions & 0 deletions opennebula/resource_opennebula_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func resourceOpennebulaHost() *schema.Resource {
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Type of the new host: kvm, qemu, lxd, lxc, firecracker, custom",
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToUpper(v.(string))
Expand All @@ -65,11 +66,13 @@ func resourceOpennebulaHost() *schema.Resource {
"virtualization": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Virtualization driver",
},
"information": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Information driver",
},
},
Expand Down Expand Up @@ -393,6 +396,8 @@ func resourceOpennebulaHostRead(ctx context.Context, d *schema.ResourceData, met
func resourceOpennebulaHostUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

var diags diag.Diagnostics
config := meta.(*Configuration)
controller := config.Controller

hc, err := getHostController(d, meta)
if err != nil {
Expand Down Expand Up @@ -427,6 +432,20 @@ func resourceOpennebulaHostUpdate(ctx context.Context, d *schema.ResourceData, m
log.Printf("[INFO] Successfully updated name for host %s\n", hostInfos.Name)
}

if d.HasChange("cluster_id") {
clusterID := d.Get("cluster_id").(int)

err := controller.Cluster(clusterID).AddHost(hc.ID)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to add host to it's new cluster",
Detail: fmt.Sprintf("host (ID: %s) cluster (ID: %d): %s", d.Id(), clusterID, err),
})
return diags
}
}

update := false
newTpl := hostInfos.Template

Expand Down
46 changes: 44 additions & 2 deletions opennebula/resource_opennebula_host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAccHost(t *testing.T) {
{
Config: testAccHostConfigAddOvercommit,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("opennebula_host.test1", "name", "test-custom"),
resource.TestCheckResourceAttr("opennebula_host.test1", "name", "test-updated"),
resource.TestCheckResourceAttr("opennebula_host.test1", "type", "custom"),
resource.TestCheckResourceAttr("opennebula_host.test1", "cluster_id", "0"),
resource.TestCheckTypeSetElemNestedAttrs("opennebula_host.test1", "custom.*", map[string]string{
Expand All @@ -49,6 +49,25 @@ func TestAccHost(t *testing.T) {
resource.TestCheckResourceAttr("opennebula_host.test1", "tags.customer", "test"),
),
},
{
Config: testAccHostConfigUpdate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("opennebula_host.test1", "name", "test-updated"),
resource.TestCheckResourceAttr("opennebula_host.test1", "type", "custom"),
resource.TestCheckResourceAttr("opennebula_host.test1", "cluster_id", "0"),
resource.TestCheckTypeSetElemNestedAttrs("opennebula_host.test1", "custom.*", map[string]string{
"virtualization": "dummy",
"information": "dummy",
}),
resource.TestCheckTypeSetElemNestedAttrs("opennebula_host.test1", "overcommit.*", map[string]string{
"cpu": "3300",
"memory": "1148576",
}),
resource.TestCheckResourceAttr("opennebula_host.test1", "tags.%", "2"),
resource.TestCheckResourceAttr("opennebula_host.test1", "tags.environment", "example-updated"),
resource.TestCheckResourceAttr("opennebula_host.test1", "tags.customer", "test"),
),
},
},
})
}
Expand Down Expand Up @@ -92,7 +111,7 @@ resource "opennebula_host" "test1" {

var testAccHostConfigAddOvercommit = `
resource "opennebula_host" "test1" {
name = "test-custom"
name = "test-updated"
type = "custom"
cluster_id = 0
Expand All @@ -112,3 +131,26 @@ resource "opennebula_host" "test1" {
}
}
`

var testAccHostConfigUpdate = `
resource "opennebula_host" "test1" {
name = "test-updated"
type = "custom"
cluster_id = 0
custom {
virtualization = "dummy"
information = "dummy"
}
overcommit {
cpu = 3300
memory = 1148576
}
tags = {
environment = "example-updated"
customer = "test"
}
}
`

0 comments on commit 016e2a1

Please sign in to comment.