From b0ea74ead501ee47e3df8301579de334326318bd Mon Sep 17 00:00:00 2001 From: Daniel Scain Date: Fri, 25 Jul 2025 15:37:29 +0200 Subject: [PATCH] Fix read TPM from state --- libvirt/domain.go | 2 +- libvirt/resource_libvirt_domain.go | 26 ++++++++++++++++++ libvirt/resource_libvirt_domain_test.go | 35 +++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/libvirt/domain.go b/libvirt/domain.go index f646f3cc0..08af8bfb9 100644 --- a/libvirt/domain.go +++ b/libvirt/domain.go @@ -853,7 +853,7 @@ func setTPMs(d *schema.ResourceData, domainDef *libvirtxml.Domain) { prefix := "tpm.0" if _, ok := d.GetOk(prefix); ok { tpm := libvirtxml.DomainTPM{} - if model, ok := d.GetOk(".model"); ok { + if model, ok := d.GetOk(prefix + ".model"); ok { tpm.Model = model.(string) } diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index e71270285..3c1ed54a9 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -861,6 +861,32 @@ func resourceLibvirtDomainRead(ctx context.Context, d *schema.ResourceData, meta d.Set("kernel", domainDef.OS.Kernel) d.Set("initrd", domainDef.OS.Initrd) + var ( + tpms []map[string]interface{} + tpm map[string]interface{} + ) + for _, tpmDef := range domainDef.Devices.TPMs { + tpm = map[string]interface{}{} + log.Printf(tpmDef.Model) + tpm["model"] = tpmDef.Model + if tpmDef.Backend.Emulator != nil { + tpm["backend_type"] = "emulator" + tpm["backend_version"] = tpmDef.Backend.Emulator.Version + } else if tpmDef.Backend.Passthrough != nil { + tpm["backend_type"] = "passthrough" + if tpmDef.Backend.Passthrough.Device != nil { + tpm["backend_device_path"] = tpmDef.Backend.Passthrough.Device.Path + } + } else { + log.Printf("TPM backend type from VM not implemented yet.") + } + + tpms = append(tpms, tpm) + } + if len(tpms) > 0 { + d.Set("tpm", tpms) + } + caps, err := getHostCapabilities(virConn) if err != nil { return diag.FromErr(err) diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index 550402a1f..73dde0f26 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -875,6 +875,41 @@ func TestAccLibvirtDomain_Cpu(t *testing.T) { }) } +func TestAccLibvirtDomain_Tpm(t *testing.T) { + var domain libvirt.Domain + randomDomainName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha) + + config := fmt.Sprintf(` + resource "libvirt_domain" "%s" { + name = "%s" + tpm { + model = "tpm-crb" + backend_type = "emulator" + backend_version = "2.0" + } + }`, randomDomainName, randomDomainName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLibvirtDomainDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "tpm.0.model", "tpm-crb"), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "tpm.0.backend_type", "emulator"), + resource.TestCheckResourceAttr( + "libvirt_domain."+randomDomainName, "tpm.0.backend_version", "2.0"), + ), + }, + }, + }) +} + func TestAccLibvirtDomain_Video(t *testing.T) { var domain libvirt.Domain randomDomainName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)