/// <summary>
 /// Create the node configuration identified by node configuration
 /// name.  (see http://aka.ms/azureautomationsdk/dscnodeconfigurations
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Automation.IDscNodeConfigurationOperations.
 /// </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 get Dsc node configuration operation.
 /// </returns>
 public static DscNodeConfigurationGetResponse CreateOrUpdate(this IDscNodeConfigurationOperations operations, string resourceGroupName, string automationAccount, DscNodeConfigurationCreateOrUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IDscNodeConfigurationOperations)s).CreateOrUpdateAsync(resourceGroupName, automationAccount, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Create the node configuration identified by node configuration
 /// name.  (see http://aka.ms/azureautomationsdk/dscnodeconfigurations
 /// for more information)
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Automation.IDscNodeConfigurationOperations.
 /// </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 get Dsc node configuration operation.
 /// </returns>
 public static Task<DscNodeConfigurationGetResponse> CreateOrUpdateAsync(this IDscNodeConfigurationOperations operations, string resourceGroupName, string automationAccount, DscNodeConfigurationCreateOrUpdateParameters parameters)
 {
     return operations.CreateOrUpdateAsync(resourceGroupName, automationAccount, parameters, CancellationToken.None);
 }
        public Model.NodeConfiguration CreateNodeConfiguration(
            string resourceGroupName,
            string automationAccountName,
            string sourcePath,
            string configurationName,
            bool overWrite)
        {
            using (var request = new RequestSettings(this.automationManagementClient))
            {
                Requires.Argument("ResourceGroupName", resourceGroupName).NotNullOrEmpty();
                Requires.Argument("AutomationAccountName", automationAccountName).NotNullOrEmpty();
                Requires.Argument("SourcePath", sourcePath).NotNullOrEmpty();
                Requires.Argument("configurationName", configurationName).NotNullOrEmpty();

                string fileContent = null;
                string nodeConfigurationName = null;
                string nodeName = null;

                if (File.Exists(Path.GetFullPath(sourcePath)))
                {
                    fileContent = System.IO.File.ReadAllText(sourcePath);
                    nodeName = System.IO.Path.GetFileNameWithoutExtension(sourcePath);
                    nodeConfigurationName = configurationName + "." + nodeName;
                }
                else
                {
                    // file path not valid.
                    throw new FileNotFoundException(
                                        string.Format(
                                            CultureInfo.CurrentCulture,
                                            Resources.ConfigurationSourcePathInvalid));
                }
                
                 // if node configuration already exists, ensure overwrite flag is specified
                var nodeConfigurationModel = this.TryGetNodeConfiguration(
                    resourceGroupName,
                    automationAccountName,
                    nodeConfigurationName,
                    null);
                if (nodeConfigurationModel != null)
                {
                    if (!overWrite)
                    {
                        throw new ResourceCommonException(typeof(Model.NodeConfiguration),
                            string.Format(CultureInfo.CurrentCulture, Resources.NodeConfigurationAlreadyExists, nodeConfigurationName));
                    }
                }

                // if configuration already exists, ensure overwrite flag is specified
                var configurationModel = this.TryGetConfigurationModel(
                    resourceGroupName,
                    automationAccountName,
                    configurationName);
                if (configurationModel == null)
                {
                    //create empty configuration if its empty
                    this.CreateConfiguration(resourceGroupName, automationAccountName, configurationName, nodeName);
                }

                var nodeConfigurationCreateParameters = new DscNodeConfigurationCreateOrUpdateParameters()
                {
                    Name = nodeConfigurationName,
                    Source = new Microsoft.Azure.Management.Automation.Models.ContentSource()
                    {
                        // only embeddedContent supported for now
                        ContentType = Model.ContentSourceType.embeddedContent.ToString(),
                        Value = fileContent
                    },
                    Configuration = new DscConfigurationAssociationProperty() 
                    {
                        Name = configurationName
                    }
                };

                var nodeConfiguration =
                    this.automationManagementClient.NodeConfigurations.CreateOrUpdate(
                        resourceGroupName,
                        automationAccountName,
                        nodeConfigurationCreateParameters).NodeConfiguration;


                return new Model.NodeConfiguration(resourceGroupName, automationAccountName, nodeConfiguration, null);
            }
        }