Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWoolfenden committed Aug 20, 2024
1 parent 3d894c4 commit 706a093
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ A clear and concise description of what the bug is.
Steps to reproduce the behaviour:

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
2. Click on '...'
3. Scroll down to '...'
4. See error

**Expected behaviour**
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@5c02493ebfd65b28fd3b082c65e5af2cd745d91f # codeql-bundle-v2.18.2
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Import resources into go - **src/resource.go**
var awsCloudwatchLogMetricFilter []byte
```

Iterate on creation of tf using a debugger
Iterate on the creation of tf using a debugger
Set a break point to see schema of new resources in parse resources in lookup.go line 34 :

``` golang
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ Plan: 12 to add, 0 to change, 0 to destroy.

### See

Shows the Terraform resource equivalent to a CloudFormation resource. Or vice-versa.
Shows the Terraform resource equivalent to a CloudFormation resource, or vice versa.

This tells you the equivalent resource required, given a CF ..... or an ARM resource;
This tells you the equivalent resource required, given a CF or an ARM resource;

```bash
$ sato see -r Microsoft.Storage/storageAccounts
Expand All @@ -155,7 +155,9 @@ ARM to Terraform conversion.

What? You've got these legacy ARM templates, and you'd dearly love to drop them, but you really don't fancy Bicep
and the rework.
I got you covered. Sato now bisects ARM into Terraform - Take one of the Azure quickstart examples from here
I got you covered. Sato now bisects ARM into Terraform.

Take one of the Azure quickstart examples from here
<https://github.com/Azure/azure-quickstart-templates/tree/master/quickstarts/microsoft.compute/vm-simple-windows>:

Clone it:
Expand Down
12 changes: 12 additions & 0 deletions examples/arm/smallest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"apiProfile": "",
"contentVersion": "",
"definitions": {},
"functions": [],
"languageVersion": "",
"outputs": {},
"parameters": {},
"resources": [],
"variables": {}
}
22 changes: 22 additions & 0 deletions examples/arm/smallest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
AWSTemplateFormatVersion: "2010-09-09"
Description: AMi Factory Set up template

Parameters:
InstanceType:
Type: String
Default: t2.small
Description: Instance Type
SubnetId:
Type: AWS::EC2::Subnet::Id
Description: Subnet ID to run the EC2 instance for AMI building
NotificationEmail:
Type: String
Description: Email id to be notified once AMI is created

Resources:
SNSTopic:
Type: AWS::SNS::Topic
Properties:
Subscription:
- Endpoint: !Ref NotificationEmail
Protocol: "email"
3 changes: 1 addition & 2 deletions src/arm/outputs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"bytes"
"fmt"
"log"
tftemplate "text/template"

"sato/src/cf"
tftemplate "text/template"
)

// ParseOutputs writes out to outputs.tf.
Expand Down
21 changes: 14 additions & 7 deletions src/arm/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"os"
"path/filepath"
"regexp"
"sato/src/cf"
"sato/src/see"
"strconv"
"strings"
tftemplate "text/template"
"unicode"

"sato/src/cf"
"sato/src/see"

"github.com/rs/zerolog/log"
"golang.org/x/exp/maps"
)
Expand Down Expand Up @@ -51,7 +50,7 @@ func (m *parseMapError) Error() string {

type m map[string]interface{}

// Parse turn CFN into Terraform.
// Parse turn ARM into Terraform.
func Parse(file string, destination string) error {
fileAbs, err := filepath.Abs(file)
if err != nil {
Expand All @@ -73,7 +72,7 @@ func Parse(file string, destination string) error {
err = json.Unmarshal(byteValue, &result)

if err != nil {
return fmt.Errorf("unmarshall failure %w", err)
return fmt.Errorf("unmarshal failure %w", err)
}

funcMap := tftemplate.FuncMap{
Expand Down Expand Up @@ -219,7 +218,12 @@ func ParseString(attribute string, result map[string]interface{}) (string, map[s
}

// Replace convert ARM functions to tf
func Replace(matches []string, newAttribute string, what *string, result map[string]interface{}) (string, map[string]interface{}) {
func Replace(
matches []string,
newAttribute string,
what *string,
result map[string]interface{}) (string, map[string]interface{}) {

var Attribute string

switch *what {
Expand Down Expand Up @@ -869,7 +873,10 @@ func Preprocess(results map[string]interface{}) map[string]interface{} {
results["resources"] = SetResourceNames(results)
locals := make(map[string]interface{})

if results["variables"] != nil {
//only satisfied if empty
_, ok := results["variables"].(map[string]interface{})

if !ok {
paraVariables := results["variables"].(map[string]interface{})

newVariables := make(map[string]interface{})
Expand Down
11 changes: 9 additions & 2 deletions src/arm/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package arm_test
import (
"path/filepath"
"reflect"
"testing"

"sato/src/arm"
"testing"
)

func TestParse(t *testing.T) {
Expand Down Expand Up @@ -36,6 +35,14 @@ func TestParse(t *testing.T) {
},
wantErr: true,
},
{
name: "Empty",
args: args{
file: filepath.FromSlash("../../examples/arm/smallest.json"),
destination: ".sato",
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down
8 changes: 4 additions & 4 deletions src/arm/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package arm
import (
"bytes"
"fmt"
"strings"
tftemplate "text/template"

"sato/src/cf"
"sato/src/see"
"strings"
tftemplate "text/template"

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

// ParseResources handles resources in ARM conversion
func ParseResources(result map[string]interface{}, funcMap tftemplate.FuncMap, destination string) (map[string]interface{}, error) {
func ParseResources(
result map[string]interface{}, funcMap tftemplate.FuncMap, destination string) (map[string]interface{}, error) {
resources, ok := result["resources"].([]interface{})

if !ok {
Expand Down
7 changes: 4 additions & 3 deletions src/arm/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package arm_test

import (
"reflect"
"sato/src/arm"
"testing"
"text/template"

"sato/src/arm"
)

func Test_parseResources(t *testing.T) {
Expand All @@ -23,7 +22,9 @@ func Test_parseResources(t *testing.T) {
want map[string]interface{}
wantErr bool
}{
//{ "Pass", args{}, ResultPass, false},
{"Fail", args{}, nil, true},
{"Empty Cast", args{nil, nil, ""}, nil, true},
//{"minimal", args{result, nil, ""}, nil, false},
}

for _, tt := range tests {
Expand Down
19 changes: 8 additions & 11 deletions src/arm/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,23 @@ import (
"encoding/json"
"fmt"
"reflect"
"sato/src/cf"
"strings"
tftemplate "text/template"

"sato/src/cf"

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

// ParseVariables convert ARM Parameters into terraform variables.
func ParseVariables(result map[string]interface{}, funcMap tftemplate.FuncMap, destination string) (map[string]interface{}, error) {
variables := make(map[string]interface{})
func ParseVariables(
result map[string]interface{},
funcMap tftemplate.FuncMap,
destination string) (map[string]interface{}, error) {

var ok bool
variables, ok := result["variables"].(map[string]interface{})

if result["variables"] != nil {
variables, ok = result["variables"].(map[string]interface{})

if !ok {
return result, &castError{"map[string]interface{}"}
}
if !ok {
return result, &castError{"map[string]interface{}"}
}

var All string
Expand Down
47 changes: 45 additions & 2 deletions src/cf/lookup_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package cf

import (
"encoding/json"
"html/template"
"reflect"
"strings"
"testing"
tftemplate "text/template"

"github.com/awslabs/goformation/v7"
"github.com/awslabs/goformation/v7/cloudformation"
)

Expand All @@ -17,20 +21,59 @@ func TestParseResources(t *testing.T) {
destination string
}

var funcMap = template.FuncMap{ //nolint:gochecknoglobals
"ToUpper": strings.ToUpper,
"Deref": func(str *string) string { return *str },
"Marshal": func(v interface{}) string {
a, _ := json.Marshal(v) //nolint:errchkjson

return string(a)
},
// Nild is a template function.
"Nild": func(str *string, myDefault string) string {
if str == nil || *str == "" {
return myDefault
}

return *str
},
"Quote": func(target string) string {
if (strings.Contains(target, "var.") || strings.Contains(target, "local.") ||
(strings.Contains(target, "_") && strings.Contains(target, "."))) && (!strings.Contains(target, "${")) {
return target
}

return "\"" + target + "\""
},
}

//cut down template for a slimmed function map
cloudFormation, _ := goformation.Open("../../examples/template.yaml")

tests := []struct {
name string
args args
wantErr bool
}{
// {},
{"Parsed", args{
resources: cloudFormation.Resources,
funcMap: funcMap,
destination: "",
}, false},
{"empty function map", args{
resources: cloudFormation.Resources,
funcMap: template.FuncMap{},
destination: "",
}, true},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

if err := parseResources(tt.args.resources, tt.args.funcMap, tt.args.destination); (err != nil) != tt.wantErr {
if err := parseResources(
tt.args.resources, tt.args.funcMap, tt.args.destination); (err != nil) != tt.wantErr {
t.Errorf("parseResources() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
1 change: 0 additions & 1 deletion tests/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func TfInit(workingDir string) error {
}

execPath, err := installer.Install(context.Background())

if err != nil {
return fmt.Errorf("failed to install terraform %w", err)
}
Expand Down

0 comments on commit 706a093

Please sign in to comment.