Skip to content

Commit 10efe2d

Browse files
{AKS} az aks enable-addons --addon monitoring: handle existing linked resource in AMPLS gracefully (#31899)
1 parent be770fe commit 10efe2d

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

src/azure-cli/azure/cli/command_modules/acs/addonconfiguration.py

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -717,22 +717,64 @@ def create_or_delete_dcr_association(cmd, cluster_region, remove_monitoring, clu
717717
raise error
718718

719719

720-
def create_ampls_scope(cmd, ampls_resource_id, dce_endpoint_name, dce_resource_id):
721-
link_dce_ampls_body = json.dumps(
720+
def is_ampls_scoped_exist(cmd, ampls_resource_id, scoped_resource_id):
721+
"""
722+
Check if the specified resource is already scoped (linked) to the AMPLS by iterating through all scoped resources.
723+
724+
Args:
725+
cmd: Command context
726+
ampls_resource_id: Full resource ID of the AMPLS
727+
scoped_resource_id: Full resource ID of the resource to be linked
728+
729+
Returns:
730+
bool: True if the resource is already scoped to the AMPLS, False otherwise
731+
"""
732+
try:
733+
# Get all scoped resources for this AMPLS
734+
ampls_scoped_resources_url = f"{cmd.cli_ctx.cloud.endpoints.resource_manager}{ampls_resource_id}/scopedresources?api-version=2021-07-01-preview"
735+
response = send_raw_request(cmd.cli_ctx, "GET", ampls_scoped_resources_url)
736+
scoped_resources_data = json.loads(response.text)
737+
738+
# Check if any scoped resource has the same linkedResourceId
739+
for scoped_resource in scoped_resources_data.get('value', []):
740+
properties = scoped_resource.get('properties', {})
741+
linked_resource_id = properties.get('linkedResourceId', '')
742+
scoped_resource_name = scoped_resource.get('name', 'unknown')
743+
# Compare case-insensitively since Azure resource IDs can have case variations
744+
if linked_resource_id.lower() == scoped_resource_id.lower():
745+
logger.info("Resource already scoped in AMPLS. Scoped resource name: %s, LinkedResourceId: %s", scoped_resource_name, linked_resource_id)
746+
return True
747+
748+
logger.info("No matching linkedResourceId found. Resource is not yet scoped.")
749+
return False
750+
751+
except CLIError as e:
752+
logger.warning("Error checking AMPLS scoped resources: %s", str(e))
753+
return False
754+
755+
756+
def create_ampls_scope(cmd, ampls_resource_id, scoped_resource_name, scoped_resource_id):
757+
# Check if the resource is already scoped to the AMPLS
758+
if is_ampls_scoped_exist(cmd, ampls_resource_id, scoped_resource_id):
759+
return
760+
761+
scoped_resource_ampls_body = json.dumps(
722762
{
723763
"properties": {
724-
"linkedResourceId": dce_resource_id,
764+
"linkedResourceId": scoped_resource_id,
725765
},
726766
}
727767
)
768+
728769
resources = get_resources_client(cmd.cli_ctx, cmd.cli_ctx.data.get('subscription_id'))
729-
ampls_scope_id = f"{ampls_resource_id}/scopedresources/{dce_endpoint_name}-connection"
770+
ampls_scope_id = f"{ampls_resource_id}/scopedresources/{scoped_resource_name}-connection"
771+
730772
for _ in range(3):
731773
try:
732774
resources.begin_create_or_update_by_id(
733775
ampls_scope_id,
734776
"2021-07-01-preview",
735-
json.loads(link_dce_ampls_body)
777+
json.loads(scoped_resource_ampls_body)
736778
)
737779
error = None
738780
break

0 commit comments

Comments
 (0)