-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1457 from azgcloudev/feature/396-support-localnet…
…workgateway create local network gateway resource
- Loading branch information
Showing
6 changed files
with
154 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 _, rg := range resourceGroups { | ||
pager := localNetworkGatewayClient.NewListPager( | ||
rg.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.Sprintf("https://portal.azure.com/#resource%s", *lng.ID), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
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,68 @@ | ||
package resourcegroup | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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/models" | ||
"github.com/tailwarden/komiser/providers" | ||
"time" | ||
) | ||
|
||
func ResourceGroups(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) { | ||
resources := make([]models.Resource, 0) | ||
|
||
resourceGroupClient, err := armresources.NewResourceGroupsClient( | ||
client.AzureClient.SubscriptionId, | ||
client.AzureClient.Credentials, | ||
&arm.ClientOptions{}, | ||
) | ||
|
||
if err != nil { | ||
return resources, err | ||
} | ||
|
||
pager := resourceGroupClient.NewListPager(nil) | ||
for pager.More() { | ||
page, err := pager.NextPage(ctx) | ||
if err != nil { | ||
return resources, err | ||
} | ||
|
||
for _, resourceGroup := range page.ResourceGroupListResult.Value { | ||
|
||
tags := make([]models.Tag, 0) | ||
|
||
for key, value := range resourceGroup.Tags { | ||
tags = append(tags, models.Tag{ | ||
Key: key, | ||
Value: *value, | ||
}) | ||
} | ||
|
||
resources = append(resources, models.Resource{ | ||
Provider: "Azure", | ||
Account: client.Name, | ||
Service: "Resource Group", | ||
Region: *resourceGroup.Location, | ||
ResourceId: *resourceGroup.ID, | ||
Cost: 0, | ||
Name: *resourceGroup.Name, | ||
FetchedAt: time.Now(), | ||
Tags: tags, | ||
Link: fmt.Sprintf("https://portal.azure.com/#resource%s", *resourceGroup.ID), | ||
}) | ||
} | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"provider": "Azure", | ||
"account": client.Name, | ||
"service": "Resource Group", | ||
"resources": len(resources), | ||
}).Info("Fetched resource groups") | ||
|
||
return resources, nil | ||
} |