|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using Azure.Provisioning; |
| 7 | +using Azure.Provisioning.Authorization; |
| 8 | +using Azure.Provisioning.CloudMachine; |
| 9 | +using Azure.Provisioning.EventGrid; |
| 10 | +using Azure.Provisioning.Expressions; |
| 11 | +using Azure.Provisioning.Primitives; |
| 12 | +using Azure.Provisioning.Resources; |
| 13 | +using Azure.Provisioning.ServiceBus; |
| 14 | +using Azure.Provisioning.Storage; |
| 15 | + |
| 16 | +namespace Azure.CloudMachine; |
| 17 | + |
| 18 | +internal class CloudMachineCoreFeature : CloudMachineFeature |
| 19 | +{ |
| 20 | + public CloudMachineCoreFeature() |
| 21 | + { } |
| 22 | + protected override ProvisionableResource EmitCore(CloudMachineInfrastructure infrastructure) |
| 23 | + { |
| 24 | + ManagedServiceIdentity managedServiceIdentity = new() |
| 25 | + { |
| 26 | + ManagedServiceIdentityType = ManagedServiceIdentityType.UserAssigned, |
| 27 | + UserAssignedIdentities = { { BicepFunction.Interpolate($"{infrastructure.Identity.Id}").Compile().ToString(), new UserAssignedIdentityDetails() } } |
| 28 | + }; |
| 29 | + |
| 30 | + var _storage = |
| 31 | + new StorageAccount("cm_storage", StorageAccount.ResourceVersions.V2023_01_01) |
| 32 | + { |
| 33 | + Kind = StorageKind.StorageV2, |
| 34 | + Sku = new StorageSku { Name = StorageSkuName.StandardLrs }, |
| 35 | + IsHnsEnabled = true, |
| 36 | + AllowBlobPublicAccess = false |
| 37 | + }; |
| 38 | + _storage.Identity = managedServiceIdentity; |
| 39 | + _storage.Name = infrastructure.Id; |
| 40 | + |
| 41 | + var _blobs = new BlobService("cm_storage_blobs") |
| 42 | + { |
| 43 | + Parent = _storage, |
| 44 | + }; |
| 45 | + var _container = new BlobContainer("cm_storage_blobs_container", "2023-01-01") |
| 46 | + { |
| 47 | + Parent = _blobs, |
| 48 | + Name = "default" |
| 49 | + }; |
| 50 | + |
| 51 | + var _serviceBusNamespace = new ServiceBusNamespace("cm_servicebus") |
| 52 | + { |
| 53 | + Sku = new ServiceBusSku |
| 54 | + { |
| 55 | + Name = ServiceBusSkuName.Standard, |
| 56 | + Tier = ServiceBusSkuTier.Standard |
| 57 | + }, |
| 58 | + Name = infrastructure.Id, |
| 59 | + }; |
| 60 | + var _serviceBusNamespaceAuthorizationRule = new ServiceBusNamespaceAuthorizationRule("cm_servicebus_auth_rule", "2021-11-01") |
| 61 | + { |
| 62 | + Parent = _serviceBusNamespace, |
| 63 | + Rights = [ServiceBusAccessRight.Listen, ServiceBusAccessRight.Send, ServiceBusAccessRight.Manage] |
| 64 | + }; |
| 65 | + var _serviceBusTopic_private = new ServiceBusTopic("cm_servicebus_topic_private", "2021-11-01") |
| 66 | + { |
| 67 | + Name = "cm_servicebus_topic_private", |
| 68 | + Parent = _serviceBusNamespace, |
| 69 | + MaxMessageSizeInKilobytes = 256, |
| 70 | + DefaultMessageTimeToLive = TimeSpan.FromDays(14), |
| 71 | + RequiresDuplicateDetection = false, |
| 72 | + EnableBatchedOperations = true, |
| 73 | + SupportOrdering = true, |
| 74 | + Status = ServiceBusMessagingEntityStatus.Active |
| 75 | + }; |
| 76 | + var _serviceBusSubscription_private = new ServiceBusSubscription(CloudMachineInfrastructure.SB_PRIVATE_SUB, "2021-11-01") |
| 77 | + { |
| 78 | + Name = CloudMachineInfrastructure.SB_PRIVATE_SUB, |
| 79 | + Parent = _serviceBusTopic_private, |
| 80 | + IsClientAffine = false, |
| 81 | + LockDuration = TimeSpan.FromSeconds(30), |
| 82 | + RequiresSession = false, |
| 83 | + DefaultMessageTimeToLive = TimeSpan.FromDays(14), |
| 84 | + DeadLetteringOnFilterEvaluationExceptions = true, |
| 85 | + DeadLetteringOnMessageExpiration = true, |
| 86 | + MaxDeliveryCount = 10, |
| 87 | + EnableBatchedOperations = true, |
| 88 | + Status = ServiceBusMessagingEntityStatus.Active |
| 89 | + }; |
| 90 | + var _serviceBusTopic_default = new ServiceBusTopic("cm_servicebus_topic_default", "2021-11-01") |
| 91 | + { |
| 92 | + Name = "cm_servicebus_default_topic", |
| 93 | + Parent = _serviceBusNamespace, |
| 94 | + MaxMessageSizeInKilobytes = 256, |
| 95 | + DefaultMessageTimeToLive = TimeSpan.FromDays(14), |
| 96 | + RequiresDuplicateDetection = false, |
| 97 | + EnableBatchedOperations = true, |
| 98 | + SupportOrdering = true, |
| 99 | + Status = ServiceBusMessagingEntityStatus.Active |
| 100 | + }; |
| 101 | + var _serviceBusSubscription_default = new ServiceBusSubscription("cm_servicebus_subscription_default", "2021-11-01") |
| 102 | + { |
| 103 | + Name = "cm_servicebus_subscription_default", |
| 104 | + Parent = _serviceBusTopic_default, |
| 105 | + IsClientAffine = false, |
| 106 | + LockDuration = TimeSpan.FromSeconds(30), |
| 107 | + RequiresSession = false, |
| 108 | + DefaultMessageTimeToLive = TimeSpan.FromDays(14), |
| 109 | + DeadLetteringOnFilterEvaluationExceptions = true, |
| 110 | + DeadLetteringOnMessageExpiration = true, |
| 111 | + MaxDeliveryCount = 10, |
| 112 | + EnableBatchedOperations = true, |
| 113 | + Status = ServiceBusMessagingEntityStatus.Active |
| 114 | + }; |
| 115 | + var _eventGridTopic_blobs = new SystemTopic("cm_eventgrid_topic_blob", "2022-06-15") |
| 116 | + { |
| 117 | + TopicType = "Microsoft.Storage.StorageAccounts", |
| 118 | + Source = _storage.Id, |
| 119 | + Identity = new() |
| 120 | + { |
| 121 | + ManagedServiceIdentityType = ManagedServiceIdentityType.UserAssigned, |
| 122 | + UserAssignedIdentities = { { BicepFunction.Interpolate($"{infrastructure.Identity.Id}").Compile().ToString(), new UserAssignedIdentityDetails() } } |
| 123 | + }, |
| 124 | + Name = infrastructure.Id |
| 125 | + }; |
| 126 | + var _eventGridSubscription_blobs = new SystemTopicEventSubscription("cm_eventgrid_subscription_blob", "2022-06-15") |
| 127 | + { |
| 128 | + Name = "cm-eventgrid-subscription-blob", |
| 129 | + Parent = _eventGridTopic_blobs, |
| 130 | + DeliveryWithResourceIdentity = new DeliveryWithResourceIdentity |
| 131 | + { |
| 132 | + Identity = new EventSubscriptionIdentity |
| 133 | + { |
| 134 | + IdentityType = EventSubscriptionIdentityType.UserAssigned, |
| 135 | + UserAssignedIdentity = infrastructure.Identity.Id |
| 136 | + }, |
| 137 | + Destination = new ServiceBusTopicEventSubscriptionDestination |
| 138 | + { |
| 139 | + ResourceId = _serviceBusTopic_private.Id |
| 140 | + } |
| 141 | + }, |
| 142 | + Filter = new EventSubscriptionFilter |
| 143 | + { |
| 144 | + IncludedEventTypes = |
| 145 | + [ |
| 146 | + "Microsoft.Storage.BlobCreated", |
| 147 | + "Microsoft.Storage.BlobDeleted", |
| 148 | + "Microsoft.Storage.BlobRenamed" |
| 149 | + ], |
| 150 | + IsAdvancedFilteringOnArraysEnabled = true |
| 151 | + }, |
| 152 | + EventDeliverySchema = EventDeliverySchema.EventGridSchema, |
| 153 | + RetryPolicy = new EventSubscriptionRetryPolicy |
| 154 | + { |
| 155 | + MaxDeliveryAttempts = 30, |
| 156 | + EventTimeToLiveInMinutes = 1440 |
| 157 | + } |
| 158 | + }; |
| 159 | + |
| 160 | + infrastructure.AddResource(infrastructure.PrincipalIdParameter); |
| 161 | + infrastructure.AddResource(infrastructure.Identity); |
| 162 | + infrastructure.AddResource(_storage); |
| 163 | + RequiredSystemRoles.Add( |
| 164 | + _storage, |
| 165 | + [ |
| 166 | + (StorageBuiltInRole.GetBuiltInRoleName(StorageBuiltInRole.StorageBlobDataContributor),StorageBuiltInRole.StorageBlobDataContributor.ToString()), |
| 167 | + (StorageBuiltInRole.GetBuiltInRoleName(StorageBuiltInRole.StorageTableDataContributor), StorageBuiltInRole.StorageTableDataContributor.ToString()) |
| 168 | + ]); |
| 169 | + |
| 170 | + infrastructure.AddResource(_container); |
| 171 | + infrastructure.AddResource(_blobs); |
| 172 | + infrastructure.AddResource(_serviceBusNamespace); |
| 173 | + |
| 174 | + RequiredSystemRoles.Add( |
| 175 | + _serviceBusNamespace, |
| 176 | + [ |
| 177 | + (ServiceBusBuiltInRole.GetBuiltInRoleName(ServiceBusBuiltInRole.AzureServiceBusDataOwner), ServiceBusBuiltInRole.AzureServiceBusDataOwner.ToString()), |
| 178 | + ]); |
| 179 | + |
| 180 | + var role = ServiceBusBuiltInRole.AzureServiceBusDataSender; |
| 181 | + RoleAssignment roleAssignment = new RoleAssignment("cm_servicebus_role"); |
| 182 | + roleAssignment.Name = BicepFunction.CreateGuid(_serviceBusNamespace.Id, infrastructure.Identity.Id, BicepFunction.GetSubscriptionResourceId("Microsoft.Authorization/roleDefinitions", role.ToString())); |
| 183 | + roleAssignment.Scope = new IdentifierExpression(_serviceBusNamespace.BicepIdentifier); |
| 184 | + roleAssignment.PrincipalType = RoleManagementPrincipalType.ServicePrincipal; |
| 185 | + roleAssignment.RoleDefinitionId = BicepFunction.GetSubscriptionResourceId("Microsoft.Authorization/roleDefinitions", role.ToString()); |
| 186 | + roleAssignment.PrincipalId = infrastructure.Identity.PrincipalId; |
| 187 | + infrastructure.AddResource(roleAssignment); |
| 188 | + |
| 189 | + // the role assignment must exist before the system topic event subscription is created. |
| 190 | + _eventGridSubscription_blobs.DependsOn.Add(roleAssignment); |
| 191 | + |
| 192 | + infrastructure.AddResource(_serviceBusNamespaceAuthorizationRule); |
| 193 | + infrastructure.AddResource(_serviceBusTopic_private); |
| 194 | + infrastructure.AddResource(_serviceBusTopic_default); |
| 195 | + infrastructure.AddResource(_serviceBusSubscription_private); |
| 196 | + infrastructure.AddResource(_serviceBusSubscription_default); |
| 197 | + infrastructure.AddResource(_eventGridSubscription_blobs); |
| 198 | + infrastructure.AddResource(_eventGridTopic_blobs); |
| 199 | + |
| 200 | + // Placeholders for now. |
| 201 | + infrastructure.AddResource(new ProvisioningOutput($"storage_name", typeof(string)) { Value = _storage.Name }); |
| 202 | + infrastructure.AddResource(new ProvisioningOutput($"servicebus_name", typeof(string)) { Value = _serviceBusNamespace.Name }); |
| 203 | + |
| 204 | + return _storage; |
| 205 | + } |
| 206 | +} |
0 commit comments