public void ProfileSaveDoesNotSerializeContext() { var dataStore = new MockDataStore(); var profilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.Instance.ProfileFile); var profile = new AzureSMProfile(profilePath); AzureSession.Instance.DataStore = dataStore; var tenant = Guid.NewGuid().ToString(); var environment = new AzureEnvironment { Name = "testCloud", ActiveDirectory = new Uri("http://contoso.com") }; var account = new AzureAccount { Id = "*****@*****.**", Type = AzureAccount.AccountType.User, }; account.SetTenants(tenant); var sub = new AzureSubscription { Id = new Guid().ToString(), Name = "Contoso Test Subscription", }; sub.SetAccount(account.Id); sub.SetEnvironment(environment.Name); sub.SetTenant(tenant); profile.EnvironmentTable[environment.Name] = environment; profile.AccountTable[account.Id] = account; profile.SubscriptionTable[sub.GetId()] = sub; profile.Save(); var profileFile = profile.ProfilePath; string profileContents = dataStore.ReadFileAsText(profileFile); var readProfile = JsonConvert.DeserializeObject <Dictionary <string, object> >(profileContents); Assert.False(readProfile.ContainsKey("DefaultContext")); AzureSMProfile parsedProfile = new AzureSMProfile(); var serializer = new JsonProfileSerializer(); Assert.True(serializer.Deserialize(profileContents, parsedProfile)); Assert.NotNull(parsedProfile); Assert.NotNull(parsedProfile.Environments); Assert.True(parsedProfile.EnvironmentTable.ContainsKey(environment.Name)); Assert.NotNull(parsedProfile.Accounts); Assert.True(parsedProfile.AccountTable.ContainsKey(account.Id)); Assert.NotNull(parsedProfile.Subscriptions); Assert.True(parsedProfile.SubscriptionTable.ContainsKey(sub.GetId())); }
public void ProfileSaveDoesNotSerializeContext() { var dataStore = new MockDataStore(); var profilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AzureSession.ProfileFile); var profile = new AzureSMProfile(profilePath); AzureSession.DataStore = dataStore; var tenant = Guid.NewGuid().ToString(); var environment = new AzureEnvironment { Name = "testCloud", Endpoints = { { AzureEnvironment.Endpoint.ActiveDirectory, "http://contoso.com" } } }; var account = new AzureAccount { Id = "*****@*****.**", Type = AzureAccount.AccountType.User, Properties = { { AzureAccount.Property.Tenants, tenant } } }; var sub = new AzureSubscription { Account = account.Id, Environment = environment.Name, Id = new Guid(), Name = "Contoso Test Subscription", Properties = { { AzureSubscription.Property.Tenants, tenant } } }; profile.Environments[environment.Name] = environment; profile.Accounts[account.Id] = account; profile.Subscriptions[sub.Id] = sub; profile.Save(); var profileFile = profile.ProfilePath; string profileContents = dataStore.ReadFileAsText(profileFile); var readProfile = JsonConvert.DeserializeObject <Dictionary <string, object> >(profileContents); Assert.False(readProfile.ContainsKey("DefaultContext")); AzureSMProfile parsedProfile = new AzureSMProfile(); var serializer = new JsonProfileSerializer(); Assert.True(serializer.Deserialize(profileContents, parsedProfile)); Assert.NotNull(parsedProfile); Assert.NotNull(parsedProfile.Environments); Assert.True(parsedProfile.Environments.ContainsKey(environment.Name)); Assert.NotNull(parsedProfile.Accounts); Assert.True(parsedProfile.Accounts.ContainsKey(account.Id)); Assert.NotNull(parsedProfile.Subscriptions); Assert.True(parsedProfile.Subscriptions.ContainsKey(sub.Id)); }
public void ProcessGetWebsiteWithNullSubscription() { currentProfile = new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile)); currentProfile.SubscriptionTable.Clear(); currentProfile.Save(); AzureSMCmdlet.CurrentProfile = currentProfile; // Test var getAzureWebsiteCommand = new GetAzureWebsiteCommand { CommandRuntime = new MockCommandRuntime() }; Testing.AssertThrows <Exception>(getAzureWebsiteCommand.ExecuteWithProcessing, Resources.InvalidDefaultSubscription); }
protected void SetupProfile(string storageName) { currentProfile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)); AzureSMCmdlet.CurrentProfile = currentProfile; var subscription = new AzureSubscription { Id = new Guid(subscriptionId) }; subscription.Properties[AzureSubscription.Property.Default] = "True"; currentProfile.Subscriptions[new Guid(subscriptionId)] = subscription; if (storageName != null) { currentProfile.Context.Subscription.Properties[AzureSubscription.Property.StorageAccount] = storageName; } currentProfile.Save(); }
protected void SetupProfile(string storageName) { currentProfile = new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile)); AzureSMCmdlet.CurrentProfile = currentProfile; var subscription = new AzureSubscription { Id = subscriptionId }; subscription.SetDefault(); currentProfile.SubscriptionTable[new Guid(subscriptionId)] = subscription; if (storageName != null) { currentProfile.Context.Subscription.SetStorageAccount(storageName); } currentProfile.Save(); }
private void UpgradeProfile() { string oldProfileFilePath = Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.OldProfileFile); string oldProfileFilePathBackup = Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.OldProfileFileBackup); string newProfileFilePath = Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile); if (AzureSession.Instance.DataStore.FileExists(oldProfileFilePath)) { string oldProfilePath = Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.OldProfileFile); try { // Try to backup old profile try { AzureSession.Instance.DataStore.CopyFile(oldProfilePath, oldProfileFilePathBackup); } catch { // Ignore any errors here } AzureSMProfile oldProfile = new AzureSMProfile(oldProfilePath); if (AzureSession.Instance.DataStore.FileExists(newProfileFilePath)) { // Merge profile files AzureSMProfile newProfile = new AzureSMProfile(newProfileFilePath); foreach (var environment in newProfile.Environments) { oldProfile.EnvironmentTable[environment.Name] = environment; } foreach (var subscription in newProfile.Subscriptions) { oldProfile.SubscriptionTable[subscription.GetId()] = subscription; } AzureSession.Instance.DataStore.DeleteFile(newProfileFilePath); } // If there were no load errors - delete backup file if (oldProfile.ProfileLoadErrors.Count == 0) { try { AzureSession.Instance.DataStore.DeleteFile(oldProfileFilePathBackup); } catch { // Give up } } // Save the profile to the disk oldProfile.Save(); // Rename WindowsAzureProfile.xml to WindowsAzureProfile.json AzureSession.Instance.DataStore.RenameFile(oldProfilePath, newProfileFilePath); } catch { // Something really bad happened - try to delete the old profile try { AzureSession.Instance.DataStore.DeleteFile(oldProfilePath); } catch { // Ignore any errors } } // In case that we changed a disk profile, reload it if (Profile != null && Profile.ProfilePath == newProfileFilePath) { Profile = new AzureSMProfile(Profile.ProfilePath); } } }