Skip to content

Commit

Permalink
Merge pull request #2 from Azure/dev
Browse files Browse the repository at this point in the history
merge from azure-powershell/dev
  • Loading branch information
jtlibing committed Dec 5, 2014
2 parents 14b5be8 + 04137b7 commit 5a88b9a
Show file tree
Hide file tree
Showing 131 changed files with 5,168 additions and 1,515 deletions.
1 change: 0 additions & 1 deletion ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Select-AzureSubscription: fixed output types in default and PassThru mode
* Compute
* Get-AzureVMSqlServerExtension
* New-AzureVMSqlServerExtension
* Set-AzureVMSqlServerExtension
* Remove-AzureVMSqlServerExtension
* HDInsight
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@
</Reference>
<Reference Include="Microsoft.WindowsAzure.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.dll</HintPath>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.1\lib\net45\Microsoft.WindowsAzure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.NetFramework.dll</HintPath>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.1\lib\net45\Microsoft.WindowsAzure.Common.NetFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.WindowsAzure.ConfigurationManager.2.0.3\lib\net40\Microsoft.WindowsAzure.Configuration.dll</HintPath>
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Commands.Common.Storage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45" />
<package id="Microsoft.Data.Services.Client" version="5.6.0" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common" version="1.4.0" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common.Dependencies" version="1.1.0" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common" version="1.4.1" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common.Dependencies" version="1.1.1" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.3" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Storage" version="3.1.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" />
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Commands.Common.Test/Commands.Common.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.WindowsAzure.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.dll</HintPath>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.1\lib\net45\Microsoft.WindowsAzure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.NetFramework.dll</HintPath>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.1\lib\net45\Microsoft.WindowsAzure.Common.NetFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\..\packages\Microsoft.WindowsAzure.ConfigurationManager.2.0.3\lib\net40\Microsoft.WindowsAzure.Configuration.dll</HintPath>
Expand Down
89 changes: 78 additions & 11 deletions src/Common/Commands.Common.Test/Common/ProfileClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void ProfileMigratesAccountsAndDefaultSubscriptions()
Assert.Equal("https://api-dogfood.resources.windows-int.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ResourceManager]);
Assert.Equal("https://management-preview.core.windows-int.net/", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.ServiceManagement]);
Assert.Equal(".database.windows.net", client.Profile.Environments["Dogfood"].Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix]);

// Verify subscriptions
Assert.Equal(3, client.Profile.Subscriptions.Count);
Assert.False(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1E")));
Expand Down Expand Up @@ -157,7 +157,7 @@ public void ProfileMigratesAccountsSkipsBadOnesAndBacksUpFile()

// Verify Environment migration
Assert.Equal(2, client.Profile.Environments.Count);

// Verify subscriptions
Assert.Equal(3, client.Profile.Subscriptions.Count);
Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("06E3F6FD-A3AA-439A-8FC4-1F5C41D2AD1F")));
Expand All @@ -168,7 +168,7 @@ public void ProfileMigratesAccountsSkipsBadOnesAndBacksUpFile()

Assert.True(client.Profile.Subscriptions.ContainsKey(new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")));
Assert.Equal("Test Bad Cert", client.Profile.Subscriptions[new Guid("d1e52cbc-b073-42e2-a0a0-c2f547118a6f")].Name);

// Verify accounts
Assert.Equal(2, client.Profile.Accounts.Count);
Assert.Equal("test@mail.com", client.Profile.Accounts["test@mail.com"].Id);
Expand Down Expand Up @@ -204,10 +204,10 @@ public void ProfileMigratesCorruptedFileAndCreatedBackup()

// Verify Environment migration
Assert.Equal(2, client.Profile.Environments.Count);

// Verify subscriptions
Assert.Equal(0, client.Profile.Subscriptions.Count);

// Verify accounts
Assert.Equal(0, client.Profile.Accounts.Count);

Expand Down Expand Up @@ -258,7 +258,7 @@ public void AddAzureAccountReturnsAccountWithAllSubscriptionsInRdfeMode()
ProfileClient.DataStore = dataStore;
ProfileClient client = new ProfileClient();

var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "test", Type = AzureAccount.AccountType.User }, AzureEnvironment.PublicEnvironments[ EnvironmentName.AzureCloud], null);
var account = client.AddAccountAndLoadSubscriptions(new AzureAccount { Id = "test", Type = AzureAccount.AccountType.User }, AzureEnvironment.PublicEnvironments[EnvironmentName.AzureCloud], null);

Assert.Equal("test", account.Id);
Assert.Equal(3, account.GetSubscriptions(client.Profile).Count);
Expand Down Expand Up @@ -603,7 +603,7 @@ public void SetAzureEnvironmentUpdatesEnvironment()
Assert.Equal(2, client.Profile.Environments.Count);

Assert.Throws<ArgumentNullException>(() => client.AddOrSetEnvironment(null));

var env2 = client.AddOrSetEnvironment(azureEnvironment);
Assert.Equal(env2.Name, azureEnvironment.Name);
Assert.NotNull(env2.Endpoints[AzureEnvironment.Endpoint.ServiceManagement]);
Expand Down Expand Up @@ -672,7 +672,7 @@ public void AddOrSetAzureSubscriptionChecksAndUpdates()
Assert.Equal(subscription, azureSubscription1);
Assert.Throws<ArgumentNullException>(() => client.AddOrSetSubscription(null));
Assert.Throws<ArgumentNullException>(() => client.AddOrSetSubscription(
new AzureSubscription { Id = new Guid(), Environment = null, Name = "foo"}));
new AzureSubscription { Id = new Guid(), Environment = null, Name = "foo" }));
}

[Fact]
Expand Down Expand Up @@ -792,7 +792,7 @@ public void RefreshSubscriptionsWorksWithMooncake()
MockDataStore dataStore = new MockDataStore();
ProfileClient.DataStore = dataStore;
ProfileClient client = new ProfileClient();

client.Profile.Accounts[azureAccount.Id] = azureAccount;

var subscriptions = client.RefreshSubscriptions(client.Profile.Environments[EnvironmentName.AzureChinaCloud]);
Expand Down Expand Up @@ -878,7 +878,7 @@ public void SetAzureSubscriptionAsDefaultSetsDefaultAndCurrent()
Assert.Equal(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
Assert.Equal(azureSubscription2.Id, AzureSession.CurrentContext.Subscription.Id);
Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsDefault("bad", null));
Assert.Throws<ArgumentNullException>(() => client.SetSubscriptionAsDefault(null, null));
Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsDefault(null, null));
}

[Fact]
Expand Down Expand Up @@ -917,7 +917,7 @@ public void SetAzureSubscriptionAsCurrentSetsCurrent()

Assert.Equal(azureSubscription2.Id, AzureSession.CurrentContext.Subscription.Id);
Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsCurrent("bad", null));
Assert.Throws<ArgumentNullException>(() => client.SetSubscriptionAsCurrent(null, null));
Assert.Throws<ArgumentException>(() => client.SetSubscriptionAsCurrent(null, null));
}

[Fact]
Expand Down Expand Up @@ -1076,6 +1076,73 @@ public void ImportPublishSettingsUsesPassedInEnvironment()
Assert.Equal(7, client.Profile.Subscriptions.Count);
}

[Fact]
public void SelectAzureSubscriptionByIdWorks()
{
MockDataStore dataStore = new MockDataStore();
ProfileClient.DataStore = dataStore;
ProfileClient client = new ProfileClient();

var tempSubscriptions = new List<AzureSubscription>
{
new AzureSubscription
{
Id = new Guid("11111111-1383-4740-8A69-748C5B63ADBA"),
Name = "Same Name Subscription",
Environment = azureEnvironment.Name,
Account = azureAccount.Id,
Properties = new Dictionary<AzureSubscription.Property, string>
{
{ AzureSubscription.Property.Default, "True" }
}
},

new AzureSubscription
{
Id = new Guid("22222222-1383-4740-8A69-748C5B63ADBA"),
Name = "Same Name Subscription",
Environment = azureEnvironment.Name,
Account = azureAccount.Id,
Properties = new Dictionary<AzureSubscription.Property, string>()
},

new AzureSubscription
{
Id = new Guid("33333333-1383-4740-8A69-748C5B63ADBA"),
Name = "Same Name Subscription",
Environment = azureEnvironment.Name,
Account = azureAccount.Id,
Properties = new Dictionary<AzureSubscription.Property, string>()
}
};

client.Profile.Accounts[azureAccount.Id] = azureAccount;
client.AddOrSetEnvironment(azureEnvironment);

foreach (var s in tempSubscriptions)
{
client.AddOrSetSubscription(s);
}

client.SetSubscriptionAsCurrent(tempSubscriptions[0].Name, tempSubscriptions[0].Account);
client.Profile.Save();

Assert.Equal(tempSubscriptions[0].Id, AzureSession.CurrentContext.Subscription.Id);

var cmdlt = new SelectAzureSubscriptionCommand();

cmdlt.CommandRuntime = new MockCommandRuntime();
cmdlt.SubscriptionId = tempSubscriptions[2].Id.ToString();
cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");

// Act
cmdlt.InvokeBeginProcessing();
cmdlt.ExecuteCmdlet();
cmdlt.InvokeEndProcessing();

Assert.Equal(tempSubscriptions[2].Id, AzureSession.CurrentContext.Subscription.Id);
}

[Fact]
public void ImportPublishSettingsAddsSecondCertificate()
{
Expand Down
14 changes: 14 additions & 0 deletions src/Common/Commands.Common.Test/Common/ProfileCmdltsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ public void ClearAzureProfileClearsTokenCache()
Assert.Equal(0, tokenCache.ReadItems().Count());
}

[Fact]
public void DeleteCorruptedTokenCache()
{
//setup
string testFileName = @"c:\foobar\TokenCache.dat";
ProfileClient.DataStore.WriteFile(testFileName, new byte[] { 0, 1 });

//Act
ProtectedFileTokenCache tokenCache = new ProtectedFileTokenCache(testFileName);

//Assert
Assert.False(ProfileClient.DataStore.FileExists(testFileName));
}

[Fact]
public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
{
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Commands.Common.Test/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<package id="Microsoft.Data.Services.Client" version="5.6.0" targetFramework="net45" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.11.10918.1222" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.28" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common" version="1.4.0" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common.Dependencies" version="1.1.0" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common" version="1.4.1" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Common.Dependencies" version="1.1.1" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="2.0.3" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management" version="2.1.0" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.Management.Compute" version="6.0.0" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,39 @@ public static ProtectedFileTokenCache Instance
// Initializes the cache against a local file.
// If the file is already present, it loads its content in the ADAL cache
private ProtectedFileTokenCache()
{
Initialize(CacheFileName);
}

private void Initialize(string fileName)
{
AfterAccess = AfterAccessNotification;
BeforeAccess = BeforeAccessNotification;
lock (fileLock)
{
if (ProfileClient.DataStore.FileExists(CacheFileName))
if (ProfileClient.DataStore.FileExists(fileName))
{
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
var existingData = ProfileClient.DataStore.ReadFileAsBytes(fileName);
if (existingData != null)
{
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
try
{
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
}
catch (CryptographicException)
{
ProfileClient.DataStore.DeleteFile(fileName);
}
}
}
}
}

public ProtectedFileTokenCache(string cacheFile)
{
Initialize(cacheFile);
}

// Empties the persistent store.
public override void Clear()
{
Expand All @@ -81,7 +98,14 @@ void BeforeAccessNotification(TokenCacheNotificationArgs args)
var existingData = ProfileClient.DataStore.ReadFileAsBytes(CacheFileName);
if (existingData != null)
{
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
try
{
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
}
catch (CryptographicException)
{
ProfileClient.DataStore.DeleteFile(CacheFileName);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Common/Commands.Common/Commands.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@
</Reference>
<Reference Include="Microsoft.WindowsAzure.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.dll</HintPath>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.1\lib\net45\Microsoft.WindowsAzure.Common.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Common.NetFramework, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.0\lib\net45\Microsoft.WindowsAzure.Common.NetFramework.dll</HintPath>
<HintPath>..\..\packages\Microsoft.WindowsAzure.Common.1.4.1\lib\net45\Microsoft.WindowsAzure.Common.NetFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.WindowsAzure.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down
26 changes: 17 additions & 9 deletions src/Common/Commands.Common/Common/AzurePowerShellClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,24 @@ private void RegisterServiceManagementProviders<T>(AzureContext context) where T
}
}

private void UpdateSubscriptionRegisteredProviders(AzureSubscription subscription, List<string> providers)
private void UpdateSubscriptionRegisteredProviders(AzureSubscription subscription, List<string> providers)
{
if (providers != null && providers.Count > 0)
{
subscription.SetOrAppendProperty(AzureSubscription.Property.RegisteredResourceProviders,
providers.ToArray());
ProfileClient profileClient = new ProfileClient();
profileClient.AddOrSetSubscription(subscription);
profileClient.Profile.Save();
}
     if (providers != null && providers.Count > 0)
     {
         subscription.SetOrAppendProperty(AzureSubscription.Property.RegisteredResourceProviders,
             providers.ToArray());
         try
         {
             ProfileClient profileClient = new ProfileClient();
             profileClient.AddOrSetSubscription(subscription);
             profileClient.Profile.Save();
         }
         catch (KeyNotFoundException)
         {
             // if using a subscription data file, do not write registration to disk
             // long term solution is using -Profile parameter
         }
     }
}
}
}
Loading

0 comments on commit 5a88b9a

Please sign in to comment.