|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "compute-api/compute" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +func makeFirewallRuleResourceName(uniquenessKey int) string { |
| 10 | + return fmt.Sprintf("firewall_rule%02d", uniquenessKey) |
| 11 | +} |
| 12 | + |
| 13 | +const configurationTemplateFirewallRule = ` |
| 14 | +resource "ddcloud_firewall_rule" "%s" { |
| 15 | + name = "%s" |
| 16 | + placement = "first" |
| 17 | + action = "%s" |
| 18 | + enabled = %t |
| 19 | +
|
| 20 | + ip_version = "%s" |
| 21 | + protocol = "%s" |
| 22 | +
|
| 23 | + %s= "%s" |
| 24 | + source_port = "%s" |
| 25 | +
|
| 26 | + %s= "%s" |
| 27 | + destination_port = "%s" |
| 28 | +
|
| 29 | + networkdomain = "%s" |
| 30 | +} |
| 31 | +` |
| 32 | + |
| 33 | +// ExportFirewallRule exports a ddcloud_firewallRule resource to Terraform configuration. |
| 34 | +func (exporter *Exporter) ExportFirewallRule(firewallRule compute.FirewallRule, networkDomainID string, uniquenessKey int) error { |
| 35 | + if firewallRule.RuleType == "DEFAULT_RULE" { |
| 36 | + return nil // Ignore built-in rules. |
| 37 | + } |
| 38 | + |
| 39 | + var ( |
| 40 | + sourceLabel string |
| 41 | + source string |
| 42 | + sourcePort string |
| 43 | + destinationLabel string |
| 44 | + destination string |
| 45 | + destinationPort string |
| 46 | + ) |
| 47 | + if firewallRule.Source.Port != nil { |
| 48 | + sourcePort = fmt.Sprintf("%d", firewallRule.Source.Port.Begin) |
| 49 | + } else { |
| 50 | + sourcePort = "any" |
| 51 | + } |
| 52 | + |
| 53 | + sourceAddress := firewallRule.Source.IPAddress |
| 54 | + if sourceAddress != nil { |
| 55 | + if sourceAddress.PrefixSize != nil { |
| 56 | + sourceLabel = "source_network " |
| 57 | + source = fmt.Sprintf("%s/%d", sourceAddress.Address, *sourceAddress.PrefixSize) |
| 58 | + } else { |
| 59 | + sourceLabel = "source_address " |
| 60 | + source = strings.ToLower(firewallRule.Source.IPAddress.Address) |
| 61 | + } |
| 62 | + } |
| 63 | + if firewallRule.Destination.Port != nil { |
| 64 | + destinationPort = fmt.Sprintf("%d", firewallRule.Destination.Port.Begin) |
| 65 | + } else { |
| 66 | + destinationPort = "any" |
| 67 | + } |
| 68 | + destinationAddress := firewallRule.Destination.IPAddress |
| 69 | + if destinationAddress != nil { |
| 70 | + if destinationAddress.PrefixSize != nil { |
| 71 | + destinationLabel = "destination_network " |
| 72 | + destination = fmt.Sprintf("%s/%d", destinationAddress.Address, *destinationAddress.PrefixSize) |
| 73 | + } else { |
| 74 | + destinationLabel = "destination_address " |
| 75 | + destination = strings.ToLower(firewallRule.Destination.IPAddress.Address) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + configuration := strings.TrimSpace( |
| 80 | + fmt.Sprintf(configurationTemplateFirewallRule, |
| 81 | + makeFirewallRuleResourceName(uniquenessKey), |
| 82 | + firewallRule.Name, |
| 83 | + convertFirewallAction(firewallRule.Action), |
| 84 | + firewallRule.Enabled, |
| 85 | + firewallRule.IPVersion, |
| 86 | + firewallRule.Protocol, |
| 87 | + sourceLabel, |
| 88 | + source, |
| 89 | + sourcePort, |
| 90 | + destinationLabel, |
| 91 | + destination, |
| 92 | + destinationPort, |
| 93 | + networkDomainID, |
| 94 | + ), |
| 95 | + ) |
| 96 | + fmt.Println(configuration) |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func convertFirewallAction(action string) string { |
| 102 | + switch action { |
| 103 | + case "ACCEPT_DECISIVELY": |
| 104 | + return "accept" |
| 105 | + case "DROP": |
| 106 | + return "DROP" |
| 107 | + default: |
| 108 | + return "UNKNOWN" |
| 109 | + } |
| 110 | +} |
0 commit comments