From 07dd7b5c864bd5eca147f684535a8eb3a35b091e Mon Sep 17 00:00:00 2001 From: lolorol Date: Tue, 1 Nov 2022 00:14:51 +0800 Subject: [PATCH 1/6] Add support to retrieve environment variable --- azurecaf/data_environment_variable.go | 67 +++++++++++++++++++ azurecaf/provider.go | 3 + .../azurecaf_environment_variable.md | 36 ++++++++++ 3 files changed, 106 insertions(+) create mode 100644 azurecaf/data_environment_variable.go create mode 100644 docs/data-sources/azurecaf_environment_variable.md diff --git a/azurecaf/data_environment_variable.go b/azurecaf/data_environment_variable.go new file mode 100644 index 0000000..62d5a8c --- /dev/null +++ b/azurecaf/data_environment_variable.go @@ -0,0 +1,67 @@ +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: "Through an error if the environment variable is not set (default: false).", + }, + "sensitive": { + Type: schema.TypeBool, + Optional: true, + Default: false, + Description: "Do not display the value in the log is the value is sensitive (default: false).", + }, + "value": { + Type: schema.TypeString, + Computed: true, + Description: "Value of the environment variable.", + }, + "value_sensitive": { + Type: schema.TypeString, + Computed: true, + Sensitive: true, + Description: "Value (sensitive) of the environment variable.", + }, + }, + } +} + +func resourceAction(ctx context.Context, d *schema.ResourceData, m 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) + + if d.Get("sensitive").(bool) { + _ = d.Set("value_sensitive", value) + _ = d.Set("value", "") + } else { + _ = d.Set("value_sensitive", "") + _ = d.Set("value", value) + } + + return diags +} \ No newline at end of file diff --git a/azurecaf/provider.go b/azurecaf/provider.go index bd8e813..bf7ee44 100644 --- a/azurecaf/provider.go +++ b/azurecaf/provider.go @@ -13,5 +13,8 @@ func Provider() *schema.Provider { "azurecaf_naming_convention": resourceNamingConvention(), "azurecaf_name": resourceName(), }, + DataSourcesMap: map[string]*schema.Resource{ + "azurecaf_environment_variable": dataEnvironmentVariable(), + }, } } diff --git a/docs/data-sources/azurecaf_environment_variable.md b/docs/data-sources/azurecaf_environment_variable.md new file mode 100644 index 0000000..e6947e8 --- /dev/null +++ b/docs/data-sources/azurecaf_environment_variable.md @@ -0,0 +1,36 @@ +# 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" +} + +# Retreive 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 + sensitive = true +} +``` + +## Argument Reference + +The following arguments are supported: + +* name - (required) Name of the environment variable. +* fails_if_empty (optional) - Through an error if the environment variable is not set (default: false). +* sensitive (optional) - Do not display the value in the log is the value is sensitive (default: false). + +# Attributes Reference +The following attributes are exported: + +* id - The id of the environment variable +* value - Value of the environment variable. +* value_sensitive - Value (sensitive) of the environment variable. + From 5062d2620b2a3059897d720aa8c22c80992fdfab Mon Sep 17 00:00:00 2001 From: lolorol Date: Tue, 1 Nov 2022 09:23:19 +0800 Subject: [PATCH 2/6] Rename to meta --- azurecaf/data_environment_variable.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurecaf/data_environment_variable.go b/azurecaf/data_environment_variable.go index 62d5a8c..7f5b2b8 100644 --- a/azurecaf/data_environment_variable.go +++ b/azurecaf/data_environment_variable.go @@ -43,7 +43,7 @@ func dataEnvironmentVariable() *schema.Resource { } } -func resourceAction(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { +func resourceAction(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics name := d.Get("name").(string) From 67c310878fc944c2990a054324c10aa8f02e4071 Mon Sep 17 00:00:00 2001 From: lolorol Date: Tue, 1 Nov 2022 10:40:13 +0800 Subject: [PATCH 3/6] Make env variable sensitive only --- azurecaf/data_environment_variable.go | 20 +------------------ .../azurecaf_environment_variable.md | 5 +---- 2 files changed, 2 insertions(+), 23 deletions(-) diff --git a/azurecaf/data_environment_variable.go b/azurecaf/data_environment_variable.go index 7f5b2b8..563aebf 100644 --- a/azurecaf/data_environment_variable.go +++ b/azurecaf/data_environment_variable.go @@ -22,22 +22,11 @@ func dataEnvironmentVariable() *schema.Resource { Default: false, Description: "Through an error if the environment variable is not set (default: false).", }, - "sensitive": { - Type: schema.TypeBool, - Optional: true, - Default: false, - Description: "Do not display the value in the log is the value is sensitive (default: false).", - }, "value": { Type: schema.TypeString, Computed: true, Description: "Value of the environment variable.", - }, - "value_sensitive": { - Type: schema.TypeString, - Computed: true, Sensitive: true, - Description: "Value (sensitive) of the environment variable.", }, }, } @@ -54,14 +43,7 @@ func resourceAction(ctx context.Context, d *schema.ResourceData, meta interface{ } d.SetId(name) - - if d.Get("sensitive").(bool) { - _ = d.Set("value_sensitive", value) - _ = d.Set("value", "") - } else { - _ = d.Set("value_sensitive", "") - _ = d.Set("value", value) - } + _ = d.Set("value", value) return diags } \ No newline at end of file diff --git a/docs/data-sources/azurecaf_environment_variable.md b/docs/data-sources/azurecaf_environment_variable.md index e6947e8..369eb08 100644 --- a/docs/data-sources/azurecaf_environment_variable.md +++ b/docs/data-sources/azurecaf_environment_variable.md @@ -15,7 +15,6 @@ data "azurecaf_environment_variable" "path" { data "azurecaf_environment_variable" "PAT" { name = "PAT_TOKEN" fails_if_empty = true - sensitive = true } ``` @@ -25,12 +24,10 @@ The following arguments are supported: * name - (required) Name of the environment variable. * fails_if_empty (optional) - Through an error if the environment variable is not set (default: false). -* sensitive (optional) - Do not display the value in the log is the value is sensitive (default: false). # Attributes Reference The following attributes are exported: * id - The id of the environment variable -* value - Value of the environment variable. -* value_sensitive - Value (sensitive) of the environment variable. +* value - Value (sensitive) of the environment variable. From 582a7ed8adbfa14cb6b8a1ecc2ac1673ac6d51ef Mon Sep 17 00:00:00 2001 From: lolorol Date: Tue, 1 Nov 2022 03:52:13 +0100 Subject: [PATCH 4/6] Update docs/data-sources/azurecaf_environment_variable.md Co-authored-by: Arnaud Lheureux --- docs/data-sources/azurecaf_environment_variable.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/data-sources/azurecaf_environment_variable.md b/docs/data-sources/azurecaf_environment_variable.md index 369eb08..0f40aa9 100644 --- a/docs/data-sources/azurecaf_environment_variable.md +++ b/docs/data-sources/azurecaf_environment_variable.md @@ -23,7 +23,7 @@ data "azurecaf_environment_variable" "PAT" { The following arguments are supported: * name - (required) Name of the environment variable. -* fails_if_empty (optional) - Through an error if the environment variable is not set (default: false). +* fails_if_empty (optional) - Throws an error if the environment variable is not set (default: false). # Attributes Reference The following attributes are exported: From 54f84a880eaaeeb112854b6ccdd758f7a4ec7994 Mon Sep 17 00:00:00 2001 From: lolorol Date: Tue, 1 Nov 2022 03:52:22 +0100 Subject: [PATCH 5/6] Update docs/data-sources/azurecaf_environment_variable.md Co-authored-by: Arnaud Lheureux --- docs/data-sources/azurecaf_environment_variable.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/data-sources/azurecaf_environment_variable.md b/docs/data-sources/azurecaf_environment_variable.md index 0f40aa9..21a9068 100644 --- a/docs/data-sources/azurecaf_environment_variable.md +++ b/docs/data-sources/azurecaf_environment_variable.md @@ -11,7 +11,7 @@ data "azurecaf_environment_variable" "path" { name = "PATH" } -# Retreive the PAT_TOKEN variable as a sensitive data and through an error if it does not exist. +# 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 From bf74ec4d980e3c300935a7c620ff04a91547b5e2 Mon Sep 17 00:00:00 2001 From: lolorol Date: Tue, 1 Nov 2022 03:52:29 +0100 Subject: [PATCH 6/6] Update azurecaf/data_environment_variable.go Co-authored-by: Arnaud Lheureux --- azurecaf/data_environment_variable.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurecaf/data_environment_variable.go b/azurecaf/data_environment_variable.go index 563aebf..d4e6640 100644 --- a/azurecaf/data_environment_variable.go +++ b/azurecaf/data_environment_variable.go @@ -20,7 +20,7 @@ func dataEnvironmentVariable() *schema.Resource { Type: schema.TypeBool, Optional: true, Default: false, - Description: "Through an error if the environment variable is not set (default: false).", + Description: "Throws an error if the environment variable is not set (default: false).", }, "value": { Type: schema.TypeString,