Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed several issues, added glob, tgz support, python zip, examples #272

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
*.dll
*.exe
*.zip
*.tar
*.tar.gz
*.tgz

.DS_Store
example.tf
terraform.tfplan
terraform.tfstate

.terraform/
terraform.tf*
.terraform.*
*.plan

bin/
modules-dev/
/pkg/
Expand All @@ -13,16 +22,13 @@ website/build
website/node_modules
.vagrant/
*.backup
./*.tfstate
.terraform/
*.log
*.bak
*~
.*.swp
.idea
.idea/
*.iml
*.test
*.iml

archive/*.zip
website/vendor
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ version it implements, and Terraform:

The provided `GNUmakefile` defines additional commands generally useful during development,
like for running tests, generating documentation, code formatting and linting.
Taking a look at it's content is recommended.
Taking a look at its content is recommended.

### Testing

Expand Down
154 changes: 137 additions & 17 deletions docs/data-sources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,163 @@ Generates an archive from content, a file, or directory of files.
## Example Usage

```terraform
# Archive a single file.
# Archive a single file to s3.

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.19.0"
}
}
}

data "archive_file" "init" {
data "archive_file" "example" {
type = "zip"
source_file = "${path.module}/init.tpl"
output_path = "${path.module}/files/init.zip"
output_path = "main.zip"
source_file = "main.py"
}

resource "aws_s3_bucket" "example" {
bucket = "bad-lambda-layer-bucket"
tags = {
"adsk:moniker" = "AMPS-C-UW2"
}
}

resource "aws_s3_object" "example" {
bucket = aws_s3_bucket.example.id
key = data.archive_file.example.output_path
source = data.archive_file.example.output_path
}
```

```terraform
# Archive multiple files and exclude file.
# Archive a file to be used with Lambda using consistent file mode

data "archive_file" "dotfiles" {
data "archive_file" "example" {
type = "zip"
output_path = "${path.module}/main.zip"
output_file_mode = "0666"
source_file = "${path.module}/main.py"
}
```

```terraform
# Archive multiple files from a template.

terraform {
required_providers {
template = {
source = "hashicorp/template"
version = "2.2.0"
}
}
}

data "template_file" "foo" {
template = file("${path.module}/foo.tpl")
vars = {
foo = "bar"
}
}

data "template_file" "hello" {
template = file("${path.module}/hello.tpl")
vars = {
hello = "world"
}
}

data "archive_file" "example" {
type = "zip"
output_path = "${path.module}/files/dotfiles.zip"
excludes = ["${path.module}/unwanted.zip"]
output_path = "${path.module}/example.zip"

source {
content = data.template_file.vimrc.rendered
filename = ".vimrc"
content = data.template_file.foo.rendered
filename = "foo.txt"
}

source {
content = data.template_file.ssh_config.rendered
filename = ".ssh/config"
content = data.template_file.hello.rendered
filename = "hello.txt"
}
}
```

```terraform
# Archive a file to be used with Lambda using consistent file mode
# Archive a single directory.

data "archive_file" "lambda_my_function" {
data "archive_file" "example" {
type = "zip"
source_file = "${path.module}/../lambda/my-function/index.js"
output_path = "${path.module}/main.zip"
source_dir = "${path.module}/dir"
output_file_mode = "0666"
output_path = "${path.module}/files/lambda-my-function.js.zip"
excludes = ["exclude.txt"]
}
```

```terraform
# Archive a single directory as tgz.

data "archive_file" "example" {
type = "tgz"
output_path = "${path.module}/main.tar.gz"
source_dir = "${path.module}/dir"
output_file_mode = "0400"
excludes = ["exclude.txt"]
}
```

```terraform
data "archive_file" "example" {
type = "zip"
output_path = "main.zip"
source_dir = "${path.module}/src"
}
```

```terraform
# Archive a single file.

data "archive_file" "example" {
type = "zip"
output_path = "${path.module}/main.zip"
output_file_mode = "0666"
source_file = "${path.module}/main.txt"
}
```

```terraform
# Archive content.

data "archive_file" "example" {
type = "zip"
output_path = "${path.module}/main.zip"
source_content_filename = "example.txt"
source_content = "example"
}
```

```terraform
# Archive content as tgz.

data "archive_file" "example" {
type = "tgz"
output_path = "${path.module}/main.tar.gz"
source_content_filename = "example.txt"
source_content = "example"
}
```

```terraform
# Archive a single file as tgz.

data "archive_file" "example" {
type = "tgz"
output_path = "${path.module}/main.tar.gz"
output_file_mode = "0400"
source_file = "${path.module}/main.txt"
}
```

Expand All @@ -58,7 +178,7 @@ data "archive_file" "lambda_my_function" {
### Required

- `output_path` (String) The output of the archive file.
- `type` (String) The type of archive to generate. NOTE: `zip` is supported.
- `type` (String) The type of archive to generate. NOTE: `zip, tgz` are supported.

### Optional

Expand Down
103 changes: 103 additions & 0 deletions docs/todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
## List of issues

https://github.com/hashicorp/terraform-provider-archive/issues

#### 149

https://github.com/hashicorp/terraform-provider-archive/issues/149
Issue archiving base64 encoded content w/ source block

As designed, the call to filebase64() returns encoded data which is written to the zip entry

#### 161

https://github.com/hashicorp/terraform-provider-archive/issues/161
archive_file doesn't re-create the archive upon content change

Basics seem to work. Need to investigate use of templatefile()

#### 173

https://github.com/hashicorp/terraform-provider-archive/issues/173
Generated archive contents include an extra (empty) file when output_path is configured within same directory as source_dir.

Fix by excluding output_path if inside source_dir.

#### 172

https://github.com/hashicorp/terraform-provider-archive/issues/172
Zip file created by terraform archive_file cannot be properly read by python

Fixed by including directories (along with files) in archive.
This will change the zip file output. i.e. file size and output sha
See TestResource_UpgradeFromVersion2_2_0_DirExcludesConfig

#### 221

https://github.com/hashicorp/terraform-provider-archive/issues/221
Error generated during the execution of acceptance test on archive_file resource

This was addressed.

#### 218

https://github.com/hashicorp/terraform-provider-archive/issues/218
archive_file data source gets created during "terraform plan" vs "terraform apply" and also is not deleted during destroy

This is by design.

#### 175

https://github.com/hashicorp/terraform-provider-archive/pull/175
Remove zip files that were generated as a result of a test.

This was addressed using t.TempDir()

#### https://github.com/hashicorp/terraform-provider-archive/pull/86

https://github.com/hashicorp/terraform-provider-archive/pull/86
Support glob matching for zip excludes

Added support in checkMatch() for filepath.Match()

#### 4

https://github.com/hashicorp/terraform-provider-archive/issues/4
gzip support for archive_file

https://github.com/hashicorp/terraform-provider-archive/issues/241
Support Additional Compression Types(Ex: tar.gz format)

https://github.com/hashicorp/terraform-provider-archive/pull/29
Support and array of compression formats
zip, tar, tar.gz, base64, tar.bz2, tar.xz, tar.lz4, tar.sz

Added support for tgz type.

* zip
* tgz

#### 2

https://github.com/hashicorp/terraform-provider-archive/issues/2
Feature request - add feature to add file to pre-existing archive in Archive provider

https://github.com/hashicorp/terraform/pull/9924

#### 64

https://github.com/hashicorp/terraform-provider-archive/issues/64
Documentation missing excludes

https://github.com/hashicorp/terraform-provider-archive/issues/35
Docs are missing exclude information

Addressed in https://registry.terraform.io/providers/hashicorp/archive/latest/docs

#### Professional thoughts on the future of this provider

https://github.com/hashicorp/terraform-provider-archive/pull/29#issuecomment-406760298

## opentofu

Port to opentofu here https://github.com/opentofu/terraform-provider-archive
1 change: 1 addition & 0 deletions examples/archive-file-s3/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello")
29 changes: 29 additions & 0 deletions examples/archive-file-s3/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Archive a single file to s3.

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.19.0"
}
}
}

data "archive_file" "example" {
type = "zip"
output_path = "main.zip"
source_file = "main.py"
}

resource "aws_s3_bucket" "example" {
bucket = "bad-lambda-layer-bucket"
tags = {
"adsk:moniker" = "AMPS-C-UW2"
}
}

resource "aws_s3_object" "example" {
bucket = aws_s3_bucket.example.id
key = data.archive_file.example.output_path
source = data.archive_file.example.output_path
}
7 changes: 0 additions & 7 deletions examples/data-sources/file/data-source.tf

This file was deleted.

8 changes: 0 additions & 8 deletions examples/data-sources/file/lambda.tf

This file was deleted.

Loading