Skip to content

Commit 242f091

Browse files
tas50claude
andcommitted
✨ Add virtual network discovery to Azure provider
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e29a520 commit 242f091

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

providers/azure/config/config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Examples run in the Azure CLI:
5555
resources.DiscoveryKeyVaults,
5656
resources.DiscoverySecurityGroups,
5757
resources.DiscoveryCosmosDb,
58+
resources.DiscoveryVirtualNetworks,
5859
},
5960
Flags: []plugin.Flag{
6061
{
@@ -142,8 +143,9 @@ Examples run in the Azure CLI:
142143
"network": {
143144
Key: "object",
144145
Values: map[string]*inventory.AssetUrlBranch{
145-
"security-group": nil,
146-
"other": nil,
146+
"security-group": nil,
147+
"virtual-network": nil,
148+
"other": nil,
147149
},
148150
},
149151
"keyvault": nil,

providers/azure/resources/discovery.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const (
4545
DiscoveryKeyVaults = "keyvaults-vaults"
4646
DiscoverySecurityGroups = "security-groups"
4747
DiscoveryCosmosDb = "cosmosdb"
48+
DiscoveryVirtualNetworks = "virtual-networks"
4849
)
4950

5051
var All = []string{
@@ -74,6 +75,7 @@ var AllAPIResources = []string{
7475
DiscoveryKeyVaults,
7576
DiscoverySecurityGroups,
7677
DiscoveryCosmosDb,
78+
DiscoveryVirtualNetworks,
7779
}
7880

7981
type azureObject struct {
@@ -290,6 +292,14 @@ func Discover(runtime *plugin.Runtime, rootConf *inventory.Config) (*inventory.I
290292
assets = append(assets, cosmosDbAccounts...)
291293
}
292294

295+
if stringx.ContainsAnyOf(targets, DiscoveryVirtualNetworks) {
296+
vnets, err := discoverVirtualNetworks(runtime, subsWithConfigs)
297+
if err != nil {
298+
return nil, err
299+
}
300+
assets = append(assets, vnets...)
301+
}
302+
293303
return &inventory.Inventory{
294304
Spec: &inventory.InventorySpec{
295305
Assets: assets,
@@ -850,6 +860,40 @@ func discoverSecurityGroups(runtime *plugin.Runtime, subsWithConfigs []subWithCo
850860
return assets, nil
851861
}
852862

863+
func discoverVirtualNetworks(runtime *plugin.Runtime, subsWithConfigs []subWithConfig) ([]*inventory.Asset, error) {
864+
assets := []*inventory.Asset{}
865+
for _, subWithConfig := range subsWithConfigs {
866+
svc, err := NewResource(runtime, "azure.subscription.networkService", map[string]*llx.RawData{
867+
"subscriptionId": llx.StringDataPtr(subWithConfig.sub.SubscriptionID),
868+
})
869+
if err != nil {
870+
return nil, err
871+
}
872+
networkSvc := svc.(*mqlAzureSubscriptionNetworkService)
873+
vnets := networkSvc.GetVirtualNetworks()
874+
if vnets.Error != nil {
875+
return nil, vnets.Error
876+
}
877+
for _, vnet := range vnets.Data {
878+
v := vnet.(*mqlAzureSubscriptionNetworkServiceVirtualNetwork)
879+
asset := mqlObjectToAsset(mqlObject{
880+
name: v.Name.Data,
881+
labels: interfaceMapToStr(v.Tags.Data),
882+
azureObject: azureObject{
883+
id: v.Id.Data,
884+
subscription: *subWithConfig.sub.SubscriptionID,
885+
tenant: subWithConfig.sub.TenantID,
886+
location: v.Location.Data,
887+
service: "network",
888+
objectType: "virtual-network",
889+
},
890+
}, subWithConfig.conf, true)
891+
assets = append(assets, asset)
892+
}
893+
}
894+
return assets, nil
895+
}
896+
853897
func discoverVaults(runtime *plugin.Runtime, subsWithConfigs []subWithConfig) ([]*inventory.Asset, error) {
854898
assets := []*inventory.Asset{}
855899
for _, subWithConfig := range subsWithConfigs {
@@ -1048,6 +1092,9 @@ func getTitleFamily(azureObject azureObject) (azureObjectPlatformInfo, error) {
10481092
if azureObject.objectType == "security-group" {
10491093
return azureObjectPlatformInfo{title: "Azure Network Security Group", platform: "azure-network-security-group"}, nil
10501094
}
1095+
if azureObject.objectType == "virtual-network" {
1096+
return azureObjectPlatformInfo{title: "Azure Virtual Network", platform: "azure-virtual-network"}, nil
1097+
}
10511098
case "keyvault":
10521099
if azureObject.objectType == "vault" {
10531100
return azureObjectPlatformInfo{title: "Azure Key Vault", platform: "azure-keyvault-vault"}, nil

0 commit comments

Comments
 (0)