Skip to content

Commit

Permalink
Add data source that creates files
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Dec 20, 2018
1 parent 53ffbdf commit 331fb17
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
71 changes: 71 additions & 0 deletions data_source_quantum_file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"crypto/sha1"
"encoding/hex"
"io/ioutil"
"os"
"path"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceQuantumFile() *schema.Resource {
return &schema.Resource{
Read: resourceLocalFileRead,

Schema: map[string]*schema.Schema{
"content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"sensitive_content"},
},
"sensitive_content": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Sensitive: true,
ConflictsWith: []string{"content"},
},
"filename": {
Type: schema.TypeString,
Description: "Path to the output file",
Required: true,
ForceNew: true,
},
},
}
}

func resourceLocalFileRead(d *schema.ResourceData, _ interface{}) error {
content := resourceLocalFileContent(d)
destination := d.Get("filename").(string)

destinationDir := path.Dir(destination)
if _, err := os.Stat(destinationDir); err != nil {
if err := os.MkdirAll(destinationDir, 0777); err != nil {
return err
}
}

if err := ioutil.WriteFile(destination, []byte(content), 0777); err != nil {
return err
}

checksum := sha1.Sum([]byte(content))
d.SetId(hex.EncodeToString(checksum[:]))

return nil
}

func resourceLocalFileContent(d *schema.ResourceData) string {
content := d.Get("content")
sensitiveContent, sensitiveSpecified := d.GetOk("sensitive_content")
useContent := content.(string)
if sensitiveSpecified {
useContent = sensitiveContent.(string)
}

return useContent
}
1 change: 1 addition & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func main() {
ProviderFunc: func() terraform.ResourceProvider {
return &schema.Provider{
DataSourcesMap: map[string]*schema.Resource{
"quantum_file": dataSourceQuantumFile(),
"quantum_query_json": dataSourceQuantumQueryJSON(),
"quantum_list_files": dataSourceQuantumListFiles(),
},
Expand Down

0 comments on commit 331fb17

Please sign in to comment.