Skip to content

Commit

Permalink
WIP: implement function to get web apps info for a resource group fro…
Browse files Browse the repository at this point in the history
…m azure sdk
  • Loading branch information
arstanaly committed Sep 21, 2023
1 parent 0a15f98 commit 51539cf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
15 changes: 11 additions & 4 deletions cmd/kosli/snapshotAzureFunctions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const snapshotAzureFunctionsExample = ``
type snapshotAzureFunctionsOptions struct {
functionNames []string

Check failure on line 18 in cmd/kosli/snapshotAzureFunctions.go

View workflow job for this annotation

GitHub Actions / test / Lint & Test

field `functionNames` is unused (unused)
functionVersion string

Check failure on line 19 in cmd/kosli/snapshotAzureFunctions.go

View workflow job for this annotation

GitHub Actions / test / Lint & Test

field `functionVersion` is unused (unused)
azureCredentials *azure.AzureFunctionsCredentials
azureCredentials *azure.AzureStaticCredentials
}

func newSnapshotAzureFunctionsCmd(out io.Writer) *cobra.Command {
o := new(snapshotAzureFunctionsOptions)
o.azureCredentials = new(azure.AzureFunctionsCredentials)
o.azureCredentials = new(azure.AzureStaticCredentials)
cmd := &cobra.Command{
Use: "azure-apps ENVIRONMENT-NAME",
Short: snapshotAzureFunctionsShortDesc,
Expand Down Expand Up @@ -55,11 +55,18 @@ func newSnapshotAzureFunctionsCmd(out io.Writer) *cobra.Command {
}

func (o *snapshotAzureFunctionsOptions) run(args []string) error {
data, err := o.azureCredentials.GetWebAppsInfo()
webAppInfo, err := o.azureCredentials.GetWebAppsInfo()
if err != nil {
return err
}
fmt.Print(data)
for _, webapp := range webAppInfo {
fmt.Printf("Webapp name: %s\n", *webapp.Name)
fmt.Printf("Webapp image: %s\n", *webapp.Properties.SiteConfig.LinuxFxVersion)
for _, host := range webapp.Properties.EnabledHostNames {
fmt.Printf("Webapp host: %s\n", *host)
}

}

// envName := args[0]

Expand Down
36 changes: 14 additions & 22 deletions internal/azure/azure_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,41 @@ package azure

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/appservice/armappservice/v2"
)

type AzureFunctionsCredentials struct {
type AzureStaticCredentials struct {
TenantId string
ClientId string
ClientSecret string
SubscriptionId string
ResourceGroupName string
}

func (staticCreds *AzureFunctionsCredentials) GetWebAppsInfo() ([]byte, error) {
fmt.Printf("TenantId: %s\n", staticCreds.TenantId)
fmt.Printf("ClientId: %s\n", staticCreds.ClientId)
fmt.Printf("ClientSecret: %s\n", staticCreds.ClientSecret)
cred, err := azidentity.NewClientSecretCredential(staticCreds.TenantId, staticCreds.ClientId, staticCreds.ClientSecret, nil)
func (staticCreds *AzureStaticCredentials) GetWebAppsInfo() ([]*armappservice.Site, error) {
credentials, err := azidentity.NewClientSecretCredential(staticCreds.TenantId, staticCreds.ClientId, staticCreds.ClientSecret, nil)
if err != nil {
return nil, err
}
fmt.Printf("Reached after new client secret credential\n")

appserviceClientFactory, err := armappservice.NewClientFactory(staticCreds.SubscriptionId, cred, nil)
appserviceClientFactory, err := armappservice.NewClientFactory(staticCreds.SubscriptionId, credentials, nil)
if err != nil {
return nil, err
}
fmt.Printf("Reached after new client factory\n")
webAppsClient := appserviceClientFactory.NewWebAppsClient()
fmt.Printf("Reached after new web apps client\n")
webappsPager := webAppsClient.NewListByResourceGroupPager(staticCreds.ResourceGroupName, nil)
fmt.Printf("Reached after new list by resource group pager\n")
var webappsArray []byte

ctx := context.Background()
webappsPager := webAppsClient.NewListByResourceGroupPager(staticCreds.ResourceGroupName, nil)

var webAppsInfo []*armappservice.Site
for webappsPager.More() {
fmt.Printf("Inside for loop\n")
var currentPageData []byte
webappsPager.UnmarshalJSON(currentPageData)
fmt.Print(currentPageData)
webappsArray = append(webappsArray, currentPageData...)
webappsPager.NextPage(ctx)
response, err := webappsPager.NextPage(ctx)
if err != nil {
return nil, err
}
webAppsInfo = append(webAppsInfo, response.Value...)
}
fmt.Printf("Reached after unmarshal json\n")
return webappsArray, nil
return webAppsInfo, nil
}

0 comments on commit 51539cf

Please sign in to comment.