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