From 6e985defaac6497c40fb1e5811952bb584a58677 Mon Sep 17 00:00:00 2001 From: Ren Shao Date: Fri, 7 Mar 2025 20:57:32 +1100 Subject: [PATCH] Port latest cache create behavior In az cli 2.70 we introduced an improvement for `az acr cache create`, where this command does not need to issue a GET registry request first. This change is needed for users with cache rule permission only. If a user only has access to cache rule resource, the GET registry request would fail with 503. This commit ported this change in `arccache` extension, so that even after this extension is installed, user can still run `acr acr cache create` command. --- src/acrcache/azext_acrcache/cache.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/acrcache/azext_acrcache/cache.py b/src/acrcache/azext_acrcache/cache.py index d2eb2f89fb3..12ec883c44c 100644 --- a/src/acrcache/azext_acrcache/cache.py +++ b/src/acrcache/azext_acrcache/cache.py @@ -6,11 +6,14 @@ from azure.cli.core.util import user_confirmation +from azure.cli.core.commands.client_factory import get_subscription_id from azure.core.serialization import NULL as AzureCoreNull from azure.cli.command_modules.acr._utils import get_resource_group_name_by_registry_name, get_registry_by_name from .vendored_sdks.containerregistry.v2023_11_01_preview.models._models_py3 import CacheRule, ArtifactSyncScopeFilterProperties, CacheRuleUpdateParameters, ImportSource, ImportImageParameters +CREDENTIAL_SET_RESOURCE_ID_TEMPLATE = '/subscriptions/{sub_id}/resourceGroups/{rg}/providers/Microsoft.ContainerRegistry/registries/{reg_name}/credentialSets/{cred_set_name}' + def _create_kql(starts_with=None, ends_with=None, contains=None): if not starts_with and not ends_with and not contains: return "Tags" @@ -100,10 +103,21 @@ def acr_cache_create(cmd, dry_run=False, yes=False): - registry, rg = get_registry_by_name(cmd.cli_ctx, registry_name, resource_group_name) + rg = get_resource_group_name_by_registry_name(cmd.cli_ctx, registry_name, resource_group_name) + if cred_set: + sub_id = get_subscription_id(cmd.cli_ctx) + # Format the credential set ID using subscription ID, resource group, registry name, and credential set name + cred_set_id = CREDENTIAL_SET_RESOURCE_ID_TEMPLATE.format( + sub_id=sub_id, + rg=rg, + reg_name=registry_name, + cred_set_name=cred_set + ) + else: + cred_set_id = AzureCoreNull sync_str = "Active" if sync else "Inactive" - cred_set_id = AzureCoreNull if not cred_set else f'{registry.id}/credentialSets/{cred_set}' + tag = None if ':' in source_repo: tag = source_repo.split(':')[1]