Skip to content

Commit 87d2988

Browse files
committed
add changed for regex
1 parent 5a969bc commit 87d2988

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

src/commands/addMIConnections/SettingsAddBaseStep.ts

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ export class SettingsAddBaseStep extends AzureWizardExecuteStep<AddMIConnections
1919
for (const connection of nonNullProp(context, 'connections')) {
2020
if (connection.name.includes('AzureWebJobsStorage')) {
2121
await addStorageConnectionsAndRoles(context, connection, true);
22-
} else if (connection.name.includes('STORAGE')) {
22+
} else if ((/DefaultEndpointsProtocol=https;AccountName=[^;]+;AccountKey=[^;]+;EndpointSuffix=[^;]+/).test(connection.value)) {
2323
await addStorageConnectionsAndRoles(context, connection);
24-
} else if (connection.name.includes('DOCUMENTDB')) {
24+
} else if ((/AccountEndpoint=https:\/\/[^;]+;AccountKey=[^;]+;/).test(connection.value)) {
2525
await addDocumentConnectionsAndRoles(context, connection);
26-
} else if (connection.name.includes('EVENTHUB')) {
27-
await addEventHubConnectionsAndRoles(context, connection);
28-
} else if (connection.name.includes('SERVICEBUS')) {
29-
await addServiceBusConnectionsAndRoles(context, connection);
26+
} else if ((/Endpoint=sb:\/\/[^;]+;SharedAccessKeyName=[^;]+;SharedAccessKey=[^;]+(?:;EntityPath=[^;]+)?/).test(connection.value)) {
27+
await addEventHubServiceBusConnectionsAndRoles(context, connection);
3028
}
3129
}
3230
}
@@ -113,56 +111,66 @@ async function addDocumentConnectionsAndRoles(context: AddMIConnectionsContext,
113111
}
114112
}
115113

116-
async function addEventHubConnectionsAndRoles(context: AddMIConnectionsContext, connection: Connection) {
114+
async function addEventHubServiceBusConnectionsAndRoles(context: AddMIConnectionsContext, connection: Connection) {
117115
// EventHub connection strings are of format: Endpoint=sb://<eventHubNamespace>.servicebus.windows.net/;SharedAccessKeyName=<sharedAccessKeyName>;SharedAccessKey=<sharedAccessKey>;
118116
if (connection.value === '') {
119-
throw new Error(localize('emptyEventHubConnectionString', 'Event hub connection string is empty. Please provide a valid connection string.'));
117+
throw new Error(localize('emptyEventHubConnectionString', 'Connection string is empty. Please provide a valid connection string.'));
120118
}
121119
try {
122-
123-
const eventHubNamespace = connection.value.split(';')[0].split('/')[2].split('.')[0];
120+
const namespace = connection.value.split(';')[0].split('/')[2].split('.')[0];
124121

125122
context.connectionsToAdd?.push(
126123
{
127-
name: `${eventHubNamespace}__fullyQualifiedNamespace`,
128-
value: `${eventHubNamespace}.servicebus.windows.net`,
124+
name: `${namespace}__fullyQualifiedNamespace`,
125+
value: `${namespace}.servicebus.windows.net`,
129126
},
130-
...getClientIdAndCredentialPropertiesForRemote(context, eventHubNamespace)
127+
...getClientIdAndCredentialPropertiesForRemote(context, namespace)
131128
);
132129
if (context.functionapp) {
133-
const scope = await getScopeHelper(context, eventHubNamespace, 'Microsoft.EventHub/Namespaces');
134-
addRole(context, scope, CommonRoleDefinitions.azureEventHubsDataOwner);
135-
addRole(context, scope, CommonRoleDefinitions.azureEventHubsDataReceiver);
130+
let scope = '';
131+
try {
132+
scope = await getScopeHelper(context, namespace, 'Microsoft.EventHub/Namespaces');
133+
} catch (e) {
134+
scope = await getScopeHelper(context, namespace, 'Microsoft.ServiceBus/Namespaces');
135+
}
136+
137+
if (scope.includes('Microsoft.EventHub/Namespaces')) {
138+
addRole(context, scope, CommonRoleDefinitions.azureEventHubsDataOwner);
139+
addRole(context, scope, CommonRoleDefinitions.azureEventHubsDataReceiver);
140+
} else {
141+
addRole(context, scope, CommonRoleDefinitions.azureServiceBusDataOwner);
142+
addRole(context, scope, CommonRoleDefinitions.azureServiceBusDataReceiver);
143+
}
136144
}
137145
} catch (e) {
138146
throw new Error(localize('invalidEventHubConnectionString', 'Unexpected EventHub connection string format: {0}', connection.value));
139147
}
140148
}
141149

142-
async function addServiceBusConnectionsAndRoles(context: AddMIConnectionsContext, connection: Connection) {
143-
// ServiceBus connection strings are of format: Endpoint=sb://<serviceBusNamespace>.servicebus.windows.net/;SharedAccessKeyName=<sharedAccessKeyName>;SharedAccessKey=<sharedAccessKey>;
144-
if (connection.value === '') {
145-
throw new Error(localize('emptyServiceBusConnectionString', 'Service bus connection string is empty. Please provide a valid connection string.'));
146-
}
147-
try {
148-
const serviceBusNamespace = connection.value.split(';')[0].split('/')[2].split('.')[0];
149-
150-
context.connectionsToAdd?.push(
151-
{
152-
name: `${serviceBusNamespace}__fullyQualifiedNamespace`,
153-
value: `${serviceBusNamespace}.servicebus.windows.net`,
154-
},
155-
...getClientIdAndCredentialPropertiesForRemote(context, serviceBusNamespace)
156-
);
157-
if (context.functionapp) {
158-
const scope = await getScopeHelper(context, serviceBusNamespace, 'Microsoft.ServiceBus/Namespaces');
159-
addRole(context, scope, CommonRoleDefinitions.azureServiceBusDataOwner);
160-
addRole(context, scope, CommonRoleDefinitions.azureServiceBusDataReceiver);
161-
}
162-
} catch (e) {
163-
throw new Error(localize('invalidServiceBusConnectionString', 'Unexpected ServiceBus connection string format: {0}', connection.value));
164-
}
165-
}
150+
// async function addServiceBusConnectionsAndRoles(context: AddMIConnectionsContext, connection: Connection) {
151+
// // ServiceBus connection strings are of format: Endpoint=sb://<serviceBusNamespace>.servicebus.windows.net/;SharedAccessKeyName=<sharedAccessKeyName>;SharedAccessKey=<sharedAccessKey>;
152+
// if (connection.value === '') {
153+
// throw new Error(localize('emptyServiceBusConnectionString', 'Service bus connection string is empty. Please provide a valid connection string.'));
154+
// }
155+
// try {
156+
// const serviceBusNamespace = connection.value.split(';')[0].split('/')[2].split('.')[0];
157+
158+
// context.connectionsToAdd?.push(
159+
// {
160+
// name: `${serviceBusNamespace}__fullyQualifiedNamespace`,
161+
// value: `${serviceBusNamespace}.servicebus.windows.net`,
162+
// },
163+
// ...getClientIdAndCredentialPropertiesForRemote(context, serviceBusNamespace)
164+
// );
165+
// if (context.functionapp) {
166+
// const scope = await getScopeHelper(context, serviceBusNamespace, 'Microsoft.ServiceBus/Namespaces');
167+
// addRole(context, scope, CommonRoleDefinitions.azureServiceBusDataOwner);
168+
// addRole(context, scope, CommonRoleDefinitions.azureServiceBusDataReceiver);
169+
// }
170+
// } catch (e) {
171+
// throw new Error(localize('invalidServiceBusConnectionString', 'Unexpected ServiceBus connection string format: {0}', connection.value));
172+
// }
173+
// }
166174

167175
function getClientIdAndCredentialPropertiesForRemote(context: AddMIConnectionsContext, connectionName: string): Connection[] {
168176
const clientIdAndConfigurationProperties: Connection[] = [];

0 commit comments

Comments
 (0)