azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Get-AzCosmosDBSqlContainer errors with null ref when run on a container that does not have a unique key policy. It runs as expected when run on a container that does have a unique key policy.

Open plzm opened this issue 6 years ago • 16 comments

Example on a SQL API account with one region, no multi-master, one database without provisioned throughput, one container with provisioned throughput

$container = Get-AzCosmosDBSqlContainer -ResourceGroupName myResourceGroup -AccountName pz-cdb-sql -DatabaseName myDatabase -Name myContainer

Yields Get-AzCosmosDBSqlContainer: Object reference not set to an instance of an object.

Running above cmdlet with -Debug yields

$container = Get-AzCosmosDBSqlContainer -ResourceGroupName myResourceGroup -AccountName pz-cdb-sql -DatabaseName myDatabase -Name myContainer -Debug

DEBUG: 11:56:58 AM - GetAzCosmosDBSqlContainer begin processing with ParameterSet 'ByNameParameterSet'. DEBUG: 11:56:58 AM - using account id '[email protected]'... DEBUG: [Common.Authentication]: Authenticating using Account: '[email protected]', environment: 'AzureCloud', tenant: '72f988bf-86f1-41af-91ab-2d7cd011db47' DEBUG: [Common.Authentication]: Authenticating using configuration values: Domain: '72f988bf-86f1-41af-91ab-2d7cd011db47', Endpoint: 'https://login.microsoftonline.com/', ClientId: '1950a258-227b-4e31-a9cf-717495945fc2', ClientRedirect: 'urn:ietf:wg:oauth:2.0:oob', ResourceClientUri: 'https://management.core.windows.net/', ValidateAuthority: 'True' DEBUG: [Common.Authentication]: Acquiring token using context with Authority 'https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/', CorrelationId: '00000000-0000-0000-0000-000000000000', ValidateAuthority: 'True' DEBUG: [Common.Authentication]: Acquiring token using AdalConfiguration with Domain: '72f988bf-86f1-41af-91ab-2d7cd011db47', AdEndpoint: 'https://login.microsoftonline.com/', ClientId: '1950a258-227b-4e31-a9cf-717495945fc2', ClientRedirectUri: urn:ietf:wg:oauth:2.0:oob DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3734859Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: ADAL PCL.CoreCLR with assembly version '3.19.2.6005', file version '3.19.50302.0130' and informational version '2a8bec6c4c76d0c1ef819b55bdc3cda2d2605056' is running...

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3736516Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: ADAL PCL.CoreCLR with assembly version '3.19.2.6005', file version '3.19.50302.0130' and informational version '2a8bec6c4c76d0c1ef819b55bdc3cda2d2605056' is running...

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3738340Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: === Token Acquisition started: CacheType: null Authentication Target: User , Authority Host: login.microsoftonline.com

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3739524Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: === Token Acquisition started: Authority: https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/ Resource: https://management.core.windows.net/ ClientId: 1950a258-227b-4e31-a9cf-717495945fc2 CacheType: null Authentication Target: User

DEBUG: [ADAL]: Verbose: 2020-03-20T15:56:58.3740906Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: Loading from cache.

DEBUG: [ADAL]: Verbose: 2020-03-20T15:56:58.3741639Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: Loading from cache.

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3851779Z: 00000000-0000-0000-0000-000000000000 - LoggerBase.cs: Deserialized 5 items to token cache.

DEBUG: [ADAL]: Verbose: 2020-03-20T15:56:58.3853854Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: Looking up cache for a token...

DEBUG: [ADAL]: Verbose: 2020-03-20T15:56:58.3855073Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: Looking up cache for a token...

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3856271Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: An item matching the requested resource was found in the cache

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3857346Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: An item matching the requested resource was found in the cache

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3858890Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: 12.641452061666667 minutes left until token in cache expires

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3860021Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: 12.641452061666667 minutes left until token in cache expires

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3860982Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: A matching item (access token or refresh token or both) was found in the cache

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3862408Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: A matching item (access token or refresh token or both) was found in the cache

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3865247Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: === Token Acquisition finished successfully. An access token was returned: Expiration Time: 3/20/2020 4:09:36 PM +00:00

DEBUG: [ADAL]: Information: 2020-03-20T15:56:58.3866493Z: 099927c1-0db7-4795-aaff-e92e16bca26c - LoggerBase.cs: === Token Acquisition finished successfully. An access token was returned: Expiration Time: 3/20/2020 4:09:36 PM +00:00Access Token Hash: nu4rcrobCYI9iQ2u26Ik5hYG+UIxAi098uDzPtosB6o= User id: b995e2c1-f7d9-41c8-a865-c5a7213150b8

DEBUG: [Common.Authentication]: Renewing Token with Type: 'Bearer', Expiry: '03/20/2020 16:09:36 +00:00', MultipleResource? 'True', Tenant: '72f988bf-86f1-41af-91ab-2d7cd011db47', UserId: '[email protected]' DEBUG: [Common.Authentication]: User info for token DisplayId: '[email protected]', Name: Patrick El-Azem, IdProvider: 'https://sts.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47/', Uid: 'b995e2c1-f7d9-41c8-a865-c5a7213150b8'
DEBUG: [Common.Authentication]: Checking token expiration, token expires '03/20/2020 16:09:36 +00:00' Comparing to '03/20/2020 15:56:58 +00:00' With threshold '00:05:00', calculated time until token expiry: '00:12:38.4853907'
DEBUG: ============================ HTTP REQUEST ============================

HTTP Method: GET

Absolute Uri: https://management.azure.com/subscriptions/e61e4c75-268b-4c94-ad48-237aa3231481/resourceGroups/myResourceGroup/providers/Microsoft.DocumentDB/databaseAccounts/pz-cdb-sql/sqlDatabases/myDatabase/containers/myContainer?api-version=2019-08-01

Headers: x-ms-client-request-id : c6369217-6854-4ddd-95bc-cf452aee25ba Accept-Language : en-US

Body:

DEBUG: ============================ HTTP RESPONSE ============================

Status Code: OK

Headers: Cache-Control : no-store, no-cache Pragma : no-cache Strict-Transport-Security : max-age=31536000; includeSubDomains x-ms-gatewayversion : version=2.10.0 Server : Microsoft-HTTPAPI/2.0 x-ms-ratelimit-remaining-subscription-reads: 11988 x-ms-request-id : a91b54a8-d7f1-4932-b5c6-991c6e1ad39d x-ms-correlation-request-id : a91b54a8-d7f1-4932-b5c6-991c6e1ad39d x-ms-routing-request-id : CANADACENTRAL:20200320T155658Z:a91b54a8-d7f1-4932-b5c6-991c6e1ad39d X-Content-Type-Options : nosniff Date : Fri, 20 Mar 2020 15:56:57 GMT

Body: { "id": "/subscriptions/e61e4c75-268b-4c94-ad48-237aa3231481/resourceGroups/myResourceGroup/providers/Microsoft.DocumentDB/databaseAccounts/pz-cdb-sql/sqlDatabases/myDatabase/containers/myContainer", "type": "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers", "name": "myContainer", "properties": { "resource": { "id": "myContainer", "indexingPolicy": { "indexingMode": "consistent", "automatic": true, "includedPaths": [ { "path": "/*" } ], "excludedPaths": [ { "path": "/"_etag"/?" } ] }, "partitionKey": { "paths": [ "/id" ], "kind": "Hash" }, "conflictResolutionPolicy": { "mode": "LastWriterWins", "conflictResolutionPath": "/_ts", "conflictResolutionProcedure": "" }, "geospatialConfig": { "type": "Geography" }, "_rid": "Rv1HAITyZjc=", "_ts": 1584714083, "_self": "dbs/Rv1HAA==/colls/Rv1HAITyZjc=/", "_etag": ""0000cc0a-0000-0100-0000-5e74d1630000"", "_docs": "docs/", "_sprocs": "sprocs/", "_triggers": "triggers/", "_udfs": "udfs/", "_conflicts": "conflicts/", "statistics": [ { "id": "0", "sizeInKB": 0, "documentCount": 0, "partitionKeys": [] } ] } } }

Get-AzCosmosDBSqlContainer: Object reference not set to an instance of an object. DEBUG: AzureQoSEvent: CommandName - Get-AzCosmosDBSqlContainer; IsSuccess - False; Duration - 00:00:00.6874740;; Exception - System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Azure.Commands.CosmosDB.Models.PSUniqueKeyPolicy..ctor(UniqueKeyPolicy uniqueKey) at Microsoft.Azure.Commands.CosmosDB.Models.PSSqlContainerGetPropertiesResource..ctor(SqlContainerGetPropertiesResource sqlContainerGetPropertiesResource) at Microsoft.Azure.Commands.CosmosDB.Models.PSSqlContainerGetResults..ctor(SqlContainerGetResults sqlContainerGetResults) at Microsoft.Azure.Commands.CosmosDB.GetAzCosmosDBSqlContainer.ExecuteCmdlet() at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord(); DEBUG: Finish sending metric. DEBUG: 11:56:59 AM - GetAzCosmosDBSqlContainer end processing.

plzm avatar Mar 20 '20 15:03 plzm

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @kushagraThapar, @srinathnarayanan, @southpolesteve, @shurd.

ghost avatar Mar 21 '20 00:03 ghost

Targeting S168.

MehaKaushik avatar Mar 22 '20 20:03 MehaKaushik

Any update on this issue? I am getting the same error when AzCosmosDBSqlContainer, however Get-AzCosmosDBAccount and Get-AzCosmosDBSqlDatabase commands are working fine.

SafwanHussain avatar Apr 04 '20 13:04 SafwanHussain

Please expect this to be resolved in the upcoming version of Az.CosmosDB 0.1.4

MehaKaushik avatar Apr 04 '20 18:04 MehaKaushik

@MehaKaushik Thanks for the update. When is this version getting released any idea?

SafwanHussain avatar Apr 05 '20 08:04 SafwanHussain

I do face the same problem, Is there any Quick hack for us to proceed until you release 0.1.4?

karthick3d avatar Apr 05 '20 18:04 karthick3d

The ETA for the 0.1.4 is around two weeks from now. For the time being you can use this cmdlet: https://docs.microsoft.com/en-us/powershell/module/az.resources/get-azresource?view=azps-3.7.0

MehaKaushik avatar Apr 05 '20 19:04 MehaKaushik

@karthick3d @SafwanHussain To expand on @MehaKaushik's suggestion to use Get-AzResource, here is a full implementation that both lists containers in a database, as well as retrieves a specified one. (Github Markdown renders the following snippet strangely but just copy-paste into your Powershell editor and it should be fine.)

---------------------------------------------------

Replace "my***Name" with your own values

$resourceGroupName = "myResourceGroupName" $accountName = "myAccountName" $databaseName = "mYDatabaseName" $containerName = "myContainerName"

---------------------------------------------------

$apiVersion = "2020-03-01" $databaseResourceName = $accountName + "/" + $databaseName $containerResourceName = $databaseResourceName + "/" + $containerName $containerResourceType = "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers"

---------------------------------------------------

Write-Host "List containers in the database" Get-AzResource -ResourceType $containerResourceType -ApiVersion $apiVersion -ResourceGroupName $resourceGroupName -Name $databaseResourceName

Write-Host "Show specified container" Get-AzResource -ResourceType $containerResourceType -ApiVersion $apiVersion -ResourceGroupName $resourceGroupName -Name $containerResourceName

---------------------------------------------------

plzm avatar Apr 06 '20 10:04 plzm

Thank you @plzm and @MehaKaushik . It worked for me that saved few days of blocker :).

karthick3d avatar Apr 06 '20 14:04 karthick3d

Root Cause: The issue reproduces on Containers created using Portal, the containers created using Portal do not have an empty UniqueKeyPolicy object, which is the case for containers created using mangement sdk(consumed by powershell). The issue was not reproducible for containers created using Powershell/Mangement SDK.

MehaKaushik avatar Apr 07 '20 23:04 MehaKaushik

Hey @karthick3d @SafwanHussain Az.CosmosDB 0.1.4 is released https://www.powershellgallery.com/packages/Az.CosmosDB/0.1.4

Please verify that the issue is resolved.

MehaKaushik avatar Apr 21 '20 20:04 MehaKaushik

@MehaKaushik When using Az.CosmosDB version 0.1.4 I have found the issue remains.

Get-AzCosmosDBSqlContainer : Object reference not set to an instance of an object.

tylersplitt avatar Apr 27 '20 23:04 tylersplitt

@tylersplitt Do you mind sharing the details of the container, for further debugging.

MehaKaushik avatar Apr 27 '20 23:04 MehaKaushik

Here are the debug logs (sanitized)

{
  "value": [
    {
      "id": "/subscriptions/XXXXXXX/resourceGroups/XXXXXXX/providers/Microsoft.DocumentDB/databaseAccounts/XXXXXXX/sqlDatabases/XXXXXXX/containers/XXXXXXX",
      "type": "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers",
      "name": "XXXXXXX",
      "properties": {
        "resource": {
          "id": "XXXXXXX",
          "indexingPolicy": {
            "indexingMode": "consistent",
            "automatic": true,
            "includedPaths": [
              {
                "path": "/*",
                "indexes": [
                  {
                    "kind": "Range",
                    "dataType": "Number",
                    "precision": -1
                  },
                  {
                    "kind": "Range",
                    "dataType": "String",
                    "precision": -1
                  },
                  {
                    "kind": "Spatial",
                    "dataType": "Point"
                  }
                ]
              }
            ],
            "excludedPaths": [
              {
                "path": "/\"_etag\"/?"
              }
            ]
          },
          "defaultTtl": 11209600,
          "conflictResolutionPolicy": {
            "mode": "LastWriterWins",
            "conflictResolutionPath": "/_ts",
            "conflictResolutionProcedure": ""
          },
          "geospatialConfig": {
            "type": "Geography"
          },
          "_rid": "TVcKAIfwYQA=",
          "_ts": 1584778657,
          "_self": "dbs/TVcKAA==/colls/TVcKAIfwYQA=/",
          "_etag": "\"00005b01-0000-0700-0000-5e75cda10000\"",
          "_docs": "docs/",
          "_sprocs": "sprocs/",
          "_triggers": "triggers/",
          "_udfs": "udfs/",
          "_conflicts": "conflicts/"
        }
      }
    }
  ]
}

Get-AzCosmosDBSqlContainer : Object reference not set to an instance of an object.
At line:1 char:1
 Get-AzCosmosDBSqlContainer -ResourceGroupName XXXXXXXX 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 CategoryInfo          : CloseError: (:) [Get-AzCosmosDBSqlContainer], NullReferenceException
 FullyQualifiedErrorId : Microsoft.Azure.Commands.CosmosDB.GetAzCosmosDBSqlContainer

DEBUG: AzureQoSEvent: CommandName - Get-AzCosmosDBSqlContainer; IsSuccess - False; Duration - 00:00:01.4232957;; Exception - System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Azure.Commands.CosmosDB.Models.PSContainerPartitionKey..ctor(ContainerPartitionKey containerPartitionKey)
   at Microsoft.Azure.Commands.CosmosDB.Models.PSSqlContainerGetPropertiesResource..ctor(SqlContainerGetPropertiesResource sqlContainerGetPropertiesResource)
   at Microsoft.Azure.Commands.CosmosDB.Models.PSSqlContainerGetResults..ctor(SqlContainerGetResults sqlContainerGetResults)
   at Microsoft.Azure.Commands.CosmosDB.GetAzCosmosDBSqlContainer.ExecuteCmdlet()
   at Microsoft.WindowsAzure.Commands.Utilities.Common.AzurePSCmdlet.ProcessRecord();
DEBUG: Finish sending metric.
DEBUG: 6:23:59 PM - GetAzCosmosDBSqlContainer end processing

tylersplitt avatar Apr 27 '20 23:04 tylersplitt

@tylersplitt please verify if your issue is resolved in the latest version 0.1.5

MehaKaushik avatar May 20 '20 19:05 MehaKaushik

Closing, since the fix was checked in and released in version 0.1.5

MehaKaushik avatar May 27 '20 19:05 MehaKaushik