Skip to content

Commit

Permalink
Handle Null objects on Azure SDK vms info on resource model plugin.
Browse files Browse the repository at this point in the history
Add a debug option to debug the VM's info.
#1
  • Loading branch information
ltamaster committed Jan 17, 2018
1 parent 25ce16b commit da75573
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableR
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext
import com.rundeck.plugins.azure.util.AzurePluginUtil
import groovy.json.JsonOutput
import org.apache.log4j.Logger

/**
* Created by luistoledo on 11/6/17.
*/
class AzureManager {

static Logger LOG = Logger.getLogger(AzureManager.class);

String clientId
String tenantId
String subscriptionId
Expand All @@ -36,6 +39,7 @@ class AzureManager {
String resourceGroup
Region region
boolean onlyRunningInstances
boolean debug

Azure azure

Expand Down Expand Up @@ -65,6 +69,8 @@ class AzureManager {

List<AzureNode> listVms(){

LOG.info("Chequeando debug3: ${debug}")

this.connect()

def vms = azure.virtualMachines()
Expand All @@ -86,8 +92,14 @@ class AzureManager {

List<AzureNode> listNodes = new ArrayList<>()



list.each { virtualMachine->

if(debug){
println(AzurePluginUtil.printVm(virtualMachine))
}

VirtualMachineSize size = azure.virtualMachines().sizes().listByRegion(virtualMachine.region()).find{ size-> size.name().equals(virtualMachine.size().toString())}

AzureNode azureNode = new AzureNode(virtualMachine,size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class AzureManagerBuilder {
boolean onlyRunningInstances
Region region

boolean debug


static AzureManagerBuilder builder() {
return new AzureManagerBuilder()
}
Expand Down Expand Up @@ -103,7 +106,18 @@ class AzureManagerBuilder {
return this
}

/**
* @param onlyRunningInstances Only Running instances
* @return this builder
*/
AzureManagerBuilder debug(boolean debug){
this.debug = debug
return this
}


AzureManager build(){

AzureManager azure = new AzureManager()
azure.setClientId(this.clientId)
azure.setTenantId(this.tenantId)
Expand All @@ -113,6 +127,7 @@ class AzureManagerBuilder {
azure.setPfxCertificatePassword(this.pfxCertificatePassword)
azure.setResourceGroup(this.resourceGroup)
azure.setOnlyRunningInstances(this.onlyRunningInstances)
azure.setDebug(debug)

return azure
}
Expand Down
41 changes: 19 additions & 22 deletions src/main/groovy/com/rundeck/plugins/azure/azure/AzureNode.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.rundeck.plugins.azure.azure

import com.microsoft.azure.management.compute.VirtualMachine
import com.microsoft.azure.management.compute.VirtualMachineSize
import org.apache.commons.lang.builder.ToStringBuilder;

class AzureNode {

Expand All @@ -28,23 +27,21 @@ class AzureNode {

this.size = size
//basic attributes
this.name = vm.osProfile().computerName()
this.username = vm.osProfile().adminUsername()
this.name = vm.osProfile()?.computerName()
this.username = vm.osProfile()?.adminUsername()

if(vm.getPrimaryPublicIPAddress()!=null){
this.hostname = vm.getPrimaryPublicIPAddress().ipAddress ( )
}
this.hostname = vm.getPrimaryPublicIPAddress()?.ipAddress ( )

if(this.hostname==null){
//the offline machines doesn't have a IP selected
this.hostname = "undefined"
}

this.osFamily = vm.storageProfile ( ).osDisk ( ).osType ( ).toString()
this.osName = vm.storageProfile ( ).imageReference ( ).offer ( ).toString()
this.osVersion = vm.storageProfile ( ).imageReference ( ).sku ( ).toString()
this.osFamily = vm.storageProfile()?.osDisk()?.osType()?.toString()
this.osName = vm.storageProfile()?.imageReference()?.offer()?.toString()
this.osVersion = vm.storageProfile()?.imageReference()?.sku()?.toString()

String osEdition = vm.size().toString()
String osEdition = vm.size()?.toString()

description ="Azure VM " + this.osName + " " + osEdition

Expand All @@ -66,9 +63,9 @@ class AzureNode {

azureAttributes.id = vm.id()
azureAttributes.vmId = vm.vmId()
azureAttributes.region = vm.region().name()
azureAttributes.region = vm.region()?.name()
azureAttributes.resourceGroup = vm.resourceGroupName()
azureAttributes.status = vm.powerState().toString().replace("PowerState/","")
azureAttributes.status = vm.powerState()?.toString().replace("PowerState/","")

if(vm.plan()!=null){
azureAttributes."plan:name" = vm.plan().name()
Expand All @@ -82,22 +79,22 @@ class AzureNode {
azureAttributes."size:maxDataDiskCount" = size.maxDataDiskCount()
azureAttributes."size:resourceDiskSizeInMB" = size.resourceDiskSizeInMB()

azureAttributes."image:type" = vm.storageProfile().imageReference().publisher().toString()
azureAttributes."image:offer" = vm.storageProfile().imageReference().offer().toString()
azureAttributes."image:sku" = vm.storageProfile().imageReference().sku().toString()
azureAttributes."image:version" = vm.storageProfile().imageReference().version().toString()
azureAttributes."image:type" = vm.storageProfile()?.imageReference()?.publisher()?.toString()
azureAttributes."image:offer" = vm.storageProfile()?.imageReference()?.offer()?.toString()
azureAttributes."image:sku" = vm.storageProfile()?.imageReference()?.sku()?.toString()
azureAttributes."image:version" = vm.storageProfile()?.imageReference()?.version()?.toString()

azureAttributes."osDisk:osType" = vm.storageProfile().osDisk().osType().toString()
azureAttributes."osDisk:name" = vm.storageProfile().osDisk().name().toString()
azureAttributes."osDisk:createOption" = vm.storageProfile().osDisk().createOption().toString()
azureAttributes."osDisk:diskSizeGB" = vm.storageProfile().osDisk().diskSizeGB().toString()
azureAttributes."osDisk:osType" = vm.storageProfile()?.osDisk()?.osType()?.toString()
azureAttributes."osDisk:name" = vm.storageProfile()?.osDisk()?.name()?.toString()
azureAttributes."osDisk:createOption" = vm.storageProfile()?.osDisk()?.createOption()?.toString()
azureAttributes."osDisk:diskSizeGB" = vm.storageProfile()?.osDisk()?.diskSizeGB()?.toString()

if(vm.instanceView().vmAgent()!=null) {
vm.instanceView().vmAgent().statuses().each { status->
vm.instanceView().vmAgent().statuses()?.each { status->
azureAttributes."provisioningState:code" = status.code()
azureAttributes."provisioningState:displayStatus" = status.displayStatus()
azureAttributes."provisioningState:message" = status.message()
azureAttributes."provisioningState:time" = status.time().toString()
azureAttributes."provisioningState:time" = status.time()?.toString()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ class AzureResourceModelSource implements ResourceModelSource {
boolean onlyRunningInstances=Boolean.parseBoolean(configuration.getProperty(AzureResourceModelSourceFactory.RUNNING_ONLY))
String extraMapping=configuration.getProperty(AzureResourceModelSourceFactory.EXTRA_MAPPING)

boolean debug=Boolean.parseBoolean(configuration.getProperty(AzureResourceModelSourceFactory.DEBUG))

if(key == null && pfxCertificatePath == null){
throw new IllegalArgumentException("You must set the key or the certificate path in order to authenticate");
}


if(manager==null) {
manager = AzureManagerBuilder.builder()
.clientId(clientId)
Expand All @@ -57,6 +58,7 @@ class AzureResourceModelSource implements ResourceModelSource {
.pfxCertificatePassword(pfxCertificatePassword)
.resourceGroup(resourceGroup)
.onlyRunningInstances(onlyRunningInstances)
.debug(debug)
.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class AzureResourceModelSourceFactory implements ResourceModelSourceFactory,Desc
public static final String RESOURCE_GROUP = "resourceGroup"
public static final String RUNNING_ONLY = "onlyRunningInstances"

public static final String DEBUG = "debugVm"

final static Map<String, Object> renderingOptionsAuthentication = AzurePluginUtil.getRenderOpt("Credentials",false)
final static Map<String, Object> renderingOptionsAuthenticationPassword = AzurePluginUtil.getRenderOpt("Credentials",false, true)
final static Map<String, Object> renderingOptionsConfig = AzurePluginUtil.getRenderOpt("Configuration",false)
Expand Down Expand Up @@ -77,6 +79,10 @@ class AzureResourceModelSourceFactory implements ResourceModelSourceFactory,Desc
"Include Running state instances only. If false, all instances will be returned that match your " +
"filters.",
false, "false", null,renderingOptionsConfig))

.property(PropertyUtil.bool(DEBUG, "Debug VM info",
"Get the VM data on rundeck's log",
false, "false", null,renderingOptionsConfig))
.build()

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ class AzurePluginUtil {
static def printVm(VirtualMachine resource) {
StringBuilder storageProfile = new StringBuilder().append("\n\tStorageProfile: ");

resource.powerState()

if (resource.getPrimaryPublicIPAddress() != null) {
storageProfile.append("\n" +
"\t\tIP:" + resource.getPrimaryPublicIPAddress().ipAddress())
Expand All @@ -43,15 +41,15 @@ class AzurePluginUtil {
storageProfile.append("\n" +
"\t\tVM-ID:" + resource.vmId())

if (resource.storageProfile().imageReference() != null) {
if (resource.storageProfile()?.imageReference() != null) {
storageProfile.append("\n\t\tImageReference:");
storageProfile.append("\n\t\t\tPublisher: ").append(resource.storageProfile().imageReference().publisher());
storageProfile.append("\n\t\t\tOffer: ").append(resource.storageProfile().imageReference().offer());
storageProfile.append("\n\t\t\tSKU: ").append(resource.storageProfile().imageReference().sku());
storageProfile.append("\n\t\t\tVersion: ").append(resource.storageProfile().imageReference().version());
}

if (resource.storageProfile().osDisk() != null) {
if (resource.storageProfile()?.osDisk() != null) {
storageProfile.append("\n\t\tOSDisk:");
storageProfile.append("\n\t\t\tOSType: ").append(resource.storageProfile().osDisk().osType());
storageProfile.append("\n\t\t\tName: ").append(resource.storageProfile().osDisk().name());
Expand Down Expand Up @@ -80,7 +78,7 @@ class AzurePluginUtil {
}
}

if (resource.storageProfile().dataDisks() != null) {
if (resource.storageProfile()?.dataDisks() != null) {
int i = 0;
for (DataDisk disk : resource.storageProfile().dataDisks()) {
storageProfile.append("\n\t\tDataDisk: #").append(i++);
Expand Down Expand Up @@ -133,7 +131,7 @@ class AzurePluginUtil {
}

StringBuilder extensions = new StringBuilder().append("\n\tExtensions: ");
for (Map.Entry<String, VirtualMachineExtension> extensionEntry : resource.listExtensions().entrySet()) {
for (Map.Entry<String, VirtualMachineExtension> extensionEntry : resource.listExtensions()?.entrySet()) {
VirtualMachineExtension extension = extensionEntry.getValue();
extensions.append("\n\t\tExtension: ").append(extension.id())
.append("\n\t\t\tName: ").append(extension.name())
Expand Down Expand Up @@ -168,7 +166,7 @@ class AzurePluginUtil {
.append(msi)
.append(zones)

println(vmInfo.toString())
return vmInfo.toString()

}

Expand Down

0 comments on commit da75573

Please sign in to comment.