public string Serialize(AzureSMProfile profile) { return(JsonConvert.SerializeObject(new { Environments = profile.EnvironmentTable.Values.ToList(), Subscriptions = profile.SubscriptionTable.Values.ToList(), Accounts = profile.AccountTable.Values.ToList() }, Formatting.Indented)); }
public bool Deserialize(string contents, AzureSMProfile profile) { DeserializeErrors = new List <string>(); try { var jsonProfile = JObject.Parse(contents); foreach (var env in jsonProfile["Environments"]) { try { profile.EnvironmentTable[(string)env["Name"]] = JsonConvert.DeserializeObject <AzureEnvironment>(env.ToString()); } catch (Exception ex) { DeserializeErrors.Add(ex.Message); } } foreach (var subscription in jsonProfile["Subscriptions"]) { try { profile.SubscriptionTable[new Guid((string)subscription["Id"])] = JsonConvert.DeserializeObject <AzureSubscription>(subscription.ToString()); } catch (Exception ex) { DeserializeErrors.Add(ex.Message); } } foreach (var account in jsonProfile["Accounts"]) { try { profile.AccountTable[(string)account["Id"]] = JsonConvert.DeserializeObject <AzureAccount>(account.ToString()); } catch (Exception ex) { DeserializeErrors.Add(ex.Message); } } } catch (Exception ex) { DeserializeErrors.Add(ex.Message); } return(DeserializeErrors.Count == 0); }
/// <summary> /// Writes profile to a specified path. /// </summary> /// <param name="path">File path on disk to save profile to</param> public void Save(string path) { if (string.IsNullOrEmpty(path)) { return; } AzureSMProfile other = new AzureSMProfile(); other.CopyFrom(this); // Removing predefined environments foreach (string env in AzureEnvironment.PublicEnvironments.Keys) { other.EnvironmentTable.Remove(env); } try { string contents = other.ToString(); string diskContents = string.Empty; if (AzureSession.Instance.DataStore.FileExists(path)) { diskContents = AzureSession.Instance.DataStore.ReadFileAsText(path); } if (diskContents != contents) { AzureSession.Instance.DataStore.WriteFile(path, contents); } } finally { // Adding back predefined environments foreach (AzureEnvironment env in AzureEnvironment.PublicEnvironments.Values) { EnvironmentTable[env.Name] = env; } } }
private void CopyFrom(AzureSMProfile other) { this.ProfilePath = other.ProfilePath; this.EnvironmentTable.Clear(); foreach (var environment in other.EnvironmentTable) { this.EnvironmentTable[environment.Key] = environment.Value; } this.AccountTable.Clear(); foreach (var account in other.AccountTable) { this.AccountTable[account.Key] = account.Value; } this.SubscriptionTable.Clear(); foreach (var subscription in other.SubscriptionTable) { this.SubscriptionTable[subscription.Key] = subscription.Value; } this.CopyPropertiesFrom(other); }
/// <summary> /// Get storage account details from the current storage account /// </summary> /// <param name="subscription">The subscription containing the account.</param> /// <param name="profile">The profile continuing the subscription.</param> /// <returns>Storage account details, usable with the windows azure storage data plane library.</returns> public static CloudStorageAccount GetCloudStorageAccount(this AzureSubscription subscription, AzureSMProfile profile) { if (subscription == null) { return(null); } var account = subscription.GetProperty(AzureSubscription.Property.StorageAccount); try { return(CloudStorageAccount.Parse(account)); } catch { using ( var storageClient = AzureSession.ClientFactory.CreateClient <StorageManagementClient>(profile, subscription, AzureEnvironment.Endpoint.ServiceManagement)) { return(StorageUtilities.GenerateCloudStorageAccount( storageClient, account)); } } }
public bool Deserialize(string contents, AzureSMProfile profile) { ProfileData data; Debug.Assert(profile != null); DeserializeErrors = new List <string>(); DataContractSerializer serializer = new DataContractSerializer(typeof(ProfileData)); // For backward compatibility since namespace of ProfileData has been changed // we need to replace previous namespace with the current one if (!string.IsNullOrWhiteSpace(contents)) { contents = contents.Replace( "http://schemas.datacontract.org/2004/07/Microsoft.Azure.Common.Authentication", "http://schemas.datacontract.org/2004/07/Microsoft.WindowsAzure.Commands.Utilities.Common"); } using (MemoryStream s = new MemoryStream(Encoding.UTF8.GetBytes(contents ?? ""))) { data = (ProfileData)serializer.ReadObject(s); } if (data != null) { foreach (AzureEnvironmentData oldEnv in data.Environments) { profile.EnvironmentTable[oldEnv.Name] = oldEnv.ToAzureEnvironment(); } List <IAzureEnvironment> envs = profile.EnvironmentTable.Values.ToList(); foreach (AzureSubscriptionData oldSubscription in data.Subscriptions) { try { var newSubscription = oldSubscription.ToAzureSubscription(envs); if (newSubscription.GetAccount() == null) { continue; } var newAccounts = oldSubscription.ToAzureAccounts(); foreach (var account in newAccounts) { if (profile.AccountTable.ContainsKey(account.Id)) { profile.AccountTable[account.Id].SetOrAppendProperty(AzureAccount.Property.Tenants, account.GetPropertyAsArray(AzureAccount.Property.Tenants)); profile.AccountTable[account.Id].SetOrAppendProperty(AzureAccount.Property.Subscriptions, account.GetPropertyAsArray(AzureAccount.Property.Subscriptions)); } else { profile.AccountTable[account.Id] = account; } } profile.SubscriptionTable[newSubscription.GetId()] = newSubscription; } catch (Exception ex) { // Skip subscription if failed to load DeserializeErrors.Add(ex.Message); } } } return(DeserializeErrors.Count == 0); }
public string Serialize(AzureSMProfile obj) { // We do not use the serialize for xml serializer anymore and rely solely on the JSON serializer. throw new NotImplementedException(); }