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

[QTI-284] Allow complex provider configuration #66

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions acceptance/scenario_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ func TestAcc_Cmd_Scenario_Generate(t *testing.T) {
[][]string{{"skip", "keep"}},
fmt.Sprintf("%x", sha256.Sum256([]byte("path [skip:keep]"))),
},
{
"scenario_generate_complex_provider",
"kubernetes",
[][]string{},
fmt.Sprintf("%x", sha256.Sum256([]byte("kubernetes"))),
},
} {
t.Run(fmt.Sprintf("%s %s %s", test.dir, test.name, test.variants), func(t *testing.T) {
outDir := filepath.Join(tmpDir, test.dir)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
terraform "default" {
required_version = ">= 1.0.0"

required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
}
}
}

module "kubernetes" {
source = "./modules/kubernetes"
}

provider "kubernetes" "default" {
host = "http://example.com"
cluster_ca_certificate = "base64cert"
exec {
api_version = "client.authentication.k8s.io/v1alpha1"
args = ["eks", "get-token", "--cluster-name", "foo"]
command = "aws"
}
}

scenario "kubernetes" {
providers = [
provider.kubernetes.default
]

step "kubernetes" {
module = module.kubernetes
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "random" {
value = "notactuallyrandom"
}
2 changes: 1 addition & 1 deletion internal/flightplan/flightplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (fp *FlightPlan) decodeTerraformCLIs(ctx *hcl.EvalContext) hcl.Diagnostics
// top-level schema.
func (fp *FlightPlan) decodeProviders(ctx *hcl.EvalContext) hcl.Diagnostics {
var diags hcl.Diagnostics
// type -> map of aliases -> provider object
// provider type -> alias name -> provider object value
providers := map[string]map[string]cty.Value{}

for _, block := range fp.BodyContent.Blocks.OfType(blockTypeProvider) {
Expand Down
74 changes: 38 additions & 36 deletions internal/flightplan/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import (

// Provider is a Enos transport configuration
type Provider struct {
Type string `cty:"type"`
Alias string `cty:"alias"`
Attrs map[string]cty.Value `cty:"attrs"`
Type string `cty:"type"`
Alias string `cty:"alias"`
Config *SchemalessBlock `cty:"config"`
}

// NewProvider returns a new Provider
func NewProvider() *Provider {
return &Provider{
Attrs: map[string]cty.Value{},
Config: NewSchemalessBlock(),
}
}

Expand All @@ -36,51 +36,38 @@ func (p *Provider) decode(block *hcl.Block, ctx *hcl.EvalContext) hcl.Diagnostic

if p.Type == "enos" {
// Since we know the schema for the "enos" provider we can more fine
// grained decoding.
// grained decoding and validation.
moreDiags := p.decodeEnosProvider(block, ctx)
diags = diags.Extend(moreDiags)
if moreDiags.HasErrors() {
return diags
}
} else {
attrs, moreDiags := block.Body.JustAttributes()
// Decode the entire provider block as a schemaless block
moreDiags := p.Config.Decode(block, ctx)
diags = diags.Extend(moreDiags)
if moreDiags.HasErrors() {
return diags
}

for name, attr := range attrs {
val, moreDiags := attr.Expr.Value(ctx)
diags = diags.Extend(moreDiags)
if moreDiags.HasErrors() {
continue
}
p.Attrs[name] = val
}
}

return diags
}

// ToCtyValue returns the provider contents as an object cty.Value.
func (p *Provider) ToCtyValue() cty.Value {
vals := map[string]cty.Value{
"type": cty.StringVal(p.Type),
"alias": cty.StringVal(p.Alias),
}

if len(p.Attrs) > 0 {
vals["attrs"] = cty.ObjectVal(p.Attrs)
} else {
vals["attrs"] = cty.NullVal(cty.EmptyObject)
}

return cty.ObjectVal(vals)
return cty.ObjectVal(map[string]cty.Value{
"type": cty.StringVal(p.Type),
"alias": cty.StringVal(p.Alias),
"config": p.Config.ToCtyValue(),
})
}

// FromCtyValue takes a cty.Value and unmarshals it onto itself. It expects
// a valid object created from ToCtyValue()
func (p *Provider) FromCtyValue(val cty.Value) error {
var err error

if val.IsNull() {
return nil
}
Expand All @@ -105,13 +92,10 @@ func (p *Provider) FromCtyValue(val cty.Value) error {
return fmt.Errorf("provider alias must be a string ")
}
p.Alias = val.AsString()
case "attrs":
if !val.CanIterateElements() {
return fmt.Errorf("provider attrs must a map of attributes")
}

for k, v := range val.AsValueMap() {
p.Attrs[k] = v
case "config":
err = p.Config.FromCtyValue(val)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown key in value object: %s", key)
Expand Down Expand Up @@ -141,7 +125,17 @@ func (p *Provider) decodeEnosProvider(block *hcl.Block, ctx *hcl.EvalContext) hc
"user", "host", "private_key", "private_key_path",
"passphrase", "passphrase_path",
}),
}, []string{"ssh"}),
"kubernetes": cty.ObjectWithOptionalAttrs(map[string]cty.Type{
"kubeconfig_base64": cty.String,
"context_name": cty.String,
"namespace": cty.String,
"pod": cty.String,
"container": cty.String,
}, []string{
"kubeconfig_base64", "context_name", "namespace", "pod",
"container",
}),
}, []string{"ssh", "kubernetes"}),
},
}

Expand All @@ -164,15 +158,23 @@ func (p *Provider) decodeEnosProvider(block *hcl.Block, ctx *hcl.EvalContext) hc
return diags
}

// We should have either a valid k8s or ssh transport.
p.Config.Type = "provider"
p.Config.Labels = []string{p.Type, p.Alias}
p.Config.Attrs["transport"] = trans

ssh, ok := trans.AsValueMap()["ssh"]
if !ok {
// We're done, we're not going to do anything else with k8s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/k8s/ssh/g

return diags
}

if ssh.IsNull() || !ssh.IsWhollyKnown() || !ssh.CanIterateElements() {
return diags
}

// We have an ssh transport. Make sure any of the paths that we've been
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, just a question, Why do we validate the transport provider here? Isn't it enough that the enos-provider does that? If we change the transport we'll need to update this.

// given exist.
sshVals := map[string]cty.Value{}
for name, val := range ssh.AsValueMap() {
// Only pass through known values
Expand Down Expand Up @@ -224,7 +226,7 @@ func (p *Provider) decodeEnosProvider(block *hcl.Block, ctx *hcl.EvalContext) hc
}
}

p.Attrs["transport"] = cty.ObjectVal(map[string]cty.Value{
p.Config.Attrs["transport"] = cty.ObjectVal(map[string]cty.Value{
"ssh": cty.ObjectVal(sshVals),
})

Expand Down
Loading