Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional support for passing env variables into provider #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions sops/data_sops_external.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package sops

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"io/ioutil"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceExternal() *schema.Resource {
return &schema.Resource{
Read: dataSourceExternalRead,

ReadContext: dataSourceExternalRead,
Schema: map[string]*schema.Schema{
"input_type": {
Type: schema.TypeString,
Expand All @@ -37,16 +39,33 @@ func dataSourceExternal() *schema.Resource {
}
}

func dataSourceExternalRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceExternalRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get the environment variables from the provider configuration
envVars, ok := meta.(map[string]interface{})
if !ok {
return diag.Errorf("Unable to get provider configuration")
}
// Set the environment variables
for key, value := range envVars {
if strValue, ok := value.(string); ok {
os.Setenv(key, strValue)
}
}

source := d.Get("source").(string)
content, err := ioutil.ReadAll(strings.NewReader(source))
if err != nil {
return err
return diag.FromErr(err)
}

format := d.Get("input_type").(string)
if err := validateInputType(format); err != nil {
return err
return diag.FromErr(err)
}
return readData(content, format, d)

if err := readData(content, format, d); err != nil {
return diag.FromErr(err)
}

return nil
}
37 changes: 27 additions & 10 deletions sops/data_sops_file.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package sops

import (
"fmt"
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"io/ioutil"
"os"
"path"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceFile() *schema.Resource {
return &schema.Resource{
Read: dataSourceFileRead,

ReadContext: dataSourceFileRead,
Schema: map[string]*schema.Schema{
"input_type": {
Type: schema.TypeString,
Expand All @@ -38,16 +39,28 @@ func dataSourceFile() *schema.Resource {
}
}

func dataSourceFileRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceFileRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Get the environment variables from the provider configuration
envVars, ok := meta.(map[string]interface{})
if !ok {
return diag.Errorf("Unable to get provider configuration")
}
// Set the environment variables
for key, value := range envVars {
if strValue, ok := value.(string); ok {
os.Setenv(key, strValue)
}
}

sourceFile := d.Get("source_file").(string)
content, err := ioutil.ReadFile(sourceFile)
if err != nil {
return err
return diag.FromErr(err)
}

var format string
if input_type := d.Get("input_type").(string); input_type != "" {
format = input_type
if inputType := d.Get("input_type").(string); inputType != "" {
format = inputType
} else {
switch ext := path.Ext(sourceFile); ext {
case ".json":
Expand All @@ -59,13 +72,17 @@ func dataSourceFileRead(d *schema.ResourceData, meta interface{}) error {
case ".ini":
format = "ini"
default:
return fmt.Errorf("Don't know how to decode file with extension %s, set input_type to json, yaml or raw as appropriate", ext)
return diag.Errorf("don't know how to decode file with extension %s, set input_type to json, yaml or raw as appropriate", ext)
}
}

if err := validateInputType(format); err != nil {
return err
return diag.FromErr(err)
}

if err := readData(content, format, d); err != nil {
return diag.FromErr(err)
}

return readData(content, format, d)
return nil
}
15 changes: 15 additions & 0 deletions sops/provider.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
package sops

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"environment": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
DataSourcesMap: map[string]*schema.Resource{
"sops_file": dataSourceFile(),
"sops_external": dataSourceExternal(),
},
ConfigureContextFunc: providerConfigure,
}
}

func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
env := d.Get("environment").(map[string]interface{})
return env, nil
}