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

Calling Invoke-EpsTemplate Inside for loop #36

Open
jeffpatton1971 opened this issue Aug 14, 2020 · 3 comments
Open

Calling Invoke-EpsTemplate Inside for loop #36

jeffpatton1971 opened this issue Aug 14, 2020 · 3 comments

Comments

@jeffpatton1971
Copy link

I know this repo is old, I'm really hoping someone is watching this, so here goes.

I'm using eps to create tf files that are passed to modules. For one resource there are a list of nested resources, so this script calls two templates, one to create the main tf and one to create the vars.tf that is passed in. When I run the vars eps template with a group of objects, it returns as expected.

When i run in the context of the script, the eps doesn't iterate over the objects passed in. I'm curious if that's because I'm calling invoke from inside a for loop and that's somehow messing things up?

Here's some stripped down samples, cant' really share everything sorry:

Calling script

$Groups = $Resource | Group-Object -Property ServerName;
$ListPath = $TemplatePath.Replace('.eps', '-list.eps');

  foreach ($Group in $Groups) {
   
    $ModuleName = "$($ServerName)-$([System.Guid]::NewGuid().Guid)";
   
      Invoke-EpsTemplate -Path $TemplatePath -binding @{
        ModuleName = $ModuleName;
        Source     = $Source;
        ResourceId = $ResourceId;
      } -Safe
      Invoke-EpsTemplate -Path $ListPath -Binding @{
        ModuleName = $ModuleName;
        Resource   = $Group.Group;
      }
    }
  }

Initial eps

module "<%= $ModuleName %>" {
  source             = "<%= $Source %>"

  list            = "<%= "var.$($ModuleName)_list" %>"

}

looped eps

variable "<%= "$($ModuleName)_list" %>" {
  default = [
<% $Count = 0 -%>
<% $Resource |ForEach-Object { -%>
    {
      name                  = "<%= $_.Name %>"
<% $Count++ -%>
    }<% if (!($Count -eq $Resource.Count)) { %>,<% } %>
<% } -%>
  ]
}

The resulting output when run through the script is below

module "sample1-550fc654-4e82-4913-a8ec-d08a14c13ed2" {
  source             = "github"
  list            = "var.sample1-550fc654-4e82-4913-a8ec-d08a14c13ed2_list"
}
variable "sample1-550fc654-4e82-4913-a8ec-d08a14c13ed2_list" {
  default = [
    {
      name                  = "subname1"
    },
  ]
}
module "sample2-f217ea8d-35a5-4c01-96d2-40268b8512f4" {
  source             = "github"
  list            = "var.sample2-f217ea8d-35a5-4c01-96d2-40268b8512f4_list"
}
variable "sample2-f217ea8d-35a5-4c01-96d2-40268b8512f4_list" {
  default = [
    {
      name                  = "subnamea"
    },
  ]
}

The resource that is passed in is an array of psobjects that have n+1 unique ServerNames.

ServerName resourceName
---------- ------------
server1    sample1
server1    sample2
server1    sample3
server1    sample4
server1    sample5
server2    samplea
server2    sampleb
server2    samplec
server2    sampled
@jeffpatton1971
Copy link
Author

I'm currently unable to repro this after closing out of all the powershells i had open. I'm not sure if there was something in the shell i was testing in that had some weirdness or what I'm going to keep hitting this, but thus far seems to work as expected in posh7 and og posh.

@straightdave
Copy link
Owner

any ideas? @dbroeglin

@jeffpatton1971
Copy link
Author

hey @straightdave I'm not entirely sure what the deal was, but after several more hours hammering away I'm going to have to say this must have been do to something weird inside that powershell session. I've tried straight core, straight powershell, then in the windows terminal for core and powershell and it worked as expected. Sorry for the unnecessary issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants