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

Add operating-system and join-path modules #3

Merged
merged 5 commits into from Mar 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/join-path/README.md
@@ -0,0 +1,16 @@
# Join path example

This folder shows examples of how to use the [join-path module](/modules/join-path) to create a file path with the
proper path separator for the current operating system.




## How do you run these examples?

1. Install [Terraform](https://www.terraform.io/).
1. `terraform init`.
1. `terraform apply`.



8 changes: 8 additions & 0 deletions examples/join-path/main.tf
@@ -0,0 +1,8 @@
module "path" {
# When using these modules in your own templates, you will need to use a Git URL with a ref attribute that pins you
# to a specific version of the modules, such as the following example:
# source = "git::git@github.com:gruntwork-io/package-terraform-utilities.git//modules/join-path?ref=v1.0.8"
source = "../../modules/join-path"

path_parts = ["foo", "bar", "baz.txt"]
}
3 changes: 3 additions & 0 deletions examples/join-path/outputs.tf
@@ -0,0 +1,3 @@
output "path" {
value = "${module.path.path}"
}
16 changes: 16 additions & 0 deletions examples/operating-system/README.md
@@ -0,0 +1,16 @@
# Operating system example

This folder shows examples of how to use the [operating-system module](/modules/operating-system) to access information
about the current operating system in Terraform.




## How do you run these examples?

1. Install [Terraform](https://www.terraform.io/).
1. `terraform init`.
1. `terraform apply`.



6 changes: 6 additions & 0 deletions examples/operating-system/main.tf
@@ -0,0 +1,6 @@
module "os" {
# When using these modules in your own templates, you will need to use a Git URL with a ref attribute that pins you
# to a specific version of the modules, such as the following example:
# source = "git::git@github.com:gruntwork-io/package-terraform-utilities.git//modules/operating-system?ref=v1.0.8"
source = "../../modules/operating-system"
}
7 changes: 7 additions & 0 deletions examples/operating-system/outputs.tf
@@ -0,0 +1,7 @@
output "os_name" {
value = "${module.os.name}"
}

output "path_separator" {
value = "${module.os.path_separator}"
}
39 changes: 39 additions & 0 deletions modules/join-path/README.md
@@ -0,0 +1,39 @@
# Join Path Module

This is a module that can be used to join a list of given path parts (that is, file and folder names) into a single
path with the appropriate path separator (backslash or forward slash) for the current operating system. This is useful
for ensuring the paths you build will work properly on Windows, Linux, and OS X.

This module uses Python under the hood so, the Python must be installed on the OS.




## Example code

See the [join-path example](/examples/join-path) for working sample code.




## Usage

Simply use the module in your Terraform code, replacing `<VERSION>` with the latest version from the [releases
page](https://github.com/gruntwork-io/package-terraform-utilities/releases), and specifying the path parts using the
`path_parts` input:

```hcl
module "path" {
source = "git::git@github.com:gruntwork-io/package-terraform-utilities.git//modules/join-path?ref=<VERSION>"

path_parts = ["foo", "bar", "baz.txt"]
}
```

You can now get the joined path using the `path` output:

```hcl
# Will be set to "foo/bar/baz.txt" on Linux and OS X, "foo\bar\baz.txt" on Windows
joined_path = "${module.path.path}"
```

4 changes: 4 additions & 0 deletions modules/join-path/main.tf
@@ -0,0 +1,4 @@
module "os" {
source = "../operating-system"
}

4 changes: 4 additions & 0 deletions modules/join-path/outputs.tf
@@ -0,0 +1,4 @@
output "path" {
value = "${join(module.os.path_separator, var.path_parts)}"
}

12 changes: 12 additions & 0 deletions modules/join-path/vars.tf
@@ -0,0 +1,12 @@
# ---------------------------------------------------------------------------------------------------------------------
# REQUIRED MODULE PARAMETERS
# These variables must be passed in by the operator.
# ---------------------------------------------------------------------------------------------------------------------

variable "path_parts" {
description = "A list of folder and file names to combine into a path, using the proper path separator for the current OS."
type = "list"
# Example:
# default = ["foo", "bar", "baz.txt"] => outputs "foo/bar/baz.txt" on Linux
}

40 changes: 40 additions & 0 deletions modules/operating-system/README.md
@@ -0,0 +1,40 @@
# Operating System Module

This is a module that can be used to figure out what operating system is being used to run Terraform. This may be used
to modify Terraform's behavior depending on the OS, such as modifying the way you format file paths on Linux vs
Windows (see also the [join-path module](/modules/join-path)).

This module uses Python under the hood so, the Python must be installed on the OS.




## Example code

See the [operating-system example](/examples/operating-system) for working sample code.




## Usage

Simply use the module in your Terraform code, replacing `<VERSION>` with the latest version from the [releases
page](https://github.com/gruntwork-io/package-terraform-utilities/releases):

```hcl
module "os" {
source = "git::git@github.com:gruntwork-io/package-terraform-utilities.git//modules/operating-system?ref=<VERSION>"
}
```

* You can now get the name of the operating system from the `name` output, which will be set to either `Linux`,
`Darwin`, or `Windows`

* You can also get the path separator for the current OS—backslash for Windows, forward slash everywhere else—from the
`path_separator` output.

```hcl
operating_system_name = "${module.os.name}"
path_separator = "${module.os.path_separator}"
```

3 changes: 3 additions & 0 deletions modules/operating-system/main.tf
@@ -0,0 +1,3 @@
data "external" "os" {
program = ["python", "-c", "import platform; print \"{\\\"platform\\\": \\\"%s\\\"}\" % platform.system()"]
}
7 changes: 7 additions & 0 deletions modules/operating-system/outputs.tf
@@ -0,0 +1,7 @@
output "name" {
value = "${data.external.os.result.platform}"
}

output "path_separator" {
value = "${data.external.os.result.platform == "Windows" ? "\\" : "/"}"
}
19 changes: 19 additions & 0 deletions test/join_path_test.go
@@ -0,0 +1,19 @@
package test

import (
"github.com/gruntwork-io/terratest"
"testing"
)

func TestJoinPath(t *testing.T) {
t.Parallel()

resourceCollection := createBaseRandomResourceCollection(t)
terratestOptions := createBaseTerratestOptions(t, "TestJoinPath", "../examples/join-path", resourceCollection)
defer terratest.Destroy(terratestOptions, resourceCollection)

apply(t, terratestOptions)

assertOutputEquals(t, "path", "foo/bar/baz.txt", terratestOptions)
}

21 changes: 21 additions & 0 deletions test/operating_system_test.go
@@ -0,0 +1,21 @@
package test

import (
"github.com/gruntwork-io/terratest"
"testing"
"runtime"
"strings"
)

func TestOperatingSystem(t *testing.T) {
t.Parallel()

resourceCollection := createBaseRandomResourceCollection(t)
terratestOptions := createBaseTerratestOptions(t, "TestOperatingSystem", "../examples/operating-system", resourceCollection)
defer terratest.Destroy(terratestOptions, resourceCollection)

apply(t, terratestOptions)

assertOutputEquals(t, "os_name", strings.Title(runtime.GOOS), terratestOptions)
assertOutputEquals(t, "path_separator", "/", terratestOptions)
}