Skip to content

Commit 5587f54

Browse files
committed
Update tf version and allow tags to be created in stateless mode if they do not exist (as opposed to simply checking that the tag set exists).
1 parent efc1d06 commit 5587f54

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

cmd/internal/converters/tagset_converter.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package converters
22

33
import (
44
"fmt"
5+
56
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/args"
67
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/client"
78
"github.com/OctopusSolutionsEngineering/OctopusTerraformExport/cmd/internal/data"
@@ -238,7 +239,6 @@ func (c *TagSetConverter) toHcl(tagSet octopus.TagSet, stateless bool, dependenc
238239
terraformResource := terraform.TerraformTagSet{
239240
Type: octopusdeployTagSetResourceType,
240241
Name: tagSetName,
241-
Count: c.getCount(stateless, tagSetName),
242242
ResourceName: tagSet.Name,
243243
Description: strutil.NilIfEmptyPointer(strutil.TrimPointer(tagSet.Description)),
244244
// 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
254254
block := gohcl.EncodeAsBlock(terraformResource, "resource")
255255

256256
if stateless {
257+
hcl.WriteUnquotedAttribute(block, "count", c.getTagSetCount(stateless, tagSetName))
257258
hcl.WriteLifecyclePreventDestroyAttribute(block)
258259
}
259260

@@ -287,7 +288,6 @@ func (c *TagSetConverter) toHcl(tagSet octopus.TagSet, stateless bool, dependenc
287288
terraformResource := terraform.TerraformTag{
288289
Type: octopusdeployTagResourceType,
289290
Name: tagsetName + "_" + tagName,
290-
Count: c.getCount(stateless, tagSetName),
291291
ResourceName: tag.Name,
292292
TagSetId: c.getTagsetId(stateless, tagSetName, tagName),
293293
Color: tag.Color,
@@ -298,7 +298,13 @@ func (c *TagSetConverter) toHcl(tagSet octopus.TagSet, stateless bool, dependenc
298298
}
299299

300300
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)
302308

303309
return string(file.Bytes()), nil
304310
}
@@ -335,12 +341,23 @@ func (c *TagSetConverter) getDependency(stateless bool, tagName string) string {
335341
return ""
336342
}
337343

338-
func (c *TagSetConverter) getCount(stateless bool, tagSetName string) *string {
344+
func (c *TagSetConverter) getTagSetCount(stateless bool, tagSetName string) string {
339345
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"
341347
}
342348

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 ""
344361
}
345362

346363
func (c *TagSetConverter) buildData(resourceName string, resource octopus.TagSet) terraform.TerraformTagSetData {

cmd/internal/model/terraform/terraform_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c TerraformConfig) CreateTerraformConfig(backend string, version string) T
4545
RequiredProviders: RequiredProviders{
4646
OctopusProvider: ProviderDefinition{
4747
Source: "OctopusDeploy/octopusdeploy",
48-
Version: strutil.DefaultIfEmpty(version, "1.3.7"),
48+
Version: strutil.DefaultIfEmpty(version, "1.3.8"),
4949
},
5050
},
5151
RequiredVersion: strutil.StrPointer(">= 1.6.0"),

0 commit comments

Comments
 (0)