-
Notifications
You must be signed in to change notification settings - Fork 463
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial prototype of a XML transformation hook based on XSLT
- Loading branch information
Showing
5 changed files
with
226 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
provider "libvirt" { | ||
uri = "qemu:///system" | ||
} | ||
|
||
resource "libvirt_domain" "xslt-demo-domain" { | ||
name = "xslt-demo-domain" | ||
memory = "512" | ||
|
||
network_interface { | ||
network_name = "default" | ||
} | ||
|
||
xml { | ||
xslt = "${file("nicmodel.xsl")}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" ?> | ||
<xsl:stylesheet version="1.0" | ||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | ||
<xsl:output omit-xml-declaration="yes" indent="yes"/> | ||
<xsl:template match="node()|@*"> | ||
<xsl:copy> | ||
<xsl:apply-templates select="node()|@*"/> | ||
</xsl:copy> | ||
</xsl:template> | ||
|
||
<xsl:template match="/domain/devices/interface[@type='network']/model/@type"> | ||
<xsl:attribute name="type"> | ||
<xsl:value-of select="'e1000'"/> | ||
</xsl:attribute> | ||
</xsl:template> | ||
|
||
</xsl:stylesheet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package libvirt | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// this function applies a XSLT transform to the xml data | ||
// and is to be reused in all resource types | ||
// your resource need to have a xml.xslt element in the schema | ||
func transformResourceXML(xml string, d *schema.ResourceData) (string, error) { | ||
xslt, ok := d.GetOk("xml.0.xslt") | ||
if !ok { | ||
return xml, nil | ||
} | ||
|
||
xsltFile, err := ioutil.TempFile("", "terraform-provider-libvirt-xslt") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer os.Remove(xsltFile.Name()) // clean up | ||
|
||
// we trim the xslt as it may contain space before the xml declaration | ||
// because of HCL heredoc | ||
if _, err := xsltFile.Write([]byte(strings.TrimSpace(xslt.(string)))); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := xsltFile.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
xmlFile, err := ioutil.TempFile("", "terraform-provider-libvirt-xml") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer os.Remove(xmlFile.Name()) // clean up | ||
|
||
if _, err := xmlFile.Write([]byte(xml)); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if err := xmlFile.Close(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
cmd := exec.Command("xsltproc", xsltFile.Name(), xmlFile.Name()) | ||
transformedXML, err := cmd.Output() | ||
if err != nil { | ||
return xml, err | ||
} | ||
log.Printf("[DEBUG] Transformed XML with user specified XSLT:\n%s", transformedXML) | ||
return string(transformedXML), nil | ||
} |