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

Commit

Permalink
feat: add markers
Browse files Browse the repository at this point in the history
  • Loading branch information
saitho committed May 22, 2020
1 parent d4887ce commit 9baa761
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/resources/server_block.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ This resource represents a [server block configuration file](https://www.nginx.c
resource "nginx_server_block" "my-server" {
filename = "test.conf"
enable = true
variables = {
docker_port = docker_container.web.ports.external
}
content = <<EOF
# content of file here
# external docker port is: {# docker_port #}
EOF
}
```
Expand All @@ -20,3 +24,4 @@ EOF
* `filename` - (Required) Name of the configuration file
* `content` - (Required) Content of the configuration file
* `enable` - (Optional) Whether to enable the resource as active configuration. If symlinks were disabled in provider, this setting is ignored. Default: true
* `markers`- (Optional) Key-Value map. Keys specified as marker (e.g. `{# key #}`) will be replaced by the assigned value.
11 changes: 10 additions & 1 deletion src/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"path"
"regexp"
)

func getNginxDirectories(filename string, m Config) (string, string) {
Expand All @@ -15,8 +16,16 @@ func getNginxDirectories(filename string, m Config) (string, string) {
return pathAvailable, pathEnabled
}

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

// Replace markers in content
var re *regexp.Regexp
for key, value := range markers {
re, _ = regexp.Compile("{#\\s*" + key + "\\s*#}")
content = re.ReplaceAllString(content, value)
}

if err := ioutil.WriteFile(fullPathAvailable, []byte(content), 0744); err != nil {
return "", err
}
Expand Down
12 changes: 9 additions & 3 deletions src/resource_server_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func resourceServerBlock() *schema.Resource {
ForceNew: true,
Description: "Content of the configuration file",
},
"markers": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: "Markers in content that should be replaced",
},
"enable": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Expand All @@ -43,7 +49,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)
fullPathAvailable, err := nginx.CreateOrUpdateServerBlock(d.Get("filename").(string), content, config, d.Get("markers").(map[string]string))
if err != nil {
return err
}
Expand Down Expand Up @@ -75,8 +81,8 @@ 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") {
_, err := nginx.CreateOrUpdateServerBlock(d.Id(), d.Get("content").(string), m.(nginx.Config))
if d.HasChange("content") || d.HasChange("variables") {
_, err := nginx.CreateOrUpdateServerBlock(d.Id(), d.Get("content").(string), m.(nginx.Config), d.Get("markers").(map[string]string))
if err != nil {
return err
}
Expand Down

0 comments on commit 9baa761

Please sign in to comment.