public void AssociateReservedIP( string usWestLocStr, string location, string storageAccountName, ref bool storageAccountCreated, string serviceName, string deploymentName, string reserveIpName, ref bool hostedServiceCreated, ref bool reserveIpCreated) { if (this.ManagementClient.Locations.List().Any( c => string.Equals(c.Name, usWestLocStr, StringComparison.OrdinalIgnoreCase))) { location = usWestLocStr; } this.CreateStorageAccount(location, storageAccountName, out storageAccountCreated); this.CreateHostedService(location, serviceName, out hostedServiceCreated); this.CreatePaaSDeployment(storageAccountName, serviceName, deploymentName, NetworkTestConstants.OneWebOneWorkerPkgFilePath, "OneWebOneWorker.cscfg", true); NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters { Name = reserveIpName, Location = location, Label = "SampleReserveIPLabel", }; OperationStatusResponse reserveIpCreate = this.NetworkClient.ReservedIPs.Create(reservedIpCreatePars); Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK); reserveIpCreated = true; NetworkReservedIPGetResponse reserveIpCreationResponse = this.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK); NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters { ServiceName = serviceName, DeploymentName = deploymentName }; OperationStatusResponse responseAssociateRip = this.NetworkClient.ReservedIPs.Associate(reserveIpName, pars); Assert.True(responseAssociateRip.StatusCode == HttpStatusCode.OK); NetworkReservedIPGetResponse receivedReservedIpFromRdfe = this.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK); Assert.True(serviceName == receivedReservedIpFromRdfe.ServiceName); Assert.True(receivedReservedIpFromRdfe.InUse == true); Assert.True(deploymentName == receivedReservedIpFromRdfe.DeploymentName); }
private static void AssociateReservedIP(ManagementClient managementClient, string usWestLocStr, string location, StorageManagementClient storageClient, string storageAccountName, ref bool storageAccountCreated, ComputeManagementClient computeClient, string serviceName, string deploymentName, string reserveIpName, NetworkTestBase _testFixture, ref bool hostedServiceCreated, ref bool reserveIpCreated) { if (managementClient.Locations.List().Any( c => string.Equals(c.Name, usWestLocStr, StringComparison.OrdinalIgnoreCase))) { location = usWestLocStr; } CreateStorageAccount(location, storageClient, storageAccountName, out storageAccountCreated); CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated); CreatePaaSDeployment(storageAccountName, computeClient, serviceName, deploymentName); NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters { Name = reserveIpName, Location = "uswest", Label = "SampleReserveIPLabel" }; OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars); Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK); reserveIpCreated = true; NetworkReservedIPGetResponse reserveIpCreationResponse = _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK); NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters { ServiceName = serviceName, DeploymentName = deploymentName }; OperationStatusResponse responseAssociateRip = _testFixture.NetworkClient.ReservedIPs.Associate(reserveIpName, pars); Assert.True(responseAssociateRip.StatusCode == HttpStatusCode.OK); NetworkReservedIPGetResponse receivedReservedIpFromRdfe = _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK); Assert.True(serviceName == receivedReservedIpFromRdfe.ServiceName); Assert.True(receivedReservedIpFromRdfe.InUse == true); Assert.True(deploymentName == receivedReservedIpFromRdfe.DeploymentName); }
private static void DisassociateReservedIP(NetworkTestBase _testFixture, string reserveIpName, string serviceName, string deploymentName) { NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters { ServiceName = serviceName, DeploymentName = deploymentName }; OperationStatusResponse responseDisassociateRip = _testFixture.NetworkClient.ReservedIPs.Disassociate(reserveIpName, pars); Assert.True(responseDisassociateRip.StatusCode == HttpStatusCode.OK); NetworkReservedIPGetResponse receivedReservedIpFromRdfe = _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK); Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.ServiceName)); Assert.True(receivedReservedIpFromRdfe.InUse == false); Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.DeploymentName)); }
/// <summary> /// Checks for the existence of a specific Azure Reserved IP, if it doesn't exist it will create it. /// </summary> /// <param name="client">The <see cref="NetworkManagementClient"/> that is performing the operation.</param> /// <param name="ipName">The name of the Reserved IP.</param> /// <param name="location">The Azure location of the Reserved IP.</param> /// <returns>The async <see cref="Task"/> wrapper.</returns> public static async Task ReserveIpIfNotReservedAsync(this NetworkManagementClient client, string ipName, string location) { Contract.Requires(client != null); Contract.Requires(!string.IsNullOrWhiteSpace(ipName)); Contract.Requires(!string.IsNullOrWhiteSpace(location)); NetworkReservedIPGetResponse service = null; FlexStreams.Publish(new CheckIfExistsEvent(AzureResource.ReservedIp, ipName)); try { service = await client.ReservedIPs.GetAsync(ipName); } catch (CloudException cex) { if (cex.Error.Code != "ResourceNotFound") { throw; } } if (service != null) { FlexStreams.Publish(new FoundExistingEvent(AzureResource.ReservedIp, ipName)); return; } await client.ReservedIPs.CreateAsync( new NetworkReservedIPCreateParameters { Name = ipName, Location = location }); FlexStreams.Publish(new ProvisionEvent(AzureResource.ReservedIp, ipName)); }
/// <summary> /// Preview Only. The Get Reserved IP operation retrieves the details /// for the virtual IP reserved for the subscription. /// </summary> /// <param name='ipName'> /// Required. The name of the reserved IP to retrieve. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Preview Only. A reserved IP associated with your subscription. /// </returns> public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Network.Models.NetworkReservedIPGetResponse> GetAsync(string ipName, CancellationToken cancellationToken) { // Validate if (ipName == null) { throw new ArgumentNullException("ipName"); } // Tracing bool shouldTrace = CloudContext.Configuration.Tracing.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = Tracing.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("ipName", ipName); Tracing.Enter(invocationId, this, "GetAsync", tracingParameters); } // Construct URL string baseUrl = this.Client.BaseUri.AbsoluteUri; string url = "/" + this.Client.Credentials.SubscriptionId.Trim() + "/services/networking/reservedips/" + ipName.Trim(); // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Get; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("x-ms-version", "2013-11-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { Tracing.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { Tracing.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false), CloudExceptionType.Xml); if (shouldTrace) { Tracing.Error(invocationId, ex); } throw ex; } // Create Result NetworkReservedIPGetResponse result = null; // Deserialize Response cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new NetworkReservedIPGetResponse(); XDocument responseDoc = XDocument.Parse(responseContent); XElement reservedIPElement = responseDoc.Element(XName.Get("ReservedIP", "http://schemas.microsoft.com/windowsazure")); if (reservedIPElement != null && reservedIPElement.IsEmpty == false) { XElement nameElement = reservedIPElement.Element(XName.Get("Name", "http://schemas.microsoft.com/windowsazure")); if (nameElement != null && nameElement.IsEmpty == false) { string nameInstance = nameElement.Value; result.Name = nameInstance; } XElement addressElement = reservedIPElement.Element(XName.Get("Address", "http://schemas.microsoft.com/windowsazure")); if (addressElement != null && addressElement.IsEmpty == false) { string addressInstance = addressElement.Value; result.Address = addressInstance; } XElement idElement = reservedIPElement.Element(XName.Get("Id", "http://schemas.microsoft.com/windowsazure")); if (idElement != null && idElement.IsEmpty == false) { string idInstance = idElement.Value; result.Id = idInstance; } XElement labelElement = reservedIPElement.Element(XName.Get("Label", "http://schemas.microsoft.com/windowsazure")); if (labelElement != null && labelElement.IsEmpty == false) { string labelInstance = labelElement.Value; result.Label = labelInstance; } XElement affinityGroupElement = reservedIPElement.Element(XName.Get("AffinityGroup", "http://schemas.microsoft.com/windowsazure")); if (affinityGroupElement != null && affinityGroupElement.IsEmpty == false) { string affinityGroupInstance = affinityGroupElement.Value; result.AffinityGroup = affinityGroupInstance; } XElement stateElement = reservedIPElement.Element(XName.Get("State", "http://schemas.microsoft.com/windowsazure")); if (stateElement != null && stateElement.IsEmpty == false) { string stateInstance = stateElement.Value; result.State = stateInstance; } XElement inUseElement = reservedIPElement.Element(XName.Get("InUse", "http://schemas.microsoft.com/windowsazure")); if (inUseElement != null && inUseElement.IsEmpty == false) { bool inUseInstance = bool.Parse(inUseElement.Value); result.InUse = inUseInstance; } XElement serviceNameElement = reservedIPElement.Element(XName.Get("ServiceName", "http://schemas.microsoft.com/windowsazure")); if (serviceNameElement != null && serviceNameElement.IsEmpty == false) { string serviceNameInstance = serviceNameElement.Value; result.ServiceName = serviceNameInstance; } XElement deploymentNameElement = reservedIPElement.Element(XName.Get("DeploymentName", "http://schemas.microsoft.com/windowsazure")); if (deploymentNameElement != null && deploymentNameElement.IsEmpty == false) { string deploymentNameInstance = deploymentNameElement.Value; result.DeploymentName = deploymentNameInstance; } } result.StatusCode = statusCode; if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { Tracing.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }
public void TestAssociateDisassociateOnMultivipIaaSDeployment() { using (var undoContext = AZT.UndoContext.Current) { undoContext.Start(); using (NetworkTestBase _testFixture = new NetworkTestBase()) { bool hostedServiceCreated = false; bool storageAccountCreated = false; string storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower(); string serviceName = AZT.TestUtilities.GenerateName("testser"); string deploymentName = AZT.TestUtilities.GenerateName("dep"); ComputeManagementClient computeClient = _testFixture.GetComputeManagementClient(); ManagementClient managementClient = _testFixture.ManagementClient; StorageManagementClient storageClient = _testFixture.GetStorageManagementClient(); List <string> createdRips = new List <string>(); try { string location = Utilities.GetTestLocation(managementClient); Assert.True(!string.IsNullOrEmpty(location)); // Create hosted service Utilities.CreateHostedService(location, computeClient, serviceName, out hostedServiceCreated); Assert.True(hostedServiceCreated); // Create storage account storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower(); Utilities.CreateStorageAccount(location, storageClient, storageAccountName, out storageAccountCreated); Assert.True(storageAccountCreated); List <string> vipNames = new List <string>() { AZT.TestUtilities.GenerateName("VipA"), AZT.TestUtilities.GenerateName("VipB"), AZT.TestUtilities.GenerateName("VipC"), AZT.TestUtilities.GenerateName("VipD"), AZT.TestUtilities.GenerateName("VipE") }; List <string> reservedIPNames = new List <string>() { AZT.TestUtilities.GenerateName("RipA"), AZT.TestUtilities.GenerateName("RipB"), AZT.TestUtilities.GenerateName("RipC"), AZT.TestUtilities.GenerateName("RipD"), AZT.TestUtilities.GenerateName("RipE") }; CreateMultivipDeploymentAndAssertSuccess(_testFixture.NetworkClient, computeClient, vipNames, serviceName, deploymentName, storageAccountName, location); // Associate 5 reserved IPs for (int i = 0; i < 5; i++) { string reserveIpName = reservedIPNames[i]; string vipName = vipNames[i]; NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters { Name = reserveIpName, Location = location, Label = "SampleReserveIPLabel" }; OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars); Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK); createdRips.Add(reserveIpName); NetworkReservedIPGetResponse reserveIpCreationResponse = _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK); NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters { ServiceName = serviceName, DeploymentName = deploymentName, VirtualIPName = vipName }; OperationStatusResponse responseAssociateRip = _testFixture.NetworkClient.ReservedIPs.Associate(reserveIpName, pars); Assert.True(responseAssociateRip.StatusCode == HttpStatusCode.OK); DeploymentGetResponse deploymentResponse = computeClient.Deployments.GetByName(serviceName: serviceName, deploymentName: deploymentName); NetworkReservedIPGetResponse receivedReservedIpFromRdfe = _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK); Assert.True(serviceName == receivedReservedIpFromRdfe.ServiceName); Assert.True(receivedReservedIpFromRdfe.InUse == true); Assert.True(deploymentName == receivedReservedIpFromRdfe.DeploymentName); Assert.True(reserveIpName == receivedReservedIpFromRdfe.Name); Assert.True(vipName == receivedReservedIpFromRdfe.VirtualIPName); var vipAssociated = deploymentResponse.VirtualIPAddresses.FirstOrDefault(vip => vip.Name == vipName); Assert.NotNull(vipAssociated); Assert.True(vipAssociated.ReservedIPName == reserveIpName); } // Disassociate the associated IPs for (int i = 0; i < 5; i++) { string reserveIpName = reservedIPNames[i]; string vipName = vipNames[i]; NetworkReservedIPMobilityParameters pars = new NetworkReservedIPMobilityParameters { ServiceName = serviceName, DeploymentName = deploymentName, VirtualIPName = vipName }; OperationStatusResponse responseDisassociateRip = _testFixture.NetworkClient.ReservedIPs.Disassociate(reserveIpName, pars); Assert.True(responseDisassociateRip.StatusCode == HttpStatusCode.OK); DeploymentGetResponse deploymentResponse = computeClient.Deployments.GetByName(serviceName: serviceName, deploymentName: deploymentName); NetworkReservedIPGetResponse receivedReservedIpFromRdfe = _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(receivedReservedIpFromRdfe.StatusCode == HttpStatusCode.OK); Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.ServiceName)); Assert.True(receivedReservedIpFromRdfe.InUse == false); Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.DeploymentName)); Assert.True(reserveIpName == receivedReservedIpFromRdfe.Name); Assert.True(string.IsNullOrEmpty(receivedReservedIpFromRdfe.VirtualIPName)); var vipAssociated = deploymentResponse.VirtualIPAddresses.FirstOrDefault(vip => vip.Name == vipName); Assert.NotNull(vipAssociated); Assert.True(string.IsNullOrEmpty(vipAssociated.ReservedIPName)); } } finally { if (hostedServiceCreated) { computeClient.HostedServices.DeleteAll(serviceName); } if (createdRips.Any()) { foreach (var rip in createdRips) { // Clean up created Reserved IPs _testFixture.NetworkClient.ReservedIPs.Delete(rip); } } } } } }
public void TestReservingExistingDeploymentIP() { using (var undoContext = AZT.UndoContext.Current) { undoContext.Start(); using (NetworkTestBase _testFixture = new NetworkTestBase()) { var managementClient = _testFixture.ManagementClient; bool storageAccountCreated = false; bool hostedServiceCreated = false; string storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower(); string serviceName = AZT.TestUtilities.GenerateName("testser"); string deploymentName = string.Format("{0}Prd", serviceName); string reserveIpName = HttpMockServer.GetAssetName("res", "testres").ToLower(); string location = managementClient.GetDefaultLocation("Storage", "Compute"); bool reservedIpCreated = false; try { _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated); _testFixture.CreateHostedService(location, serviceName, out hostedServiceCreated); var deployment = _testFixture.CreatePaaSDeployment(storageAccountName, serviceName, deploymentName, NetworkTestConstants.OneWebOneWorkerPkgFilePath, "OneWebOneWorker.cscfg", startDeployment: true); NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters { Name = reserveIpName, Label = "TestLabel", DeploymentName = deploymentName, ServiceName = serviceName, Location = location }; OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars); Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK); reservedIpCreated = true; NetworkReservedIPGetResponse reserveIpCreationResponse = _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK); Assert.True(reserveIpCreationResponse.ServiceName == serviceName); Assert.True(reserveIpCreationResponse.DeploymentName == deploymentName); Assert.True(reserveIpCreationResponse.InUse == true); Assert.True(reserveIpCreationResponse.Address == deployment.VirtualIPAddresses[0].Address); } catch (Exception) { throw; } finally { if (storageAccountCreated) { _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName); } if (hostedServiceCreated) { _testFixture.ComputeClient.HostedServices.DeleteAll(serviceName); } if (reservedIpCreated) { _testFixture.NetworkClient.ReservedIPs.Delete(reserveIpName); } } } } }
public void TestReserveIPWithIPTagsSimple() { using (var undoContext = AZT.UndoContext.Current) { undoContext.Start(); using (NetworkTestBase _testFixture = new NetworkTestBase()) { var managementClient = _testFixture.ManagementClient; bool storageAccountCreated = false; string storageAccountName = HttpMockServer.GetAssetName("tststr1234", "tststr").ToLower(); string reserveIpName = HttpMockServer.GetAssetName("res", "testresIPtag").ToLower(); string location = "West Central US"; bool reservedIpCreated = false; IPTag iptag = new IPTag(); iptag.IPTagType = "FirstPartyUsage"; iptag.Value = "/tagTypes/SystemService/operators/Microsoft/platforms/Azure/services/Microsoft.AzureAD"; List <IPTag> iptags = new List <IPTag>(); iptags.Add(iptag); try { _testFixture.CreateStorageAccount(location, storageAccountName, out storageAccountCreated); NetworkReservedIPCreateParameters reservedIpCreatePars = new NetworkReservedIPCreateParameters { Name = reserveIpName, Label = "TestResTagLabel", Location = location, IPTags = iptags }; OperationStatusResponse reserveIpCreate = _testFixture.NetworkClient.ReservedIPs.Create(reservedIpCreatePars); Assert.True(reserveIpCreate.StatusCode == HttpStatusCode.OK); reservedIpCreated = true; NetworkReservedIPGetResponse reserveIpCreationResponse = _testFixture.NetworkClient.ReservedIPs.Get(reserveIpName); Assert.True(reserveIpCreationResponse.StatusCode == HttpStatusCode.OK); Assert.True(reserveIpCreationResponse.IPTags.Count == iptags.Count); foreach (var iptag1 in iptags) { Assert.True(reserveIpCreationResponse.IPTags.Any(x => x.IPTagType == iptag1.IPTagType && x.Value == iptag1.Value)); } } catch (Exception ex) { Console.WriteLine("TestReserveIPWithIPTagsSimple test did not succeed with error being ," + ex.Message); throw; } finally { if (storageAccountCreated) { _testFixture.StorageClient.StorageAccounts.Delete(storageAccountName); } if (reservedIpCreated) { _testFixture.NetworkClient.ReservedIPs.Delete(reserveIpName); } } } } }