Skip to content

Commit

Permalink
add azure managementGroup functions
Browse files Browse the repository at this point in the history
Signed-off-by: Markus Blaschke <[email protected]>
  • Loading branch information
mblaschke committed Dec 12, 2023
1 parent 60e6f87 commit b79ae7c
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 103 deletions.
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,19 @@ Arguments:

:information_source: Functions can also be used starting with `azure` prefix instead of `az`

| Function | Parameters | Description |
|-----------------------------------------|-----------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `azAccountInfo` | | Output of `az account show` |
| `azSubscription` | `subscriptionID` (string, optional) | Fetches Azure subscription (current selected one if `subscriptionID` is empty) |
| `azSubscriptionList` | | Fetches list of all visible azure subscriptions |
| `azResource` | `resourceID` (string), `apiVersion` (string) | Fetches Azure resource information (json representation, interface object) |
| `azResourceList` | `scope` (string), `filter` (string, optional) | Fetches list of Azure resources and filters it by using [$filter](https://learn.microsoft.com/en-us/rest/api/resources/resources/list), scope can be subscription ID or resourceGroup ID (array, json representation, interface object) |
| `azPublicIpAddress` | `resourceID` (string) | Fetches ip address from Azure Public IP |
| `azPublicIpPrefixAddressPrefix` | `resourceID` (string) | Fetches ip address prefix from Azure Public IP prefix |
| `azVirtualNetworkAddressPrefixes` | `resourceID` (string) | Fetches address prefix (string array) from Azure VirtualNetwork |
| `azVirtualNetworkSubnetAddressPrefixes` | `resourceID` (string), `subnetName` (string) | Fetches address prefix (string array) from Azure VirtualNetwork subnet |
| Function | Parameters | Description |
|------------------------------------------|-----------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `azAccountInfo` | | Output of `az account show` |
| `azManagementGroup` | `groupID` (string) | Fetches Azure managementGroup |
| `azManagementGroupSubscriptionList` | `groupID` (string) | Fetches list of all subscriptions (recursive) inside an Azure managementGroup |
| `azSubscription` | `subscriptionID` (string, optional) | Fetches Azure subscription (current selected one if `subscriptionID` is empty) |
| `azSubscriptionList` | | Fetches list of all visible azure subscriptions |
| `azResource` | `resourceID` (string), `apiVersion` (string) | Fetches Azure resource information (json representation, interface object) |
| `azResourceList` | `scope` (string), `filter` (string, optional) | Fetches list of Azure resources and filters it by using [$filter](https://learn.microsoft.com/en-us/rest/api/resources/resources/list), scope can be subscription ID or resourceGroup ID (array, json representation, interface object) |
| `azPublicIpAddress` | `resourceID` (string) | Fetches ip address from Azure Public IP |
| `azPublicIpPrefixAddressPrefix` | `resourceID` (string) | Fetches ip address prefix from Azure Public IP prefix |
| `azVirtualNetworkAddressPrefixes` | `resourceID` (string) | Fetches address prefix (string array) from Azure VirtualNetwork |
| `azVirtualNetworkSubnetAddressPrefixes` | `resourceID` (string), `subnetName` (string) | Fetches address prefix (string array) from Azure VirtualNetwork subnet |

### Azure Keyvault functions
| Function | Parameters | Description |
Expand Down
66 changes: 66 additions & 0 deletions azuretpl/azure.mgmtgroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package azuretpl

import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups"
"github.com/webdevops/go-common/utils/to"
)

// azManagementGroup fetches Azure ManagementGroup
func (e *AzureTemplateExecutor) azManagementGroup(groupID string) (interface{}, error) {
e.logger.Infof(`fetching Azure ManagementGroup '%v'`, groupID)

if val, enabled := e.lintResult(); enabled {
return val, nil
}

cacheKey := generateCacheKey(`azManagementGroup`, groupID)
return e.cacheResult(cacheKey, func() (interface{}, error) {
client, err := armmanagementgroups.NewClient(e.azureClient().GetCred(), e.azureClient().NewArmClientOptions())
if err != nil {
return nil, fmt.Errorf(`failed to create ManagementGroup "%v": %w`, groupID, err)
}

managementGroup, err := client.Get(e.ctx, groupID, nil)
if err != nil {
return nil, fmt.Errorf(`failed to fetch ManagementGroup "%v": %w`, groupID, err)
}

return transformToInterface(managementGroup)
})
}

// azManagementGroupSubscriptionList fetches list of Azure Subscriptions under Azure ManagementGroup
func (e *AzureTemplateExecutor) azManagementGroupSubscriptionList(groupID string) (interface{}, error) {
e.logger.Infof(`fetching subscriptions from Azure ManagementGroup '%v'`, groupID)

if val, enabled := e.lintResult(); enabled {
return val, nil
}

cacheKey := generateCacheKey(`azManagementGroupSubscriptionList`, groupID)
return e.cacheResult(cacheKey, func() (interface{}, error) {
client, err := armmanagementgroups.NewClient(e.azureClient().GetCred(), e.azureClient().NewArmClientOptions())
if err != nil {
return nil, fmt.Errorf(`failed to create ManagementGroup "%v": %w`, groupID, err)
}

pager := client.NewGetDescendantsPager(groupID, nil)
ret := []interface{}{}
for pager.More() {
result, err := pager.NextPage(e.ctx)
if err != nil {
e.logger.Panic(err)
}

for _, resource := range result.Value {
if strings.EqualFold(to.String(resource.Type), "Microsoft.Management/managementGroups/subscriptions") {
ret = append(ret, resource)
}
}
}
return transformToInterface(ret)
})
}
2 changes: 2 additions & 0 deletions azuretpl/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func (e *AzureTemplateExecutor) TxtFuncMap(tmpl *textTemplate.Template) textTemp
// azure
`azResource`: e.azResource,
`azResourceList`: e.azResourceList,
`azManagementGroup`: e.azManagementGroup,
`azManagementGroupSubscriptionList`: e.azManagementGroupSubscriptionList,
`azSubscription`: e.azSubscription,
`azSubscriptionList`: e.azSubscriptionList,
`azPublicIpAddress`: e.azPublicIpAddress,
Expand Down
57 changes: 29 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,36 @@ module github.com/webdevops/helm-azure-tpl
go 1.21

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.0-beta.1
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.9.1
github.com/Azure/azure-sdk-for-go/sdk/data/azappconfig v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2 v2.1.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2 v2.2.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/containerservice/armcontainerservice v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups v1.2.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork v1.1.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/redis/armredis v1.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions v1.2.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.4.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armsubscriptions v1.3.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.5.0
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets v1.0.1
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.2.0
github.com/Masterminds/sprig/v3 v3.2.3
github.com/PaesslerAG/jsonpath v0.1.1
github.com/jessevdk/go-flags v1.5.0
github.com/microsoft/kiota-abstractions-go v1.3.0
github.com/microsoftgraph/msgraph-sdk-go v1.23.0
github.com/microsoftgraph/msgraph-sdk-go-core v1.0.0
github.com/microsoft/kiota-abstractions-go v1.5.3
github.com/microsoftgraph/msgraph-sdk-go v1.27.0
github.com/microsoftgraph/msgraph-sdk-go-core v1.0.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/webdevops/go-common v0.0.0-20231022162947-a6adfb05a7e9
github.com/webdevops/go-common v0.0.0-20231212211339-afe7e1672aa1
go.uber.org/zap v1.26.0
gopkg.in/yaml.v3 v3.0.1
helm.sh/helm/v3 v3.13.1
helm.sh/helm/v3 v3.13.2
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.4.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.4.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.8.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.5.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resourcegraph/armresourcegraph v0.9.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.0.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
Expand All @@ -41,16 +42,16 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cjlapao/common-go v0.0.39 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang-jwt/jwt/v5 v5.0.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/golang-jwt/jwt/v5 v5.2.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/microsoft/kiota-authentication-azure-go v1.0.1 // indirect
github.com/microsoft/kiota-http-go v1.1.0 // indirect
github.com/microsoft/kiota-http-go v1.1.1 // indirect
github.com/microsoft/kiota-serialization-form-go v1.0.0 // indirect
github.com/microsoft/kiota-serialization-json-go v1.0.4 // indirect
github.com/microsoft/kiota-serialization-multipart-go v1.0.0 // indirect
Expand All @@ -66,19 +67,19 @@ require (
github.com/prometheus/procfs v0.12.0 // indirect
github.com/remeh/sizedwaitgroup v1.0.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/std-uritemplate/std-uritemplate/go v0.0.46 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/std-uritemplate/std-uritemplate/go v0.0.49 // indirect
github.com/stretchr/testify v1.8.4 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
k8s.io/apimachinery v0.28.3 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
k8s.io/apimachinery v0.28.4 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect
)
Loading

0 comments on commit b79ae7c

Please sign in to comment.