Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Commit

Permalink
feat: Introduce markers_split setting
Browse files Browse the repository at this point in the history
  • Loading branch information
saitho committed Jun 11, 2020
1 parent 5a7423e commit 003f4a5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
8 changes: 4 additions & 4 deletions docs/resources/server_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ resource "nginx_server_block" "my-server" {
enable = true
markers = {
docker_port = docker_container.web.ports.external
docker_ports = [
docker_container.web.ports.external,
docker_container.web2.ports.external
]
docker_ports = "${docker_container.web.ports.external},${docker_container.web2.ports.external}"
}
markers_split = {
docker_ports = ","
}
content = <<EOF
# content of file here
Expand Down
27 changes: 15 additions & 12 deletions src/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"regexp"
"strings"
)

func getNginxDirectories(filename string, m Config) (string, string) {
Expand All @@ -17,12 +18,12 @@ func getNginxDirectories(filename string, m Config) (string, string) {
return pathAvailable, pathEnabled
}

func CreateOrUpdateServerBlock(filename string, content string, m Config, markers map[string]interface{}) (string, error) {
func CreateOrUpdateServerBlock(filename string, content string, m Config, markers map[string]interface{}, markers_split map[string]interface{}) (string, error) {
fullPathAvailable, _ := getNginxDirectories(filename, m)

// Replace markers in content
var re *regexp.Regexp
for key, value := range ProcessMarkers(markers) {
for key, value := range ProcessMarkers(markers, markers_split) {
quotedKey := regexp.QuoteMeta(key)
re, _ = regexp.Compile("{#\\s*" + quotedKey + "\\s*#}") // {#marker#}
content = re.ReplaceAllString(content, value)
Expand All @@ -39,20 +40,22 @@ func CreateOrUpdateServerBlock(filename string, content string, m Config, marker
}

/// ProcessMarkers resolves array values into single string replaces
func ProcessMarkers(markers map[string]interface{}) map[string]string {
func ProcessMarkers(markers map[string]interface{}, markers_split map[string]interface{}) map[string]string {
markers_split_keys := make([]string, 0, len(markers_split))
for k := range markers_split {
markers_split_keys = append(markers_split_keys, k)
}

output := make(map[string]string)
for key, value := range markers {
switch value.(type) {
case []string:
// Resolve array references if value is array
for i, slice := range value.([]string) {
stringValue := value.(string)
if markers_split[key].(string) != "" {
// Split value by character
for i, slice := range strings.Split(stringValue, markers_split[key].(string)) {
output[fmt.Sprintf("%s[%d]", key, i)] = slice
}
break
default:
// Pass string as is
output[key] = value.(string)
break
} else {
output[key] = stringValue
}
}
return output
Expand Down
10 changes: 8 additions & 2 deletions src/resource_server_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func resourceServerBlock() *schema.Resource {
ForceNew: true,
Description: "Markers in content that should be replaced",
},
"markers_split": &schema.Schema{
Type: schema.TypeMap,
Default: "",
Description: "Define marker name as key and the character where the string is split as value. Chunks can be accessed as array",
Optional: true,
},
"enable": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Expand All @@ -49,7 +55,7 @@ func resourceServerBlockCreate(d *schema.ResourceData, m interface{}) error {

// Create file
content := d.Get("content").(string)
fullPathAvailable, err := nginx.CreateOrUpdateServerBlock(d.Get("filename").(string), content, config, d.Get("markers").(map[string]interface{}))
fullPathAvailable, err := nginx.CreateOrUpdateServerBlock(d.Get("filename").(string), content, config, d.Get("markers").(map[string]interface{}), d.Get("markers_split").(map[string]interface{}))
if err != nil {
return err
}
Expand Down Expand Up @@ -82,7 +88,7 @@ func resourceServerBlockRead(d *schema.ResourceData, m interface{}) error {
func resourceServerBlockUpdate(d *schema.ResourceData, m interface{}) error {
// Content changed: replace old file content
if d.HasChange("content") || d.HasChange("variables") {
_, err := nginx.CreateOrUpdateServerBlock(d.Id(), d.Get("content").(string), m.(nginx.Config), d.Get("markers").(map[string]interface{}))
_, err := nginx.CreateOrUpdateServerBlock(d.Id(), d.Get("content").(string), m.(nginx.Config), d.Get("markers").(map[string]interface{}), d.Get("markers_split").(map[string]interface{}))
if err != nil {
return err
}
Expand Down

0 comments on commit 003f4a5

Please sign in to comment.