Skip to content

Commit

Permalink
Merge pull request #10 from coveo/file-data-source
Browse files Browse the repository at this point in the history
File data source
  • Loading branch information
julienduchesne authored Dec 20, 2018
2 parents 53ffbdf + a4503eb commit ca1bb1c
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 11 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
}
59 changes: 59 additions & 0 deletions data_source_quantum_file_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
25 changes: 14 additions & 11 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
18 changes: 18 additions & 0 deletions provider_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit ca1bb1c

Please sign in to comment.