14
14
15
15
using Microsoft . Azure . Commands . Automation . Model ;
16
16
using Microsoft . Azure . Commands . Automation . Properties ;
17
+ using Microsoft . Azure . Commands . Common . Authentication ;
18
+ using Microsoft . Azure . Commands . Common . Authentication . Abstractions ;
17
19
using Microsoft . Azure . Management . Automation ;
18
20
using Microsoft . Azure . Management . Automation . Models ;
19
21
using Microsoft . Rest . Azure . OData ;
20
22
using Newtonsoft . Json ;
23
+ using Newtonsoft . Json . Serialization ;
21
24
using System ;
22
25
using System . Collections ;
23
26
using System . Collections . Generic ;
34
37
using Job = Microsoft . Azure . Management . Automation . Models . Job ;
35
38
using JobSchedule = Microsoft . Azure . Management . Automation . Models . JobSchedule ;
36
39
using Schedule = Microsoft . Azure . Commands . Automation . Model . Schedule ;
40
+ using Microsoft . Azure . Management . ResourceManager ;
37
41
38
42
namespace Microsoft . Azure . Commands . Automation . Common
39
43
{
@@ -773,7 +777,8 @@ public void RegisterDscNode(string resourceGroupName,
773
777
string actionAfterReboot ,
774
778
bool moduleOverwriteFlag ,
775
779
string azureVmResourceGroup ,
776
- string azureVmLocation )
780
+ string azureVmLocation ,
781
+ IAzureContext azureContext )
777
782
{
778
783
// get the location from AutomationAccountName. This will validate the account too
779
784
string location = this . GetAutomationAccount ( resourceGroupName , automationAccountName ) . Location ;
@@ -790,50 +795,135 @@ public void RegisterDscNode(string resourceGroupName,
790
795
azureVmResourceGroup = resourceGroupName ;
791
796
}
792
797
793
- var deploymentName = Guid . NewGuid ( ) . ToString ( ) ;
794
-
795
798
// get the endpoint and keys
796
799
Model . AgentRegistration agentRegistrationInfo = this . GetAgentRegistration (
797
800
resourceGroupName ,
798
801
automationAccountName ) ;
802
+
803
+ var parameters = GetDSCDeploymenttemplateParameters ( resourceGroupName ,
804
+ automationAccountName ,
805
+ azureVMName ,
806
+ nodeconfigurationName ,
807
+ configurationMode ,
808
+ configurationModeFrequencyMins ,
809
+ refreshFrequencyMins ,
810
+ rebootFlag ,
811
+ actionAfterReboot ,
812
+ moduleOverwriteFlag ,
813
+ azureVmResourceGroup ,
814
+ location ,
815
+ agentRegistrationInfo . Endpoint ,
816
+ agentRegistrationInfo . PrimaryKey ) ;
817
+
818
+ var armClient = AzureSession . Instance . ClientFactory . CreateArmClient < ResourceManagementClient > ( azureContext , AzureEnvironment . Endpoint . ResourceManager ) ;
819
+
820
+ var deployment = new Management . ResourceManager . Models . Deployment
821
+ {
822
+ Properties = new Management . ResourceManager . Models . DeploymentProperties
823
+ {
824
+ TemplateLink = new Management . ResourceManager . Models . TemplateLink ( Constants . TemplateFile ) ,
825
+ Parameters = parameters
826
+ }
827
+ } ;
799
828
800
- // prepare the parameters to be used in New-AzureRmResourceGroupDeployment cmdlet
801
- Hashtable templateParameters = new Hashtable ( ) ;
802
- templateParameters . Add ( "vmName" , azureVMName ) ;
803
- templateParameters . Add ( "location" , location ) ;
804
- templateParameters . Add ( "modulesUrl" , Constants . ModulesUrl ) ;
805
- templateParameters . Add ( "configurationFunction" , Constants . ConfigurationFunction ) ;
806
- templateParameters . Add ( "registrationUrl" , agentRegistrationInfo . Endpoint ) ;
807
- templateParameters . Add ( "registrationKey" , agentRegistrationInfo . PrimaryKey ) ;
808
- templateParameters . Add ( "nodeConfigurationName" , nodeconfigurationName ) ;
809
- templateParameters . Add ( "configurationMode" , configurationMode ) ;
810
- templateParameters . Add ( "configurationModeFrequencyMins" , configurationModeFrequencyMins ) ;
811
- templateParameters . Add ( "refreshFrequencyMins" , refreshFrequencyMins ) ;
812
- templateParameters . Add ( "rebootNodeIfNeeded" , rebootFlag ) ;
813
- templateParameters . Add ( "actionAfterReboot" , actionAfterReboot ) ;
814
- templateParameters . Add ( "allowModuleOverwrite" , moduleOverwriteFlag ) ;
815
- templateParameters . Add ( "timestamp" , DateTimeOffset . UtcNow . ToString ( "o" ) ) ;
816
-
817
- // invoke the New-AzureRmResourceGroupDeployment cmdlet
818
- using ( var pipe = System . Management . Automation . PowerShell . Create ( RunspaceMode . NewRunspace ) )
819
- {
820
- Command invokeCommand = new Command ( "New-AzureRmResourceGroupDeployment" ) ;
821
- invokeCommand . Parameters . Add ( "Name" , deploymentName ) ;
822
- invokeCommand . Parameters . Add ( "ResourceGroupName" , azureVmResourceGroup ) ;
823
- invokeCommand . Parameters . Add ( "TemplateParameterObject" , templateParameters ) ;
824
- invokeCommand . Parameters . Add ( "TemplateFile" , Constants . TemplateFile ) ;
825
-
826
- pipe . AddCommand ( invokeCommand . ToString ( ) ) ;
827
-
828
- pipe . AddCommand ( "Out-Default" ) ;
829
-
830
- Collection < PSObject > results = pipe . Invoke ( ) ;
831
- }
829
+ armClient . Deployments . CreateOrUpdate ( azureVmResourceGroup , Guid . NewGuid ( ) . ToString ( ) , deployment ) ;
830
+
832
831
}
833
832
834
- #endregion
833
+ private string GetDSCDeploymenttemplateParameters ( string resourceGroupName ,
834
+ string automationAccountName ,
835
+ string azureVMName ,
836
+ string nodeconfigurationName ,
837
+ string configurationMode ,
838
+ int configurationModeFrequencyMins ,
839
+ int refreshFrequencyMins ,
840
+ bool rebootFlag ,
841
+ string actionAfterReboot ,
842
+ bool moduleOverwriteFlag ,
843
+ string azureVmResourceGroup ,
844
+ string azureVmLocation ,
845
+ string registrationEndPoint ,
846
+ string registrationKey )
847
+ {
848
+ var template = new DeploymentTemplateParameters
849
+ {
850
+ ContentVersion = "1.0.0.0" ,
851
+ Schema = "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#" ,
852
+ Parameters = new ParametersObj {
853
+ ActionAfterReboot = new TemplateParameters {
854
+ Value = actionAfterReboot
855
+ } ,
856
+ AllowModuleOverwrite = new TemplateParameters
857
+ {
858
+ Value = moduleOverwriteFlag
859
+ } ,
860
+ ConfigurationFunction = new TemplateParameters
861
+ {
862
+ Value = Constants . ConfigurationFunction
863
+ } ,
864
+ ConfigurationMode = new TemplateParameters
865
+ {
866
+ Value = configurationMode
867
+ } ,
868
+ ConfigurationModeFrequencyMins = new TemplateParameters
869
+ {
870
+ Value = configurationModeFrequencyMins
871
+ } ,
872
+ Location = new TemplateParameters
873
+ {
874
+ Value = azureVmLocation
875
+ } ,
876
+ ModulesUrl = new TemplateParameters
877
+ {
878
+ Value = Constants . ModulesUrl
879
+ } ,
880
+ NodeConfigurationName = new TemplateParameters
881
+ {
882
+ Value = nodeconfigurationName
883
+ } ,
884
+ RebootNodeIfNeeded = new TemplateParameters
885
+ {
886
+ Value = rebootFlag
887
+ } ,
888
+ RefreshFrequencyMins = new TemplateParameters
889
+ {
890
+ Value = refreshFrequencyMins
891
+ } ,
892
+ RegistrationKey = new TemplateParameters
893
+ {
894
+ Value = registrationKey
895
+ } ,
896
+ RegistrationUrl = new TemplateParameters
897
+ {
898
+ Value = registrationEndPoint
899
+ } ,
900
+ Timestamp = new TemplateParameters
901
+ {
902
+ Value = DateTimeOffset . UtcNow . ToString ( "o" )
903
+ } ,
904
+ VmName = new TemplateParameters
905
+ {
906
+ Value = azureVMName
907
+ }
908
+ }
909
+ } ;
910
+
911
+ var serializationSettings = new JsonSerializerSettings
912
+ {
913
+ NullValueHandling = NullValueHandling . Ignore ,
914
+ ContractResolver = new CamelCasePropertyNamesContractResolver ( )
915
+ } ;
916
+
917
+ var serializer = JsonSerializer . Create ( serializationSettings ) ;
918
+ var textWriter = new StringWriter ( ) ;
919
+ serializer . Serialize ( textWriter , template ) ;
920
+ return textWriter . ToString ( ) ;
921
+ }
922
+
923
+
924
+ #endregion
835
925
836
- #region compilationjob
926
+ #region compilationjob
837
927
838
928
public Model . CompilationJob GetCompilationJob ( string resourceGroupName , string automationAccountName , Guid Id )
839
929
{
@@ -1718,4 +1808,52 @@ private string GetNodeConfigurationListFilterString(string configurationName)
1718
1808
1719
1809
#endregion
1720
1810
}
1811
+
1812
+
1813
+ internal class DeploymentTemplateParameters
1814
+ {
1815
+ [ JsonProperty ( "$schema" ) ]
1816
+ public string Schema { get ; set ; }
1817
+
1818
+ public string ContentVersion { get ; set ; }
1819
+
1820
+ public ParametersObj Parameters { get ; set ; }
1821
+ }
1822
+
1823
+ internal class ParametersObj
1824
+ {
1825
+ public TemplateParameters VmName { get ; set ; }
1826
+
1827
+ public TemplateParameters Location { get ; set ; }
1828
+
1829
+ public TemplateParameters ModulesUrl { get ; set ; }
1830
+
1831
+ public TemplateParameters ConfigurationFunction { get ; set ; }
1832
+
1833
+ public TemplateParameters RegistrationKey { get ; set ; }
1834
+
1835
+ public TemplateParameters RegistrationUrl { get ; set ; }
1836
+
1837
+ public TemplateParameters NodeConfigurationName { get ; set ; }
1838
+
1839
+ public TemplateParameters ConfigurationMode { get ; set ; }
1840
+
1841
+ public TemplateParameters ConfigurationModeFrequencyMins { get ; set ; }
1842
+
1843
+ public TemplateParameters RefreshFrequencyMins { get ; set ; }
1844
+
1845
+ public TemplateParameters RebootNodeIfNeeded { get ; set ; }
1846
+
1847
+ public TemplateParameters ActionAfterReboot { get ; set ; }
1848
+
1849
+ public TemplateParameters AllowModuleOverwrite { get ; set ; }
1850
+
1851
+ public TemplateParameters Timestamp { get ; set ; }
1852
+ }
1853
+
1854
+ internal class TemplateParameters
1855
+ {
1856
+ public object Value { get ; set ; }
1857
+ }
1858
+
1721
1859
}
0 commit comments