in internal/meta/base_meta.go [139:317]
func NewBaseMeta(cfg config.CommonConfig) (*baseMeta, error) {
if cfg.Parallelism == 0 {
return nil, fmt.Errorf("Parallelism not set in the config")
}
if cfg.ProviderVersion != "" && cfg.DevProvider {
return nil, fmt.Errorf("ProviderVersion conflicts with DevProvider in the config")
}
if cfg.TFClient != nil && !cfg.HCLOnly {
return nil, fmt.Errorf("TFClient must be used together with HCLOnly")
}
// Determine the module directory and module address
var (
moduleAddr string
moduleDir = cfg.OutputDir
)
if cfg.ModulePath != "" {
modulePaths := strings.Split(cfg.ModulePath, ".")
// Resolve the Terraform module address
var segs []string
for _, moduleName := range modulePaths {
segs = append(segs, "module."+moduleName)
}
moduleAddr = strings.Join(segs, ".")
var err error
moduleDir, err = getModuleDir(modulePaths, cfg.OutputDir)
if err != nil {
return nil, err
}
}
// Construct Azure resources client
b := client.ClientBuilder{
Credential: cfg.AzureSDKCredential,
Opt: cfg.AzureSDKClientOption,
}
resClient, err := b.NewResourcesClient(cfg.SubscriptionId)
if err != nil {
return nil, fmt.Errorf("new resource client")
}
outputFileNames := cfg.OutputFileNames
if outputFileNames.TerraformFileName == "" {
outputFileNames.TerraformFileName = "terraform.tf"
}
if outputFileNames.ProviderFileName == "" {
outputFileNames.ProviderFileName = "provider.tf"
}
if outputFileNames.MainFileName == "" {
outputFileNames.MainFileName = "main.tf"
}
if outputFileNames.ImportBlockFileName == "" {
outputFileNames.ImportBlockFileName = "import.tf"
}
tc := cfg.TelemetryClient
if tc == nil {
tc = telemetry.NewNullClient()
}
if !cfg.DevProvider && cfg.ProviderVersion == "" {
switch cfg.ProviderName {
case "azurerm":
cfg.ProviderVersion = azurerm.ProviderSchemaInfo.Version
case "azapi":
cfg.ProviderVersion = azapi.ProviderSchemaInfo.Version
}
}
// Update provider config if not explicitly defined
providerConfig := cfg.ProviderConfig
if providerConfig == nil {
providerConfig = map[string]cty.Value{}
}
setIfNoExist := func(k string, v cty.Value) {
if _, ok := providerConfig[k]; !ok {
providerConfig[k] = v
}
}
// Update provider config for auth config
if cfg.SubscriptionId != "" {
setIfNoExist("subscription_id", cty.StringVal(cfg.SubscriptionId))
}
if v := cfg.AuthConfig.Environment; v != "" {
setIfNoExist("environment", cty.StringVal(v))
}
if v := cfg.AuthConfig.TenantID; v != "" {
setIfNoExist("tenant_id", cty.StringVal(v))
}
if len(cfg.AuthConfig.AuxiliaryTenantIDs) != 0 {
var tenantIds []cty.Value
for _, id := range cfg.AuthConfig.AuxiliaryTenantIDs {
tenantIds = append(tenantIds, cty.StringVal(id))
}
setIfNoExist("auxiliary_tenant_ids", cty.ListVal(tenantIds))
}
if v := cfg.AuthConfig.ClientID; v != "" {
setIfNoExist("client_id", cty.StringVal(v))
}
if v := cfg.AuthConfig.ClientSecret; v != "" {
setIfNoExist("client_secret", cty.StringVal(v))
}
if v := cfg.AuthConfig.ClientCertificateEncoded; v != "" {
setIfNoExist("client_certificate", cty.StringVal(v))
}
if v := cfg.AuthConfig.ClientCertificatePassword; v != "" {
setIfNoExist("client_certificate_password", cty.StringVal(v))
}
if v := cfg.AuthConfig.OIDCTokenRequestToken; v != "" {
setIfNoExist("oidc_request_token", cty.StringVal(v))
}
if v := cfg.AuthConfig.OIDCTokenRequestURL; v != "" {
setIfNoExist("oidc_request_url", cty.StringVal(v))
}
if v := cfg.AuthConfig.OIDCAssertionToken; v != "" {
setIfNoExist("oidc_token", cty.StringVal(v))
}
setIfNoExist("use_msi", cty.BoolVal(cfg.AuthConfig.UseManagedIdentity))
setIfNoExist("use_cli", cty.BoolVal(cfg.AuthConfig.UseAzureCLI))
setIfNoExist("use_oidc", cty.BoolVal(cfg.AuthConfig.UseOIDC))
// Update provider config for provider registration
switch cfg.ProviderName {
case "azurerm":
setIfNoExist("resource_provider_registrations", cty.StringVal("none"))
case "azapi":
setIfNoExist("skip_provider_registration", cty.BoolVal(true))
}
// Update the exclude pattern of azure resource ids
var excludeAzureResources []regexp.Regexp
for _, p := range cfg.ExcludeAzureResources {
re, err := regexp.Compile(fmt.Sprintf(`(?i)%s`, p))
if err != nil {
continue
}
excludeAzureResources = append(excludeAzureResources, *re)
}
meta := &baseMeta{
logger: cfg.Logger,
subscriptionId: cfg.SubscriptionId,
azureSDKCred: cfg.AzureSDKCredential,
azureSDKClientOpt: cfg.AzureSDKClientOption,
outdir: cfg.OutputDir,
outputFileNames: outputFileNames,
resourceClient: resClient,
providerVersion: cfg.ProviderVersion,
devProvider: cfg.DevProvider,
backendType: cfg.BackendType,
backendConfig: cfg.BackendConfig,
providerConfig: providerConfig,
providerName: cfg.ProviderName,
fullConfig: cfg.FullConfig,
maskSensitive: cfg.MaskSensitive,
parallelism: cfg.Parallelism,
preImportHook: cfg.PreImportHook,
postImportHook: cfg.PostImportHook,
generateImportFile: cfg.GenerateImportBlock,
hclOnly: cfg.HCLOnly,
tfclient: cfg.TFClient,
moduleAddr: moduleAddr,
moduleDir: moduleDir,
excludeAzureResources: excludeAzureResources,
excludeTerraformResources: cfg.ExcludeTerraformResources,
tc: tc,
}
return meta, nil
}