/// <summary>
 /// Create the configuration identified by configuration name.  (see
 /// http://aka.ms/azureautomationsdk/configurationoperations for more
 /// information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Automation.IDscConfigurationOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group
 /// </param>
 /// <param name='automationAccount'>
 /// Required. The automation account name.
 /// </param>
 /// <param name='parameters'>
 /// Required. The create or update parameters for configuration.
 /// </param>
 /// <returns>
 /// The response model for the configuration create response.
 /// </returns>
 public static DscConfigurationCreateOrUpdateResponse CreateOrUpdate(this IDscConfigurationOperations operations, string resourceGroupName, string automationAccount, DscConfigurationCreateOrUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IDscConfigurationOperations)s).CreateOrUpdateAsync(resourceGroupName, automationAccount, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Create the configuration identified by configuration name.  (see
 /// http://aka.ms/azureautomationsdk/configurationoperations for more
 /// information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Automation.IDscConfigurationOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group
 /// </param>
 /// <param name='automationAccount'>
 /// Required. The automation account name.
 /// </param>
 /// <param name='parameters'>
 /// Required. The create or update parameters for configuration.
 /// </param>
 /// <returns>
 /// The response model for the configuration create response.
 /// </returns>
 public static Task<DscConfigurationCreateOrUpdateResponse> CreateOrUpdateAsync(this IDscConfigurationOperations operations, string resourceGroupName, string automationAccount, DscConfigurationCreateOrUpdateParameters parameters)
 {
     return operations.CreateOrUpdateAsync(resourceGroupName, automationAccount, parameters, CancellationToken.None);
 }
        public Model.DscConfiguration CreateConfiguration(
           string resourceGroupName,
           string automationAccountName,
           string configrationName,
           string nodeName)
        {
            string configurationContent = "Configuration #configrationName# { Node #nodeName# { } } ";
            configurationContent = configurationContent.Replace("#configrationName#", configrationName);
            configurationContent = configurationContent.Replace("#nodeName#", nodeName);

            using (var request = new RequestSettings(this.automationManagementClient))
            {
                
                // location of the configuration is set to same as that of automation account
                string location = this.GetAutomationAccount(resourceGroupName, automationAccountName).Location;

                var configurationCreateParameters = new DscConfigurationCreateOrUpdateParameters()
                {
                    Name = configrationName,
                    Location = location,                
                    Properties = new DscConfigurationCreateOrUpdateProperties()
                    {
                        Description = String.Empty,
                        LogVerbose = false,
                        Source = new Microsoft.Azure.Management.Automation.Models.ContentSource()
                        {
                            // only embeddedContent supported for now
                            ContentType = Model.ContentSourceType.embeddedContent.ToString(),
                            Value = configurationContent
                        }
                    }
                };

                var configuration =
                    this.automationManagementClient.Configurations.CreateOrUpdate(
                        resourceGroupName,
                        automationAccountName,
                        configurationCreateParameters).Configuration;

                return new Model.DscConfiguration(resourceGroupName, automationAccountName, configuration);
            }
        }
        public Model.DscConfiguration CreateConfiguration(
            string resourceGroupName,
            string automationAccountName,
            string sourcePath,
            IDictionary tags, 
            string description,
            bool? logVerbose,
            bool published,
            bool overWrite)
        {
            using (var request = new RequestSettings(this.automationManagementClient))
            {
                Requires.Argument("ResourceGroupName", resourceGroupName).NotNull();
                Requires.Argument("AutomationAccountName", automationAccountName).NotNull();
                Requires.Argument("SourcePath", sourcePath).NotNull();

                string fileContent = null;
                string configurationName = String.Empty;

                try
                {
                    if (File.Exists(Path.GetFullPath(sourcePath)))
                    {
                        fileContent = System.IO.File.ReadAllText(sourcePath);
                    }
                }
                catch (Exception)
                {
                    // exception in accessing the file path
                    throw new FileNotFoundException(
                                        string.Format(
                                            CultureInfo.CurrentCulture,
                                            Resources.ConfigurationSourcePathInvalid));
                }

                // configuration name is same as filename
                configurationName = Path.GetFileNameWithoutExtension(sourcePath);

                // for the private preview, configuration can be imported in Published mode only
                // Draft mode is not implemented
                if (!published)
                {
                    throw new NotImplementedException(
                                        string.Format(
                                            CultureInfo.CurrentCulture,
                                            Resources.ConfigurationNotPublished));
                }

                // if configuration already exists, ensure overwrite flag is specified
                var configurationModel = this.TryGetConfigurationModel(
                    resourceGroupName,
                    automationAccountName,
                    configurationName);
                if (configurationModel != null)
                {
                    if (!overWrite)
                    {
                        throw new ResourceCommonException(typeof(Model.DscConfiguration),
                            string.Format(CultureInfo.CurrentCulture, Resources.ConfigurationAlreadyExists, configurationName));
                    }
                }

                // location of the configuration is set to same as that of automation account
                string location = this.GetAutomationAccount(resourceGroupName, automationAccountName).Location;

                IDictionary<string, string> configurationTags = null;
                if (tags != null) configurationTags = tags.Cast<DictionaryEntry>().ToDictionary(kvp => kvp.Key.ToString(), kvp => kvp.Value.ToString());

                var configurationCreateParameters = new DscConfigurationCreateOrUpdateParameters()
                                                        {
                                                            Name = configurationName,
                                                            Location = location,
                                                            Tags = configurationTags,
                                                            Properties = new DscConfigurationCreateOrUpdateProperties()
                                                                    {
                                                                        Description = String.IsNullOrEmpty(description) ? String.Empty : description,
                                                                        LogVerbose = (logVerbose.HasValue) ? logVerbose.Value : false,
                                                                        Source = new Microsoft.Azure.Management.Automation.Models.ContentSource()
                                                                                {
                                                                                    // only embeddedContent supported for now
                                                                                    ContentType = Model.ContentSourceType.embeddedContent.ToString(),
                                                                                    Value = fileContent
                                                                                }
                                                                    }
                                                        };

                var configuration =
                    this.automationManagementClient.Configurations.CreateOrUpdate(
                        resourceGroupName,
                        automationAccountName,
                        configurationCreateParameters).Configuration;

                return new Model.DscConfiguration(resourceGroupName, automationAccountName, configuration);
            }
        }