@@ -2,7 +2,9 @@ package provider
22
33import (
44 "context"
5+ "fmt"
56
7+ "github.com/hashicorp/terraform-plugin-log/tflog"
68 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
79 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
810 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -146,20 +148,90 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
146148 return diag .FromErr (err )
147149 }
148150
149- instance , err := c .CreateInstance (ctx , d .Get ("environment" ).(string ), d .Get ("resource_id" ).(string ), & api.InstanceMessage {
150- Title : d .Get ("title" ).(string ),
151- Engine : d .Get ("engine" ).(string ),
152- ExternalLink : d .Get ("external_link" ).(string ),
153- State : api .Active ,
154- DataSources : dataSourceList ,
151+ environmentID := d .Get ("environment" ).(string )
152+ instanceID := d .Get ("resource_id" ).(string )
153+ instanceName := fmt .Sprintf ("environments/%s/instances/%s" , environmentID , instanceID )
154+
155+ existedInstance , err := c .GetInstance (ctx , & api.InstanceFindMessage {
156+ EnvironmentID : environmentID ,
157+ InstanceID : instanceID ,
158+ ShowDeleted : true ,
155159 })
156160 if err != nil {
157- return diag . FromErr ( err )
161+ tflog . Debug ( ctx , fmt . Sprintf ( "get instance %s failed with error: %v" , instanceName , err ) )
158162 }
159163
160- d .SetId (instance .Name )
164+ var diags diag.Diagnostics
165+ if existedInstance != nil && err == nil {
166+ diags = append (diags , diag.Diagnostic {
167+ Severity : diag .Warning ,
168+ Summary : "Instance already exists" ,
169+ Detail : fmt .Sprintf ("Instance %s already exists, try to exec the update operation" , instanceName ),
170+ })
171+
172+ engine := d .Get ("engine" ).(string )
173+ if existedInstance .Engine != engine {
174+ diags = append (diags , diag.Diagnostic {
175+ Severity : diag .Error ,
176+ Summary : "Invalid argument" ,
177+ Detail : fmt .Sprintf ("cannot update instance %s engine to %s" , instanceName , engine ),
178+ })
179+ return diags
180+ }
181+
182+ if existedInstance .State == api .Deleted {
183+ diags = append (diags , diag.Diagnostic {
184+ Severity : diag .Warning ,
185+ Summary : "Instance is deleted" ,
186+ Detail : fmt .Sprintf ("Instance %s already deleted, try to undelete the instance" , instanceName ),
187+ })
188+ if _ , err := c .UndeleteInstance (ctx , environmentID , instanceID ); err != nil {
189+ diags = append (diags , diag.Diagnostic {
190+ Severity : diag .Error ,
191+ Summary : "Failed to undelete instance" ,
192+ Detail : fmt .Sprintf ("Undelete instance %s failed, error: %v" , instanceName , err ),
193+ })
194+ return diags
195+ }
196+ }
161197
162- return resourceInstanceRead (ctx , d , m )
198+ title := d .Get ("title" ).(string )
199+ externalLink := d .Get ("external_link" ).(string )
200+ instance , err := c .UpdateInstance (ctx , environmentID , instanceID , & api.InstancePatchMessage {
201+ Title : & title ,
202+ ExternalLink : & externalLink ,
203+ DataSources : dataSourceList ,
204+ })
205+ if err != nil {
206+ diags = append (diags , diag.Diagnostic {
207+ Severity : diag .Error ,
208+ Summary : "Failed to update instance" ,
209+ Detail : fmt .Sprintf ("Update instance %s failed, error: %v" , instanceName , err ),
210+ })
211+ return diags
212+ }
213+
214+ d .SetId (instance .Name )
215+ } else {
216+ instance , err := c .CreateInstance (ctx , environmentID , instanceID , & api.InstanceMessage {
217+ Title : d .Get ("title" ).(string ),
218+ Engine : d .Get ("engine" ).(string ),
219+ ExternalLink : d .Get ("external_link" ).(string ),
220+ State : api .Active ,
221+ DataSources : dataSourceList ,
222+ })
223+ if err != nil {
224+ return diag .FromErr (err )
225+ }
226+ d .SetId (instance .Name )
227+ }
228+
229+ diag := resourceInstanceRead (ctx , d , m )
230+ if diag != nil {
231+ diags = append (diags , diag ... )
232+ }
233+
234+ return diags
163235}
164236
165237func resourceInstanceRead (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
@@ -189,16 +261,50 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
189261 return diag .FromErr (err )
190262 }
191263
192- patch := & api.InstanceMessage {}
193- if d .HasChange ("title" ) {
194- if title , ok := d .GetOk ("title" ); ok {
195- patch .Title = title .(string )
264+ instanceName := fmt .Sprintf ("environments/%s/instances/%s" , envID , instanceID )
265+
266+ existedInstance , err := c .GetInstance (ctx , & api.InstanceFindMessage {
267+ EnvironmentID : envID ,
268+ InstanceID : instanceID ,
269+ ShowDeleted : true ,
270+ })
271+ if err != nil {
272+ return diag .FromErr (err )
273+ }
274+
275+ var diags diag.Diagnostics
276+ if existedInstance .Engine != d .Get ("engine" ).(string ) {
277+ diags = append (diags , diag.Diagnostic {
278+ Severity : diag .Error ,
279+ Summary : "Invalid argument" ,
280+ Detail : fmt .Sprintf ("cannot update instance %s engine to %s" , instanceName , d .Get ("engine" ).(string )),
281+ })
282+ return diags
283+ }
284+ if existedInstance .State == api .Deleted {
285+ diags = append (diags , diag.Diagnostic {
286+ Severity : diag .Warning ,
287+ Summary : "Instance is deleted" ,
288+ Detail : fmt .Sprintf ("Instance %s already deleted, try to undelete the instance" , instanceName ),
289+ })
290+ if _ , err := c .UndeleteInstance (ctx , envID , instanceID ); err != nil {
291+ diags = append (diags , diag.Diagnostic {
292+ Severity : diag .Error ,
293+ Summary : "Failed to undelete instance" ,
294+ Detail : fmt .Sprintf ("Undelete instance %s failed, error: %v" , instanceName , err ),
295+ })
296+ return diags
196297 }
197298 }
299+
300+ patch := & api.InstancePatchMessage {}
301+ if d .HasChange ("title" ) {
302+ v := d .Get ("title" ).(string )
303+ patch .Title = & v
304+ }
198305 if d .HasChange ("external_link" ) {
199- if link , ok := d .GetOk ("external_link" ); ok {
200- patch .ExternalLink = link .(string )
201- }
306+ v := d .Get ("external_link" ).(string )
307+ patch .ExternalLink = & v
202308 }
203309 if d .HasChange ("data_sources" ) {
204310 dataSourceList , err := convertDataSourceCreateList (d )
@@ -212,7 +318,12 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
212318 return diag .FromErr (err )
213319 }
214320
215- return resourceInstanceRead (ctx , d , m )
321+ diag := resourceInstanceRead (ctx , d , m )
322+ if diag != nil {
323+ diags = append (diags , diag ... )
324+ }
325+
326+ return diags
216327}
217328
218329func resourceInstanceDelete (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
0 commit comments