Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions PROVIDERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,12 @@ Microsoft Azure can be integrated by using the following configuration block.
subscription_id: $AZURE_SUBSCRIPTION_ID
#use_cli_auth if set to true cloudlist will use azure cli auth
use_cli_auth: true
# service_concurrency limits Azure service fetches per subscription (default: 2)
service_concurrency: 2
```

`service_concurrency` accepts a positive integer. Set it to `1` for serial service fetching. It limits service-level overlap, not concurrency inside individual services; higher values can increase Azure API throttling.

`tenant_id`, `client_id`, `client_secret` can be obtained/generated from `All services` > `Azure Active Directory` > `App registrations`
`subscription_id` can be retrieved from `All services` > `Subscriptions`

Expand Down
320 changes: 131 additions & 189 deletions pkg/providers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azure
import (
"context"
"fmt"
"strconv"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions"
Expand All @@ -11,12 +12,13 @@ import (
)

const (
id = `id`
tenantID = `tenant_id`
clientID = `client_id`
clientSecret = `client_secret`
subscriptionID = `subscription_id` // optional
useCliAuth = `use_cli_auth`
id = `id`
tenantID = `tenant_id`
clientID = `client_id`
clientSecret = `client_secret`
subscriptionID = `subscription_id` // optional
useCliAuth = `use_cli_auth`
serviceConcurrencyKey = `service_concurrency`

providerName = "azure"
)
Expand All @@ -25,16 +27,21 @@ var Services = []string{"vm", "publicip", "trafficmanager", "cdn", "dns", "loadb

// Provider is a data provider for Azure API using Track 2 SDK
type Provider struct {
id string
SubscriptionIDs []string
Credential azcore.TokenCredential // Track 2: replaced autorest.Authorizer
services schema.ServiceMap
extendedMetadata bool
id string
SubscriptionIDs []string
Credential azcore.TokenCredential // Track 2: replaced autorest.Authorizer
services schema.ServiceMap
extendedMetadata bool
serviceConcurrency int
}

// New creates a new provider client for Azure API using Track 2 SDK
func New(options schema.OptionBlock) (*Provider, error) {
ID, _ := options.GetMetadata(id)
serviceConcurrency, err := serviceConcurrencyFromOptions(options)
if err != nil {
return nil, err
}

// Track 2: Create credential using new authentication layer
credential, err := createCredential(options)
Expand All @@ -46,9 +53,10 @@ func New(options schema.OptionBlock) (*Provider, error) {
services := options.ResolveServices(Services)

provider := &Provider{
Credential: credential, // Track 2: use credential instead of authorizer
id: ID,
services: services,
Credential: credential, // Track 2: use credential instead of authorizer
id: ID,
services: services,
serviceConcurrency: serviceConcurrency,
}
if extendedMetadata, ok := options.GetMetadata("extended_metadata"); ok {
provider.extendedMetadata = extendedMetadata == "true"
Expand Down Expand Up @@ -108,196 +116,130 @@ func (p *Provider) Services() []string {
return p.services.Keys()
}

// Resources returns the provider for an resource deployment source.
func (p *Provider) Resources(ctx context.Context) (*schema.Resources, error) {
resources := schema.NewResources()

// Process each subscription
for _, subscriptionID := range p.SubscriptionIDs {
if p.services.Has("vm") {
vmp := &vmProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
vmIPs, err := vmp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing VM public IPs for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(vmIPs)
}
}

if p.services.Has("publicip") {
publicIPp := &publicIPProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
publicIPs, err := publicIPp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing public IPs for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(publicIPs)
}
}

if p.services.Has("trafficmanager") {
trafficManagerp := &trafficManagerProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
trafficManager, err := trafficManagerp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing traffic manager for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(trafficManager)
}
}

if p.services.Has("cdn") {
cdnp := &cdnProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
cdn, err := cdnp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing CDN for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(cdn)
}
}
const azureServiceConcurrency = 2

if p.services.Has("dns") {
dnsp := &dnsProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
dnsRecords, err := dnsp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing DNS records for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(dnsRecords)
}
}

if p.services.Has("loadbalancer") {
lbp := &loadBalancerProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
loadBalancers, err := lbp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing load balancers for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(loadBalancers)
}
}

if p.services.Has("applicationgateway") {
agp := &applicationGatewayProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
appGateways, err := agp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing application gateways for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(appGateways)
}
}

if p.services.Has("aks") {
aksp := &aksProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
aksClusters, err := aksp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing AKS clusters for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(aksClusters)
}
}
func serviceConcurrencyFromOptions(options schema.OptionBlock) (int, error) {
if _, ok := options[serviceConcurrencyKey]; !ok {
return azureServiceConcurrency, nil
}
value, ok := options.GetMetadata(serviceConcurrencyKey)
if !ok {
return 0, fmt.Errorf("%s must be a positive integer", serviceConcurrencyKey)
}
limit, err := strconv.Atoi(value)
if err != nil || limit < 1 {
return 0, fmt.Errorf("%s must be a positive integer", serviceConcurrencyKey)
}
return limit, nil
}

if p.services.Has("storage") {
storagep := &storageProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
storageAccounts, err := storagep.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing storage accounts for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(storageAccounts)
}
}
type azureService struct {
name string
description string
fetch func(context.Context) (*schema.Resources, error)
}

if p.services.Has("containerinstances") {
containerInstancesp := &containerInstancesProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
containerInstances, err := containerInstancesp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing container instances for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(containerInstances)
}
}
type azureServiceResult struct {
index int
resources *schema.Resources
err error
}

if p.services.Has("appservice") {
appServicep := &appServiceProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
appService, err := appServicep.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing App Service for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(appService)
}
}
// Resources returns the provider for an resource deployment source.
func (p *Provider) Resources(ctx context.Context) (*schema.Resources, error) {
resources := schema.NewResources()
for _, subscriptionID := range p.SubscriptionIDs {
p.fetchSubscription(ctx, subscriptionID, resources)
}
return resources, nil
}

if p.services.Has("functions") {
functionsp := &functionsProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
functions, err := functionsp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing Functions for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(functions)
}
}
func (p *Provider) fetchSubscription(ctx context.Context, subscriptionID string, resources *schema.Resources) {
fetchAzureServices(ctx, subscriptionID, p.servicesForSubscription(subscriptionID), resources, p.serviceConcurrency)
}

if p.services.Has("apimanagement") {
apimgmtp := &apiManagementProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
apiManagement, err := apimgmtp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing API Management for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(apiManagement)
}
func (p *Provider) servicesForSubscription(subscriptionID string) []azureService {
services := []azureService{
{"vm", "VM public IPs", (&vmProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"publicip", "public IPs", (&publicIPProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"trafficmanager", "traffic manager", (&trafficManagerProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"cdn", "CDN", (&cdnProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"dns", "DNS records", (&dnsProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"loadbalancer", "load balancers", (&loadBalancerProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"applicationgateway", "application gateways", (&applicationGatewayProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"aks", "AKS clusters", (&aksProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"storage", "storage accounts", (&storageProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"containerinstances", "container instances", (&containerInstancesProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"appservice", "App Service", (&appServiceProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"functions", "Functions", (&functionsProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"apimanagement", "API Management", (&apiManagementProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"containerapps", "container apps", (&containerAppsProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"staticwebapps", "static web apps", (&staticWebAppsProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
{"frontdoor", "Front Door", (&frontDoorProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource},
}
dbp := &databaseProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
for _, fetcher := range dbp.fetchers() {
services = append(services, azureService{name: fetcher.service, description: fetcher.service, fetch: fetcher.fetch})
}
services = append(services, azureService{
name: "servicefabric",
description: "Service Fabric clusters",
fetch: (&serviceFabricProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}).GetResource,
})
enabled := make([]azureService, 0, len(services))
for _, service := range services {
if p.services.Has(service.name) {
enabled = append(enabled, service)
}
}
return enabled
}

if p.services.Has("containerapps") {
containerAppsp := &containerAppsProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
containerApps, err := containerAppsp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing container apps for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(containerApps)
}
}
func fetchAzureServices(ctx context.Context, subscriptionID string, services []azureService, resources *schema.Resources, serviceConcurrency int) {
if len(services) == 0 {
return
}

if p.services.Has("staticwebapps") {
staticWebAppsp := &staticWebAppsProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
staticWebApps, err := staticWebAppsp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing static web apps for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(staticWebApps)
}
}
limit := min(serviceConcurrency, len(services))
completed := make(chan azureServiceResult, limit)
nextStart := 0
running := 0
start := func() {
index := nextStart
nextStart++
running++
go func() {
fetched, err := services[index].fetch(ctx)
completed <- azureServiceResult{index: index, resources: fetched, err: err}
}()
}

if p.services.Has("frontdoor") {
fdp := &frontDoorProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
frontDoors, err := fdp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing Front Door for subscription %s: %s", subscriptionID, err)
} else {
resources.Merge(frontDoors)
}
for running < limit && nextStart < len(services) && ctx.Err() == nil {
start()
}
pending := make([]azureServiceResult, len(services))
ready := make([]bool, len(services))
nextMerge := 0
for running > 0 {
result := <-completed
running--
pending[result.index] = result
ready[result.index] = true
if nextStart < len(services) && ctx.Err() == nil {
start()
}

dbp := &databaseProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
for _, fetcher := range dbp.fetchers() {
if !p.services.Has(fetcher.service) {
continue
}
databases, err := fetcher.fetch(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing %s for subscription %s: %s", fetcher.service, subscriptionID, err)
for nextMerge < len(services) && ready[nextMerge] {
service := services[nextMerge]
result := pending[nextMerge]
if result.err != nil {
gologger.Warning().Msgf("Error listing %s for subscription %s: %s", service.description, subscriptionID, result.err)
} else {
resources.Merge(databases)
resources.Merge(result.resources)
}
}

if p.services.Has("servicefabric") {
sfp := &serviceFabricProvider{Credential: p.Credential, SubscriptionID: subscriptionID, id: p.id, extendedMetadata: p.extendedMetadata}
clusters, err := sfp.GetResource(ctx)
if err != nil {
gologger.Warning().Msgf("Error listing Service Fabric clusters for subscription %s: %s", subscriptionID, err)
continue
}
resources.Merge(clusters)
pending[nextMerge] = azureServiceResult{}
nextMerge++
}
}
return resources, nil
}

// Verify checks if the provider is valid using simple API call with Track 2 SDK
Expand Down
Loading
Loading