-
Notifications
You must be signed in to change notification settings - Fork 546
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
213 additions
and
4 deletions.
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
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,191 @@ | ||
package proxmox | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/sha256" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/Telmate/proxmox-api-go/proxmox" | ||
"github.com/kdomanski/iso9660" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
const ( | ||
isoContentType = "iso" | ||
) | ||
|
||
func resourceCloudInitDisk() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceCloudInitDiskCreate, | ||
ReadContext: resourceCloudInitDiskRead, | ||
DeleteContext: resourceCloudInitDiskDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"pve_node": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"storage": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"user_data": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"meta_data": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"network_config": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"content_sha256": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
"size": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func createCloudInitISO(metaData, userData, networkConfig string) (io.Reader, string, error) { | ||
isoWriter, err := iso9660.NewWriter() | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
defer isoWriter.Cleanup() | ||
|
||
err = isoWriter.AddFile(strings.NewReader(metaData), "meta-data") | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
err = isoWriter.AddFile(strings.NewReader(userData), "user-data") | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
err = isoWriter.AddFile(strings.NewReader(networkConfig), "network-config") | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
var b bytes.Buffer | ||
err = isoWriter.WriteTo(&b, "cidata") | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
// Calculate the sha256 of the content | ||
hasher := sha256.New() | ||
var content bytes.Buffer | ||
content.WriteString(metaData) | ||
content.WriteString(userData) | ||
content.WriteString(networkConfig) | ||
if _, err := io.Copy(hasher, &content); err != nil { | ||
return nil, "", err | ||
} | ||
contentSum := fmt.Sprintf("%x", hasher.Sum(nil)) | ||
|
||
return bytes.NewReader(b.Bytes()), contentSum, nil | ||
} | ||
|
||
func resourceCloudInitDiskCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
pconf := m.(*providerConfiguration) | ||
client := pconf.Client | ||
|
||
r, sum, err := createCloudInitISO(d.Get("meta_data").(string), d.Get("user_data").(string), d.Get("network_config").(string)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
fileName := fmt.Sprintf("%s.iso", d.Get("name").(string)) | ||
err = client.Upload(d.Get("pve_node").(string), d.Get("storage").(string), isoContentType, fileName, r) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
// The volume ID is the storage name and the file name | ||
volId := fmt.Sprintf("%s:%s/%s", d.Get("storage").(string), isoContentType, fileName) | ||
d.SetId(volId) | ||
d.Set("content_sha256", sum) | ||
|
||
return resourceCloudInitDiskRead(ctx, d, m) | ||
} | ||
|
||
func resourceCloudInitDiskRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
pconf := m.(*providerConfiguration) | ||
client := pconf.Client | ||
|
||
var isoFound bool | ||
pveNode := d.Get("pve_node").(string) | ||
vmRef := &proxmox.VmRef{} | ||
vmRef.SetNode(pveNode) | ||
vmRef.SetVmType("qemu") | ||
storageContent, err := client.GetStorageContent(vmRef, d.Get("storage").(string)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
contents := storageContent["data"].([]interface{}) | ||
for c := range contents { | ||
storageContentMap := contents[c].(map[string]interface{}) | ||
if storageContentMap["volid"].(string) == d.Id() { | ||
size := storageContentMap["size"].(float64) | ||
d.Set("size", ByteCountIEC(int64(size))) | ||
isoFound = true | ||
break | ||
} | ||
} | ||
|
||
if !isoFound { | ||
// ISO not found so we (re)create it | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
// ISO can't be downloaded so we need to compare the content sha256 | ||
_, sum, err := createCloudInitISO(d.Get("meta_data").(string), d.Get("user_data").(string), d.Get("network_config").(string)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.Set("content_sha256", sum) | ||
// If the sha256 doesn't match the state we need to recreate the resource | ||
if d.HasChange("content_sha256") { | ||
d.SetId("") | ||
} | ||
return nil | ||
} | ||
|
||
func resourceCloudInitDiskDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
pconf := m.(*providerConfiguration) | ||
client := pconf.Client | ||
|
||
storage := strings.SplitN(d.Id(), ":", 2)[0] | ||
isoURL := fmt.Sprintf("/nodes/%s/storage/%s/content/%s", d.Get("pve_node").(string), storage, d.Id()) | ||
err := client.Delete(isoURL) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
return nil | ||
} |
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