Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rpatrick00 committed Oct 31, 2018
0 parents commit 24d04c0
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*.dll
*.exe
terraform.tfplan
terraform.tfstate
*.backup
./*.tfstate
.terraform/
*.log
*.bak
*~
.*.swp
.idea/
*.iml
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Environment Provider

The Environment provider is a utility provider for including local environment variables as part of a Terraform configuration. The provider itself requires no configuration.

## environment_variable Data Source

The environment_variable data source provides a mechanism for a Terraform configuration to read values of local environment variables and incorporate them into a Terraform configuration. This helps make it easier to make the Terraform script machine independent. For example, when configuring a managed Kubernetes cluster on AWS, the Kubernetes config file needs to be modified to allow Kubernetes commands to successfully connect and authenticate to the Kubernetes API server. The config file is typically written to `~/.kube/` directory, but this path does not work on Windows. By providing access to the HOME environment variable, it is possible to compute the path based on the user's home directory without assuming an operating system.

```hcl
provider "environment" {}
data "environment_variable" "HOME" {
name = "HOME"
failIfEmpty = true
}
provider "kubernetes" {
config_file = "${data.environment_variable.HOME.value}/.kube/my-cluster-config"
}
```

### Argument Reference
___
The following arguments are supported:

- `name` - (Required) The name of the environment variable whose value should be used.
- `default` - (Optional) The default value to use should the environment variable not be set or set to an empty string.
- `fail_if_empty` - (Optional) Whether or not the data source read should fail if the final value of the environment variable (after applying the `default`, if specified), if empty. The default value is `false`.

### Attributes
___
- `value` - The final value of the environment variable (after applying the `default`, if specified).
64 changes: 64 additions & 0 deletions environment/datasource-environment-variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package environment

import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
"os"
)

func dataSourceEnvironmentVariable() *schema.Resource {
return &schema.Resource{
Read: dataSourceEnvironmentVariableRead,

Schema: map[string]*schema.Schema {
"name": &schema.Schema {
Type: schema.TypeString,
Required: true,
},
"value": &schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"default": &schema.Schema {
Description: "The default value to return if the variable value is empty",
Type: schema.TypeString,
Optional: true,
Computed: false,
Default: "",
},
"fail_if_empty": &schema.Schema {
Description:
"If true, an error will be generated if the variable value and its default value are empty",
Type: schema.TypeBool,
Optional: true,
Computed: false,
Default: false,
},
},
}
}

func dataSourceEnvironmentVariableRead(d *schema.ResourceData, _ interface{}) error {
name := d.Get("name").(string)
if name != "" {
d.SetId(name)
value := os.Getenv(name)
if value == "" {
defaultValue := d.Get("default").(string)
log.Printf("[INFO] env var %#v default value = %#v", name, defaultValue)
failIfEmpty := d.Get("fail_if_empty").(bool)
if defaultValue == "" && failIfEmpty {
return fmt.Errorf("the environment variable %v value was empty", name)
} else {
d.Set("value", defaultValue)
}
} else {
log.Printf("[INFO] Setting %#v = %#v", name, value)
d.Set("value", value)
}
} else {
return fmt.Errorf("the environment variable name was not specified")
}
return nil
}
16 changes: 16 additions & 0 deletions environment/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package environment

import "github.com/hashicorp/terraform/helper/schema"

func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{},

ResourcesMap: map[string]*schema.Resource{},

DataSourcesMap: map[string]*schema.Resource{
"environment_variable": dataSourceEnvironmentVariable(),
},
}
}

15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
"github.com/rpatrick00/terraform-provider-environment/environment"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return environment.Provider()
},
})
}

0 comments on commit 24d04c0

Please sign in to comment.