Skip to content

Commit

Permalink
Merge pull request #202 from aztfmod/env
Browse files Browse the repository at this point in the history
Add support to retrieve environment variable
  • Loading branch information
arnaudlh authored Nov 1, 2022
2 parents cfe853a + 660b76a commit 2e67e94
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
49 changes: 49 additions & 0 deletions azurecaf/data_environment_variable.go
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
}
3 changes: 3 additions & 0 deletions azurecaf/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ func Provider() *schema.Provider {
"azurecaf_naming_convention": resourceNamingConvention(),
"azurecaf_name": resourceName(),
},
DataSourcesMap: map[string]*schema.Resource{
"azurecaf_environment_variable": dataEnvironmentVariable(),
},
}
}
33 changes: 33 additions & 0 deletions docs/data-sources/azurecaf_environment_variable.md
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.

0 comments on commit 2e67e94

Please sign in to comment.