public static async Task DeleteCloudConfiguration(AutomationDSC configuration, AutomationManagementClient automationManagementClient, string resourceGroupName, string accountName) { CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); await automationManagementClient.Configurations.DeleteAsync(resourceGroupName, accountName, configuration.Name, cts.Token); }
public static async Task DownloadConfiguration(AutomationDSC configuration, AutomationManagementClient automationManagementClient, string workspace, string resourceGroupName, AutomationAccount account) { CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); DscConfigurationGetResponse response = await automationManagementClient.Configurations.GetAsync(resourceGroupName, account.Name, configuration.Name, cts.Token); DscConfigurationGetResponse draftResponse = null; DscConfigurationGetContentResponse configurationContentResponse = null; cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); if (response.Configuration.Properties.State == "Published") { configurationContentResponse = await automationManagementClient.Configurations.GetContentAsync(resourceGroupName, account.Name, configuration.Name, cts.Token); } else { // Draft not supported yet } String configFilePath = System.IO.Path.Combine(workspace, configuration.Name + ".ps1"); try { File.WriteAllText(configFilePath, configurationContentResponse.Content.ToString(), Encoding.UTF8); } catch (Exception Ex) { // Atempting to write the file while it is being read. Wait a second and retry. if (Ex.HResult == -2147024864) { Thread.Sleep(1000); File.WriteAllText(configFilePath, configurationContentResponse.Content.ToString(), Encoding.UTF8); } } configuration.localFileInfo = new FileInfo(configFilePath); if (response.Configuration.Properties.State == "Published") { await UploadConfigurationAsDraft(configuration, automationManagementClient, resourceGroupName, account); cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); draftResponse = await automationManagementClient.Configurations.GetAsync(resourceGroupName, account.Name, configuration.Name, cts.Token); } /* Ensures the correct sync status is detected */ if (draftResponse != null) { configuration.localFileInfo.LastWriteTime = draftResponse.Configuration.Properties.LastModifiedTime.LocalDateTime; configuration.LastModifiedLocal = draftResponse.Configuration.Properties.LastModifiedTime.LocalDateTime; configuration.LastModifiedCloud = draftResponse.Configuration.Properties.LastModifiedTime.LocalDateTime; } }
public static void DeleteLocalConfiguration(AutomationDSC configuration) { File.Delete(configuration.localFileInfo.FullName); }
public static async Task UploadConfigurationAsDraft(AutomationDSC configuration, AutomationManagementClient automationManagementClient, string resourceGroupName, AutomationAccount account) { string fileContent = null; try { if (File.Exists(Path.GetFullPath(configuration.localFileInfo.FullName))) { fileContent = System.IO.File.ReadAllText(configuration.localFileInfo.FullName); } } catch (Exception) { // exception in accessing the file path throw new FileNotFoundException( string.Format( CultureInfo.CurrentCulture, Resources.LocalConfigurationFileNotFound)); } DscConfigurationCreateOrUpdateProperties draftProperties; draftProperties = new DscConfigurationCreateOrUpdateProperties(); // Get current properties if is not a new configuration and set these on the draft also so they are preserved. DscConfigurationGetResponse response = null; CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); if (configuration.SyncStatus != AutomationAuthoringItem.Constants.SyncStatus.LocalOnly) { response = await automationManagementClient.Configurations.GetAsync(resourceGroupName, account.Name, configuration.Name, cts.Token); } // Create properties DscConfigurationCreateOrUpdateParameters draftParams = new DscConfigurationCreateOrUpdateParameters(draftProperties); draftParams.Name = configuration.Name; draftParams.Location = account.Location; draftParams.Properties.Description = configuration.Description; // If this is not a new configuration, set the existing properties of the configuration if (response != null) { draftParams.Tags = response.Configuration.Tags; draftParams.Location = response.Configuration.Location; draftParams.Properties.LogVerbose = response.Configuration.Properties.LogVerbose; draftParams.Properties.Description = response.Configuration.Properties.Description; } cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); /* Update the configuration content from .ps1 file */ DscConfigurationCreateOrUpdateParameters draftUpdateParams = new DscConfigurationCreateOrUpdateParameters() { Name = configuration.Name, Location = draftParams.Location, Tags = draftParams.Tags, Properties = new DscConfigurationCreateOrUpdateProperties() { Description = draftParams.Properties.Description, LogVerbose = draftParams.Properties.LogVerbose, Source = new Microsoft.Azure.Management.Automation.Models.ContentSource() { ContentType = ContentSourceType.EmbeddedContent, Value = fileContent } } }; cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); await automationManagementClient.Configurations.CreateOrUpdateAsync(resourceGroupName, account.Name, draftUpdateParams, cts.Token); /* Ensure the correct sync status is detected */ DscConfiguration draft = await GetConfigurationDraft(configuration.Name, automationManagementClient, resourceGroupName, account.Name); configuration.localFileInfo.LastWriteTime = draft.Properties.LastModifiedTime.LocalDateTime; configuration.LastModifiedLocal = draft.Properties.LastModifiedTime.LocalDateTime; configuration.LastModifiedCloud = draft.Properties.LastModifiedTime.LocalDateTime; }