public void ResourceIdFormatterTestSuccessful() { Guid subscriptionGuid = Guid.NewGuid(); string myResourceGroupName = "rgName"; IList <KeyValuePair <string, string> > resources = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("k1", "v1") }; string actualValue = ResourceIdFormatter.GenerateResourceId(subscriptionGuid, myResourceGroupName, resources); string expectedValue = $"/subscriptions/{subscriptionGuid}/resourceGroups/{myResourceGroupName}/providers/Microsoft.StorageSync/{resources[0].Key}/{resources[0].Value}"; Xunit.Assert.Equal(expectedValue, actualValue); actualValue = ResourceIdFormatter.GenerateResourceId(subscriptionGuid, myResourceGroupName, null); expectedValue = $"/subscriptions/{subscriptionGuid}/resourceGroups/{myResourceGroupName}"; Xunit.Assert.Equal(expectedValue, actualValue); }
/// <summary> /// This function processes the registration and perform following steps /// 1. EnsureSyncServerCertificate /// 2. GetSyncServerCertificate /// 3. GetSyncServerId /// 4. Get ClusterInfo /// 5. Populate RegistrationServerResource /// </summary> /// <param name="managementEndpointUri">Management Endpoint Uri</param> /// <param name="subscriptionId">Subscription Id</param> /// <param name="storageSyncServiceName">Storage Sync Service Name</param> /// <param name="resourceGroupName">Resource Group Name</param> /// <param name="certificateProviderName">Certificate Provider Name</param> /// <param name="certificateHashAlgorithm">Certificate Hash Algorithm</param> /// <param name="certificateKeyLength">Certificate Key Length</param> /// <param name="monitoringDataPath">Monitoring data path</param> /// <param name="agentVersion">Agent Version</param> /// <returns>Registered Server Resource</returns> /// <exception cref="Commands.StorageSync.Interop.Exceptions.ServerRegistrationException"> /// </exception> /// <exception cref="ArgumentException">serverGuid /// or /// clusterId</exception> /// <exception cref="ServerRegistrationException"></exception> public override ServerRegistrationData Setup(Uri managementEndpointUri, Guid subscriptionId, string storageSyncServiceName, string resourceGroupName, string certificateProviderName, string certificateHashAlgorithm, uint certificateKeyLength, string monitoringDataPath, string agentVersion) { int hr = EcsManagementInteropClient.EnsureSyncServerCertificate(managementEndpointUri.OriginalString, subscriptionId.ToString(), storageSyncServiceName, resourceGroupName, certificateProviderName, certificateHashAlgorithm, certificateKeyLength); bool success = hr == 0; if (!success) { throw new ServerRegistrationException(ServerRegistrationErrorCode.EnsureSyncServerCertificateFailed, hr, ErrorCategory.InvalidResult); } hr = EcsManagementInteropClient.GetSyncServerCertificate(isPrimary: true, serverCertificate: out string syncServerCertificate); success = hr == 0; if (!success) { throw new ServerRegistrationException(ServerRegistrationErrorCode.GetSyncServerCertificateFailed, hr, ErrorCategory.InvalidResult); } hr = EcsManagementInteropClient.GetSyncServerId(out string syncServerId); Guid serverGuid = Guid.Empty; bool hasServerGuid = Guid.TryParse(syncServerId, out serverGuid); if (!hasServerGuid) { throw new ArgumentException(nameof(Guid.Empty)); } success = hr == 0; if (!success) { throw new ServerRegistrationException(ServerRegistrationErrorCode.GetSyncServerIdFailed, hr, ErrorCategory.InvalidResult); } bool isInCluster; isInCluster = EcsManagementInteropClient.IsInCluster(); string clusterId = default(string); string clusterName = default(string); if (isInCluster) { hr = EcsManagementInteropClient.GetClusterInfo(out clusterId, out clusterName); success = hr == 0; if (!success) { throw new ServerRegistrationException(ServerRegistrationErrorCode.GetClusterInfoFailed, hr, ErrorCategory.InvalidResult); } } var resources = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>( StorageSyncConstants.StorageSyncServiceTypeName, storageSyncServiceName), new KeyValuePair <string, string>( StorageSyncConstants.RegisteredServerTypeName, syncServerId) }; string resourceId = ResourceIdFormatter.GenerateResourceId(subscriptionId, resourceGroupName, resources); string osVersion = null; // Get OS version using Win32_OperatingSystem WMI object try { var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem"); ManagementObject info = searcher.Get().Cast <ManagementObject>().FirstOrDefault(); PropertyData versionProperty = info.Properties["Version"]; PropertyData servicePackMajorVersionProperty = info.Properties["ServicePackMajorVersion"]; string version = versionProperty.Value.ToString(); var versionRegex = new Regex(@"^\d*\.\d*\.\d*$"); string servicePackMajorVersion = servicePackMajorVersionProperty.Value.ToString(); // we expect the version format to be something like 10.0.14943.0 // In order to construct this, we need to combine the version output with the service pack major version. osVersion = $"{version}.{servicePackMajorVersion}"; } catch (Exception) { // Fall back to the old way osVersion = Environment.OSVersion.Version.ToString(); } var serverRegistrationData = new ServerRegistrationData { Id = resourceId, ServerId = serverGuid, ServerCertificate = syncServerCertificate.ToBase64Bytes(true), ServerRole = isInCluster ? ServerRoleType.ClusterNode : ServerRoleType.Standalone, ServerOSVersion = osVersion, AgentVersion = agentVersion }; if (isInCluster) { Guid clusterGuid = Guid.Empty; bool clusterGuidValue = Guid.TryParse(clusterId, out clusterGuid); if (!clusterGuidValue) { throw new ArgumentException(nameof(clusterId)); } serverRegistrationData.ClusterId = clusterGuid; serverRegistrationData.ClusterName = clusterName; } else { serverRegistrationData.ClusterId = Guid.Empty; } return(serverRegistrationData); }
public void ResourceIdFormatterTestFailed() { Xunit.Assert.Throws <ArgumentException>(() => ResourceIdFormatter.GenerateResourceId(Guid.NewGuid(), null, null)); }