Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix source accessor to allow use in source block #11318

Merged
merged 2 commits into from
Oct 11, 2021

Conversation

SwampDragons
Copy link
Contributor

@SwampDragons SwampDragons commented Oct 8, 2021

Makes sure that the source name and type are part of the eval context for builds; this allows you to use source.type and source.

Test func:

source "amazon-ebs" "cannonical-ubuntu-server" {
// ...
  run_volume_tags = {
    builder-type-hcl = source.type
    builder-name-hcl = source.name
  }
}

Before:

==> vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tags to source instance
    vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "Name": "Packer Builder"
    vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "builder-name-hcl": "<unknown>"
    vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "builder-type-hcl": "<unknown>"

After:

==> vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tags to source instance
    vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "Name": "Packer Builder"
    vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "builder-name-hcl": "cannonical-ubuntu-server"
    vanilla.amazon-ebs.cannonical-ubuntu-server: Adding tag: "builder-type-hcl": "amazon-ebs"

@SwampDragons SwampDragons requested a review from a team as a code owner October 8, 2021 21:04
Copy link
Contributor

@azr azr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I would maybe add a test for this one, to avoid regressions :)

Copy link
Member

@sylviamoss sylviamoss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏼

Comment on lines 114 to 117
ectx.Variables[sourcesAccessor] = cty.ObjectVal(map[string]cty.Value{
"type": cty.StringVal(source.Type),
"name": cty.StringVal(source.Name),
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following will give the same result and if in the future we add a new attribute we will have to change only in one place.

Suggested change
ectx.Variables[sourcesAccessor] = cty.ObjectVal(map[string]cty.Value{
"type": cty.StringVal(source.Type),
"name": cty.StringVal(source.Name),
})
ectx.Variables[sourcesAccessor] = cty.ObjectVal(source.ctyValues())

The ctyValues is also used here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is cool! Thanks for sharing.

Copy link
Member

@nywilken nywilken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Also nice learning here on
ectx.Variables[sourcesAccessor] = cty.ObjectVal(source.ctyValues())

@SwampDragons
Copy link
Contributor Author

Added tests -- I'll merge when green

@SwampDragons SwampDragons merged commit e7ba4d3 into master Oct 11, 2021
@azr azr deleted the fix_source_accessor branch October 12, 2021 07:43
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants