Skip to content

Commit

Permalink
Merge pull request #1 from AsrOneSdk/sriramvu-dev
Browse files Browse the repository at this point in the history
Network and Storage operations (E2E, E2A)
  • Loading branch information
sriramvu committed Dec 29, 2014
2 parents 0a5ea1c + 325b10e commit 1c98d6c
Show file tree
Hide file tree
Showing 20 changed files with 1,454 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ private SiteRecoveryManagementClient GetSiteRecoveryManagementClient()
return new SiteRecoveryManagementClient(
asrVaultCreds.CloudServiceName,
asrVaultCreds.ResourceName,
(SubscriptionCloudCredentials)environment.Credentials,
AzureSession.CurrentContext.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement)).WithHandler(HttpMockServer.CreateInstance());
RecoveryServicesMgmtClient.Credentials,
RecoveryServicesMgmtClient.BaseUri).WithHandler(HttpMockServer.CreateInstance());
}

private static bool IgnoreCertificateErrorHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
</Reference>
<Reference Include="Microsoft.WindowsAzure.Management.SiteRecovery, Version=0.9.0.0, Culture=neutral, PublicKeyToken=c66ce9294aae1300, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.RecoveryServices.0.1.1-preview\lib\net40\Microsoft.WindowsAzure.Management.SiteRecovery.dll</HintPath>
<HintPath>..\..\..\packages\Microsoft.Azure.Management.RecoveryServices.0.2.2-preview\lib\net40\Microsoft.WindowsAzure.Management.SiteRecovery.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -97,6 +97,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="lib\PSStorageObjects.cs" />
<Compile Include="lib\PSNetworkObjects.cs" />
<Compile Include="lib\PSContracts.cs" />
<Compile Include="lib\PSObjects.cs" />
<Compile Include="lib\PSParameterSets.cs" />
Expand All @@ -106,6 +108,10 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesStorageClient.cs" />
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesStorageMappingClient.cs" />
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesNetworkMappingClient.cs" />
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesNetworkClient.cs" />
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesVMGroupClient.cs" />
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesPEClient.cs" />
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesClient.cs">
Expand All @@ -118,6 +124,14 @@
<Compile Include="PSRecoveryServicesClient\PSRecoveryServicesVMClient.cs" />
<Compile Include="RecoveryServicesCmdletBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Service\GetAzureSiteRecoveryStorage.cs" />
<Compile Include="Service\GetAzureSiteRecoveryStorageMapping.cs" />
<Compile Include="Service\NewAzureSiteRecoveryStorageMapping.cs" />
<Compile Include="Service\RemoveAzureSiteRecoveryStorageMapping.cs" />
<Compile Include="Service\RemoveAzureSiteRecoveryNetworkMapping.cs" />
<Compile Include="Service\NewAzureSiteRecoveryNetworkMapping.cs" />
<Compile Include="Service\GetAzureSiteRecoveryNetworkMapping.cs" />
<Compile Include="Service\GetAzureSiteRecoveryNetwork.cs" />
<Compile Include="Service\CreateAzureSiteRecoveryRecoveryPlan.cs" />
<Compile Include="Service\GetAzureSiteRecoveryRecoveryPlanFile.cs" />
<Compile Include="Service\RemoveAzureSiteRecoveryRecoveryPlan.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Management.SiteRecovery;
using Microsoft.WindowsAzure.Management.SiteRecovery.Models;

namespace Microsoft.Azure.Commands.RecoveryServices
{
/// <summary>
/// Recovery services convenience client.
/// </summary>
public partial class PSRecoveryServicesClient
{
/// <summary>
/// Gets Azure Site Recovery Networks.
/// </summary>
/// <param name="serverId">Server ID</param>
/// <returns>Network list response</returns>
public NetworkListResponse GetAzureSiteRecoveryNetworks(string serverId)
{
return this.GetSiteRecoveryClient().Networks.List(serverId, this.GetRequestHeaders());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Management.SiteRecovery;
// using Microsoft.WindowsAzure.Management.
using Microsoft.WindowsAzure.Management.SiteRecovery.Models;
using Microsoft.WindowsAzure.Commands.Common.Models;
using Microsoft.WindowsAzure.Commands.Common;
using System.Collections.Generic;

namespace Microsoft.Azure.Commands.RecoveryServices
{
/// <summary>
/// Target network type.
/// </summary>
public enum TargetNetworkType
{
/// <summary>
/// Server.
/// </summary>
Server = 0,

/// <summary>
/// Azure.
/// </summary>
Azure,
}

[DataContract(Namespace = "http://schemas.microsoft.com/windowsazure")]
public class CreateNetworkMappingInput
{
[DataMember(Order = 1)]
public string PrimaryServerId { get; set; }

[DataMember(Order = 2)]
public string PrimaryNetworkId { get; set; }

[DataMember(Order = 3)]
public string RecoveryServerId { get; set; }

[DataMember(Order = 4)]
public string RecoveryNetworkId { get; set; }
}

[DataContract(Namespace = "http://schemas.microsoft.com/windowsazure")]
public class CreateAzureNetworkMappingInput
{
[DataMember(Order = 1)]
public string PrimaryServerId { get; set; }

[DataMember(Order = 2)]
public string PrimaryNetworkId { get; set; }

[DataMember(Order = 3)]
public string AzureVMNetworkId { get; set; }

[DataMember(Order = 4)]
public string AzureVMNetworkName { get; set; }
}

/// <summary>
/// Recovery services convenience client.
/// </summary>
public partial class PSRecoveryServicesClient
{
/// <summary>
/// Gets Azure Site Recovery Network mappings.
/// </summary>
/// <param name="primaryServerId">Primary server ID</param>
/// <param name="recoveryServerId">Recovery server ID</param>
/// <returns>Network mapping list response</returns>
public NetworkMappingListResponse GetAzureSiteRecoveryNetworkMappings(
string primaryServerId,
string recoveryServerId)
{
return this.GetSiteRecoveryClient()
.NetworkMappings
.List(primaryServerId, recoveryServerId, this.GetRequestHeaders());
}

/// <summary>
/// Create Azure Site Recovery Network Mapping.
/// </summary>
/// <param name="primaryServerId">Primary server Id</param>
/// <param name="primaryNetworkId">Primary network Id</param>
/// <param name="recoveryServerId">Recovery server Id</param>
/// <param name="recoveryNetworkId">Recovery network Id</param>
/// <returns>Job response</returns>
public JobResponse NewAzureSiteRecoveryNetworkMapping(
string primaryServerId,
string primaryNetworkId,
string recoveryServerId,
string recoveryNetworkId)
{
CreateNetworkMappingInput createNetworkMappingInput =
new CreateNetworkMappingInput();
createNetworkMappingInput.PrimaryServerId = primaryServerId;
createNetworkMappingInput.PrimaryNetworkId = primaryNetworkId;
createNetworkMappingInput.RecoveryServerId = recoveryServerId;
createNetworkMappingInput.RecoveryNetworkId = recoveryNetworkId;

NetworkMappingInput networkMappingInput = new NetworkMappingInput();
networkMappingInput.TargetNetworkType = TargetNetworkType.Server.ToString();
networkMappingInput.CreateNetworkMappingInput =
DataContractUtils.Serialize<CreateNetworkMappingInput>(createNetworkMappingInput);
return this.GetSiteRecoveryClient()
.NetworkMappings
.Create(networkMappingInput, this.GetRequestHeaders());
}

/// <summary>
/// Create Azure Site Recovery Azure Network Mapping.
/// </summary>
/// <param name="primaryServerId">Primary server Id</param>
/// <param name="primaryNetworkId">Primary network Id</param>
/// <param name="recoveryNetworkName">Recovery server Id</param>
/// <param name="recoveryNetworkId">Recovery network Id</param>
/// <returns>Job response</returns>
public JobResponse NewAzureSiteRecoveryAzureNetworkMapping(
string primaryServerId,
string primaryNetworkId,
string recoveryNetworkName,
string recoveryNetworkId)
{
CreateAzureNetworkMappingInput createAzureNetworkMappingInput =
new CreateAzureNetworkMappingInput();
createAzureNetworkMappingInput.PrimaryServerId = primaryServerId;
createAzureNetworkMappingInput.PrimaryNetworkId = primaryNetworkId;
createAzureNetworkMappingInput.AzureVMNetworkName = recoveryNetworkName;
createAzureNetworkMappingInput.AzureVMNetworkId = recoveryNetworkId;

NetworkMappingInput networkMappingInput = new NetworkMappingInput();
networkMappingInput.TargetNetworkType = TargetNetworkType.Azure.ToString();
networkMappingInput.CreateNetworkMappingInput =
DataContractUtils.Serialize<CreateAzureNetworkMappingInput>(createAzureNetworkMappingInput);
return this.GetSiteRecoveryClient()
.NetworkMappings
.Create(networkMappingInput, this.GetRequestHeaders());
}

public void ValidateSubscriptionAccountAssociation(string azureSubscriptionId)
{
bool associatedSubscription = false;
ProfileClient pc = new ProfileClient();
List<AzureSubscription> subscriptions =
pc.RefreshSubscriptions(AzureSession.CurrentContext.Environment);

foreach (AzureSubscription sub in subscriptions)
{
if (azureSubscriptionId.Equals(sub.Id.ToString(), StringComparison.OrdinalIgnoreCase))
{
associatedSubscription = true;
break;
}
}

if (!associatedSubscription)
{
throw new InvalidOperationException(
string.Format(
Properties.Resources.SubscriptionIsNotAssociatedWithTheAccount,
azureSubscriptionId));
}
}

public void ValidateVMNetworkSubscriptionAssociation(string subscriptionId, string azureNetworkId)
{
/*
NetworkManagementClient networkClient =
AzureSession.ClientFactory.CreateClient<NetworkManagementClient>(AzureSession.CurrentContext.Subscription, AzureEnvironment.Endpoint.ServiceManagement);
var response = this.networkClient.Networks.List();
var sites = response.VirtualNetworkSites;
*/
}
/// <summary>
/// Delete Azure Site Recovery Network Mapping.
/// </summary>
/// <param name="primaryServerId">Primary server Id</param>
/// <param name="primaryNetworkId">Primary network Id</param>
/// <param name="recoveryServerId">Recovery server Id</param>
/// <returns>Job response</returns>
public JobResponse RemoveAzureSiteRecoveryNetworkMapping(
string primaryServerId,
string primaryNetworkId,
string recoveryServerId)
{
NetworkUnMappingInput networkUnMappingInput = new NetworkUnMappingInput();
networkUnMappingInput.PrimaryServerId = primaryServerId;
networkUnMappingInput.PrimaryNetworkId = primaryNetworkId;
networkUnMappingInput.RecoveryServerId = recoveryServerId;

return this.GetSiteRecoveryClient()
.NetworkMappings
.Delete(networkUnMappingInput, this.GetRequestHeaders());
}
}

/// <summary>
/// Helper around serialization/deserialization of objects. This one is a thin wrapper around
/// DataContractUtils<T> which is the one doing the heavy lifting.
/// </summary>
public static class DataContractUtils
{
/// <summary>
/// Serializes the supplied object to the string.
/// </summary>
/// <typeparam name="T">The object type.</typeparam>
/// <param name="The object to serialize."></param>
/// <returns>Serialized string.</returns>
public static string Serialize<T>(T obj)
{
return DataContractUtils<T>.Serialize(obj);
}

/// <summary>
/// Deserialize the string to the expected object type.
/// </summary>
/// <param name="xmlString">Serialized string.</param>
/// <param name="result">Deserialized object.</param>
public static void Deserialize<T>(string xmlString, out T result)
{
result = DataContractUtils<T>.Deserialize(xmlString);
}
}

public static class DataContractUtils<T>
{
/// <summary>
/// Serializes the propertyBagContainer to the string.
/// </summary>
/// <param name="propertyBagContainer"></param>
/// <returns></returns>
public static string Serialize(T propertyBagContainer)
{
var serializer = new DataContractSerializer(typeof(T));
string xmlString;
StringWriter sw = null;
try
{
sw = new StringWriter();
using (var writer = new XmlTextWriter(sw))
{
// Indent the XML so it's human readable.
writer.Formatting = Formatting.Indented;
serializer.WriteObject(writer, propertyBagContainer);
writer.Flush();
xmlString = sw.ToString();
}
}
finally
{
if (sw != null)
sw.Close();
}

return xmlString;
}

/// <summary>
/// Deserialize the string to the propertyBagContainer.
/// </summary>
/// <param name="xmlString"></param>
/// <returns></returns>
public static T Deserialize(string xmlString)
{
T propertyBagContainer;
using (Stream stream = new MemoryStream())
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(xmlString);
stream.Write(data, 0, data.Length);
stream.Position = 0;
DataContractSerializer deserializer = new DataContractSerializer(typeof(T));
propertyBagContainer = (T)deserializer.ReadObject(stream);
}

return propertyBagContainer;
}
}
}
Loading

0 comments on commit 1c98d6c

Please sign in to comment.