WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 372046d

Browse files
authored
Handling TCP: prefix in firewall rule creation (#2507)
* Handling TCP: prefix in firewall rule creation * reverting scope change * lower-casing const for readability
1 parent 10784cc commit 372046d

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/Microsoft.SqlTools.ResourceProvider.Core/Firewall/FirewallRuleService.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ IAzureAuthenticationManager AuthenticationManager
5353
/// </summary>
5454
public class FirewallRuleService : IFirewallRuleService
5555
{
56+
57+
/// <summary>
58+
/// TCP prefix for server names
59+
/// </summary>
60+
const string TCP_PREFIX = "tcp:";
61+
5662
/// <summary>
5763
/// Creates firewall rule for given server name and IP address range. Throws exception if operation fails
5864
/// </summary>
@@ -257,6 +263,12 @@ private async Task<IAzureSqlServerResource> FindAzureResourceForSubscriptionAsyn
257263
{
258264
return null;
259265
}
266+
267+
if (serverName.StartsWith(TCP_PREFIX, StringComparison.OrdinalIgnoreCase))
268+
{
269+
serverName = serverName.Substring(TCP_PREFIX.Length);
270+
}
271+
260272
foreach (IAzureSqlServerResource resource in resources)
261273
{
262274
if (serverName.Equals(resource.FullyQualifiedDomainName, StringComparison.OrdinalIgnoreCase))

test/Microsoft.SqlTools.ServiceLayer.UnitTests/ResourceProvider/FirewallRuleServiceTest.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ public async Task CreateShouldFindTheRightResourceGivenValidResourceInMiddle()
241241
await VerifyCreateAsync(testContext, testContext.ServerName);
242242
}
243243

244+
[Test]
245+
public async Task CreateShouldFindTheRightResourceWithTcpPrefix()
246+
{
247+
ServiceTestContext testContext = new ServiceTestContext();
248+
var resources = new List<IAzureSqlServerResource>
249+
{
250+
ServiceTestContext.CreateAzureSqlServer(testContext.ServerName),
251+
};
252+
testContext.SubscriptionToResourcesMap[testContext.ValidSubscription.Subscription.SubscriptionId] = resources;
253+
254+
testContext = CreateMocks(testContext);
255+
256+
await VerifyCreateAsync(testContext, "tcp:" + testContext.ServerName, testContext.ServerName);
257+
}
258+
244259
[Test]
245260
public void CreateThrowExceptionIfResourceNotFound()
246261
{
@@ -305,16 +320,21 @@ public async Task CreateShouldCreateFirewallForTheRightServerFullyQualifiedName(
305320
testContext = CreateMocks(testContext);
306321

307322
await VerifyCreateAsync(testContext, testContext.ServerName);
308-
}
323+
}
324+
325+
private Task<FirewallRuleResponse> VerifyCreateAsync(ServiceTestContext testContext, string serverName, bool verifyFirewallRuleCreated = true)
326+
{
327+
return VerifyCreateAsync(testContext, serverName, serverName, verifyFirewallRuleCreated);
328+
}
309329

310-
private async Task<FirewallRuleResponse> VerifyCreateAsync(ServiceTestContext testContext, string serverName, bool verifyFirewallRuleCreated = true)
330+
private async Task<FirewallRuleResponse> VerifyCreateAsync(ServiceTestContext testContext, string firewallServerName, string azureServerName, bool verifyFirewallRuleCreated = true)
311331
{
312332
try
313333
{
314334
FirewallRuleService service = new FirewallRuleService();
315335
CreateFirewallRuleParams createFirewallRuleParams = new CreateFirewallRuleParams()
316336
{
317-
ServerName = serverName,
337+
ServerName = firewallServerName,
318338
StartIpAddress = testContext.StartIpAddress,
319339
EndIpAddress = testContext.EndIpAddress
320340
};
@@ -325,15 +345,15 @@ private async Task<FirewallRuleResponse> VerifyCreateAsync(ServiceTestContext te
325345
{
326346
testContext.AzureResourceManagerMock.Verify(x => x.CreateFirewallRuleAsync(
327347
It.Is<IAzureResourceManagementSession>(s => s.SubscriptionContext.Subscription.SubscriptionId == testContext.ValidSubscription.Subscription.SubscriptionId),
328-
It.Is<IAzureSqlServerResource>(r => r.FullyQualifiedDomainName == serverName),
348+
It.Is<IAzureSqlServerResource>(r => r.FullyQualifiedDomainName == azureServerName),
329349
It.Is<FirewallRuleRequest>(y => y.EndIpAddress.ToString().Equals(testContext.EndIpAddress) && y.StartIpAddress.ToString().Equals(testContext.StartIpAddress))),
330350
Times.AtLeastOnce);
331351
}
332352
else
333353
{
334354
testContext.AzureResourceManagerMock.Verify(x => x.CreateFirewallRuleAsync(
335355
It.Is<IAzureResourceManagementSession>(s => s.SubscriptionContext.Subscription.SubscriptionId == testContext.ValidSubscription.Subscription.SubscriptionId),
336-
It.Is<IAzureSqlServerResource>(r => r.FullyQualifiedDomainName == serverName),
356+
It.Is<IAzureSqlServerResource>(r => r.FullyQualifiedDomainName == firewallServerName),
337357
It.Is<FirewallRuleRequest>(y => y.EndIpAddress.ToString().Equals(testContext.EndIpAddress) && y.StartIpAddress.ToString().Equals(testContext.StartIpAddress))),
338358
Times.Never);
339359
}

0 commit comments

Comments
 (0)