11package sumologic
22
33import (
4+ "encoding/json"
45 "fmt"
56
67 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -75,24 +76,14 @@ func resourceSumologicCsoarPlaybook() *schema.Resource {
7576 Default : true ,
7677 },
7778 "links" : {
78- Type : schema .TypeList ,
79- Optional : true ,
80- Elem : & schema.Schema {
81- Type : schema .TypeMap ,
82- Elem : & schema.Schema {
83- Type : schema .TypeString ,
84- },
85- },
79+ Type : schema .TypeString ,
80+ Optional : true ,
81+ Description : "JSON string representation of playbook links" ,
8682 },
8783 "nodes" : {
88- Type : schema .TypeList ,
89- Optional : true ,
90- Elem : & schema.Schema {
91- Type : schema .TypeMap ,
92- Elem : & schema.Schema {
93- Type : schema .TypeString ,
94- },
95- },
84+ Type : schema .TypeString ,
85+ Optional : true ,
86+ Description : "JSON string representation of playbook nodes" ,
9687 },
9788 },
9889 }
@@ -108,53 +99,107 @@ func resourceSumologicCsoarPlaybookUpdate(d *schema.ResourceData, meta interface
10899 c := meta .(* Client )
109100
110101 playbook := Playbook {
111- Description : d .Get ("description" ).(string ),
112- Name : d .Get ("name" ).(string ),
113- UpdatedName : d .Get ("updated_name" ).(string ),
114- Tags : d .Get ("tags" ).(string ),
115- IsDeleted : d .Get ("is_deleted" ).(bool ),
116- Draft : d .Get ("draft" ).(bool ),
117- IsPublished : d .Get ("is_published" ).(bool ),
118- LastUpdated : int64 (d .Get ("last_updated" ).(int )),
119- CreatedBy : int64 (d .Get ("created_by" ).(int )),
120- UpdatedBy : int64 (d .Get ("updated_by" ).(int )),
121- Nested : d .Get ("nested" ).(bool ),
122- Type : d .Get ("type" ).(string ),
123- IsEnabled : d .Get ("is_enabled" ).(bool ),
124- }
125-
126- if v , ok := d .Get ("links" ).([]interface {}); ok {
127- links := make ([]map [string ]interface {}, len (v ))
128- for i , link := range v {
129- if linkMap , ok := link .(map [string ]interface {}); ok {
130- links [i ] = linkMap
131- }
102+ Name : d .Get ("name" ).(string ),
103+ }
104+
105+ playbook .Description = d .Get ("description" ).(string )
106+ playbook .UpdatedName = d .Get ("updated_name" ).(string )
107+ playbook .Tags = d .Get ("tags" ).(string )
108+ playbook .Type = d .Get ("type" ).(string )
109+ playbook .IsDeleted = d .Get ("is_deleted" ).(bool )
110+ playbook .Draft = d .Get ("draft" ).(bool )
111+ playbook .IsPublished = d .Get ("is_published" ).(bool )
112+ playbook .Nested = d .Get ("nested" ).(bool )
113+ playbook .IsEnabled = d .Get ("is_enabled" ).(bool )
114+
115+ // Handle links as JSON string
116+ if linksJSON , ok := d .Get ("links" ).(string ); ok && linksJSON != "" {
117+ var links []map [string ]interface {}
118+ if err := json .Unmarshal ([]byte (linksJSON ), & links ); err != nil {
119+ return fmt .Errorf ("error parsing links JSON: %v" , err )
132120 }
133121 playbook .Links = links
134122 }
135123
136- if v , ok := d .Get ("nodes" ).([]interface {}); ok {
137- nodes := make ([]map [string ]interface {}, len (v ))
138- for i , node := range v {
139- if nodeMap , ok := node .(map [string ]interface {}); ok {
140- nodes [i ] = nodeMap
141- }
124+ // Handle nodes as JSON string
125+ if nodesJSON , ok := d .Get ("nodes" ).(string ); ok && nodesJSON != "" {
126+ var nodes []map [string ]interface {}
127+ if err := json .Unmarshal ([]byte (nodesJSON ), & nodes ); err != nil {
128+ return fmt .Errorf ("error parsing nodes JSON: %v" , err )
142129 }
143130 playbook .Nodes = nodes
144131 }
145132
146- return c .UpdatePlaybook (playbook )
133+ err := c .UpdatePlaybook (playbook )
134+ if err != nil {
135+ return err
136+ }
137+
138+ // Always set state after successful update
139+ d .Set ("description" , playbook .Description )
140+ d .Set ("tags" , playbook .Tags )
141+ d .Set ("updated_name" , playbook .UpdatedName )
142+ d .Set ("type" , playbook .Type )
143+ d .Set ("is_deleted" , playbook .IsDeleted )
144+ d .Set ("draft" , playbook .Draft )
145+ d .Set ("is_published" , playbook .IsPublished )
146+ d .Set ("nested" , playbook .Nested )
147+ d .Set ("is_enabled" , playbook .IsEnabled )
148+
149+ linksJSON := d .Get ("links" ).(string )
150+ d .Set ("links" , linksJSON )
151+
152+ nodesJSON := d .Get ("nodes" ).(string )
153+ d .Set ("nodes" , nodesJSON )
154+
155+ return nil
147156}
148157
149158func resourceSumologicCsoarPlaybookCreate (d * schema.ResourceData , meta interface {}) error {
150- return fmt .Errorf ("playbooks cannot be created via Terraform. Please create the playbook in the Sumo Logic UI and then import it using 'terraform import'" )
159+ return fmt .Errorf ("playbooks cannot be created via Terraform. Please create the playbook in the CSOAR UI, export it as JSON, and then import it using 'terraform import'" )
151160}
152161
153162func resourceSumologicCsoarPlaybookRead (d * schema.ResourceData , meta interface {}) error {
154163 if d .Id () == "" {
155164 return fmt .Errorf ("resource ID is empty" )
156165 }
157166
167+ // For import-only resources, ensure the name matches the ID
168+ d .Set ("name" , d .Id ())
169+
170+ // For import-only resources, preserve existing state values or set reasonable defaults
171+ // This prevents Terraform from thinking the resource doesn't exist
172+ if d .Get ("description" ) == nil {
173+ d .Set ("description" , "" )
174+ }
175+ if d .Get ("tags" ) == nil {
176+ d .Set ("tags" , "" )
177+ }
178+ if d .Get ("is_deleted" ) == nil {
179+ d .Set ("is_deleted" , false )
180+ }
181+ if d .Get ("draft" ) == nil {
182+ d .Set ("draft" , false )
183+ }
184+ if d .Get ("is_published" ) == nil {
185+ d .Set ("is_published" , true )
186+ }
187+ if d .Get ("nested" ) == nil {
188+ d .Set ("nested" , false )
189+ }
190+ if d .Get ("type" ) == nil {
191+ d .Set ("type" , "General" )
192+ }
193+ if d .Get ("is_enabled" ) == nil {
194+ d .Set ("is_enabled" , true )
195+ }
196+ if d .Get ("nodes" ) == nil {
197+ d .Set ("nodes" , "[]" )
198+ }
199+ if d .Get ("links" ) == nil {
200+ d .Set ("links" , "[]" )
201+ }
202+
158203 return nil
159204}
160205
@@ -168,5 +213,17 @@ func resourceSumologicCsoarPlaybookImport(d *schema.ResourceData, meta interface
168213 d .SetId (playbookName )
169214 d .Set ("name" , playbookName )
170215
216+ // Set default values for all schema fields to prevent Terraform from thinking this is a new resource
217+ d .Set ("description" , "" )
218+ d .Set ("tags" , "" )
219+ d .Set ("is_deleted" , false )
220+ d .Set ("draft" , false )
221+ d .Set ("is_published" , true )
222+ d .Set ("nested" , false )
223+ d .Set ("type" , "General" )
224+ d .Set ("is_enabled" , true )
225+ d .Set ("nodes" , "[]" )
226+ d .Set ("links" , "[]" )
227+
171228 return []* schema.ResourceData {d }, nil
172229}
0 commit comments