diff --git a/data_source_quantum_file.go b/data_source_quantum_file.go new file mode 100644 index 0000000..e5309c9 --- /dev/null +++ b/data_source_quantum_file.go @@ -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 +} diff --git a/data_source_quantum_file_test.go b/data_source_quantum_file_test.go new file mode 100644 index 0000000..adcdb1d --- /dev/null +++ b/data_source_quantum_file_test.go @@ -0,0 +1,59 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "testing" + + r "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestFile_basic(t *testing.T) { + var cases = []struct { + path string + content string + config string + }{ + { + "quantum_file", + "This is some content", + `data "quantum_file" "file" { + content = "This is some content" + filename = "quantum_file" + }`, + }, + { + "quantum_file", + "This is some sensitive content", + `data "quantum_file" "file" { + sensitive_content = "This is some sensitive content" + filename = "quantum_file" + }`, + }, + } + + for _, tt := range cases { + r.UnitTest(t, r.TestCase{ + Providers: testProviders, + Steps: []r.TestStep{ + { + Config: tt.config, + Check: func(s *terraform.State) error { + content, err := ioutil.ReadFile(tt.path) + if err != nil { + return fmt.Errorf("config:\n%s\n,got: %s\n", tt.config, err) + } + if string(content) != tt.content { + return fmt.Errorf("config:\n%s\ngot:\n%s\nwant:\n%s\n", tt.config, content, tt.content) + } + return nil + }, + }, + }, + }) + } + + os.Remove("quantum_file") +} diff --git a/provider.go b/provider.go index 3ca3aad..b8e8629 100644 --- a/provider.go +++ b/provider.go @@ -6,18 +6,21 @@ import ( "github.com/hashicorp/terraform/terraform" ) +func Provider() terraform.ResourceProvider { + return &schema.Provider{ + DataSourcesMap: map[string]*schema.Resource{ + "quantum_file": dataSourceQuantumFile(), + "quantum_query_json": dataSourceQuantumQueryJSON(), + "quantum_list_files": dataSourceQuantumListFiles(), + }, + ResourcesMap: map[string]*schema.Resource{ + "quantum_password": resourceQuantumPassword(), + }, + } +} + func main() { plugin.Serve(&plugin.ServeOpts{ - ProviderFunc: func() terraform.ResourceProvider { - return &schema.Provider{ - DataSourcesMap: map[string]*schema.Resource{ - "quantum_query_json": dataSourceQuantumQueryJSON(), - "quantum_list_files": dataSourceQuantumListFiles(), - }, - ResourcesMap: map[string]*schema.Resource{ - "quantum_password": resourceQuantumPassword(), - }, - } - }, + ProviderFunc: Provider, }) } diff --git a/provider_test.go b/provider_test.go new file mode 100644 index 0000000..b5ab89f --- /dev/null +++ b/provider_test.go @@ -0,0 +1,18 @@ +package main + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/terraform" +) + +var testProviders = map[string]terraform.ResourceProvider{ + "quantum": Provider(), +} + +func TestProvider(t *testing.T) { + if err := Provider().(*schema.Provider).InternalValidate(); err != nil { + t.Fatalf("err: %s", err) + } +}