0

On my Azure account, I have setup 2 FunctionApps as myself. I am doing this via the Contributor role. I can view it via the CLI:

az functionapp list --resource-group <my subscription>

I am trying to do the same via a Scala app. The identity has both the Contributor and the Reader roles, so it should be able to read all resources.

      val profile = new AzureProfile(params.azureTenantId, params.azureSubscriptionId, AzureEnvironment.AZURE)
      val credential = params.getCredential()
      val azure = AzureResourceManager.authenticate(credential, profile).withSubscription(params.azureSubscriptionId)

When looking for VMs or VPCs, I get return values. However, when I try to do the same with FunctionApps, I get an empty list. I am expecting the same results as the CLI call:

      val test = azure.functionApps().list().asScala.toList
      logger.error("Test length is "+test.size)

In this case, I end up with an empty list. Both the CLI and the Scala app are using the Contributor / Reader roles, but the Scala app is not returning values.

Am I missing something?

2
  • Check if the FunctionApps belong to the same resource group and try using azure.functionApps().listByResourceGroup("<resource-group-name>"). Commented Mar 10 at 3:33
  • The root cause was I was looking in the wrong resource group. Commented Apr 8 at 18:07

1 Answer 1

0

I used the functionApps = azure.functionApps().listByResourceGroup(resourceGroup) function in the Scala code to get the list of Azure Function Apps.

I have assigned the Reader role to the Service Principal for my Resource Group.

enter image description here

I added the AZURE_CLIENT_ID, AZURE_TENANT_ID and AZURE_CLIENT_SECRET in system environment variables to use DefaultAzureCredential.

enter image description here

KamApp.scala :

import com.azure.identity.DefaultAzureCredentialBuilder
import com.azure.resourcemanager.AzureResourceManager
import com.azure.core.management.profile.AzureProfile
import com.azure.core.management.AzureEnvironment
import scala.jdk.CollectionConverters._

object KamApp {
  def main(args: Array[String]): Unit = {
    val tenantId = "<TenantID>"
    val subscriptionId = "<SubscriptionID>"

    val profile = new AzureProfile(tenantId, subscriptionId, AzureEnvironment.AZURE)
    val credential = new DefaultAzureCredentialBuilder().build()

    val azure = AzureResourceManager
      .authenticate(credential, profile)
      .withSubscription(subscriptionId)

    val resourceGroup = "<resourceGroupName>"

    try {
      val functionApps = azure.functionApps().listByResourceGroup(resourceGroup).asScala.toList

      if (functionApps.isEmpty) {
        println(s"No FunctionApps found in resource group: $resourceGroup")
      } else {
        println(s"Function Apps in $resourceGroup:")
        functionApps.foreach(app => println(s"- ${app.name()} (${app.regionName()})"))
      }
    } catch {
      case e: Exception => e.printStackTrace()
    }
  }
}

build.sbt :

libraryDependencies ++= Seq(
  "com.azure.resourcemanager" % "azure-resourcemanager" % "2.37.0",
  "com.azure" % "azure-identity" % "1.10.0",
  "ch.qos.logback" % "logback-classic" % "1.4.14"
)

Output :

I successfully retrieved the list of Azure Function Apps using Scala as shown below.

enter image description here
enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.