-
Notifications
You must be signed in to change notification settings - Fork 322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for azurerm_virtual_machine #87
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,20 @@ import ( | |
var keyNames []string | ||
var nameParser *regexp.Regexp | ||
|
||
// Azure has separate resources for the VM and the NIC that holds the IP address | ||
// Everytime we encounter an azurerm_network_interface we will store the IP address | ||
// in this map with the NIC id as the key. Then when we are looking for the VM address | ||
// we'll check if the VM's (primary) NIC exists in the map. | ||
var azureNICPrimaryIps map[string]string | ||
|
||
// Azure related keys | ||
const azureNicResourceKey string = "azurerm_network_interface" | ||
const azureNicIpKey string = "private_ip_address" | ||
const azureIdKey string = "id" | ||
const azureVMResourceKey string = "azurerm_virtual_machine" | ||
const azureVMPrimaryNicKey string = "primary_network_interface_id" | ||
const azureVMSecondaryNicKey string = "network_interface_ids.0" | ||
|
||
func init() { | ||
keyNames = []string{ | ||
"ipv4_address", // DO and SoftLayer | ||
|
@@ -34,6 +48,8 @@ func init() { | |
"primaryip", // Joyent Triton | ||
} | ||
|
||
azureNICPrimaryIps = map[string]string{} | ||
|
||
// type.name.0 | ||
nameParser = regexp.MustCompile(`^(\w+)\.([\w\-]+)(?:\.(\d+))?$`) | ||
} | ||
|
@@ -76,6 +92,11 @@ func NewResource(keyName string, state resourceState) (*Resource, error) { | |
} | ||
} | ||
|
||
// Special case for azurerm_network_interface | ||
if m[1] == azureNicResourceKey { | ||
AzureStoreNicIp(state) | ||
} | ||
|
||
return &Resource{ | ||
State: state, | ||
keyName: keyName, | ||
|
@@ -146,6 +167,15 @@ func (r Resource) Tags() map[string]string { | |
t[kk] = vv | ||
} | ||
} | ||
case "azurerm_virtual_machine": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be using |
||
for k, v := range r.Attributes() { | ||
parts := strings.SplitN(k, ".", 2) | ||
if len(parts) == 2 && parts[0] == "tags" && parts[1] != "%" { | ||
kk := strings.ToLower(parts[1]) | ||
vv := strings.ToLower(v) | ||
t[kk] = vv | ||
} | ||
} | ||
} | ||
return t | ||
} | ||
|
@@ -163,6 +193,13 @@ func (r Resource) NameWithCounter() string { | |
|
||
// Address returns the IP address of this resource. | ||
func (r Resource) Address() string { | ||
|
||
switch r.resourceType { | ||
case azureNicResourceKey, azureVMResourceKey: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// Special case for azurerm_network_interface, azurerm_virtual_machine | ||
return r.AzureAddress() | ||
} | ||
|
||
if keyName := os.Getenv("TF_KEY_NAME"); keyName != "" { | ||
if ip := r.State.Primary.Attributes[keyName]; ip != "" { | ||
return ip | ||
|
@@ -177,3 +214,30 @@ func (r Resource) Address() string { | |
|
||
return "" | ||
} | ||
|
||
func (r Resource) AzureAddress() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a docstring to this method. |
||
// We'll actually only handle azurerm_virtual_machine and ignore | ||
// azurerm_network_interface as that is not a real VM resource | ||
if r.resourceType == azureVMResourceKey { | ||
nicId := r.State.Primary.Attributes[azureVMPrimaryNicKey] | ||
if nicId == "" { | ||
nicId = r.State.Primary.Attributes[azureVMSecondaryNicKey] | ||
} | ||
if nicId != "" { | ||
ip := azureNICPrimaryIps[nicId] | ||
return ip | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func AzureStoreNicIp(state resourceState) { | ||
// Store the first ipAddress (primary) to the map with nic id | ||
ip := state.Primary.Attributes[azureNicIpKey] | ||
nicId := state.Primary.Attributes[azureIdKey] | ||
|
||
if ip != "" && nicId != "" { | ||
azureNICPrimaryIps[nicId] = ip | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like this global mutable map of things. Could we eliminate by either doing the lookup lazily (i.e. re-scanning the entire struct when looking up IPs for Azure VMs), or make it a member of some other object (probably
state
)?