Skip to content

Commit

Permalink
Fixed loading of stack, env, block in data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
BSick7 committed Dec 3, 2020
1 parent 1fdbb3c commit 528eded
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 38 deletions.
21 changes: 21 additions & 0 deletions internal/provider/config_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package provider

import "github.com/hashicorp/terraform-plugin-go/tfprotov5/tftypes"

func stringFromConfig(config map[string]tftypes.Value, key string) string {
if config[key].IsNull() {
return ""
}
val := ""
config[key].As(&val)
return val
}

func boolFromConfig(config map[string]tftypes.Value, key string) bool {
if config[key].IsNull() {
return false
}
val := false
config[key].As(&val)
return val
}
38 changes: 13 additions & 25 deletions internal/provider/data_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"regexp"

"github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/tftypes"
"github.com/nullstone-io/terraform-provider-ns/ns"
Expand Down Expand Up @@ -120,37 +121,24 @@ func (d *dataConnection) Validate(ctx context.Context, config map[string]tftypes
}

func (d *dataConnection) Read(ctx context.Context, config map[string]tftypes.Value) (map[string]tftypes.Value, []*tfprotov5.Diagnostic, error) {
var (
name string
type_ string
optional bool
via string
)

if err := config["name"].As(&name); err != nil {
return nil, nil, err
}
if err := config["type"].As(&type_); err != nil {
return nil, nil, err
}
if err := config["optional"].As(&optional); err != nil {
return nil, nil, err
}
if err := config["via"].As(&via); err != nil {
return nil, nil, err
}
name := stringFromConfig(config, "name")
type_ := stringFromConfig(config, "type")
optional := boolFromConfig(config, "optional")
via := stringFromConfig(config, "via")

outputsValue := tftypes.NewValue(tftypes.Map{AttributeType: tftypes.String}, map[string]tftypes.Value{})
workspace := d.p.PlanConfig.GetConnection(name)
if workspace != "" {
stateFile, err := d.getStateFile(workspace)
if err != nil {
return nil, nil, fmt.Errorf("error retrieving workspace state file: %w", err)
}

outputsValue, err = stateFile.Outputs.ToProtov5()
if err != nil {
return nil, nil, err
if err != tfe.ErrResourceNotFound {
return nil, nil, fmt.Errorf("error retrieving workspace state file: %w", err)
}
} else {
outputsValue, err = stateFile.Outputs.ToProtov5()
if err != nil {
return nil, nil, err
}
}
}

Expand Down
20 changes: 9 additions & 11 deletions internal/provider/data_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,17 @@ func (d *dataWorkspace) Validate(ctx context.Context, config map[string]tftypes.
}

func (d *dataWorkspace) Read(ctx context.Context, config map[string]tftypes.Value) (map[string]tftypes.Value, []*tfprotov5.Diagnostic, error) {
stack := d.p.PlanConfig.Stack
env := d.p.PlanConfig.Env
block := d.p.PlanConfig.Block

// We have already validated these conversions, let's load up the values
if !config["stack"].IsNull() {
config["stack"].As(&stack)
stack := stringFromConfig(config, "stack")
if stack == "" {
stack = d.p.PlanConfig.Stack
}
if !config["env"].IsNull() {
config["env"].As(&env)
env := stringFromConfig(config, "env")
if env == "" {
env = d.p.PlanConfig.Env
}
if !config["block"].IsNull() {
config["block"].As(&block)
block := stringFromConfig(config, "block")
if block == "" {
block = d.p.PlanConfig.Block
}

tags := map[string]tftypes.Value{
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"

"github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/tftypes"
Expand Down Expand Up @@ -80,8 +81,6 @@ func (p *provider) Validate(ctx context.Context, config map[string]tftypes.Value
}

func (p *provider) Configure(ctx context.Context, config map[string]tftypes.Value) (diags []*tfprotov5.Diagnostic, err error) {


if !config["organization"].IsNull() {
// This is already checked in Validate, just cast it
config["organization"].As(&p.PlanConfig.Org)
Expand Down

0 comments on commit 528eded

Please sign in to comment.