-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 24d04c0
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}, | ||
}) | ||
} |