straightdave / eps

A templating engine for PowerShell

Home Page:http://straightdave.github.io/eps/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling Invoke-EpsTemplate Inside for loop

jeffpatton1971 opened this issue · comments

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

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.

any ideas? @dbroeglin

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.