|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Azure.Provisioning.Authorization; |
| 5 | +using Azure.Provisioning.EventGrid; |
| 6 | +using Azure.Provisioning.Expressions; |
| 7 | +using Azure.Provisioning.Resources; |
| 8 | +using Azure.Provisioning.Roles; |
| 9 | +using Azure.Provisioning.ServiceBus; |
| 10 | +using Azure.Provisioning.Storage; |
| 11 | + |
| 12 | +namespace Azure.Provisioning.CloudMachine; |
| 13 | + |
| 14 | +public class CloudMachineInfrastructure : Infrastructure |
| 15 | +{ |
| 16 | + private readonly string _name; |
| 17 | + private UserAssignedIdentity _identity; |
| 18 | + private StorageAccount _storage; |
| 19 | + private BlobService _blobs; |
| 20 | + private BlobContainer _container; |
| 21 | + private ServiceBusNamespace _serviceBusNamespace; |
| 22 | + private ServiceBusNamespaceAuthorizationRule _serviceBusNamespaceAuthorizationRule; |
| 23 | + private ServiceBusTopic _serviceBusTopic_main; |
| 24 | + private ServiceBusTopic _serviceBusTopic_app; |
| 25 | + private ServiceBusSubscription _serviceBusSubscription_main; |
| 26 | + private ServiceBusSubscription _serviceBusSubscription_app; |
| 27 | + private SystemTopic _eventGridTopic_Blobs; |
| 28 | + private SystemTopicEventSubscription _systemTopicEventSubscription; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// The common principalId parameter. |
| 32 | + /// </summary> |
| 33 | + public BicepParameter PrincipalIdParameter => new BicepParameter("principalId", typeof(string)); |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// The common principalType parameter. |
| 37 | + /// </summary> |
| 38 | + public BicepParameter PrincipalTypeParameter => new BicepParameter("principalType", typeof(string)); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// The common principalName parameter. |
| 42 | + /// </summary> |
| 43 | + public BicepParameter PrincipalNameParameter => new BicepParameter("principalName", typeof(string)); |
| 44 | + |
| 45 | + public CloudMachineInfrastructure(string name = "cm") : base(name!) |
| 46 | + { |
| 47 | + _name = name ?? "cm"; |
| 48 | + _identity = new($"{_name}_identity"); |
| 49 | + ManagedServiceIdentity managedServiceIdentity = new() |
| 50 | + { |
| 51 | + ManagedServiceIdentityType = ManagedServiceIdentityType.UserAssigned, |
| 52 | + UserAssignedIdentities = { { BicepFunction.Interpolate($"{_identity.Id}").Compile().ToString(), new UserAssignedIdentityDetails() } } |
| 53 | + }; |
| 54 | + |
| 55 | + _storage = StorageResources.CreateAccount($"{_name}_sa"); |
| 56 | + _storage.Identity = managedServiceIdentity; |
| 57 | + |
| 58 | + _blobs = new($"{_name}_blobs") |
| 59 | + { |
| 60 | + Parent = _storage, |
| 61 | + }; |
| 62 | + _container = new BlobContainer($"{_name}_container", "2023-01-01") |
| 63 | + { |
| 64 | + Parent = _blobs, |
| 65 | + Name = "default" |
| 66 | + }; |
| 67 | + |
| 68 | + _serviceBusNamespace = new($"{_name}_sb") |
| 69 | + { |
| 70 | + Sku = new ServiceBusSku |
| 71 | + { |
| 72 | + Name = ServiceBusSkuName.Standard, |
| 73 | + Tier = ServiceBusSkuTier.Standard |
| 74 | + }, |
| 75 | + }; |
| 76 | + _serviceBusNamespaceAuthorizationRule = new($"{_name}_sb_auth_rule", "2021-11-01") |
| 77 | + { |
| 78 | + Parent = _serviceBusNamespace, |
| 79 | + Rights = [ServiceBusAccessRight.Listen, ServiceBusAccessRight.Send, ServiceBusAccessRight.Manage] |
| 80 | + }; |
| 81 | + _serviceBusTopic_main = new($"{_name}_sb_topic_main", "2021-11-01") |
| 82 | + { |
| 83 | + Parent = _serviceBusNamespace, |
| 84 | + MaxMessageSizeInKilobytes = 256, |
| 85 | + DefaultMessageTimeToLive = new StringLiteral("P14D"), |
| 86 | + RequiresDuplicateDetection = false, |
| 87 | + EnableBatchedOperations = true, |
| 88 | + SupportOrdering = true, |
| 89 | + Status = ServiceBusMessagingEntityStatus.Active |
| 90 | + }; |
| 91 | + _serviceBusSubscription_main = new($"{_name}_sb_sub_main", "2021-11-01") |
| 92 | + { |
| 93 | + Parent = _serviceBusTopic_main, |
| 94 | + IsClientAffine = false, |
| 95 | + LockDuration = new StringLiteral("PT30S"), |
| 96 | + RequiresSession = false, |
| 97 | + DefaultMessageTimeToLive = new StringLiteral("P14D"), |
| 98 | + DeadLetteringOnFilterEvaluationExceptions = true, |
| 99 | + DeadLetteringOnMessageExpiration = true, |
| 100 | + MaxDeliveryCount = 10, |
| 101 | + EnableBatchedOperations = true, |
| 102 | + Status = ServiceBusMessagingEntityStatus.Active |
| 103 | + }; |
| 104 | + _serviceBusTopic_app = new($"{_name}_sb_topic_app", "2021-11-01") |
| 105 | + { |
| 106 | + Parent = _serviceBusNamespace, |
| 107 | + // Name = "default", |
| 108 | + MaxMessageSizeInKilobytes = 256, |
| 109 | + DefaultMessageTimeToLive = new StringLiteral("P14D"), |
| 110 | + RequiresDuplicateDetection = false, |
| 111 | + EnableBatchedOperations = true, |
| 112 | + SupportOrdering = true, |
| 113 | + Status = ServiceBusMessagingEntityStatus.Active |
| 114 | + }; |
| 115 | + _serviceBusSubscription_app = new($"{_name}_sb_sub_app", "2021-11-01") |
| 116 | + { |
| 117 | + Parent = _serviceBusTopic_app, |
| 118 | + IsClientAffine = false, |
| 119 | + LockDuration = new StringLiteral("PT30S"), |
| 120 | + RequiresSession = false, |
| 121 | + DefaultMessageTimeToLive = new StringLiteral("P14D"), |
| 122 | + DeadLetteringOnFilterEvaluationExceptions = true, |
| 123 | + DeadLetteringOnMessageExpiration = true, |
| 124 | + MaxDeliveryCount = 10, |
| 125 | + EnableBatchedOperations = true, |
| 126 | + Status = ServiceBusMessagingEntityStatus.Active |
| 127 | + }; |
| 128 | + _eventGridTopic_Blobs = new($"{_name}_eg_blob", "2022-06-15") |
| 129 | + { |
| 130 | + TopicType = "Microsoft.Storage.StorageAccounts", |
| 131 | + Source = _storage.Id, |
| 132 | + Identity = managedServiceIdentity |
| 133 | + }; |
| 134 | + _systemTopicEventSubscription = new($"{_name}_eg_blob_sub", "2022-06-15") |
| 135 | + { |
| 136 | + Parent = _eventGridTopic_Blobs, |
| 137 | + DeliveryWithResourceIdentity = new DeliveryWithResourceIdentity |
| 138 | + { |
| 139 | + Identity = new EventSubscriptionIdentity |
| 140 | + { |
| 141 | + IdentityType = EventSubscriptionIdentityType.UserAssigned, |
| 142 | + UserAssignedIdentity = _identity.Id |
| 143 | + }, |
| 144 | + Destination = new EventHubEventSubscriptionDestination |
| 145 | + { |
| 146 | + ResourceId = _serviceBusTopic_main.Id |
| 147 | + } |
| 148 | + }, |
| 149 | + Filter = new EventSubscriptionFilter |
| 150 | + { |
| 151 | + IncludedEventTypes = |
| 152 | + [ |
| 153 | + "Microsoft.Storage.BlobCreated", |
| 154 | + "Microsoft.Storage.BlobDeleted", |
| 155 | + "Microsoft.Storage.BlobRenamed" |
| 156 | + ], |
| 157 | + IsAdvancedFilteringOnArraysEnabled = true |
| 158 | + }, |
| 159 | + EventDeliverySchema = EventDeliverySchema.EventGridSchema, |
| 160 | + RetryPolicy = new EventSubscriptionRetryPolicy |
| 161 | + { |
| 162 | + MaxDeliveryAttempts = 30, |
| 163 | + EventTimeToLiveInMinutes = 1440 |
| 164 | + } |
| 165 | + }; |
| 166 | + } |
| 167 | + |
| 168 | + public override ProvisioningPlan Build(ProvisioningContext? context = null) |
| 169 | + { |
| 170 | + // Always add a default location parameter. |
| 171 | + // azd assumes there will be a location parameter for every module. |
| 172 | + // The Infrastructure location resolver will resolve unset Location properties to this parameter. |
| 173 | + Add(new BicepParameter("location", typeof(string)) |
| 174 | + { |
| 175 | + Description = "The location for the resource(s) to be deployed.", |
| 176 | + Value = BicepFunction.GetResourceGroup().Location |
| 177 | + }); |
| 178 | + |
| 179 | + Add(PrincipalIdParameter); |
| 180 | + Add(PrincipalTypeParameter); |
| 181 | + Add(PrincipalNameParameter); |
| 182 | + |
| 183 | + Add(_identity); |
| 184 | + Add(_storage); |
| 185 | + Add(_storage.AssignRole(StorageBuiltInRole.StorageBlobDataContributor, RoleManagementPrincipalType.User, PrincipalIdParameter)); |
| 186 | + Add(_storage.AssignRole(StorageBuiltInRole.StorageTableDataContributor, RoleManagementPrincipalType.User, PrincipalIdParameter)); |
| 187 | + Add(_container); |
| 188 | + Add(_blobs); |
| 189 | + Add(_serviceBusNamespace); |
| 190 | + Add(_serviceBusNamespace.AssignRole(ServiceBusBuiltInRole.AzureServiceBusDataOwner, RoleManagementPrincipalType.User, PrincipalIdParameter)); |
| 191 | + Add(_serviceBusNamespaceAuthorizationRule); |
| 192 | + Add(_serviceBusTopic_main); |
| 193 | + Add(_serviceBusTopic_app); |
| 194 | + Add(_serviceBusSubscription_main); |
| 195 | + Add(_serviceBusSubscription_app); |
| 196 | + |
| 197 | + // This is necessary until SystemTopic adds an AssignRole method. |
| 198 | + var role = ServiceBusBuiltInRole.AzureServiceBusDataOwner; |
| 199 | + RoleAssignment roleAssignment = new RoleAssignment(_eventGridTopic_Blobs.ResourceName + "_" + _identity.ResourceName + "_" + ServiceBusBuiltInRole.GetBuiltInRoleName(role)); |
| 200 | + roleAssignment.Name = BicepFunction.CreateGuid(_eventGridTopic_Blobs.Id, _identity.Id, BicepFunction.GetSubscriptionResourceId("Microsoft.Authorization/roleDefinitions", role.ToString())); |
| 201 | + roleAssignment.Scope = new IdentifierExpression(_eventGridTopic_Blobs.ResourceName); |
| 202 | + roleAssignment.PrincipalType = RoleManagementPrincipalType.ServicePrincipal; |
| 203 | + roleAssignment.RoleDefinitionId = BicepFunction.GetSubscriptionResourceId("Microsoft.Authorization/roleDefinitions", role.ToString()); |
| 204 | + roleAssignment.PrincipalId = _identity.PrincipalId; |
| 205 | + Add(roleAssignment); |
| 206 | + Add(_systemTopicEventSubscription); |
| 207 | + Add(_eventGridTopic_Blobs); |
| 208 | + |
| 209 | + // Placeholders for now. |
| 210 | + Add(new BicepOutput($"storage_name", typeof(string)) { Value = _storage.Name }); |
| 211 | + Add(new BicepOutput($"servicebus_name", typeof(string)) { Value = _serviceBusNamespace.Name }); |
| 212 | + |
| 213 | + return base.Build(context); |
| 214 | + } |
| 215 | +} |
0 commit comments