Skip to content

Commit

Permalink
arm revision 1
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWoolfenden committed Jun 7, 2023
1 parent c30c085 commit 5392c0d
Show file tree
Hide file tree
Showing 37 changed files with 2,093 additions and 792 deletions.
40 changes: 40 additions & 0 deletions src/arm/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package arm

import (
"bytes"
"sato/src/cf"
tftemplate "text/template"

"github.com/rs/zerolog/log"
)

// parseData writes out to data.tf
func parseData(result map[string]interface{}, funcMap tftemplate.FuncMap, destination string) error {
if result["data"] == nil {
return nil
}

data := result["data"]

var output bytes.Buffer
tmpl, err := tftemplate.New("test").Funcs(funcMap).Parse(string(dataFile))
if err != nil {
return err
}
err = tmpl.Execute(&output, m{
"data": data,
})

if err != nil {
log.Print(err)
return err
}

err = cf.Write(output.String(), destination, "data")
if err != nil {
log.Print(err)
return err
}

return nil
}
8 changes: 7 additions & 1 deletion src/arm/data.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ data "azurerm_resource_group" "sato" {
name = "sato"
}
{{- end }}

{{- if eq true .data.client_config }}
data "azurerm_client_config" "sato" {
}
{{- end }}
{{- if .data.uuid }}
{{Uuid .data.uuid }}
{{- end }}
provider "azurerm" {
features{}
}
34 changes: 34 additions & 0 deletions src/arm/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package arm

import (
"testing"
"text/template"
)

func Test_parseData(t *testing.T) {
t.Parallel()

type args struct {
result map[string]interface{}
funcMap template.FuncMap
destination string
}

tests := []struct {
name string
args args
wantErr bool
}{
// {},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if err := parseData(tt.args.result, tt.args.funcMap, tt.args.destination); (err != nil) != tt.wantErr {
t.Errorf("parseData() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
83 changes: 73 additions & 10 deletions src/arm/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package arm
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -76,11 +77,11 @@ func fixType(myItem map[string]interface{}) (map[string]interface{}, error) {
temptTypes = "{\n" + strings.TrimSuffix(myType, "\n") + "}"
}
if result != "" {
result = result + "," + name + "= [" + temp + "]"
types = types + "," + name + "= list(object(" + temptTypes + "))"
result += "," + name + "= [" + temp + "]"
types += "," + name + "= list(object(" + temptTypes + "))"
} else {
result = result + name + "= [" + temp + "]"
types = types + name + "= list(object(" + temptTypes + "))"
result += name + "= [" + temp + "]"
types += name + "= list(object(" + temptTypes + "))"
}
}
case map[string]interface{}:
Expand All @@ -90,12 +91,11 @@ func fixType(myItem map[string]interface{}) (map[string]interface{}, error) {
case string:
{
if result == "" {
result = name + " = " + escapeQuote(item)
result = name + " = " + "\"" + escapeQuote(item) + "\""
types = name + " = " + "string"
} else {
temp := result
result = temp + ",\n\t" + name + " = " + escapeQuote(item)
types = types + ",\n\t" + name + " = " + "string"
result += ",\n\t" + name + " = " + "\"" + escapeQuote(item) + "\""
types += ",\n\t" + name + " = " + "string"
}
}
case bool:
Expand Down Expand Up @@ -144,7 +144,7 @@ func fixType(myItem map[string]interface{}) (map[string]interface{}, error) {
{
myItem["default"] = escapeQuote(myItem["default"])
}
case typeListString, typeNumber:
case typeListString, typeNumber, "bool":
{
// do nothing
}
Expand Down Expand Up @@ -182,7 +182,11 @@ func arrayToString(defaultValue []interface{}) string {
func tags(tags map[string]interface{}) string {
tagged := "{\n"
for item, name := range tags {
tagged += "\t\"" + item + "\"" + " = " + "\"" + name.(string) + "\"\n"
if _, ok := name.(string); ok {
tagged += "\t\"" + item + "\"" + " = " + "\"" + name.(string) + "\"\n"
} else {
tagged += "\t\"" + item + "\"" + " = " + "\"OBJECT\"\n"
}
}

tagged += "\t}"
Expand All @@ -198,3 +202,62 @@ func notNil(unknown interface{}) bool {

return true
}

func enabled(status string) bool {
if strings.ToLower(status) == "enabled" {
return true
}
return false
}

func loseSQBrackets(newAttribute string) string {
re := regexp.MustCompile(`^\[(.*)\]`) // format('{0}/{1}',
Matched := re.FindStringSubmatch(newAttribute)
if len(Matched) > 1 {
return Matched[1]
}
return newAttribute
}

func ditch(Attribute string, name string) string {

leftBrackets := strings.SplitAfter(Attribute, "(")

if len(leftBrackets) == 0 {
return Attribute
}

var brackets []string

for _, item := range leftBrackets {
rbrackets := strings.SplitAfter(item, ")")
brackets = append(brackets, rbrackets...)
}

y := 100
var raw []string
for x, item := range brackets {
if strings.Contains(item, name) {
y = len(brackets) - 2 + x
raw = append(raw, strings.Replace(item, name+"(", "", 1))
}

if y != x && !strings.Contains(item, name) {
raw = append(raw, item)
}

if y == x {
raw = append(raw, strings.Replace(item, ")", "", 1))
}
}
return strings.Join(raw, "")
}

func uuid(count int) string {
var i int
var uuids string
for i = 0; i < (count); i++ {
uuids += "resource \"random_uuid\" \"sato" + strconv.Itoa(i) + "\" {}\n"
}
return uuids
}

0 comments on commit 5392c0d

Please sign in to comment.