-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from aztfmod/env
Add support to retrieve environment variable
- Loading branch information
Showing
3 changed files
with
85 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,49 @@ | ||
package azurecaf | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
) | ||
|
||
func dataEnvironmentVariable() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: resourceAction, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Name of the environment variable.", | ||
}, | ||
"fails_if_empty": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Description: "Throws an error if the environment variable is not set (default: false).", | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Value of the environment variable.", | ||
Sensitive: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAction(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
|
||
name := d.Get("name").(string) | ||
value := os.Getenv(name) | ||
|
||
if d.Get("fails_if_empty").(bool) { | ||
return diag.Errorf("Value is not set for environment variable: %s", name) | ||
} | ||
|
||
d.SetId(name) | ||
_ = d.Set("value", value) | ||
|
||
return diags | ||
} |
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
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,33 @@ | ||
# azurecaf_environment_variable | ||
|
||
The data source azurecaf_environment_variable retrieve an OS environment variable. | ||
|
||
## Exemple usage | ||
This example shows how to get the value of an environment variable. | ||
|
||
```hcl | ||
# Retrieve the PATH variable | ||
data "azurecaf_environment_variable" "path" { | ||
name = "PATH" | ||
} | ||
# Retrieve the PAT_TOKEN variable as a sensitive data and through an error if it does not exist. | ||
data "azurecaf_environment_variable" "PAT" { | ||
name = "PAT_TOKEN" | ||
fails_if_empty = true | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* name - (required) Name of the environment variable. | ||
* fails_if_empty (optional) - Throws an error if the environment variable is not set (default: false). | ||
|
||
# Attributes Reference | ||
The following attributes are exported: | ||
|
||
* id - The id of the environment variable | ||
* value - Value (sensitive) of the environment variable. | ||
|