Skip to content

Commit

Permalink
Fixes #406 - introduce instance_type_id to ovirt_vm
Browse files Browse the repository at this point in the history
  • Loading branch information
eslutsky committed May 31, 2022
1 parent 8f72e7a commit f39a421
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
22 changes: 22 additions & 0 deletions internal/ovirt/resource_ovirt_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ var vmSchema = map[string]*schema.Schema{
Optional: true,
Description: "Enable or disable the serial console.",
},
"instance_type_id": {
Type: schema.TypeString,
Optional: true,
Description: "Defines the VM instance type ID overrides the hardware parameters of the created VM.",
ValidateDiagFunc: validateUUID,
},
}

func vmAffinityValues() []string {
Expand Down Expand Up @@ -216,6 +222,7 @@ func (p *provider) vmCreate(
handleVMMemory,
handleVMMemoryPolicy,
handleVMSerialConsole,
handleVMInstanceTypeID,
} {
diags = f(client, data, params, diags)
}
Expand Down Expand Up @@ -521,6 +528,21 @@ func handleVMInitialization(
return diags
}

func handleVMInstanceTypeID(
_ ovirtclient.Client,
data *schema.ResourceData,
params ovirtclient.BuildableVMParameters,
diags diag.Diagnostics,
) diag.Diagnostics {
if instanceTypeID, ok := data.GetOk("instance_type_id"); ok {
_, err := params.WithInstanceTypeID(ovirtclient.InstanceTypeID(instanceTypeID.(string)))
if err != nil {
diags = append(diags, errorToDiag("set instance_type_id on VM", err))
}
}
return diags
}

func (p *provider) vmRead(
ctx context.Context,
data *schema.ResourceData,
Expand Down
62 changes: 62 additions & 0 deletions internal/ovirt/resource_ovirt_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,68 @@ resource "ovirt_vm" "foo" {
)
}

func TestVMResourceInstanceTypeID(t *testing.T) {
t.Parallel()

p := newProvider(newTestLogger(t))
clusterID := p.getTestHelper().GetClusterID()
templateID := p.getTestHelper().GetBlankTemplateID()
client := p.getTestHelper().GetClient().WithContext(context.Background())

instanceTypes, err := client.ListInstanceTypes()
testHelper := p.getTestHelper()

if err != nil {
t.Fatalf("Failed to list instance types (%v)", err)
}
instanceType := instanceTypes[0].ID()

config := fmt.Sprintf(
`
provider "ovirt" {
mock = true
}
resource "ovirt_vm" "foo" {
cluster_id = "%s"
template_id = "%s"
name = "test"
instance_type_id = "%s"
}
`,
clusterID,
templateID,
instanceType,
)

resource.UnitTest(
t, resource.TestCase{
ProviderFactories: p.getProviderFactories(),
Steps: []resource.TestStep{
{
Config: config,
Check: func(state *terraform.State) error {
client := testHelper.GetClient()
vmID := state.RootModule().Resources["ovirt_vm.foo"].Primary.ID
vm, err := client.GetVM(ovirtclient.VMID(vmID))
if err != nil {
return err
}
if *vm.InstanceTypeID() != instanceType {
return fmt.Errorf("incorrect value for instance Type ID: %s ", *vm.InstanceTypeID())
}
return nil
},
},
{
Config: config,
Destroy: true,
},
},
},
)
}

type testVM struct {
id ovirtclient.VMID
name string
Expand Down

0 comments on commit f39a421

Please sign in to comment.