github.com/gavinw2006/hashicorp-terraform@v0.11.12-beta1/configs/provider.go (about) 1 package configs 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/hcl2/gohcl" 7 "github.com/hashicorp/hcl2/hcl" 8 "github.com/hashicorp/hcl2/hcl/hclsyntax" 9 ) 10 11 // Provider represents a "provider" block in a module or file. A provider 12 // block is a provider configuration, and there can be zero or more 13 // configurations for each actual provider. 14 type Provider struct { 15 Name string 16 NameRange hcl.Range 17 Alias string 18 AliasRange *hcl.Range // nil if no alias set 19 20 Version VersionConstraint 21 22 Config hcl.Body 23 24 DeclRange hcl.Range 25 } 26 27 func decodeProviderBlock(block *hcl.Block) (*Provider, hcl.Diagnostics) { 28 content, config, diags := block.Body.PartialContent(providerBlockSchema) 29 30 provider := &Provider{ 31 Name: block.Labels[0], 32 NameRange: block.LabelRanges[0], 33 Config: config, 34 DeclRange: block.DefRange, 35 } 36 37 if attr, exists := content.Attributes["alias"]; exists { 38 valDiags := gohcl.DecodeExpression(attr.Expr, nil, &provider.Alias) 39 diags = append(diags, valDiags...) 40 provider.AliasRange = attr.Expr.Range().Ptr() 41 42 if !hclsyntax.ValidIdentifier(provider.Alias) { 43 diags = append(diags, &hcl.Diagnostic{ 44 Severity: hcl.DiagError, 45 Summary: "Invalid provider configuration alias", 46 Detail: fmt.Sprintf("An alias must be a valid name. %s", badIdentifierDetail), 47 }) 48 } 49 } 50 51 if attr, exists := content.Attributes["version"]; exists { 52 var versionDiags hcl.Diagnostics 53 provider.Version, versionDiags = decodeVersionConstraint(attr) 54 diags = append(diags, versionDiags...) 55 } 56 57 return provider, diags 58 } 59 60 func (p *Provider) moduleUniqueKey() string { 61 if p.Alias != "" { 62 return fmt.Sprintf("%s.%s", p.Name, p.Alias) 63 } 64 return p.Name 65 } 66 67 // ProviderRequirement represents a declaration of a dependency on a particular 68 // provider version without actually configuring that provider. This is used in 69 // child modules that expect a provider to be passed in from their parent. 70 type ProviderRequirement struct { 71 Name string 72 Requirement VersionConstraint 73 } 74 75 func decodeRequiredProvidersBlock(block *hcl.Block) ([]*ProviderRequirement, hcl.Diagnostics) { 76 attrs, diags := block.Body.JustAttributes() 77 var reqs []*ProviderRequirement 78 for name, attr := range attrs { 79 req, reqDiags := decodeVersionConstraint(attr) 80 diags = append(diags, reqDiags...) 81 if !diags.HasErrors() { 82 reqs = append(reqs, &ProviderRequirement{ 83 Name: name, 84 Requirement: req, 85 }) 86 } 87 } 88 return reqs, diags 89 } 90 91 var providerBlockSchema = &hcl.BodySchema{ 92 Attributes: []hcl.AttributeSchema{ 93 { 94 Name: "alias", 95 }, 96 { 97 Name: "version", 98 }, 99 }, 100 }