Skip to content

Commit

Permalink
Implement archive provider and "archive_file" resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Sickles committed Jun 25, 2016
1 parent 3c1d8e9 commit 143ecec
Show file tree
Hide file tree
Showing 21 changed files with 157 additions and 207 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"github.com/hashicorp/terraform/builtin/providers/zip"
"github.com/hashicorp/terraform/builtin/providers/archive"
"github.com/hashicorp/terraform/plugin"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: zip.Provider,
ProviderFunc: archive.Provider,
})
}
Binary file added builtin/providers/archive/archive-content.zip
Binary file not shown.
Binary file added builtin/providers/archive/archive-dir.zip
Binary file not shown.
Binary file added builtin/providers/archive/archive-file.zip
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zip
package archive

import (
"fmt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zip
package archive

import (
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -10,7 +10,7 @@ func Provider() terraform.ResourceProvider {
Schema: map[string]*schema.Schema{},

ResourcesMap: map[string]*schema.Resource{
"zip_file": resourceZipFile(),
"archive_file": resourceArchiveFile(),
},
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zip
package archive

import (
"testing"
Expand All @@ -8,7 +8,7 @@ import (
)

var testProviders = map[string]terraform.ResourceProvider{
"zip": Provider(),
"archive": Provider(),
}

func TestProvider(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zip
package archive

import (
"crypto/sha1"
Expand All @@ -9,13 +9,13 @@ import (
"os"
)

func resourceZipFile() *schema.Resource {
func resourceArchiveFile() *schema.Resource {
return &schema.Resource{
Create: resourceZipFileCreate,
Read: resourceZipFileRead,
Update: resourceZipFileUpdate,
Delete: resourceZipFileDelete,
Exists: resourceZipFileExists,
Create: resourceArchiveFileCreate,
Read: resourceArchiveFileRead,
Update: resourceArchiveFileUpdate,
Delete: resourceArchiveFileDelete,
Exists: resourceArchiveFileExists,

Schema: map[string]*schema.Schema{
"type": &schema.Schema{
Expand Down Expand Up @@ -66,14 +66,14 @@ func resourceZipFile() *schema.Resource {
}
}

func resourceZipFileCreate(d *schema.ResourceData, meta interface{}) error {
if err := resourceZipFileUpdate(d, meta); err != nil {
func resourceArchiveFileCreate(d *schema.ResourceData, meta interface{}) error {
if err := resourceArchiveFileUpdate(d, meta); err != nil {
return err
}
return resourceZipFileRead(d, meta)
return resourceArchiveFileRead(d, meta)
}

func resourceZipFileRead(d *schema.ResourceData, meta interface{}) error {
func resourceArchiveFileRead(d *schema.ResourceData, meta interface{}) error {
output_path := d.Get("output_path").(string)
fi, err := os.Stat(output_path)
if os.IsNotExist(err) {
Expand All @@ -93,7 +93,7 @@ func resourceZipFileRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceZipFileUpdate(d *schema.ResourceData, meta interface{}) error {
func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
archiveType := d.Get("type").(string)
outputPath := d.Get("output_path").(string)

Expand Down Expand Up @@ -137,7 +137,7 @@ func resourceZipFileUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceZipFileDelete(d *schema.ResourceData, meta interface{}) error {
func resourceArchiveFileDelete(d *schema.ResourceData, meta interface{}) error {
output_path := d.Get("output_path").(string)
fi, err := os.Stat(output_path)
if os.IsNotExist(err) {
Expand All @@ -151,7 +151,7 @@ func resourceZipFileDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}

func resourceZipFileExists(d *schema.ResourceData, meta interface{}) (bool, error) {
func resourceArchiveFileExists(d *schema.ResourceData, meta interface{}) (bool, error) {
output_path := d.Get("output_path").(string)
_, err := os.Stat(output_path)
if os.IsNotExist(err) {
Expand Down
92 changes: 92 additions & 0 deletions builtin/providers/archive/resource_archive_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package archive

import (
"fmt"
r "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"os"
"testing"
)

func TestAccArchiveFile_Basic(t *testing.T) {
var fileSize string
r.Test(t, r.TestCase{
Providers: testProviders,
CheckDestroy: r.ComposeTestCheckFunc(
testAccArchiveFileMissing("zip_file_acc_test.zip"),
),
Steps: []r.TestStep{
r.TestStep{
Config: testAccArchiveFileContentConfig,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
),
},
r.TestStep{
Config: testAccArchiveFileFileConfig,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
),
},
r.TestStep{
Config: testAccArchiveFileDirConfig,
Check: r.ComposeTestCheckFunc(
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
),
},
},
})
}

func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc {
return func(s *terraform.State) error {
*fileSize = ""
fi, err := os.Stat(filename)
if err != nil {
return err
}
*fileSize = fmt.Sprintf("%d", fi.Size())
return nil
}
}

func testAccArchiveFileMissing(filename string) r.TestCheckFunc {
return func(s *terraform.State) error {
_, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return fmt.Errorf("found file expected to be deleted: %s", filename)
}
}

var testAccArchiveFileContentConfig = `
resource "archive_file" "foo" {
type = "zip"
source_content = "This is some content"
source_content_filename = "content.txt"
output_path = "zip_file_acc_test.zip"
}
`

var testAccArchiveFileFileConfig = `
resource "archive_file" "foo" {
type = "zip"
source_file = "test-fixtures/test-file.txt"
output_path = "zip_file_acc_test.zip"
}
`

var testAccArchiveFileDirConfig = `
resource "archive_file" "foo" {
type = "zip"
source_dir = "test-fixtures/test-dir"
output_path = "zip_file_acc_test.zip"
}
`
1 change: 1 addition & 0 deletions builtin/providers/archive/test-fixtures/test-dir/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file 1
1 change: 1 addition & 0 deletions builtin/providers/archive/test-fixtures/test-dir/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file 2
1 change: 1 addition & 0 deletions builtin/providers/archive/test-fixtures/test-dir/file3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is file 3
1 change: 1 addition & 0 deletions builtin/providers/archive/test-fixtures/test-file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is test content
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zip
package archive

import (
"archive/zip"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
package zip
package archive

import (
"archive/zip"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func TestZipArchiver_Content(t *testing.T) {
dir, err := createTestDestination()
if err != nil {
t.Fatalf("could not prepare temporary files: %s", err)
}
defer dropTestDestination(dir)

zipfilepath := filepath.Join(dir, "content.zip")
zipfilepath := "archive-content.zip"
archiver := NewZipArchiver(zipfilepath)
if err := archiver.ArchiveContent([]byte("This is some content"), "content.txt"); err != nil {
t.Fatalf("unexpected error: %s", err)
Expand All @@ -27,18 +19,9 @@ func TestZipArchiver_Content(t *testing.T) {
}

func TestZipArchiver_File(t *testing.T) {
dir, err := createTestDestination()
if err != nil {
t.Fatalf("could not prepare temporary files: %s", err)
}
defer dropTestDestination(dir)

testfilename := filepath.Join(dir, "test-file.txt")
ioutil.WriteFile(testfilename, []byte("This is test content"), os.FileMode(0666))

zipfilepath := filepath.Join(dir, "content.zip")
zipfilepath := "archive-file.zip"
archiver := NewZipArchiver(zipfilepath)
if err := archiver.ArchiveFile(testfilename); err != nil {
if err := archiver.ArchiveFile("./test-fixtures/test-file.txt"); err != nil {
t.Fatalf("unexpected error: %s", err)
}

Expand All @@ -48,21 +31,9 @@ func TestZipArchiver_File(t *testing.T) {
}

func TestZipArchiver_Dir(t *testing.T) {
dir, err := createTestDestination()
if err != nil {
t.Fatalf("could not prepare temporary files: %s", err)
}
defer dropTestDestination(dir)

testdirpath := filepath.Join(dir, "contents")
os.MkdirAll(testdirpath, os.FileMode(0666))
ioutil.WriteFile(filepath.Join(testdirpath, "file1.txt"), []byte("This is file 1"), os.FileMode(0666))
ioutil.WriteFile(filepath.Join(testdirpath, "file2.txt"), []byte("This is file 2"), os.FileMode(0666))
ioutil.WriteFile(filepath.Join(testdirpath, "file3.txt"), []byte("This is file 3"), os.FileMode(0666))

zipfilepath := filepath.Join(dir, "content.zip")
zipfilepath := "archive-dir.zip"
archiver := NewZipArchiver(zipfilepath)
if err := archiver.ArchiveDir(testdirpath); err != nil {
if err := archiver.ArchiveDir("./test-fixtures/test-dir"); err != nil {
t.Fatalf("unexpected error: %s", err)
}

Expand All @@ -73,14 +44,6 @@ func TestZipArchiver_Dir(t *testing.T) {
})
}

func createTestDestination() (string, error) {
return ioutil.TempDir("./", "zip_archiver_")
}

func dropTestDestination(dir string) {
os.RemoveAll(dir)
}

func ensureContents(t *testing.T, zipfilepath string, wants map[string][]byte) {
r, err := zip.OpenReader(zipfilepath)
if err != nil {
Expand Down
Loading

0 comments on commit 143ecec

Please sign in to comment.