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 currentProfile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)); AzureSession.DataStore = dataStore; var client = new ProfileClient(currentProfile); 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 } } }; client.AddOrSetEnvironment(environment); client.AddOrSetAccount(account); client.AddOrSetSubscription(sub); currentProfile.Save(); var profileFile = currentProfile.ProfilePath; string profileContents = dataStore.ReadFileAsText(profileFile); var readProfile = JsonConvert.DeserializeObject <Dictionary <string, object> >(profileContents); Assert.False(readProfile.ContainsKey("Context")); AzureProfile parsedProfile = new AzureProfile(); 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)); }
protected override void EndProcessing() { CompatibilityProfileData profile; IEnumerable <Exception> errors; using (var pwsh = SMA.PowerShell.Create()) { var collectorBuilder = new CompatibilityProfileCollector.Builder(); if (ExcludeModulePathPrefix != null && ExcludeModulePathPrefix.Length > 0) { collectorBuilder.ExcludedModulePathPrefixes = ExcludeModulePathPrefix; } if (ExcludeAssemblyPathPrefix != null && ExcludeAssemblyPathPrefix.Length > 0) { collectorBuilder.ExcludedAssemblyPathPrefixes = ExcludeAssemblyPathPrefix; } using (var profileCollector = collectorBuilder.Build(pwsh)) { profile = string.IsNullOrEmpty(PlatformId) ? profileCollector.GetCompatibilityData(out errors) : profileCollector.GetCompatibilityData(PlatformId, out errors); } } // Report any problems we hit foreach (Exception e in errors) { this.WriteExceptionAsWarning(e); } if (Validate) { var validator = new QuickCheckValidator(); if (validator.IsProfileValid(profile, out IEnumerable <Exception> validationErrors)) { foreach (Exception validationError in validationErrors) { this.WriteExceptionAsWarning(validationError); } } } // If PassThru is set, just pass the object back and we're done if (PassThru) { WriteObject(profile); return; } // Set the default profile path if it was not provided string outFilePath; if (string.IsNullOrEmpty(OutFile)) { string profileName = ProfileName ?? profile.Id; outFilePath = GetProfilePath(profileName); } else { // Normalize the path to the output file we were given outFilePath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(OutFile); } // Create the directory containing the profile // If it already exists, this call will do nothing // If it cannot be created or is a non-directory file, an exception will be raised, which we let users handle Directory.CreateDirectory(Path.GetDirectoryName(outFilePath)); // Create the JSON serializer JsonProfileSerializer jsonSerializer = NoCompress ? JsonProfileSerializer.Create(Formatting.Indented) : JsonProfileSerializer.Create(Formatting.None); // Write the file var outFile = new FileInfo(outFilePath); jsonSerializer.SerializeToFile(profile, outFile); // Return the FileInfo object for the profile WriteObject(outFile); }
public ConvertFromPSCompatibilityJsonCommand() { _serializer = JsonProfileSerializer.Create(); }
protected override void BeginProcessing() { _serializer = JsonProfileSerializer.Create(NoWhitespace ? Formatting.None : Formatting.Indented); }