public override void ExecuteCmdlet() { Utility.ValidateResourceGroupAndResourceName(null, PrimaryServerName); Utility.ValidateResourceGroupAndResourceName(null, SecondaryServerName); RedisResource primaryCache = CacheClient.GetCache(PrimaryServerName); RedisResource secondaryCache = CacheClient.GetCache(SecondaryServerName); ConfirmAction( string.Format(Resources.LinkingRedisCache, SecondaryServerName, PrimaryServerName), PrimaryServerName, () => { RedisLinkedServerWithProperties redisLinkedServer = CacheClient.SetLinkedServer( resourceGroupName: Utility.GetResourceGroupNameFromRedisCacheId(primaryCache.Id), cacheName: primaryCache.Name, linkedCacheName: secondaryCache.Name, linkedCacheId: secondaryCache.Id, linkedCacheLocation: secondaryCache.Location, serverRole: ReplicationRole.Secondary); if (redisLinkedServer == null) { throw new CloudException(string.Format(Resources.LinkedServerCreationFailed, SecondaryServerName, PrimaryServerName)); } WriteObject(new PSRedisLinkedServer(redisLinkedServer)); } ); }
public override void ExecuteCmdlet() { Utility.ValidateResourceGroupAndResourceName(null, Name); Utility.ValidateResourceGroupAndResourceName(null, PrimaryServerName); Utility.ValidateResourceGroupAndResourceName(null, SecondaryServerName); if (!string.IsNullOrWhiteSpace(Name)) { // All links for cache List <PSRedisLinkedServer> list = GetAllLinks(Name); WriteObject(list, true); } else if (!string.IsNullOrWhiteSpace(PrimaryServerName) && !string.IsNullOrWhiteSpace(SecondaryServerName)) { // specific link only string resourceGroupName = CacheClient.GetResourceGroupNameIfNotProvided(null, PrimaryServerName); RedisLinkedServerWithProperties redisLinkedServer = CacheClient.GetLinkedServer( resourceGroupName: resourceGroupName, cacheName: PrimaryServerName, linkedCacheName: SecondaryServerName); if (redisLinkedServer == null || redisLinkedServer.ServerRole != ReplicationRole.Secondary) { throw new CloudException(string.Format(Resources.LinkedServerNotFound, PrimaryServerName, SecondaryServerName)); } WriteObject(new PSRedisLinkedServer(redisLinkedServer)); } else if (!string.IsNullOrWhiteSpace(PrimaryServerName)) { // all primary links only List <PSRedisLinkedServer> list = GetAllLinksByRoleType(PrimaryServerName, ReplicationRole.Primary); WriteObject(list, true); } else { // all secondary links only List <PSRedisLinkedServer> list = GetAllLinksByRoleType(SecondaryServerName, ReplicationRole.Secondary); WriteObject(list, true); } }
internal PSRedisLinkedServer(RedisLinkedServerWithProperties redisLinkedServer) { ProvisioningState = redisLinkedServer.ProvisioningState; if (redisLinkedServer.ServerRole == ReplicationRole.Primary) { /* ID is of the form: * "/subscriptions/<subscription id>/resourceGroups/<resource group name>/providers/Microsoft.Cache/Redis/ * <primary cache name>/linkedServers/<secondary cache name>" */ string[] ele = redisLinkedServer.Id.Split('/'); PrimaryServerName = ele[10]; SecondaryServerName = ele[8]; } else { /* ID is of the form: * "/subscriptions/<subscription id>/resourceGroups/<resource group name>/providers/Microsoft.Cache/Redis/ * <secondary cache name>/linkedServers/<primary cache name>" */ string[] ele = redisLinkedServer.Id.Split('/'); PrimaryServerName = ele[8]; SecondaryServerName = ele[10]; } }
public void GeoReplicationFunctionalTest() { using (var context = MockContext.Start(this.GetType())) { var resourceGroupName = TestUtilities.GenerateName("RedisGeo"); var redisCacheName1 = TestUtilities.GenerateName("RedisGeo1"); var redisCacheName2 = TestUtilities.GenerateName("RedisGeo2"); var _redisCacheManagementHelper = new RedisCacheManagementHelper(this, context); _redisCacheManagementHelper.TryRegisterSubscriptionForResource(); _redisCacheManagementHelper.TryCreateResourceGroup(resourceGroupName, RedisCacheManagementHelper.Location); var _client = RedisCacheManagementTestUtilities.GetRedisManagementClient(this, context); // Create cache in ncus RedisResource ncResponse = _client.Redis.BeginCreate(resourceGroupName, redisCacheName1, parameters: new RedisCreateParameters { Location = RedisCacheManagementHelper.Location, Sku = new Sku() { Name = SkuName.Premium, Family = SkuFamily.P, Capacity = 1 } }); Assert.Contains(redisCacheName1, ncResponse.Id); Assert.Equal(redisCacheName1, ncResponse.Name); Assert.Equal("creating", ncResponse.ProvisioningState, ignoreCase: true); Assert.Equal(SkuName.Premium, ncResponse.Sku.Name); Assert.Equal(SkuFamily.P, ncResponse.Sku.Family); // Create cache in scus RedisResource scResponse = _client.Redis.BeginCreate(resourceGroupName, redisCacheName2, parameters: new RedisCreateParameters { Location = RedisCacheManagementHelper.SecondaryLocation, Sku = new Sku() { Name = SkuName.Premium, Family = SkuFamily.P, Capacity = 1 } }); Assert.Contains(redisCacheName2, scResponse.Id); Assert.Equal(redisCacheName2, scResponse.Name); Assert.Equal("creating", scResponse.ProvisioningState, ignoreCase: true); Assert.Equal(SkuName.Premium, scResponse.Sku.Name); Assert.Equal(SkuFamily.P, scResponse.Sku.Family); // Wait for both cache creation to comeplete for (int i = 0; i < 120; i++) { ncResponse = _client.Redis.Get(resourceGroupName, redisCacheName1); scResponse = _client.Redis.Get(resourceGroupName, redisCacheName2); if ("succeeded".Equals(ncResponse.ProvisioningState, StringComparison.OrdinalIgnoreCase) && "succeeded".Equals(scResponse.ProvisioningState, StringComparison.OrdinalIgnoreCase)) { break; } TestUtilities.Wait(new TimeSpan(0, 0, 30)); } // Fail if any of 2 cache is not created successfully Assert.Equal("succeeded", ncResponse.ProvisioningState, ignoreCase: true); Assert.Equal("succeeded", scResponse.ProvisioningState, ignoreCase: true); // Set up replication link RedisLinkedServerWithProperties linkServerWithProperties = _client.LinkedServer.Create(resourceGroupName, redisCacheName1, redisCacheName2, new RedisLinkedServerCreateParameters { LinkedRedisCacheId = scResponse.Id, LinkedRedisCacheLocation = RedisCacheManagementHelper.SecondaryLocation, ServerRole = ReplicationRole.Secondary }); Assert.Equal(redisCacheName2, linkServerWithProperties.Name); Assert.Equal(scResponse.Id, linkServerWithProperties.LinkedRedisCacheId); Assert.Equal(RedisCacheManagementHelper.SecondaryLocation, linkServerWithProperties.LinkedRedisCacheLocation); Assert.Equal(ReplicationRole.Secondary, linkServerWithProperties.ServerRole); Assert.Equal("succeeded", linkServerWithProperties.ProvisioningState, ignoreCase: true); // test get response from primary RedisLinkedServerWithProperties primaryLinkProperties = _client.LinkedServer.Get(resourceGroupName, redisCacheName1, redisCacheName2); Assert.Equal(scResponse.Id, primaryLinkProperties.LinkedRedisCacheId); Assert.Equal(RedisCacheManagementHelper.SecondaryLocation, primaryLinkProperties.LinkedRedisCacheLocation); Assert.Equal(ReplicationRole.Secondary, primaryLinkProperties.ServerRole); // test list response from primary IPage <RedisLinkedServerWithProperties> allPrimaryLinkProperties = _client.LinkedServer.List(resourceGroupName, redisCacheName1); Assert.Single(allPrimaryLinkProperties); // test get response from secondary RedisLinkedServerWithProperties secondaryLinkProperties = _client.LinkedServer.Get(resourceGroupName, redisCacheName2, redisCacheName1); Assert.Equal(ncResponse.Id, secondaryLinkProperties.LinkedRedisCacheId); Assert.Equal(RedisCacheManagementHelper.Location, secondaryLinkProperties.LinkedRedisCacheLocation); Assert.Equal(ReplicationRole.Primary, secondaryLinkProperties.ServerRole); // test list response from secondary IPage <RedisLinkedServerWithProperties> allSecondaryLinkProperties = _client.LinkedServer.List(resourceGroupName, redisCacheName2); Assert.Single(allSecondaryLinkProperties); // Delete link on primary _client.LinkedServer.Delete(resourceGroupName, redisCacheName1, redisCacheName2); // links should disappear in 5 min IPage <RedisLinkedServerWithProperties> afterDeletePrimaryLinkProperties = null; IPage <RedisLinkedServerWithProperties> afterDeleteSecondaryLinkProperties = null; for (int i = 0; i < 10; i++) { TestUtilities.Wait(new TimeSpan(0, 0, 30)); afterDeletePrimaryLinkProperties = _client.LinkedServer.List(resourceGroupName, redisCacheName1); afterDeleteSecondaryLinkProperties = _client.LinkedServer.List(resourceGroupName, redisCacheName2); if (afterDeletePrimaryLinkProperties.Count() == 0 && afterDeleteSecondaryLinkProperties.Count() == 0) { break; } } Assert.NotNull(afterDeletePrimaryLinkProperties); Assert.Empty(afterDeletePrimaryLinkProperties); Assert.NotNull(afterDeleteSecondaryLinkProperties); Assert.Empty(afterDeleteSecondaryLinkProperties); // Clean up both caches and delete resource group _client.Redis.Delete(resourceGroupName: resourceGroupName, name: redisCacheName1); _client.Redis.Delete(resourceGroupName: resourceGroupName, name: redisCacheName2); _redisCacheManagementHelper.DeleteResourceGroup(resourceGroupName); } }