@@ -2,6 +2,7 @@ package converters
2
2
3
3
import (
4
4
"fmt"
5
+
5
6
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/args"
6
7
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/client"
7
8
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/data"
@@ -238,7 +239,6 @@ func (c *TagSetConverter) toHcl(tagSet octopus.TagSet, stateless bool, dependenc
238
239
terraformResource := terraform.TerraformTagSet {
239
240
Type : octopusdeployTagSetResourceType ,
240
241
Name : tagSetName ,
241
- Count : c .getCount (stateless , tagSetName ),
242
242
ResourceName : tagSet .Name ,
243
243
Description : strutil .NilIfEmptyPointer (strutil .TrimPointer (tagSet .Description )),
244
244
// The new provider is strict, and fields must not change value.
@@ -254,6 +254,7 @@ func (c *TagSetConverter) toHcl(tagSet octopus.TagSet, stateless bool, dependenc
254
254
block := gohcl .EncodeAsBlock (terraformResource , "resource" )
255
255
256
256
if stateless {
257
+ hcl .WriteUnquotedAttribute (block , "count" , c .getTagSetCount (stateless , tagSetName ))
257
258
hcl .WriteLifecyclePreventDestroyAttribute (block )
258
259
}
259
260
@@ -287,7 +288,6 @@ func (c *TagSetConverter) toHcl(tagSet octopus.TagSet, stateless bool, dependenc
287
288
terraformResource := terraform.TerraformTag {
288
289
Type : octopusdeployTagResourceType ,
289
290
Name : tagsetName + "_" + tagName ,
290
- Count : c .getCount (stateless , tagSetName ),
291
291
ResourceName : tag .Name ,
292
292
TagSetId : c .getTagsetId (stateless , tagSetName , tagName ),
293
293
Color : tag .Color ,
@@ -298,7 +298,13 @@ func (c *TagSetConverter) toHcl(tagSet octopus.TagSet, stateless bool, dependenc
298
298
}
299
299
300
300
file := hclwrite .NewEmptyFile ()
301
- file .Body ().AppendBlock (gohcl .EncodeAsBlock (terraformResource , "resource" ))
301
+ block := gohcl .EncodeAsBlock (terraformResource , "resource" )
302
+
303
+ if stateless {
304
+ hcl .WriteUnquotedAttribute (block , "count" , c .getTagCount (stateless , tagSetName , tag .Name ))
305
+ }
306
+
307
+ file .Body ().AppendBlock (block )
302
308
303
309
return string (file .Bytes ()), nil
304
310
}
@@ -335,12 +341,23 @@ func (c *TagSetConverter) getDependency(stateless bool, tagName string) string {
335
341
return ""
336
342
}
337
343
338
- func (c * TagSetConverter ) getCount (stateless bool , tagSetName string ) * string {
344
+ func (c * TagSetConverter ) getTagSetCount (stateless bool , tagSetName string ) string {
339
345
if stateless {
340
- return strutil . StrPointer ( "${ length(data." + octopusdeployTagSetsData + "." + tagSetName + ".tag_sets) != 0 ? 0 : 1}" )
346
+ return " length(data." + octopusdeployTagSetsData + "." + tagSetName + ".tag_sets) != 0 ? 0 : 1"
341
347
}
342
348
343
- return nil
349
+ return ""
350
+ }
351
+
352
+ func (c * TagSetConverter ) getTagCount (stateless bool , tagSetName string , tagName string ) string {
353
+ if stateless {
354
+ // The count is 0 if:
355
+ // * The tagset exists and
356
+ // * The tag exists in the tagset
357
+ return "length(data." + octopusdeployTagSetsData + "." + tagSetName + ".tag_sets) != 0 && length([for item in data." + octopusdeployTagSetsData + "." + tagSetName + ".tag_sets[0].tags : item if item.name == \" " + tagName + "\" ]) != 0 ? 0 : 1"
358
+ }
359
+
360
+ return ""
344
361
}
345
362
346
363
func (c * TagSetConverter ) buildData (resourceName string , resource octopus.TagSet ) terraform.TerraformTagSetData {
0 commit comments