azure-sdk-for-go icon indicating copy to clipboard operation
azure-sdk-for-go copied to clipboard

CheckExistenceByID and CheckExistence always returning false with Get or GetByID confirming the resources exists

Open kabal2010 opened this issue 2 years ago • 15 comments

Bug Report

  • Import path of package: github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources
  • SDK version: v63.4.0
  • go version: go1.18.1 windows/amd64
  • What happened? I created a code shown below.
// Set the package
package main

import (
	"context"
	"log"
	"testing"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
	"github.com/stretchr/testify/assert"
)

// Set the required variables
var subscriptionID = "82d66c37-8d61-4db4-b138-xxxxxxxxxxxx"
var apiVersion = "2022-02-01-preview"
var resourceID = "/subscriptions/82d66c37-8d61-4db4-b138-xxxxxxxxxxxx/resourceGroups/DEXT-EUN-RGRP01/providers/Microsoft.KeyVault/vaults/dexteunautomationkv01"

// Set the function to test resource existsence
func TestAzureKeyVaultExistence(t *testing.T) {
	// Create a context
	ctx := context.Background()

	// Create a new credential
	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatal(err)
	}

	// Create a new resource client
	resourceClient, _ := armresources.NewClient(subscriptionID, cred, nil)

	// Check if the resource exists
	exists, err := resourceClient.CheckExistenceByID(ctx, resourceID, apiVersion, nil)
	if err != nil {
		log.Fatal(err)
	}
	assert.True(t, exists.Success)
}
  • When I executed the command go test -v, I got the error shown below.
=== RUN   TestAzureKeyVaultExistence
    integration_test.go:31:
                Error Trace:    integration_test.go:31    
                Error:          Should be true
                Test:           TestAzureKeyVaultExistence
--- FAIL: TestAzureKeyVaultExistence (2.83s)
FAIL
exit status 1
FAIL    test.com        4.764s
  • What did you expect or want to happen? I should have a successful output as shown in the snip below.
=== RUN   TestAzureKeyVaultExistence
--- PASS: TestAzureKeyVaultExistence (1.98s)
PASS
ok      test.com        3.437s
  • How can we reproduce it?

    • Create a similar go code as above.
    • Ensure an Azure resource exists.
    • Execute the code to check the resource from the go code.
  • Anything we should know about your environment. The Get-AzKeyVault command shows the resource exists as shown below

Vault Name          : dexteunautomationkv01
Resource Group Name : DEXT-EUN-RGRP01
Location            : northeurope
Resource ID         : /subscriptions/82d66c37-8d61-4db4-b138-xxxxxxxxxxxx/resourceGroups/DEXT-EUN-RGRP01/providers/Microsoft.KeyVault/vaults/dexteunautomationkv01
Tags                : 

kabal2010 avatar May 05 '22 08:05 kabal2010

Changing the verb from HEAD to GET the request succeeds, but that shouldn't be necessary. @lirenhe can you please follow up with the service team about this?

jhendrixMSFT avatar May 05 '22 16:05 jhendrixMSFT

Thank you for your feedback. This has been routed to the support team for assistance.

msftbot[bot] avatar May 06 '22 10:05 msftbot[bot]

@kabal2010 Thanks for your feedback. It seems KeyVault service does not implement the HEAD operation for vaults resources. We'll involve service team to have a look. Could you please use resourceClient.GetById as a temporarily work around?

tadelesh avatar May 06 '22 10:05 tadelesh

Need KeyVault team support.

tadelesh avatar May 06 '22 10:05 tadelesh

@kabal2010 Thanks for your feedback. It seems KeyVault service does not implement the HEAD operation for vaults resources. We'll involve service team to have a look. Could you please use resourceClient.GetById as a temporarily work around?

@tadelesh Yes I can confirm that resourceClient.GetById works and have been using it currently, but will prefer if this can be made to work since it produces a Boolean value which I just need to check to complete my testing. Also, it's worth mentioning that this issue is not just for Azure Key Vault as I tried even a resource group and it came back with a false. It's worth the team checking this against all types of Azure resource, but I just logged this with the Key Vault resource type.

kabal2010 avatar May 06 '22 10:05 kabal2010

@kabal2010 Apologies for the late reply. I see that this PR has been filed Azure/autorest.go#819 to address this. As per this comment the Fix is in upcoming preview.41 release. If you have any follow-up questions please feel free to reopen this thread. We would be happy to address it.

navba-MSFT avatar May 13 '22 05:05 navba-MSFT

@navba-MSFT The PR is to fix the wrong handling with 405 response from service in SDK side. Actually, I think the root cause @kabal2010 mentioned is this method has not implemented by service. Could you help to triage this issue to service team to have a look.

tadelesh avatar May 13 '22 05:05 tadelesh

@tadelesh Thanks for clarifying this. I am adding Service Team to have a look into this further.

navba-MSFT avatar May 13 '22 05:05 navba-MSFT

@navba-MSFT Can I please ask if there has been any update regarding this issue?

Best Regards

kabal2010 avatar Jun 01 '22 07:06 kabal2010

@kabal2010 Unfortunately I don't have any update on this. This is currently pending on the Service Team now.

navba-MSFT avatar Jun 01 '22 08:06 navba-MSFT

@josephkwchan @jennyhunter-msft Could you please review this ask and help us loop in the right owners for this ? Thanks in advance.

navba-MSFT avatar Jun 01 '22 08:06 navba-MSFT

@navba-MSFT Can I please ask if this has now been fixed?

kabal2010 avatar Jun 24 '22 15:06 kabal2010

@kabal2010 This is pending with Service team now.

@josephkwchan @jennyhunter-msft Could you please provide an update on this ?

navba-MSFT avatar Jun 24 '22 16:06 navba-MSFT

@navba-MSFT Thank you

kabal2010 avatar Jun 24 '22 16:06 kabal2010

@navba-MSFT

I think the issue here has to do with using the HEAD method for querying for the resource as seen in https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/resourcemanager/resources/armresources/zz_generated_client.go#L139. I've tested again with another resource and got the error in the snip below.

=== RUN   TestResourceExistence
2022/06/28 21:02:19 HEAD https://management.azure.com/subscriptions/82d66c37-8d61-4db4-b138-xxxxxxxxxxxx/resourceGroups/DEXT-EUN-RGRP01/providers/Microsoft.Network/privateDnsZones/konga.lab/virtualNetworkLinks/dext-eun-pvnet-03
--------------------------------------------------------------------------------
RESPONSE 405: 405 Method Not Allowed
ERROR CODE UNAVAILABLE
--------------------------------------------------------------------------------
Response contained no body
--------------------------------------------------------------------------------
FAIL    test.com        2.439s
FAIL

The test code as as below.

// Set the package
package inegration_test

import (
	"context"
	"log"
	"strings"
	"testing"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
)

// Set function to test for resources
func TestResourceExistence(t *testing.T) {
	// Set the required variables
	resourceId := "/subscriptions/82d66c37-8d61-4db4-b138-xxxxxxxxxxxx/resourceGroups/DEXT-EUN-RGRP01/providers/Microsoft.Network/privateDnsZones/konga.lab/virtualNetworkLinks/dext-eun-pvnet-03"
	apiVersion := "2021-04-01"

	// Create a new context
	ctx := context.Background()

	// Create a new credential
	cred, err := azidentity.NewAzureCLICredential(nil)
	if err != nil {
		log.Fatal(err)
	}

	// Split the resource ID
	resource := strings.Split(resourceId, "/")

	// Create a new client
	client, err := armresources.NewClient(resource[2], cred, nil)
	if err != nil {
		log.Fatal(err)
	}

	// Check existence by ID
	checkExistenceByID, err := client.CheckExistenceByID(ctx, resourceId, apiVersion, nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(checkExistenceByID.Success)
}

Changing the method to http.MethodGet should resolve he issue. I've tested this using the Azure Rest API and can only get the resource when the method is GET and not HEAD.

kabal2010 avatar Jun 28 '22 20:06 kabal2010

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @darshanhs90, @AshishGargMicrosoft.

Issue Details

Bug Report

  • Import path of package: github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources
  • SDK version: v63.4.0
  • go version: go1.18.1 windows/amd64
  • What happened? I created a code shown below.
// Set the package
package main

import (
	"context"
	"log"
	"testing"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
	"github.com/stretchr/testify/assert"
)

// Set the required variables
var subscriptionID = "82d66c37-8d61-4db4-b138-xxxxxxxxxxxx"
var apiVersion = "2022-02-01-preview"
var resourceID = "/subscriptions/82d66c37-8d61-4db4-b138-xxxxxxxxxxxx/resourceGroups/DEXT-EUN-RGRP01/providers/Microsoft.KeyVault/vaults/dexteunautomationkv01"

// Set the function to test resource existsence
func TestAzureKeyVaultExistence(t *testing.T) {
	// Create a context
	ctx := context.Background()

	// Create a new credential
	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatal(err)
	}

	// Create a new resource client
	resourceClient, _ := armresources.NewClient(subscriptionID, cred, nil)

	// Check if the resource exists
	exists, err := resourceClient.CheckExistenceByID(ctx, resourceID, apiVersion, nil)
	if err != nil {
		log.Fatal(err)
	}
	assert.True(t, exists.Success)
}
  • When I executed the command go test -v, I got the error shown below.
=== RUN   TestAzureKeyVaultExistence
    integration_test.go:31:
                Error Trace:    integration_test.go:31    
                Error:          Should be true
                Test:           TestAzureKeyVaultExistence
--- FAIL: TestAzureKeyVaultExistence (2.83s)
FAIL
exit status 1
FAIL    test.com        4.764s
  • What did you expect or want to happen? I should have a successful output as shown in the snip below.
=== RUN   TestAzureKeyVaultExistence
--- PASS: TestAzureKeyVaultExistence (1.98s)
PASS
ok      test.com        3.437s
  • How can we reproduce it?

    • Create a similar go code as above.
    • Ensure an Azure resource exists.
    • Execute the code to check the resource from the go code.
  • Anything we should know about your environment. The Get-AzKeyVault command shows the resource exists as shown below

Vault Name          : dexteunautomationkv01
Resource Group Name : DEXT-EUN-RGRP01
Location            : northeurope
Resource ID         : /subscriptions/82d66c37-8d61-4db4-b138-xxxxxxxxxxxx/resourceGroups/DEXT-EUN-RGRP01/providers/Microsoft.KeyVault/vaults/dexteunautomationkv01
Tags                : 
Author: kabal2010
Assignees: tadelesh, lirenhe
Labels:

question, Resource Authorization, Service Attention, Mgmt, customer-reported, needs-team-attention

Milestone: -

msftbot[bot] avatar Sep 01 '22 21:09 msftbot[bot]