-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
acceptance/scenarios/scenario_generate_complex_provider/enos.hcl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
3 changes: 3 additions & 0 deletions
3
acceptance/scenarios/scenario_generate_complex_provider/modules/kubernetes/main.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "random" { | ||
value = "notactuallyrandom" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(), | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
@@ -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) | ||
|
@@ -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"}), | ||
}, | ||
} | ||
|
||
|
@@ -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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
// given exist. | ||
sshVals := map[string]cty.Value{} | ||
for name, val := range ssh.AsValueMap() { | ||
// Only pass through known values | ||
|
@@ -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), | ||
}) | ||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/k8s/ssh/g