Skip to content

Commit

Permalink
code format and remove GenTFResources
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxu92 committed Oct 19, 2023
1 parent 793d642 commit f298c4f
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 170 deletions.
136 changes: 136 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ package main

import (
"fmt"
"os"
"os/exec"
"strings"

"github.com/Azure/aztfexport/internal/cfgfile"
"github.com/Azure/aztfexport/pkg/config"
"github.com/Azure/aztfexport/pkg/telemetry"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/gofrs/uuid"
"github.com/hashicorp/go-hclog"
"github.com/magodo/terraform-client-go/tfclient"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -178,6 +187,133 @@ func (flag FlagSet) DescribeCLI(mode string) string {
return "aztfexport " + strings.Join(args, " ")
}

func initTelemetryClient(subscriptionId string) telemetry.Client {
cfg, err := cfgfile.GetConfig()
if err != nil {
return telemetry.NewNullClient()
}
enabled, installId := cfg.TelemetryEnabled, cfg.InstallationId
if !enabled {
return telemetry.NewNullClient()
}
if installId == "" {
uuid, err := uuid.NewV4()
if err == nil {
installId = uuid.String()
} else {
installId = "undefined"
}
}

sessionId := "undefined"
if uuid, err := uuid.NewV4(); err == nil {
sessionId = uuid.String()
}
return telemetry.NewAppInsight(subscriptionId, installId, sessionId)
}

// buildAzureSDKCredAndClientOpt builds the Azure SDK credential and client option from multiple sources (i.e. environment variables, MSI, Azure CLI).
func buildAzureSDKCredAndClientOpt(fset FlagSet) (azcore.TokenCredential, *arm.ClientOptions, error) {
var cloudCfg cloud.Configuration
switch env := fset.flagEnv; strings.ToLower(env) {
case "public":
cloudCfg = cloud.AzurePublic
case "usgovernment":
cloudCfg = cloud.AzureGovernment
case "china":
cloudCfg = cloud.AzureChina
default:
return nil, nil, fmt.Errorf("unknown environment specified: %q", env)
}

// Maps the auth related environment variables used in the provider to what azidentity honors
if v, ok := os.LookupEnv("ARM_TENANT_ID"); ok {
// #nosec G104
os.Setenv("AZURE_TENANT_ID", v)
}
if v, ok := os.LookupEnv("ARM_CLIENT_ID"); ok {
// #nosec G104
os.Setenv("AZURE_CLIENT_ID", v)
}
if v, ok := os.LookupEnv("ARM_CLIENT_SECRET"); ok {
// #nosec G104
os.Setenv("AZURE_CLIENT_SECRET", v)
}
if v, ok := os.LookupEnv("ARM_CLIENT_CERTIFICATE_PATH"); ok {
// #nosec G104
os.Setenv("AZURE_CLIENT_CERTIFICATE_PATH", v)
}

clientOpt := &arm.ClientOptions{
ClientOptions: policy.ClientOptions{
Cloud: cloudCfg,
Telemetry: policy.TelemetryOptions{
ApplicationID: "aztfexport",
Disabled: false,
},
Logging: policy.LogOptions{
IncludeBody: true,
},
},
}

tenantId := os.Getenv("ARM_TENANT_ID")
var (
cred azcore.TokenCredential
err error
)
switch {
case fset.flagUseEnvironmentCred:
cred, err = azidentity.NewEnvironmentCredential(&azidentity.EnvironmentCredentialOptions{
ClientOptions: clientOpt.ClientOptions,
})
if err != nil {
return nil, nil, fmt.Errorf("failed to new Environment credential: %v", err)
}
return cred, clientOpt, nil
case fset.flagUseManagedIdentityCred:
cred, err = azidentity.NewManagedIdentityCredential(&azidentity.ManagedIdentityCredentialOptions{
ClientOptions: clientOpt.ClientOptions,
})
if err != nil {
return nil, nil, fmt.Errorf("failed to new Managed Identity credential: %v", err)
}
return cred, clientOpt, nil
case fset.flagUseAzureCLICred:
cred, err = azidentity.NewAzureCLICredential(&azidentity.AzureCLICredentialOptions{
TenantID: tenantId,
})
if err != nil {
return nil, nil, fmt.Errorf("failed to new Azure CLI credential: %v", err)
}
return cred, clientOpt, nil
case fset.flagUseOIDCCred:
cred, err = NewOidcCredential(&OidcCredentialOptions{
ClientOptions: clientOpt.ClientOptions,
TenantID: tenantId,
ClientID: os.Getenv("ARM_CLIENT_ID"),
RequestToken: fset.flagOIDCRequestToken,
RequestUrl: fset.flagOIDCRequestURL,
Token: fset.flagOIDCToken,
TokenFilePath: fset.flagOIDCTokenFilePath,
})
if err != nil {
return nil, nil, fmt.Errorf("failed to new OIDC credential: %v", err)
}
return cred, clientOpt, nil
default:
opt := &azidentity.DefaultAzureCredentialOptions{
ClientOptions: clientOpt.ClientOptions,
TenantID: tenantId,
}
cred, err := azidentity.NewDefaultAzureCredential(opt)
if err != nil {
return nil, nil, fmt.Errorf("failed to new Default credential: %v", err)
}
return cred, clientOpt, nil
}
}

func (f FlagSet) BuildCommonConfig() (config.CommonConfig, error) {
cred, clientOpt, err := buildAzureSDKCredAndClientOpt(f)
if err != nil {
Expand Down
11 changes: 1 addition & 10 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"
"strings"

"github.com/Azure/aztfexport/internal/resourceset"
"github.com/hashicorp/go-version"
tfjson "github.com/hashicorp/terraform-json"

Expand Down Expand Up @@ -260,7 +259,6 @@ func (meta *baseMeta) CleanTFState(ctx context.Context, addr string) {
func (meta *baseMeta) ParallelImport(ctx context.Context, items []*ImportItem) error {
meta.tc.Trace(telemetry.Info, "ParallelImport Enter")
defer meta.tc.Trace(telemetry.Info, "ParallelImport Leave")
// return nil

itemsCh := make(chan *ImportItem, len(items))
for _, item := range items {
Expand Down Expand Up @@ -589,7 +587,7 @@ func (meta *baseMeta) buildProviderConfig() string {
}

if meta.useAzAPI() {
_ = f.Body().AppendNewBlock("provider", []string{"azapi"}).Body()
f.Body().AppendNewBlock("provider", []string{"azapi"}).Body()
}
return string(f.Bytes())
}
Expand Down Expand Up @@ -1066,13 +1064,6 @@ func (meta *baseMeta) deinit_tf(ctx context.Context) error {
return nil
}

func (meta *baseMeta) GenTFResources(set *resourceset.AzureResourceSet) []resourceset.TFResource {
if meta.useAzAPI() {
return set.ToAzAPIResources()
}
return set.ToTFResources(meta.parallelism, meta.azureSDKCred, meta.azureSDKClientOpt)
}

func getModuleDir(modulePaths []string, moduleDir string) (string, error) {
// Ensure the module path is something called by the main module
// We are following the module source and recursively call the LoadModule below. This is valid since we only support local path modules.
Expand Down
27 changes: 16 additions & 11 deletions internal/meta/meta_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,23 @@ func (meta *MetaQuery) ListResource(ctx context.Context) (ImportList, error) {
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Populate resource set")
if err := rset.PopulateResource(); err != nil {
return nil, fmt.Errorf("tweaking single resources in the azure resource set: %v", err)
}
log.Printf("[DEBUG] Reduce resource set")
if err := rset.ReduceResource(); err != nil {
return nil, fmt.Errorf("tweaking across resources in the azure resource set: %v", err)
}
var rl []resourceset.TFResource
if meta.useAzAPI() {
log.Printf("[DEBUG] Azure Resource set map to TF resource set")
rl = rset.ToTFAzAPIResources()
} else {
log.Printf("[DEBUG] Populate resource set")
if err := rset.PopulateResource(); err != nil {
return nil, fmt.Errorf("tweaking single resources in the azure resource set: %v", err)
}
log.Printf("[DEBUG] Reduce resource set")
if err := rset.ReduceResource(); err != nil {
return nil, fmt.Errorf("tweaking across resources in the azure resource set: %v", err)
}

log.Printf("[DEBUG] Azure Resource set map to TF resource set")
// rl := rset.ToTFResources(meta.parallelism, meta.azureSDKCred, meta.azureSDKClientOpt)
rl := meta.GenTFResources(rset)
log.Printf("[DEBUG] Azure Resource set map to TF resource set")
rl = rset.ToTFAzureRMResources(meta.parallelism, meta.azureSDKCred, meta.azureSDKClientOpt)
}

var l ImportList
for i, res := range rl {
Expand Down
10 changes: 8 additions & 2 deletions internal/meta/meta_res.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ func (meta *MetaResource) ListResource(_ context.Context) (ImportList, error) {
},
}
log.Printf("[DEBUG] Azure Resource set map to TF resource set")
rl := meta.GenTFResources(resourceSet)

var rl []resourceset.TFResource
if meta.useAzAPI() {
rl = resourceSet.ToTFAzAPIResources()
} else {
rl = resourceSet.ToTFAzureRMResources(meta.parallelism, meta.azureSDKCred, meta.azureSDKClientOpt)
}

// This is to record known resource types. In case there is a known resource type and there comes another same typed resource,
// then we need to modify the resource name. Otherwise, there will be a resource address conflict.
Expand All @@ -79,7 +85,7 @@ func (meta *MetaResource) ListResource(_ context.Context) (ImportList, error) {

// Some special Azure resource is missing the essential property that is used by aztft to detect their TF resource type.
// In this case, users can use the `--type` option to manually specify the TF resource type.
if meta.ResourceType != "" {
if meta.ResourceType != "" && !meta.useAzAPI() {
if meta.AzureId.Equal(res.AzureId) {
tfid, err := aztft.QueryId(meta.AzureId.String(), meta.ResourceType,
&aztft.APIOption{
Expand Down
27 changes: 17 additions & 10 deletions internal/meta/meta_rg.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,24 @@ func (meta *MetaResourceGroup) ListResource(ctx context.Context) (ImportList, er
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Populate resource set")
if err := rset.PopulateResource(); err != nil {
return nil, fmt.Errorf("tweaking single resources in the azure resource set: %v", err)
}
log.Printf("[DEBUG] Reduce resource set")
if err := rset.ReduceResource(); err != nil {
return nil, fmt.Errorf("tweaking across resources in the azure resource set: %v", err)
}

log.Printf("[DEBUG] Azure Resource set map to TF resource set")
rl := meta.GenTFResources(rset)
var rl []resourceset.TFResource
if meta.useAzAPI() {
rl = rset.ToTFAzAPIResources()
} else {

log.Printf("[DEBUG] Populate resource set")
if err := rset.PopulateResource(); err != nil {
return nil, fmt.Errorf("tweaking single resources in the azure resource set: %v", err)
}
log.Printf("[DEBUG] Reduce resource set")
if err := rset.ReduceResource(); err != nil {
return nil, fmt.Errorf("tweaking across resources in the azure resource set: %v", err)
}

log.Printf("[DEBUG] Azure Resource set map to TF resource set")
rl = rset.ToTFAzureRMResources(meta.parallelism, meta.azureSDKCred, meta.azureSDKClientOpt)
}

var l ImportList
for i, res := range rl {
Expand Down
4 changes: 2 additions & 2 deletions internal/resourceset/azure_resource_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type PesudoResourceInfo struct {
TFId string
}

func (rset AzureResourceSet) ToTFResources(parallelism int, cred azcore.TokenCredential, clientOpt arm.ClientOptions) []TFResource {
func (rset AzureResourceSet) ToTFAzureRMResources(parallelism int, cred azcore.TokenCredential, clientOpt arm.ClientOptions) []TFResource {
tfresources := []TFResource{}

wp := workerpool.NewWorkPool(parallelism)
Expand Down Expand Up @@ -101,7 +101,7 @@ func (rset AzureResourceSet) ToTFResources(parallelism int, cred azcore.TokenCre
return tfresources
}

func (rset AzureResourceSet) ToAzAPIResources() (result []TFResource) {
func (rset AzureResourceSet) ToTFAzAPIResources() (result []TFResource) {
for _, res := range rset.Resources {
result = append(result, TFResource{
AzureId: res.Id,
Expand Down
Loading

0 comments on commit f298c4f

Please sign in to comment.