Azure / aztfexport

A tool to bring existing Azure resources under Terraform's management
https://azure.github.io/aztfexport/
Mozilla Public License 2.0
1.53k stars 174 forks source link

Changing the logger type to `slog.Logger` #534

Closed magodo closed 3 weeks ago

magodo commented 3 weeks ago

Previously, the global logger is defined as a custom logger interface and we set a hclog.Logger to it. This works fine except that it is less conventional now that slog is provided by the standard library.

This PR changes the global logger's type to slog.Logger. Another benefit of this change is that users of the aztfexport as a depending module can intuitively do OpenTelemetry logging as the logger interface provided by OTEL is also a slog.Logger.

This PR also updated the dependency of azlist to introduce its change for its logger type to be slog.Logger.

Result

BEFORE

2024-06-14T10:39:40.268+0800 [INFO]  aztfexport: aztfexport starts with config: config.Config{CommonConfig:config.CommonConfig{SubscriptionId:"****", AzureSDKCredential:(*azidentity.DefaultAzureCredential)(0xc003a40bc0), AzureSDKClientOption:policy.ClientOptions{ClientOptions:policy.ClientOptions{APIVersion:"", Cloud:cloud.Configuration{ActiveDirectoryAuthorityHost:"https://login.microsoftonline.com/", Services:map[cloud.ServiceName]cloud.ServiceConfiguration{"resourceManager":cloud.ServiceConfiguration{Audience:"https://management.core.windows.net/", Endpoint:"https://management.azure.com"}}}, Logging:policy.LogOptions{IncludeBody:true, AllowedHeaders:[]string(nil), AllowedQueryParams:[]string(nil)}, Retry:policy.RetryOptions{MaxRetries:0, TryTimeout:0, RetryDelay:0, MaxRetryDelay:0, StatusCodes:[]int(nil), ShouldRetry:(func(*http.Response, error) bool)(nil)}, Telemetry:policy.TelemetryOptions{ApplicationID:"aztfexport(azurerm)", Disabled:false}, TracingProvider:tracing.Provider{newTracerFn:(func(string, string) tracing.Tracer)(nil)}, Transport:exported.Transporter(nil), PerCallPolicies:[]exported.Policy(nil), PerRetryPolicies:[]exported.Policy(nil)}, AuxiliaryTenants:[]string(nil), DisableRPRegistration:false}, OutputDir:"/tmp/test", OutputFileNames:config.OutputFileNames{TerraformFileName:"", ProviderFileName:"", MainFileName:"", ImportBlockFileName:""}, ProviderVersion:"", DevProvider:true, ProviderName:"azurerm", ContinueOnError:false, BackendType:"local", BackendConfig:[]string(nil), ProviderConfig:map[string]cty.Value(nil), FullConfig:false, Parallelism:10, ModulePath:"", HCLOnly:false, TFClient:tfclient.Client(nil), TelemetryClient:telemetry.AppInsightClient{TelemetryClient:(*appinsights.telemetryClient)(0xc001d2dae0), subscriptionId:"****", installId:"fdc4c1a4-ecda-11ea-8e42-00155df0777c", sessionId:"b9b0d701-5416-4a56-bee4-9a6e9703103a"}, GenerateImportBlock:false}, ResourceId:"", ResourceGroupName:"zhwen-domain", ARGPredicate:"", MappingFile:"", ResourceNamePattern:"res-", RecursiveQuery:true, TFResourceName:"", TFResourceType:"", IncludeRoleAssignment:false, IncludeResourceGroup:false}
2024-06-14T10:39:40.268+0800 [INFO]  aztfexport: New resource group meta
2024-06-14T10:39:40.271+0800 [INFO]  aztfexport: Init Terraform
2024-06-14T10:39:40.307+0800 [INFO]  aztfexport: Find terraform binary at /home/magodo/.local/bin/terraform
2024-06-14T10:39:40.307+0800 [INFO]  aztfexport: Init provider
2024-06-14T10:39:40.307+0800 [INFO]  aztfexport: Output directory doesn't contain provider setting, create one then
2024-06-14T10:39:40.307+0800 [INFO]  aztfexport: Output directory doesn't contain terraform block, create one then
2024-06-14T10:39:41.803+0800 [INFO]  aztfexport: List for subscription **** via predicate resourceGroup =~ "zhwen-domain" (parallelism: 10 | recursive true | include managed false)
2024-06-14T10:39:41.803+0800 [INFO]  aztfexport: New Client
2024-06-14T10:39:41.803+0800 [INFO]  aztfexport: Listing tracked resources
2024-06-14T10:39:43.898+0800 [INFO]  aztfexport: Build ARM schema tree
2024-06-14T10:39:43.904+0800 [INFO]  aztfexport: Listing child resources
2024-06-14T10:39:45.107+0800 [INFO]  aztfexport: 4 resources are listed
2024-06-14T10:39:45.113+0800 [WARN]  aztfexport: No query result for resource type and TF id for /subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.DomainRegistration/domains/emoranran.com
2024-06-14T10:39:45.113+0800 [WARN]  aztfexport: No query result for resource type and TF id for /subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.Network/dnszones/emoranran.com/SOA/@
2024-06-14T10:39:45.299+0800 [INFO]  aztfexport: Skipping /subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.DomainRegistration/domains/emoranran.com
2024-06-14T10:39:45.299+0800 [INFO]  aztfexport: Importing /subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.Network/dnsZones/emoranran.com as azurerm_dns_zone.res-2
2024-06-14T10:39:45.299+0800 [INFO]  aztfexport: Skipping /subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.Network/dnszones/emoranran.com/SOA/@
2024-06-14T10:39:45.299+0800 [INFO]  aztfexport: Importing /subscriptions/****/resourceGroups/zhwen-domain as azurerm_resource_group.res-0
2024-06-14T10:39:45.299+0800 [INFO]  aztfexport: Importing /subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.Network/dnsZones/emoranran.com/NS/@ as azurerm_dns_ns_record.res-3
2024-06-14T10:39:50.834+0800 [INFO]  aztfexport: aztfexport ends

AFTER

time=2024-06-14T10:38:28.796+08:00 level=INFO msg="aztfexport starts" config="config.Config{CommonConfig:config.CommonConfig{SubscriptionId:\"****\", AzureSDKCredential:(*azidentity.DefaultAzureCredential)(0xc0000d0470), AzureSDKClientOption:policy.ClientOptions{ClientOptions:policy.ClientOptions{APIVersion:\"\", Cloud:cloud.Configuration{ActiveDirectoryAuthorityHost:\"https://login.microsoftonline.com/\", Services:map[cloud.ServiceName]cloud.ServiceConfiguration{\"resourceManager\":cloud.ServiceConfiguration{Audience:\"https://management.core.windows.net/\", Endpoint:\"https://management.azure.com\"}}}, Logging:policy.LogOptions{IncludeBody:true, AllowedHeaders:[]string(nil), AllowedQueryParams:[]string(nil)}, Retry:policy.RetryOptions{MaxRetries:0, TryTimeout:0, RetryDelay:0, MaxRetryDelay:0, StatusCodes:[]int(nil), ShouldRetry:(func(*http.Response, error) bool)(nil)}, Telemetry:policy.TelemetryOptions{ApplicationID:\"aztfexport(azurerm)\", Disabled:false}, TracingProvider:tracing.Provider{newTracerFn:(func(string, string) tracing.Tracer)(nil)}, Transport:exported.Transporter(nil), PerCallPolicies:[]exported.Policy(nil), PerRetryPolicies:[]exported.Policy(nil)}, AuxiliaryTenants:[]string(nil), DisableRPRegistration:false}, OutputDir:\"/tmp/test\", OutputFileNames:config.OutputFileNames{TerraformFileName:\"\", ProviderFileName:\"\", MainFileName:\"\", ImportBlockFileName:\"\"}, ProviderVersion:\"\", DevProvider:true, ProviderName:\"azurerm\", ContinueOnError:false, BackendType:\"local\", BackendConfig:[]string(nil), ProviderConfig:map[string]cty.Value(nil), FullConfig:false, Parallelism:10, ModulePath:\"\", HCLOnly:false, TFClient:tfclient.Client(nil), TelemetryClient:telemetry.AppInsightClient{TelemetryClient:(*appinsights.telemetryClient)(0xc00146fa80), subscriptionId:\"****\", installId:\"fdc4c1a4-ecda-11ea-8e42-00155df0777c\", sessionId:\"f375b25d-63b4-46b4-9f72-6ae3bef5cb60\"}, GenerateImportBlock:false}, ResourceId:\"\", ResourceGroupName:\"zhwen-domain\", ARGPredicate:\"\", MappingFile:\"\", ResourceNamePattern:\"res-\", RecursiveQuery:true, TFResourceName:\"\", TFResourceType:\"\", IncludeRoleAssignment:false, IncludeResourceGroup:false}"
time=2024-06-14T10:38:28.796+08:00 level=INFO msg="New resource group meta"
time=2024-06-14T10:38:28.800+08:00 level=INFO msg="Init Terraform"
time=2024-06-14T10:38:29.023+08:00 level=INFO msg="Found terraform binary" path=/home/magodo/.local/bin/terraform
time=2024-06-14T10:38:29.023+08:00 level=INFO msg="Init provider"
time=2024-06-14T10:38:29.023+08:00 level=INFO msg="Output directory doesn't contain provider setting, create one then"
time=2024-06-14T10:38:29.024+08:00 level=INFO msg="Output directory doesn't contain terraform block, create one then"
time=2024-06-14T10:38:31.138+08:00 level=INFO msg="List begins" subscription=**** predicate="resourceGroup =~ \"zhwen-domain\"" parallelism=10 recursive=true "include managed resources"=false
time=2024-06-14T10:38:31.138+08:00 level=INFO msg="New Client"
time=2024-06-14T10:38:31.138+08:00 level=INFO msg="Listing tracked resources"
time=2024-06-14T10:38:33.138+08:00 level=INFO msg="Build ARM schema tree"
time=2024-06-14T10:38:33.144+08:00 level=INFO msg="Listing child resources"
time=2024-06-14T10:38:34.553+08:00 level=INFO msg="List ends" "list count"=4
time=2024-06-14T10:38:34.560+08:00 level=WARN msg="No query result for resource type and TF id" id=/subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.DomainRegistration/domains/emoranran.com
time=2024-06-14T10:38:34.560+08:00 level=WARN msg="No query result for resource type and TF id" id=/subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.Network/dnszones/emoranran.com/SOA/@
time=2024-06-14T10:38:34.732+08:00 level=INFO msg="Skipping resource" tf_id=/subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.DomainRegistration/domains/emoranran.com
time=2024-06-14T10:38:34.732+08:00 level=INFO msg="Importing a resource" tf_id=/subscriptions/****/resourceGroups/zhwen-domain tf_addr=azurerm_resource_group.res-0
time=2024-06-14T10:38:34.732+08:00 level=INFO msg="Importing a resource" tf_id=/subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.Network/dnsZones/emoranran.com/NS/@ tf_addr=azurerm_dns_ns_record.res-3
time=2024-06-14T10:38:34.732+08:00 level=INFO msg="Skipping resource" tf_id=/subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.Network/dnszones/emoranran.com/SOA/@
time=2024-06-14T10:38:34.732+08:00 level=INFO msg="Importing a resource" tf_id=/subscriptions/****/resourceGroups/zhwen-domain/providers/Microsoft.Network/dnsZones/emoranran.com tf_addr=azurerm_dns_zone.res-2
time=2024-06-14T10:38:41.819+08:00 level=INFO msg="aztfexport ends"
magodo commented 3 weeks ago

gosec failure is pending once https://github.com/securego/gosec/issues/1156 is resolved.