-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create local network gateway resource with resource group
- Loading branch information
Aldair Zamora
committed
Jul 6, 2024
1 parent
f1b149a
commit f52f0cf
Showing
6 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package networking | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/tailwarden/komiser/providers/azure/resourcegroup" | ||
"time" | ||
|
||
"github.com/tailwarden/komiser/models" | ||
"github.com/tailwarden/komiser/providers" | ||
) | ||
|
||
func LocalNetworkGateways(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { | ||
resources := make([]models.Resource, 0) | ||
resourceGroups, err := resourcegroup.ResourceGroups(ctx, client) | ||
if err != nil { | ||
return resources, err | ||
} | ||
|
||
if len(resourceGroups) < 1 { | ||
return resources, nil | ||
} | ||
|
||
localNetworkGatewayClient, err := armnetwork.NewLocalNetworkGatewaysClient( | ||
client.AzureClient.SubscriptionId, | ||
client.AzureClient.Credentials, | ||
&arm.ClientOptions{}, | ||
) | ||
if err != nil { | ||
return resources, err | ||
} | ||
|
||
// check resources on each resource group | ||
for _, name := range resourceGroups { | ||
pager := localNetworkGatewayClient.NewListPager( | ||
name, nil) | ||
for pager.More() { | ||
page, err := pager.NextPage(ctx) | ||
if err != nil { | ||
return resources, err | ||
} | ||
|
||
for _, lng := range page.LocalNetworkGatewayListResult.Value { | ||
tags := make([]models.Tag, 0) | ||
|
||
for key, value := range lng.Tags { | ||
tags = append(tags, models.Tag{ | ||
Key: key, | ||
Value: *value, | ||
}) | ||
} | ||
|
||
resources = append(resources, models.Resource{ | ||
Provider: "Azure", | ||
Account: client.Name, | ||
Service: "Local Network Gateway", | ||
Region: *lng.Location, | ||
ResourceId: *lng.ID, | ||
Cost: 0, | ||
Name: *lng.Name, | ||
FetchedAt: time.Now(), | ||
Tags: tags, | ||
Link: fmt.Sprint("https://portal.azure.com/#resource%s", *lng.ID), | ||
Check failure on line 66 in providers/azure/networking/local_network_gateways.go GitHub Actions / build_test_cli
|
||
}) | ||
} | ||
} | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"provider": "Azure", | ||
"account": client.Name, | ||
"service": "Local Network Gateway", | ||
"resources": len(resources), | ||
}).Info("Fetched resources") | ||
|
||
return resources, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package resourcegroup | ||
|
||
import ( | ||
"context" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" | ||
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/tailwarden/komiser/providers" | ||
) | ||
|
||
func ResourceGroups(ctx context.Context, client providers.ProviderClient) ([]string, error) { | ||
resourceGroupNames := make([]string, 0) | ||
|
||
resourceGroupClient, err := armresources.NewResourceGroupsClient( | ||
client.AzureClient.SubscriptionId, | ||
client.AzureClient.Credentials, | ||
&arm.ClientOptions{}, | ||
) | ||
|
||
if err != nil { | ||
return resourceGroupNames, err | ||
} | ||
|
||
pager := resourceGroupClient.NewListPager(nil) | ||
for pager.More() { | ||
page, err := pager.NextPage(ctx) | ||
if err != nil { | ||
return resourceGroupNames, err | ||
} | ||
|
||
for _, resourceGroup := range page.ResourceGroupListResult.Value { | ||
|
||
resourceGroupNames = append(resourceGroupNames, *resourceGroup.Name) | ||
} | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"provider": "Azure", | ||
"account": client.Name, | ||
"service": "Resource Group", | ||
"resources": len(resourceGroupNames), | ||
}).Info("Fetched resource groups") | ||
|
||
return resourceGroupNames, nil | ||
} |