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 template generation for cloudstack #598

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions terraform/cloudstack/template_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@ package cloudstack

import (
"embed"
"fmt"
"strings"

"github.com/cloudfoundry/bosh-bootloader/storage"
)

type templates struct {
providerVars string
provider string
resourcesOutputs string
resourcesVars string
resources string
}

type TemplateGenerator struct {
EmbedData embed.FS
Path string
Expand All @@ -23,16 +32,49 @@ func NewTemplateGenerator() TemplateGenerator {
}

func (t TemplateGenerator) Generate(state storage.State) string {
var vals []string
tmpls := t.readTemplates()
template := strings.Join([]string{tmpls.providerVars, tmpls.provider, tmpls.resourcesOutputs, tmpls.resourcesVars, tmpls.resources}, "\n")
return template
}

content, err := t.EmbedData.ReadDir(t.Path)
if err != nil {
panic(err)
func (t TemplateGenerator) readTemplates() templates {
listings := map[string]string{
"provider-vars.tf": "",
"provider.tf": "",
"resources-outputs.tf": "",
"resources-vars.tf": "",
"resources.tf": "",
}

for _, embedDataEntry := range content {
vals = append(vals, embedDataEntry.Name())
var errors []error
for item := range listings {
content, err := t.EmbedData.ReadDir(t.Path)
for _, embedDataEntry := range content {
if strings.Contains(embedDataEntry.Name(), item) {
out, err := t.EmbedData.ReadFile(fmt.Sprintf("%s/%s", t.Path, embedDataEntry.Name()))
if err != nil {
errors = append(errors, err)
break
}
listings[item] = string(out)
break
}
}
if err != nil {
errors = append(errors, err)
continue
}
}

return strings.Join(vals, "\n")
if errors != nil {
panic(errors)
}

return templates{
providerVars: listings["provider-vars.tf"],
provider: listings["provider.tf"],
resourcesOutputs: listings["resources-outputs.tf"],
resourcesVars: listings["resources-vars.tf"],
resources: listings["resources.tf"],
}
}
68 changes: 0 additions & 68 deletions terraform/cloudstack/templates/resources-security-groups-vars.tf

This file was deleted.

233 changes: 0 additions & 233 deletions terraform/cloudstack/templates/resources-security-groups.tf

This file was deleted.

2 changes: 1 addition & 1 deletion terraform/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func formatVars(inputs map[string]interface{}) string {
} else if valList, ok := value.([]string); ok {
value = fmt.Sprintf(`["%s"]`, strings.Join(valList, `","`))
}
formattedVars = fmt.Sprintf("%s\n%s=%s", formattedVars, name, value)
formattedVars = fmt.Sprintf("%s\n%s=%v", formattedVars, name, value)
}
return formattedVars
}
Expand Down
Loading