Skip to content

Commit

Permalink
Option accepts arm.ClientOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
magodo committed Jan 29, 2023
1 parent b4da68f commit c2096af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 35 deletions.
34 changes: 2 additions & 32 deletions azlist/azlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (

"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/resourcemanager/resourcegraph/armresourcegraph"
"github.com/magodo/armid"
"github.com/magodo/workerpool"
Expand Down Expand Up @@ -42,9 +40,9 @@ type Option struct {
// Required
SubscriptionId string
Cred azcore.TokenCredential
ClientOpt arm.ClientOptions

// Optional
Env string
Parallelism int
Recursive bool
IncludeManaged bool
Expand Down Expand Up @@ -81,39 +79,11 @@ func List(ctx context.Context, predicate string, opt Option) (*ListResult, error
if opt.Parallelism == 0 {
opt.Parallelism = runtime.NumCPU()
}
if opt.Env == "" {
opt.Env = "public"
}

log.Printf("[INFO] List for subscription %s via predicate %s (parallelism: %d | recursive %t | include managed %t)", opt.SubscriptionId, predicate, opt.Parallelism, opt.Recursive, opt.IncludeManaged)

var cloudCfg cloud.Configuration
switch strings.ToLower(opt.Env) {
case "public":
cloudCfg = cloud.AzurePublic
case "usgovernment":
cloudCfg = cloud.AzureGovernment
case "china":
cloudCfg = cloud.AzureChina
default:
return nil, fmt.Errorf("unknown environment specified: %q", opt.Env)
}

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

log.Printf("[INFO] New Client")
client, err := NewClient(opt.SubscriptionId, opt.Cred, clientOpt)
client, err := NewClient(opt.SubscriptionId, opt.Cred, opt.ClientOpt)
if err != nil {
return nil, fmt.Errorf("new client: %v", err)
}
Expand Down
19 changes: 16 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
Expand Down Expand Up @@ -140,11 +141,22 @@ func main() {
os.Setenv("AZURE_CLIENT_CERTIFICATE_PATH", v)
}

cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{
clientOpt := arm.ClientOptions{
ClientOptions: policy.ClientOptions{
Cloud: cloudCfg,
Telemetry: policy.TelemetryOptions{
ApplicationID: "azlist",
Disabled: false,
},
Logging: policy.LogOptions{
IncludeBody: true,
},
},
TenantID: os.Getenv("ARM_TENANT_ID"),
}

cred, err := azidentity.NewDefaultAzureCredential(&azidentity.DefaultAzureCredentialOptions{
ClientOptions: clientOpt.ClientOptions,
TenantID: os.Getenv("ARM_TENANT_ID"),
})
if err != nil {
return fmt.Errorf("failed to obtain a credential: %v", err)
Expand All @@ -153,7 +165,8 @@ func main() {
opt := azlist.Option{
SubscriptionId: flagSubscriptionId,
Cred: cred,
Env: flagEnvironment,
ClientOpt: clientOpt,

Parallelism: flagParallelism,
Recursive: flagRecursive,
IncludeManaged: flagIncludeManaged,
Expand Down

0 comments on commit c2096af

Please sign in to comment.