@@ -23,9 +23,9 @@ import (
2323
2424func resourceAutomationDscNodeConfiguration () * pluginsdk.Resource {
2525 return & pluginsdk.Resource {
26- Create : resourceAutomationDscNodeConfigurationCreateUpdate ,
26+ Create : resourceAutomationDscNodeConfigurationCreate ,
2727 Read : resourceAutomationDscNodeConfigurationRead ,
28- Update : resourceAutomationDscNodeConfigurationCreateUpdate ,
28+ Update : resourceAutomationDscNodeConfigurationUpdate ,
2929 Delete : resourceAutomationDscNodeConfigurationDelete ,
3030
3131 Importer : pluginsdk .ImporterValidatingResourceId (func (id string ) error {
@@ -71,61 +71,89 @@ func resourceAutomationDscNodeConfiguration() *pluginsdk.Resource {
7171 }
7272}
7373
74- func resourceAutomationDscNodeConfigurationCreateUpdate (d * pluginsdk.ResourceData , meta interface {}) error {
74+ func resourceAutomationDscNodeConfigurationCreate (d * pluginsdk.ResourceData , meta interface {}) error {
7575 client := meta .(* clients.Client ).Automation .DscNodeConfiguration
7676 subscriptionId := meta .(* clients.Client ).Account .SubscriptionId
77- ctx , cancel := timeouts .ForCreateUpdate (meta .(* clients.Client ).StopContext , d )
77+ ctx , cancel := timeouts .ForCreate (meta .(* clients.Client ).StopContext , d )
7878 defer cancel ()
7979
8080 log .Printf ("[INFO] preparing arguments for AzureRM Automation Dsc Node Configuration creation." )
8181
8282 id := dscnodeconfiguration .NewNodeConfigurationID (subscriptionId , d .Get ("resource_group_name" ).(string ), d .Get ("automation_account_name" ).(string ), d .Get ("name" ).(string ))
8383
84- if d .IsNewResource () {
85- existing , err := client .Get (ctx , id )
86- if err != nil {
87- if ! response .WasNotFound (existing .HttpResponse ) {
88- return fmt .Errorf ("checking for presence of existing %s: %s" , id , err )
89- }
90- }
91-
84+ existing , err := client .Get (ctx , id )
85+ if err != nil {
9286 if ! response .WasNotFound (existing .HttpResponse ) {
93- return tf . ImportAsExistsError ( "azurerm_automation_dsc_nodeconfiguration " , id . ID () )
87+ return fmt . Errorf ( "checking for presence of existing %s: %s " , id , err )
9488 }
9589 }
9690
97- content := d .Get ("content_embedded" ).(string )
98-
99- // configuration name is always the first part of the dsc node configuration
100- // e.g. webserver.prod or webserver.local will be associated to the dsc configuration webserver
101-
102- configurationName := strings .Split (id .NodeConfigurationName , "." )[0 ]
103-
104- contentSourceType := dscnodeconfiguration .ContentSourceTypeEmbeddedContent
91+ if ! response .WasNotFound (existing .HttpResponse ) {
92+ return tf .ImportAsExistsError ("azurerm_automation_dsc_nodeconfiguration" , id .ID ())
93+ }
10594
10695 parameters := dscnodeconfiguration.DscNodeConfigurationCreateOrUpdateParameters {
10796 Properties : & dscnodeconfiguration.DscNodeConfigurationCreateOrUpdateParametersProperties {
10897 Source : dscnodeconfiguration.ContentSource {
109- Type : & contentSourceType ,
110- Value : pointer .To (content ),
98+ Type : pointer . To ( dscnodeconfiguration . ContentSourceTypeEmbeddedContent ) ,
99+ Value : pointer .To (d . Get ( "content_embedded" ).( string ) ),
111100 },
101+ // configuration name is always the first part of the dsc node configuration
102+ // e.g. webserver.prod or webserver.local will be associated to the dsc configuration webserver
112103 Configuration : dscnodeconfiguration.DscConfigurationAssociationProperty {
113- Name : pointer .To (configurationName ),
104+ Name : pointer .To (strings . Split ( id . NodeConfigurationName , "." )[ 0 ] ),
114105 },
115106 },
116107 Name : pointer .To (id .NodeConfigurationName ),
117108 }
118109
119- err := client .CreateOrUpdateThenPoll (ctx , id , parameters )
120- if err != nil {
121- return fmt .Errorf ("creating/updating %s: %+v" , id , err )
110+ if err := client .CreateOrUpdateThenPoll (ctx , id , parameters ); err != nil {
111+ return fmt .Errorf ("creating %s: %+v" , id , err )
122112 }
123113
124114 d .SetId (id .ID ())
125115
126116 return resourceAutomationDscNodeConfigurationRead (d , meta )
127117}
128118
119+ func resourceAutomationDscNodeConfigurationUpdate (d * pluginsdk.ResourceData , meta interface {}) error {
120+ client := meta .(* clients.Client ).Automation .DscNodeConfiguration
121+ ctx , cancel := timeouts .ForUpdate (meta .(* clients.Client ).StopContext , d )
122+ defer cancel ()
123+
124+ id , err := dscnodeconfiguration .ParseNodeConfigurationID (d .Id ())
125+ if err != nil {
126+ return err
127+ }
128+
129+ // there is only one property that can be updated and is not force new: content_embedded
130+ // if content_embedded is changed, we need to update the dsc node configuration
131+ // otherwise we can just return as the resource is already in the desired state
132+ if d .HasChange ("content_embedded" ) {
133+ parameters := dscnodeconfiguration.DscNodeConfigurationCreateOrUpdateParameters {
134+ Properties : & dscnodeconfiguration.DscNodeConfigurationCreateOrUpdateParametersProperties {
135+ Source : dscnodeconfiguration.ContentSource {
136+ Type : pointer .To (dscnodeconfiguration .ContentSourceTypeEmbeddedContent ),
137+ // content_embedded is not returned by the API, so we get it from d.Get
138+ Value : pointer .To (d .Get ("content_embedded" ).(string )),
139+ },
140+ // configuration name is always the first part of the dsc node configuration
141+ // e.g. webserver.prod or webserver.local will be associated to the dsc configuration webserver
142+ Configuration : dscnodeconfiguration.DscConfigurationAssociationProperty {
143+ Name : pointer .To (strings .Split (id .NodeConfigurationName , "." )[0 ]),
144+ },
145+ },
146+ Name : pointer .To (id .NodeConfigurationName ),
147+ }
148+
149+ if err := client .CreateOrUpdateThenPoll (ctx , * id , parameters ); err != nil {
150+ return fmt .Errorf ("updating %s: %+v" , * id , err )
151+ }
152+ }
153+
154+ return resourceAutomationDscNodeConfigurationRead (d , meta )
155+ }
156+
129157func resourceAutomationDscNodeConfigurationRead (d * pluginsdk.ResourceData , meta interface {}) error {
130158 client := meta .(* clients.Client ).Automation .DscNodeConfiguration
131159 ctx , cancel := timeouts .ForRead (meta .(* clients.Client ).StopContext , d )
0 commit comments