-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moves plan generator from
utils
package to testing
.
Cherry picked commit, fixed typo in directory name and removed config change from test as it won't work here. Will be added later.
- Loading branch information
Showing
2 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// Licensed under the Apache License, Version 2.0, see LICENCE file for details. | ||
|
||
package testsing | ||
|
||
import ( | ||
"bytes" | ||
"text/template" | ||
) | ||
|
||
type TemplateData map[string]string | ||
|
||
// GetStringFromTemplateWithData returns a string from a template with data. | ||
// | ||
// Here is the example usage: | ||
// templateStr := GetStringFromTemplateWithData( | ||
// | ||
// "testAccResourceApplicationWithRevisionAndConfig", | ||
// ` | ||
// | ||
// resource "juju_model" "this" { | ||
// name = "{{.ModelName}}" | ||
// } | ||
// | ||
// resource "juju_application" "{{.AppName}}" { | ||
// name = "{{.AppName}}" | ||
// model = juju_model.this.name | ||
// | ||
// charm { | ||
// name = "{{.AppName}}" | ||
// revision = {{.Revision}} | ||
// channel = "latest/edge" | ||
// } | ||
// | ||
// {{ if ne .ConfigParamName "" }} | ||
// config = { | ||
// {{.ConfigParamName}} = "{{.ConfigParamName}}-value" | ||
// } | ||
// {{ end }} | ||
// | ||
// units = 1 | ||
// } | ||
// | ||
// `, utils.TemplateData{ | ||
// "ModelName": "test-model", | ||
// "AppName": "test-app" | ||
// "Revision": fmt.Sprintf("%d", 7), | ||
// "ConfigParamName": "test-config-param-name", | ||
// }) | ||
// | ||
// The templateStr will be: | ||
// | ||
// resource "juju_model" "this" { | ||
// name = "test-model" | ||
// } | ||
// | ||
// resource "juju_application" "test-app" { | ||
// name = "test-app" | ||
// model = juju_model.this.name | ||
// | ||
// charm { | ||
// name = "test-app" | ||
// revision = 7 | ||
// channel = "latest/edge" | ||
// } | ||
// | ||
// config = { | ||
// test-config-param-name = "test-config-param-name-value" | ||
// } | ||
// | ||
// units = 1 | ||
// } | ||
func GetStringFromTemplateWithData(templateName string, templateStr string, templateData TemplateData) string { | ||
tmpl, err := template.New(templateName).Parse(templateStr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var tpl bytes.Buffer | ||
err = tmpl.Execute(&tpl, templateData) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return tpl.String() | ||
} |