@@ -131,60 +131,62 @@ func datasourceAutomationVariableCommonSchema(attType pluginsdk.ValueType) map[s
131131 }
132132}
133133
134- func resourceAutomationVariableCreateUpdate (d * pluginsdk.ResourceData , meta interface {}, varType string ) error {
134+ func formatAutomationVariableValue (d * pluginsdk.ResourceData , varType string ) (string , error ) {
135+ switch strings .ToLower (varType ) {
136+ case "datetime" :
137+ vTime , err := time .Parse (time .RFC3339 , d .Get ("value" ).(string ))
138+ if err != nil {
139+ return "" , fmt .Errorf ("invalid time format: %+v" , err )
140+ }
141+ return fmt .Sprintf ("\" \\ /Date(%d)\\ /\" " , vTime .UnixNano ()/ 1000000 ), nil
142+ case "bool" :
143+ return strconv .FormatBool (d .Get ("value" ).(bool )), nil
144+ case "int" :
145+ return strconv .Itoa (d .Get ("value" ).(int )), nil
146+ case "object" :
147+ // We don't quote the object so it gets saved as a JSON object
148+ return d .Get ("value" ).(string ), nil
149+ case "string" :
150+ return strconv .Quote (d .Get ("value" ).(string )), nil
151+ default :
152+ return "" , nil
153+ }
154+ }
155+
156+ func resourceAutomationVariableCreate (d * pluginsdk.ResourceData , meta interface {}, varType string ) error {
135157 client := meta .(* clients.Client ).Automation .Variable
136158 subscriptionId := meta .(* clients.Client ).Account .SubscriptionId
137- ctx , cancel := timeouts .ForCreateUpdate (meta .(* clients.Client ).StopContext , d )
159+ ctx , cancel := timeouts .ForCreate (meta .(* clients.Client ).StopContext , d )
138160 defer cancel ()
139161
140162 varTypeLower := strings .ToLower (varType )
141163
142164 id := variable .NewVariableID (subscriptionId , d .Get ("resource_group_name" ).(string ), d .Get ("automation_account_name" ).(string ), d .Get ("name" ).(string ))
143165
144- if d .IsNewResource () {
145- resp , err := client .Get (ctx , id )
146- if err != nil {
147- if ! response .WasNotFound (resp .HttpResponse ) {
148- return fmt .Errorf ("checking for presence of existing Automation %s Variable %s: %+v" , varType , id , err )
149- }
150- }
151-
166+ resp , err := client .Get (ctx , id )
167+ if err != nil {
152168 if ! response .WasNotFound (resp .HttpResponse ) {
153- return tf . ImportAsExistsError ( fmt .Sprintf ( "azurerm_automation_variable_%s " , varTypeLower ) , id . ID () )
169+ return fmt .Errorf ( "checking for presence of existing Automation %s Variable %s: %+v " , varType , id , err )
154170 }
155171 }
156172
157- description := d .Get ("description" ).(string )
158- encrypted := d .Get ("encrypted" ).(bool )
159- value := ""
160-
161- switch varTypeLower {
162- case "datetime" :
163- vTime , parseErr := time .Parse (time .RFC3339 , d .Get ("value" ).(string ))
164- if parseErr != nil {
165- return fmt .Errorf ("invalid time format: %+v" , parseErr )
166- }
167- value = fmt .Sprintf ("\" \\ /Date(%d)\\ /\" " , vTime .UnixNano ()/ 1000000 )
168- case "bool" :
169- value = strconv .FormatBool (d .Get ("value" ).(bool ))
170- case "int" :
171- value = strconv .Itoa (d .Get ("value" ).(int ))
172- case "object" :
173- // We don't quote the object so it gets saved as a JSON object
174- value = d .Get ("value" ).(string )
175- case "string" :
176- value = strconv .Quote (d .Get ("value" ).(string ))
173+ if ! response .WasNotFound (resp .HttpResponse ) {
174+ return tf .ImportAsExistsError (fmt .Sprintf ("azurerm_automation_variable_%s" , varTypeLower ), id .ID ())
177175 }
178176
179177 parameters := variable.VariableCreateOrUpdateParameters {
180178 Name : id .VariableName ,
181179 Properties : variable.VariableCreateOrUpdateProperties {
182- Description : pointer .To (description ),
183- IsEncrypted : pointer .To (encrypted ),
180+ Description : pointer .To (d . Get ( " description" ).( string ) ),
181+ IsEncrypted : pointer .To (d . Get ( " encrypted" ).( bool ) ),
184182 },
185183 }
186184
187185 if varTypeLower != "null" {
186+ value , err := formatAutomationVariableValue (d , varType )
187+ if err != nil {
188+ return err
189+ }
188190 parameters .Properties .Value = pointer .To (value )
189191 }
190192
@@ -197,6 +199,64 @@ func resourceAutomationVariableCreateUpdate(d *pluginsdk.ResourceData, meta inte
197199 return resourceAutomationVariableRead (d , meta , varType )
198200}
199201
202+ func resourceAutomationVariableUpdate (d * pluginsdk.ResourceData , meta interface {}, varType string ) error {
203+ client := meta .(* clients.Client ).Automation .Variable
204+ ctx , cancel := timeouts .ForUpdate (meta .(* clients.Client ).StopContext , d )
205+ defer cancel ()
206+
207+ id , err := variable .ParseVariableID (d .Id ())
208+ if err != nil {
209+ return err
210+ }
211+
212+ // The PATCH endpoint (Update) does not support changing `encrypted`, so we use
213+ // the PUT endpoint (CreateOrUpdate) and overlay changed fields on the existing resource.
214+ existing , err := client .Get (ctx , * id )
215+ if err != nil {
216+ return fmt .Errorf ("retrieving existing Automation %s Variable %s: %+v" , varType , id , err )
217+ }
218+
219+ if existing .Model == nil || existing .Model .Properties == nil {
220+ return fmt .Errorf ("retrieving existing Automation %s Variable %s: model or properties were nil" , varType , id )
221+ }
222+
223+ existingProps := existing .Model .Properties
224+
225+ parameters := variable.VariableCreateOrUpdateParameters {
226+ Name : id .VariableName ,
227+ Properties : variable.VariableCreateOrUpdateProperties {
228+ Description : existingProps .Description ,
229+ IsEncrypted : existingProps .IsEncrypted ,
230+ Value : existingProps .Value ,
231+ },
232+ }
233+
234+ if d .HasChange ("description" ) {
235+ parameters .Properties .Description = pointer .To (d .Get ("description" ).(string ))
236+ }
237+
238+ if d .HasChange ("encrypted" ) {
239+ parameters .Properties .IsEncrypted = pointer .To (d .Get ("encrypted" ).(bool ))
240+ }
241+
242+ if d .HasChange ("value" ) {
243+ varTypeLower := strings .ToLower (varType )
244+ if varTypeLower != "null" {
245+ value , err := formatAutomationVariableValue (d , varType )
246+ if err != nil {
247+ return err
248+ }
249+ parameters .Properties .Value = pointer .To (value )
250+ }
251+ }
252+
253+ if _ , err := client .CreateOrUpdate (ctx , * id , parameters ); err != nil {
254+ return fmt .Errorf ("updating Automation %s Variable %s: %+v" , varType , id , err )
255+ }
256+
257+ return resourceAutomationVariableRead (d , meta , varType )
258+ }
259+
200260func resourceAutomationVariableRead (d * pluginsdk.ResourceData , meta interface {}, varType string ) error {
201261 client := meta .(* clients.Client ).Automation .Variable
202262 ctx , cancel := timeouts .ForRead (meta .(* clients.Client ).StopContext , d )
0 commit comments