Skip to content

Commit

Permalink
Merge pull request #133 from overmindtech/trim-terraform-addresses
Browse files Browse the repository at this point in the history
Introduce more human-readable terraform_name on parsed items in `subm…
  • Loading branch information
DavidS-ovm authored Dec 4, 2023
2 parents f24b8a8 + 2f5269b commit 46a8133
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions cmd/submitplan.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,25 @@ func itemDiffFromResourceChange(resourceChange ResourceChange) (*sdp.ItemDiff, e
Status: status,
}

// shorten the address by removing the type prefix if and only if it is the
// first part. Longer terraform addresses created in modules will not be
// shortened to avoid confusion.
trimmedAddress, _ := strings.CutPrefix(resourceChange.Address, fmt.Sprintf("%v.", resourceChange.Type))

if beforeAttributes != nil {
result.Before = &sdp.Item{
Type: resourceChange.Type,
UniqueAttribute: "terraform_address",
UniqueAttribute: "terraform_name",
Attributes: beforeAttributes,
Scope: "terraform_plan",
}

err = result.Before.Attributes.Set("terraform_name", trimmedAddress)
if err != nil {
// since Address is a string, this should never happen
sentry.CaptureException(fmt.Errorf("failed to set terraform_name '%v' on before attributes: %w", trimmedAddress, err))
}

err = result.Before.Attributes.Set("terraform_address", resourceChange.Address)
if err != nil {
// since Address is a string, this should never happen
Expand All @@ -197,11 +208,17 @@ func itemDiffFromResourceChange(resourceChange ResourceChange) (*sdp.ItemDiff, e
if afterAttributes != nil {
result.After = &sdp.Item{
Type: resourceChange.Type,
UniqueAttribute: "terraform_address",
UniqueAttribute: "terraform_name",
Attributes: afterAttributes,
Scope: "terraform_plan",
}

err = result.After.Attributes.Set("terraform_name", trimmedAddress)
if err != nil {
// since Address is a string, this should never happen
sentry.CaptureException(fmt.Errorf("failed to set terraform_name '%v' on after attributes: %w", trimmedAddress, err))
}

err = result.After.Attributes.Set("terraform_address", resourceChange.Address)
if err != nil {
// since Address is a string, this should never happen
Expand Down

0 comments on commit 46a8133

Please sign in to comment.