|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Net; |
| 5 | +using Azure.Mcp.Core.Commands.Subscription; |
| 6 | +using Azure.Mcp.Core.Services.Azure.Subscription; |
| 7 | +using Azure.Mcp.Tools.ManagedCleanroom.Options.Collaboration; |
| 8 | +using Azure.Mcp.Tools.ManagedCleanroom.Services; |
| 9 | +using Microsoft.Extensions.Logging; |
| 10 | +using Microsoft.Mcp.Core.Commands; |
| 11 | +using Microsoft.Mcp.Core.Models.Command; |
| 12 | + |
| 13 | +namespace Azure.Mcp.Tools.ManagedCleanroom.Commands.Collaboration; |
| 14 | + |
| 15 | +[CommandMetadata( |
| 16 | + Id = "e247b9e0-2d87-43a7-8e5d-57eea22237a3", |
| 17 | + Name = "create", |
| 18 | + Title = "Create Cleanroom Collaboration", |
| 19 | + Description = """ |
| 20 | + Creates an Azure Cleanroom collaboration ARM resource in the specified resource group and location. |
| 21 | + Polls provisioningState every 30 seconds until the resource reaches a terminal state (expected ~25 minutes). |
| 22 | + Returns the final ARM resource properties and a summary message with the outcome and elapsed time. |
| 23 | + Required options: |
| 24 | + - --name: unique collaboration name within the resource group |
| 25 | + - --location: Azure region for the ARM resource (e.g., 'eastus') |
| 26 | + - --resource-group: resource group to create the collaboration in |
| 27 | + - --subscription: Azure subscription |
| 28 | + """, |
| 29 | + Destructive = false, |
| 30 | + Idempotent = true, |
| 31 | + OpenWorld = false, |
| 32 | + ReadOnly = false, |
| 33 | + Secret = false, |
| 34 | + LocalRequired = false)] |
| 35 | +public sealed class CollaborationCreateCommand( |
| 36 | + ILogger<CollaborationCreateCommand> logger, |
| 37 | + IManagedCleanroomService service, |
| 38 | + ISubscriptionResolver subscriptionResolver) |
| 39 | + : SubscriptionCommand<CollaborationCreateOptions, CollaborationCreateCommand.CollaborationCreateCommandResult>(subscriptionResolver) |
| 40 | +{ |
| 41 | + private readonly ILogger<CollaborationCreateCommand> _logger = logger; |
| 42 | + private readonly IManagedCleanroomService _service = service; |
| 43 | + |
| 44 | + public override async Task<CommandResponse> ExecuteAsync( |
| 45 | + CommandContext context, CollaborationCreateOptions options, CancellationToken cancellationToken) |
| 46 | + { |
| 47 | + try |
| 48 | + { |
| 49 | + var result = await _service.CreateCollaborationArmResourceAsync( |
| 50 | + options.Name, |
| 51 | + options.ResourceGroup, |
| 52 | + options.Subscription!, |
| 53 | + options.Location, |
| 54 | + options.ResourceLocation, |
| 55 | + options.Collaborator, |
| 56 | + options.Tenant, |
| 57 | + options.RetryPolicy, |
| 58 | + cancellationToken).ConfigureAwait(false); |
| 59 | + |
| 60 | + context.Response.Message = result.Message; |
| 61 | + context.Response.Results = ResponseResult.Create( |
| 62 | + result.Properties, |
| 63 | + ManagedCleanroomJsonContext.Default.JsonElement); |
| 64 | + } |
| 65 | + catch (Exception ex) |
| 66 | + { |
| 67 | + _logger.LogError(ex, |
| 68 | + "Error creating cleanroom collaboration. Name: {Name}, ResourceGroup: {ResourceGroup}, Subscription: {Subscription}", |
| 69 | + options.Name, options.ResourceGroup, options.Subscription); |
| 70 | + HandleException(context, ex); |
| 71 | + } |
| 72 | + |
| 73 | + return context.Response; |
| 74 | + } |
| 75 | + |
| 76 | + protected override string GetErrorMessage(Exception ex) => ex switch |
| 77 | + { |
| 78 | + RequestFailedException reqEx when reqEx.Status == (int)HttpStatusCode.Conflict => |
| 79 | + "A collaboration with this name already exists in the resource group.", |
| 80 | + RequestFailedException reqEx when reqEx.Status == (int)HttpStatusCode.Forbidden => |
| 81 | + $"Authorization failed creating the collaboration. Details: {reqEx.Message}", |
| 82 | + RequestFailedException reqEx when reqEx.Status == (int)HttpStatusCode.NotFound => |
| 83 | + "Resource group not found. Verify the resource group exists and you have access.", |
| 84 | + RequestFailedException reqEx => reqEx.Message, |
| 85 | + _ => base.GetErrorMessage(ex) |
| 86 | + }; |
| 87 | + |
| 88 | + protected override HttpStatusCode GetStatusCode(Exception ex) => ex switch |
| 89 | + { |
| 90 | + RequestFailedException reqEx when reqEx.Status == (int)HttpStatusCode.Conflict => |
| 91 | + HttpStatusCode.Conflict, |
| 92 | + RequestFailedException reqEx when reqEx.Status == (int)HttpStatusCode.Forbidden => |
| 93 | + HttpStatusCode.Forbidden, |
| 94 | + RequestFailedException reqEx when reqEx.Status == (int)HttpStatusCode.NotFound => |
| 95 | + HttpStatusCode.NotFound, |
| 96 | + RequestFailedException reqEx => (HttpStatusCode)reqEx.Status, |
| 97 | + _ => base.GetStatusCode(ex) |
| 98 | + }; |
| 99 | + |
| 100 | + public record CollaborationCreateCommandResult; |
| 101 | +} |
0 commit comments