Skip to content

Commit 65523fa

Browse files
BSick7stack72
authored andcommitted
provider/archive: Converting to datasource. (hashicorp#8492)
* Converting archive_file to datasource. * Ratcheting back new dir perms. * Ratcheting back new dir perms. * goimports * Adding output_base64sha256 attribute to archive_file. Updating docs. * Dropping CheckDestroy since this is a data source. * Correcting data source attribute checks.
1 parent f4a4962 commit 65523fa

5 files changed

Lines changed: 68 additions & 106 deletions

File tree

builtin/providers/archive/resource_archive_file.go renamed to builtin/providers/archive/data_source_archive_file.go

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package archive
22

33
import (
44
"crypto/sha1"
5+
"crypto/sha256"
6+
"encoding/base64"
57
"encoding/hex"
68
"fmt"
79
"io/ioutil"
@@ -11,13 +13,9 @@ import (
1113
"github.com/hashicorp/terraform/helper/schema"
1214
)
1315

14-
func resourceArchiveFile() *schema.Resource {
16+
func dataSourceFile() *schema.Resource {
1517
return &schema.Resource{
16-
Create: resourceArchiveFileCreate,
17-
Read: resourceArchiveFileRead,
18-
Update: resourceArchiveFileUpdate,
19-
Delete: resourceArchiveFileDelete,
20-
Exists: resourceArchiveFileExists,
18+
Read: dataSourceFileRead,
2119

2220
Schema: map[string]*schema.Schema{
2321
"type": &schema.Schema{
@@ -64,50 +62,56 @@ func resourceArchiveFile() *schema.Resource {
6462
ForceNew: true,
6563
Description: "SHA1 checksum of output file",
6664
},
65+
"output_base64sha256": &schema.Schema{
66+
Type: schema.TypeString,
67+
Computed: true,
68+
ForceNew: true,
69+
Description: "Base64 Encoded SHA256 checksum of output file",
70+
},
6771
},
6872
}
6973
}
7074

71-
func resourceArchiveFileCreate(d *schema.ResourceData, meta interface{}) error {
72-
if err := resourceArchiveFileUpdate(d, meta); err != nil {
75+
func dataSourceFileRead(d *schema.ResourceData, meta interface{}) error {
76+
outputPath := d.Get("output_path").(string)
77+
78+
outputDirectory := path.Dir(outputPath)
79+
if outputDirectory != "" {
80+
if _, err := os.Stat(outputDirectory); err != nil {
81+
if err := os.MkdirAll(outputDirectory, 0755); err != nil {
82+
return err
83+
}
84+
}
85+
}
86+
87+
if err := archive(d); err != nil {
7388
return err
7489
}
75-
return resourceArchiveFileRead(d, meta)
76-
}
7790

78-
func resourceArchiveFileRead(d *schema.ResourceData, meta interface{}) error {
79-
outputPath := d.Get("output_path").(string)
91+
// Generate archived file stats
8092
fi, err := os.Stat(outputPath)
81-
if os.IsNotExist(err) {
82-
d.SetId("")
83-
d.MarkNewResource()
84-
return nil
93+
if err != nil {
94+
return err
8595
}
8696

87-
sha, err := genFileSha1(outputPath)
97+
sha1, base64sha256, err := genFileShas(outputPath)
8898
if err != nil {
89-
return fmt.Errorf("could not generate file checksum sha: %s", err)
99+
100+
return fmt.Errorf("could not generate file checksum sha256: %s", err)
90101
}
91-
d.Set("output_sha", sha)
102+
d.Set("output_sha", sha1)
103+
d.Set("output_base64sha256", base64sha256)
104+
92105
d.Set("output_size", fi.Size())
93106
d.SetId(d.Get("output_sha").(string))
94107

95108
return nil
96109
}
97110

98-
func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
111+
func archive(d *schema.ResourceData) error {
99112
archiveType := d.Get("type").(string)
100113
outputPath := d.Get("output_path").(string)
101114

102-
outputDirectory := path.Dir(outputPath)
103-
if outputDirectory != "" {
104-
if _, err := os.Stat(outputDirectory); err != nil {
105-
if err := os.MkdirAll(outputDirectory, 0777); err != nil {
106-
return err
107-
}
108-
}
109-
}
110-
111115
archiver := getArchiver(archiveType, outputPath)
112116
if archiver == nil {
113117
return fmt.Errorf("archive type not supported: %s", archiveType)
@@ -129,55 +133,22 @@ func resourceArchiveFileUpdate(d *schema.ResourceData, meta interface{}) error {
129133
} else {
130134
return fmt.Errorf("one of 'source_dir', 'source_file', 'source_content_filename' must be specified")
131135
}
132-
133-
// Generate archived file stats
134-
fi, err := os.Stat(outputPath)
135-
if err != nil {
136-
return err
137-
}
138-
139-
sha, err := genFileSha1(outputPath)
140-
if err != nil {
141-
return fmt.Errorf("could not generate file checksum sha: %s", err)
142-
}
143-
d.Set("output_sha", sha)
144-
d.Set("output_size", fi.Size())
145-
d.SetId(d.Get("output_sha").(string))
146-
147-
return nil
148-
}
149-
150-
func resourceArchiveFileDelete(d *schema.ResourceData, meta interface{}) error {
151-
outputPath := d.Get("output_path").(string)
152-
if _, err := os.Stat(outputPath); os.IsNotExist(err) {
153-
return nil
154-
}
155-
156-
if err := os.Remove(outputPath); err != nil {
157-
return fmt.Errorf("could not delete zip file %q: %s", outputPath, err)
158-
}
159-
160136
return nil
161137
}
162138

163-
func resourceArchiveFileExists(d *schema.ResourceData, meta interface{}) (bool, error) {
164-
outputPath := d.Get("output_path").(string)
165-
_, err := os.Stat(outputPath)
166-
if os.IsNotExist(err) {
167-
return false, nil
168-
}
169-
if err != nil {
170-
return false, err
171-
}
172-
return true, nil
173-
}
174-
175-
func genFileSha1(filename string) (string, error) {
139+
func genFileShas(filename string) (string, string, error) {
176140
data, err := ioutil.ReadFile(filename)
177141
if err != nil {
178-
return "", fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
142+
return "", "", fmt.Errorf("could not compute file '%s' checksum: %s", filename, err)
179143
}
180144
h := sha1.New()
181145
h.Write([]byte(data))
182-
return hex.EncodeToString(h.Sum(nil)), nil
146+
sha1 := hex.EncodeToString(h.Sum(nil))
147+
148+
h256 := sha256.New()
149+
h256.Write([]byte(data))
150+
shaSum := h256.Sum(nil)
151+
sha256base64 := base64.StdEncoding.EncodeToString(shaSum[:])
152+
153+
return sha1, sha256base64, nil
183154
}

builtin/providers/archive/resource_archive_file_test.go renamed to builtin/providers/archive/data_source_archive_file_test.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,26 @@ func TestAccArchiveFile_Basic(t *testing.T) {
1313
var fileSize string
1414
r.Test(t, r.TestCase{
1515
Providers: testProviders,
16-
CheckDestroy: r.ComposeTestCheckFunc(
17-
testAccArchiveFileMissing("zip_file_acc_test.zip"),
18-
),
1916
Steps: []r.TestStep{
2017
r.TestStep{
2118
Config: testAccArchiveFileContentConfig,
2219
Check: r.ComposeTestCheckFunc(
2320
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
24-
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
21+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
2522
),
2623
},
2724
r.TestStep{
2825
Config: testAccArchiveFileFileConfig,
2926
Check: r.ComposeTestCheckFunc(
3027
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
31-
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
28+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
3229
),
3330
},
3431
r.TestStep{
3532
Config: testAccArchiveFileDirConfig,
3633
Check: r.ComposeTestCheckFunc(
3734
testAccArchiveFileExists("zip_file_acc_test.zip", &fileSize),
38-
r.TestCheckResourceAttrPtr("archive_file.foo", "output_size", &fileSize),
35+
r.TestCheckResourceAttrPtr("data.archive_file.foo", "output_size", &fileSize),
3936
),
4037
},
4138
r.TestStep{
@@ -60,21 +57,8 @@ func testAccArchiveFileExists(filename string, fileSize *string) r.TestCheckFunc
6057
}
6158
}
6259

63-
func testAccArchiveFileMissing(filename string) r.TestCheckFunc {
64-
return func(s *terraform.State) error {
65-
_, err := os.Stat(filename)
66-
if err != nil {
67-
if os.IsNotExist(err) {
68-
return nil
69-
}
70-
return err
71-
}
72-
return fmt.Errorf("found file expected to be deleted: %s", filename)
73-
}
74-
}
75-
7660
var testAccArchiveFileContentConfig = `
77-
resource "archive_file" "foo" {
61+
data "archive_file" "foo" {
7862
type = "zip"
7963
source_content = "This is some content"
8064
source_content_filename = "content.txt"
@@ -84,7 +68,7 @@ resource "archive_file" "foo" {
8468

8569
var tmpDir = os.TempDir() + "/test"
8670
var testAccArchiveFileOutputPath = fmt.Sprintf(`
87-
resource "archive_file" "foo" {
71+
data "archive_file" "foo" {
8872
type = "zip"
8973
source_content = "This is some content"
9074
source_content_filename = "content.txt"
@@ -93,15 +77,15 @@ resource "archive_file" "foo" {
9377
`, tmpDir)
9478

9579
var testAccArchiveFileFileConfig = `
96-
resource "archive_file" "foo" {
80+
data "archive_file" "foo" {
9781
type = "zip"
9882
source_file = "test-fixtures/test-file.txt"
9983
output_path = "zip_file_acc_test.zip"
10084
}
10185
`
10286

10387
var testAccArchiveFileDirConfig = `
104-
resource "archive_file" "foo" {
88+
data "archive_file" "foo" {
10589
type = "zip"
10690
source_dir = "test-fixtures/test-dir"
10791
output_path = "zip_file_acc_test.zip"

builtin/providers/archive/provider.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import (
77

88
func Provider() terraform.ResourceProvider {
99
return &schema.Provider{
10-
Schema: map[string]*schema.Schema{},
11-
10+
DataSourcesMap: map[string]*schema.Resource{
11+
"archive_file": dataSourceFile(),
12+
},
1213
ResourcesMap: map[string]*schema.Resource{
13-
"archive_file": resourceArchiveFile(),
14+
"archive_file": schema.DataSourceResourceShim(
15+
"archive_file",
16+
dataSourceFile(),
17+
),
1418
},
1519
}
1620
}

website/source/docs/providers/archive/r/file.html.md renamed to website/source/docs/providers/archive/d/archive_file.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: "archive"
33
page_title: "Archive: archive_file"
4-
sidebar_current: "docs-archive-resource-file"
4+
sidebar_current: "docs-archive-datasource-archive-file"
55
description: |-
66
Generates an archive from content, a file, or directory of files.
77
---
@@ -13,9 +13,9 @@ Generates an archive from content, a file, or directory of files.
1313
## Example Usage
1414

1515
```
16-
resource "archive_file" "init" {
17-
type = "zip"
18-
source_content_filename = "${path.module}/init.tpl"
16+
data "archive_file" "init" {
17+
type = "zip"
18+
source_file = "${path.module}/init.tpl"
1919
output_path = "${path.module}/files/init.zip"
2020
}
2121
```
@@ -44,4 +44,7 @@ NOTE: One of `source_content_filename` (with `source_content`), `source_file`, o
4444
The following attributes are exported:
4545

4646
* `output_size` - The size of the output archive file.
47+
4748
* `output_sha` - The SHA1 checksum of output archive file.
49+
50+
* `output_base64sha256` - The base64-encoded SHA256 checksum of output archive file.

website/source/layouts/archive.erb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
<a href="/docs/providers/archive/index.html">Archive Provider</a>
1111
</li>
1212

13-
<li<%= sidebar_current(/^docs-archive-resource/) %>>
14-
<a href="#">Resources</a>
13+
<li<%= sidebar_current(/^docs-archive-datasource/) %>>
14+
<a href="#">Data Sources</a>
1515
<ul class="nav nav-visible">
16-
<li<%= sidebar_current("docs-archive-resource-file") %>>
17-
<a href="/docs/providers/archive/r/file.html">archive_file</a>
16+
<li<%= sidebar_current("docs-archive-datasource-archive-file") %>>
17+
<a href="/docs/providers/archive/d/archive_file.html">archive_file</a>
1818
</li>
1919
</ul>
2020
</li>

0 commit comments

Comments
 (0)