Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesWoolfenden committed May 18, 2023
1 parent ddb80ef commit 216abdb
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 15 deletions.
77 changes: 71 additions & 6 deletions src/arm/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package arm

import (
"strconv"
"strings"

"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -50,21 +51,73 @@ func fixType(myItem map[string]interface{}) map[string]interface{} {
switch myType {
case "object":
{
myItem["type"] = "object()"
var types string
var result string

for name, item := range myItem["defaultValue"].(map[string]interface{}) {

switch item.(type) {
case []interface{}:
{
var temp string
var temptTypes string
var myType string
for _, y := range item.([]interface{}) {
for name, value := range y.(map[string]interface{}) {
temp = temp + "\t " + name + " = \"" + value.(string) + "\"\n"
myType = myType + "\t " + name + " = string\n"
}
temp = "{\n" + strings.TrimSuffix(temp, "\n") + "}"
temptTypes = "{\n" + strings.TrimSuffix(myType, "\n") + "}"
}
if result != "" {
result = result + "," + name + "= [" + temp + "]"
types = types + "," + name + "= list(object(" + temptTypes + "))"
} else {
result = result + name + "= [" + temp + "]"
types = types + name + "= list(object(" + temptTypes + "))"
}
}
case map[string]interface{}:
{
log.Print(item)
}
case string:
{
log.Print(item)
}
case bool:
{
if result == "" {
result = name + " = " + strconv.FormatBool(item.(bool))
types = name + " = " + "bool"
} else {
temp := result
result = temp + ",\n\t" + name + " = " + strconv.FormatBool(item.(bool))
types = types + ",\n\t" + name + " = " + "bool"
}

}
default:
{
log.Print(item)
}
}
}
myItem["default"] = "{\n\t" + result + "}"
myItem["type"] = "object({\n\t" + types + "})"
}
case "int", "float":
{
myItem["type"] = "number"
}
case "string", "map[string]interface{}":
case "map[string]interface{}":
{
myItem["type"] = "string"
}
case "securestring":
case "list(string)", "string", "number", "securestring":
{
{
myItem["type"] = "securestring"
}
//do nothing- should move this all to preprocess
}
default:
{
Expand All @@ -73,3 +126,15 @@ func fixType(myItem map[string]interface{}) map[string]interface{} {
}
return myItem
}

func arrayToString(defaultValue []interface{}) string {
var newValue = "["
for count, value := range defaultValue {
if count == len(defaultValue)-1 {
newValue = newValue + "\"" + value.(string) + "\""
} else {
newValue = newValue + "\"" + value.(string) + "\"" + ","
}
}
return newValue + "]"
}
1 change: 1 addition & 0 deletions src/arm/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func lookup(myType string) []byte {
"Microsoft.Storage/storageAccounts": azurermStorageAccount,
"Microsoft.Network/virtualNetworks/subnets": azurermSubnet,
"Microsoft.AnalysisServices/servers": azurermAnalysisServicesServer,
"Microsoft.ApiManagement/service": azurermApiManagement,
}

var myContent []byte
Expand Down
22 changes: 18 additions & 4 deletions src/arm/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ func ParseVariables(result map[string]interface{}, funcMap tftemplate.FuncMap, d

myItem["name"] = name
myItem["type"] = "string"
//myItem["type"] = reflect.TypeOf(value).String()
}

myItem = fixType(myItem)
Expand Down Expand Up @@ -655,17 +654,32 @@ func preprocess(results map[string]interface{}) map[string]interface{} {
if strings.Contains(defaultValue, "[") {
newLocals[item] = defaultValue
}
myResult["default"] = myResult["defaultValue"].(string)
}
case "int", "object":
case "int":
{

myResult["type"] = "number"
paraParameters[item] = myResult
myResult["default"] = fmt.Sprintf("%v", myResult["defaultValue"].(float64))
}
case "object", "list(string)":
{
//todo
//myResult["default"] = myResult["defaultValue"]
myResult["default"] = ""
}
case "array":
{
myResult["type"] = "list(string)"
myResult["default"] = arrayToString(myResult["defaultValue"].([]interface{}))
paraParameters[item] = myResult
}
default:
{
log.Printf("unhandled %s", myType)
}
}

results["parameters"] = paraParameters
} else {
continue
}
Expand Down
3 changes: 3 additions & 0 deletions src/arm/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ var azurermSubnet []byte

//go:embed resources/azurerm_analysis_services_server.template
var azurermAnalysisServicesServer []byte

//go:embed resources/azurerm_api_management.template
var azurermApiManagement []byte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ resource "azurerm_analysis_services_server" "{{.item}}" {
sku = {{ .resource.sku.name|Quote }}
{{- if .resource.properties.adminUsers}}
admin_users = {{Array .resource.properties.adminUsers}}
{{- end}}1
{{- end}}
{{- if .resource.properties.querypoolConnectionMode}}
querypool_connection_mode = {{ .resource.properties.querypoolConnectionMode}}
{{- end}}
Expand Down
79 changes: 79 additions & 0 deletions src/arm/resources/azurerm_api_management.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
resource "azurerm_api_management" "{{.item}}" {
{{- if .resource.location}}
location = {{ .resource.location|Quote }}
{{- else}}
location = data.azurerm_resource_group.sato.location
{{- end }}
name = {{ .resource.name|Quote }}
{{- if .resource.resourceGroupName }}
resource_group_name = {{ .resource.resourceGroupName|Quote }}
{{- else }}
resource_group_name = data.azurerm_resource_group.sato.name
{{- end}}
publisher_name = {{ .resource.properties.publisherName}}
publisher_email = {{ .resource.properties.publisherEmail}}
sku_name = {{ .resource.sku.name}}
{{- if .resource.additionalLocation}}
additional_location={{ .resource.additionalLocation}}
{{- end}}
{{- if .resource.certificate}}
certificate={{ .resource.certificate}}
{{- end}}
{{- if .resource.clientCertificateEnabled }}
client_certificate_enabled={{ .resource.clientCertificateEnabled}}
{{- end}}
{{- if .resource.delegation}}
delegation={{ .resource.delegation}}
{{- end}}
{{- if .resource.delegation}}
gateway_disabled={{ .resource.delegation}}
{{- end}}
{{- if .resource.delegation}}
min_api_version={{ .resource.delegation}}
{{- end}}
{{- if .resource.delegation}}
zones={{ .resource.delegation}}
{{- end}}
{{- if .resource.delegation}}
identity={{ .resource.delegation}}
{{- end}}
{{- if .resource.hostnameConfiguration}}
hostname_configuration={{ .resource.hostnameConfiguration}}
{{- end}}
{{- if .resource.notificationSenderEmails}}
notification_sender_email={{ .resource.notificationSenderEmails}}
{{- end}}
{{- if .resource.policy}}
policy ={{ .resource.policy}}
{{- end}}
{{- if .resource.policy}}
protocols={{ .resource.protocols}}
{{- end}}
{{- if .resource.security}}
security={{ .resource.security}}
{{- end}}
{{- if .resource.signIn}}
sign_in={{ .resource.signIn}}
{{- end}}
{{- if .resource.signUp}}
sign_up={{ .resource.signUp}}
{{- end}}
{{- if .resource.tenantAccess}}
tenant_access={{ .resource.tenantAccess}}
{{- end}}
{{- if .resource.publicIpAddressId}}
public_ip_address_id={{ .resource.publicIpAddressId}}
{{- end}}
{{- if .resource.publicNetworkAccessEnabled}}
public_network_access_enabled={{ .resource.publicNetworkAccessEnabled}}
{{- end}}
{{- if .resource.virtualNetworkType}}
virtual_network_type={{ .resource.virtualNetworkType}}
{{- end}}
{{- if .resource.virtualNetworkConfiguration}}
virtual_network_configuration={{ .resource.virtualNetworkConfiguration}}
{{- end}}
{{- if .resource.tags}}
tags={{ .resource.tags}}
{{- end}}
}
6 changes: 5 additions & 1 deletion src/arm/variable.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ variable "{{.item}}" {
type = {{.variable.type}}
{{- end }}
{{- if .variable.default}}
default = {{ Replace .variable.default "${" "$${"|Quote }}
{{- if or (eq .variable.type "string") (eq .variable.type "securestring")}}
default = {{ Replace .variable.default "${" "$${" | Quote }}
{{- else}}
default = {{ Replace .variable.default "${" "$${"}}
{{- end }}
{{- end }}
{{- if .variable.metadata.description }}
description = "{{ .variable.metadata.description }}"
Expand Down
3 changes: 2 additions & 1 deletion src/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ func Dequote(target string) string {
// Quote is a template function
func Quote(target string) string {
//is it a resource or variable
if strings.Contains(target, "var.") || strings.Contains(target, "_") {
if strings.Contains(target, "var.") ||
(strings.Contains(target, "_") && strings.Contains(target, ".")) {
return target
}
return "\"" + target + "\""
Expand Down
11 changes: 9 additions & 2 deletions src/see/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,22 @@ func Lookup(resource string) (*string, error) {
"AWS::ServiceDiscovery::Service": "aws_service_discovery_service",
"AWS::StepFunctions::StateMachine": "aws_sfn_state_machine",
"AWS::WAFv2::WebACLAssociation": "aws_wafv2_webacl_association",
"Microsoft.AnalysisServices/servers": "azurerm_analysis_services_server",
"Microsoft.ApiManagement/service": "azurerm_api_management",
"Microsoft.App/containerApps": "azurerm_container_app",
"Microsoft.App/managedEnvironments": "azurerm_app_service_environment",
"Microsoft.Authorization/roleAssignments": "azurerm_role_assignment",
"Microsoft.Compute/virtualMachines": "azurerm_virtual_machine",
"Microsoft.Compute/virtualMachines/extensions": "azurerm_virtual_machine_extension",
"Microsoft.ManagedIdentity/userAssignedIdentities": "azurerm_user_assigned_identity",
"Microsoft.Network/networkInterfaces": "azurerm_network_interface",
"Microsoft.Network/networkSecurityGroups": "azurerm_network_security_group",
"Microsoft.Network/publicIPAddresses": "azurerm_public_ip",
"Microsoft.Network/virtualNetworks": "azurerm_virtual_network",
"Microsoft.Storage/storageAccounts": "azurerm_storage_account",
"Microsoft.Network/virtualNetworks/subnets": "azurerm_subnet",
"Microsoft.AnalysisServices/servers": "azurerm_analysis_services_server",
"Microsoft.OperationalInsights/workspaces": "azurerm_log_analytics_workspace",
"Microsoft.Resources/deployments": "azurerm_template_deployment",
"Microsoft.Storage/storageAccounts": "azurerm_storage_account",
}
result := Lookup[resource]
var err error
Expand Down

0 comments on commit 216abdb

Please sign in to comment.