2022-08-06 14:21:18 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// providerConfig takes the list of providers in a TestCase and returns a
|
|
|
|
// config with only empty provider blocks. This is useful for Import, where no
|
|
|
|
// config is provided, but the providers must be defined.
|
2022-12-24 16:57:19 +00:00
|
|
|
func (c TestCase) providerConfig(_ context.Context, skipProviderBlock bool) string {
|
2022-08-06 14:21:18 +00:00
|
|
|
var providerBlocks, requiredProviderBlocks strings.Builder
|
|
|
|
|
|
|
|
// [BF] The Providers field handling predates the logic being moved to this
|
|
|
|
// method. It's not entirely clear to me at this time why this field
|
|
|
|
// is being used and not the others, but leaving it here just in case
|
|
|
|
// it does have a special purpose that wasn't being unit tested prior.
|
|
|
|
for name := range c.Providers {
|
|
|
|
providerBlocks.WriteString(fmt.Sprintf("provider %q {}\n", name))
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, externalProvider := range c.ExternalProviders {
|
2022-12-24 16:57:19 +00:00
|
|
|
if !skipProviderBlock {
|
|
|
|
providerBlocks.WriteString(fmt.Sprintf("provider %q {}\n", name))
|
|
|
|
}
|
2022-08-06 14:21:18 +00:00
|
|
|
|
|
|
|
if externalProvider.Source == "" && externalProvider.VersionConstraint == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
requiredProviderBlocks.WriteString(fmt.Sprintf(" %s = {\n", name))
|
|
|
|
|
|
|
|
if externalProvider.Source != "" {
|
|
|
|
requiredProviderBlocks.WriteString(fmt.Sprintf(" source = %q\n", externalProvider.Source))
|
|
|
|
}
|
|
|
|
|
|
|
|
if externalProvider.VersionConstraint != "" {
|
|
|
|
requiredProviderBlocks.WriteString(fmt.Sprintf(" version = %q\n", externalProvider.VersionConstraint))
|
|
|
|
}
|
|
|
|
|
|
|
|
requiredProviderBlocks.WriteString(" }\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
if requiredProviderBlocks.Len() > 0 {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
terraform {
|
|
|
|
required_providers {
|
|
|
|
%[1]s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
%[2]s
|
|
|
|
`, strings.TrimSuffix(requiredProviderBlocks.String(), "\n"), providerBlocks.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return providerBlocks.String()
|
|
|
|
}
|