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

[FEATURE REQ] AppConfiguration ConfigurationClient is missing CreateIfNotExists API

Open jongio opened this issue 3 years ago • 4 comments

Library or service name.

AppConfig

Is your feature request related to a problem? Please describe.

Scenario: I want an API that will only create a setting and set its value if it doesn't already exist. If the setting exists, then the API does nothing. It does not update the value.

  1. Add fails if it already exists
  2. Get fails if it doesn’t exists
  3. Set will overwrite the value if it exists

I ultimately put the “Add” call in a Try/Catch block to get this behavior.

Someone on the Python team mentioned that the Python Add method acts like CreateIfNotExists, meaning it won't throw exception if the item already exists.

azsdke2e

jongio avatar Sep 02 '20 16:09 jongio

We did talk about having that behavior when we designed it and if I remember correctly, we went with the current design because it matches what a Dictionary type does in .NET, which is to throw if you try to add an existing key.

AlexGhiondea avatar Sep 02 '20 19:09 AlexGhiondea

Sure, but I think we can support this scenario with a CreateIfNotExists method.

jongio avatar Sep 03 '20 02:09 jongio

Following the logic of using Dictionary semantics, Dictionary has the ability to test if a key exists with ContainsKey.

My only way to get this behavior is with a try/catch block, which indicates a missing feature in the lib.

The other libs have CreateIfNotExists, i.e.

// App Config
            ConfigurationClient = new ConfigurationClient(new Uri(Env.GetString("AZURE_APP_CONFIG_ENDPOINT")), credential);
            
            // Blob
            BlobServiceClient = new BlobServiceClient(new Uri(Env.GetString("AZURE_STORAGE_BLOB_ENDPOINT")), credential);
            ContainerClient = BlobServiceClient.GetBlobContainerClient(Env.GetString("AZURE_STORAGE_BLOB_CONTAINER_NAME"));
            await ContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer);

            // Queue
            QueueServiceClient = new QueueServiceClient(new Uri(Env.GetString("AZURE_STORAGE_QUEUE_ENDPOINT")), credential);
            QueueClient = QueueServiceClient.GetQueueClient(Env.GetString("AZURE_STORAGE_QUEUE_NAME"));
            await QueueClient.CreateIfNotExistsAsync();

            // Client Sync Queue
            ClientSyncQueueClient = QueueServiceClient.GetQueueClient(Env.GetString("AZURE_STORAGE_CLIENT_SYNC_QUEUE_NAME"));
            await ClientSyncQueueClient.CreateIfNotExistsAsync();

But for AppConfig I have to special case it and use a try/catch to get the behavior I want. I would like the API surface to be consistent across the libs.

jongio avatar Sep 09 '20 01:09 jongio

We should add this to the list of APIs that should not throw. /cc @annelo-msft

AlexGhiondea avatar Dec 17 '21 19:12 AlexGhiondea

This is tracked under #25626 and will take a different form than discussed in this issue. Closing this out.

jsquire avatar Jun 20 '23 20:06 jsquire