Skip to content

Commit

Permalink
Allow provider organization configuration w/ env
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonc committed Jan 18, 2023
1 parent b588d3b commit 47f8003
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tfe/plugin_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tfe
import (
"context"
"fmt"
"os"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
Expand Down Expand Up @@ -77,6 +78,10 @@ func (p *pluginProviderServer) ConfigureProvider(ctx context.Context, req *tfpro
return resp, nil
}

if meta.organization == "" {
meta.organization = os.Getenv("TFE_ORGANIZATION")
}

p.tfeClient = client
p.organization = meta.organization
return resp, nil
Expand Down
3 changes: 3 additions & 0 deletions tfe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func Provider() *schema.Provider {
func configure() schema.ConfigureContextFunc {
return func(ctx context.Context, rd *schema.ResourceData) (any, diag.Diagnostics) {
providerOrganization := rd.Get("organization").(string)
if providerOrganization == "" {
providerOrganization = os.Getenv("TFE_ORGANIZATION")
}

client, err := configureClient(rd)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions tfe/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,38 @@ func testAccPreCheck(t *testing.T) {
}
}

func TestConfigureEnvOrganization(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
originalTFEOrganization := os.Getenv("TFE_ORGANIZATION")
reset := func() {
if originalTFEOrganization != "" {
os.Setenv("TFE_ORGANIZATION", originalTFEOrganization)
} else {
os.Unsetenv("TFE_ORGANIZATION")
}
}
defer reset()

expectedOrganization := fmt.Sprintf("tst-organization-%d", rInt)
os.Setenv("TFE_ORGANIZATION", expectedOrganization)

provider := Provider()

// The credentials must be provided by the CLI config file for testing.
if diags := provider.Configure(context.Background(), &terraform.ResourceConfig{}); diags.HasError() {
for _, d := range diags {
if d.Severity == diag.Error {
t.Fatalf("err: %s", d.Summary)
}
}
}

config := provider.Meta().(ConfiguredClient)
if config.Organization != expectedOrganization {
t.Fatalf("unexpected organization configuration: got %s, wanted %s", config.Organization, expectedOrganization)
}
}

func testAccGithubPreCheck(t *testing.T) {
if envGithubToken == "" {
t.Skip("Please set GITHUB_TOKEN to run this test")
Expand Down
1 change: 1 addition & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,4 @@ The following arguments are supported:
* `organization` - (Optional) The default organization that resources should
belong to. If provided, it's usually possible to omit resource-specific `organization`
arguments. Ensure that the organization already exists prior to using this argument.
This can also be specified using the `TFE_ORGANIZATION` environment variable.

0 comments on commit 47f8003

Please sign in to comment.