Skip to content

Commit

Permalink
Merge pull request #9 from rcomanne/master
Browse files Browse the repository at this point in the history
NullPointer fixes & option to add tag name and tag value
  • Loading branch information
ltamaster authored Dec 31, 2019
2 parents 2f4852f + fec0b09 commit 58dc455
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
package com.rundeck.plugins.azure.azure

import com.microsoft.azure.AzureEnvironment
import com.microsoft.azure.Resource
import com.microsoft.azure.credentials.ApplicationTokenCredentials
import com.microsoft.azure.management.Azure
import com.microsoft.azure.management.compute.Disk
import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage
import com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage
import com.microsoft.azure.management.compute.VirtualMachine
import com.microsoft.azure.management.compute.VirtualMachineImage
import com.microsoft.azure.management.compute.VirtualMachineOffer
import com.microsoft.azure.management.compute.VirtualMachinePublisher
import com.microsoft.azure.management.compute.VirtualMachineSize
import com.microsoft.azure.management.compute.VirtualMachineSizeTypes
import com.microsoft.azure.management.compute.VirtualMachineSku
import com.microsoft.azure.management.compute.VirtualMachineUnmanagedDataDisk
import com.microsoft.azure.management.resources.fluentcore.arm.Region
import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource
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.
*/
Expand All @@ -37,6 +23,8 @@ class AzureManager {
String pfxCertificatePassword

String resourceGroup
String tagName
String tagValue
Region region
boolean onlyRunningInstances
boolean debug
Expand Down Expand Up @@ -88,9 +76,13 @@ class AzureManager {
list = list.findAll({p-> p.region()==region})
}

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

if(tagName!=null && tagValue != null){
list = list.findAll({p->
p.tags().find { t -> t.getKey() == tagName && tagValue == t.getValue() } != null
})
}

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

list.each { virtualMachine->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class AzureManagerBuilder {
String pfxCertificatePassword

String resourceGroup
String tagName
String tagValue
boolean onlyRunningInstances
Region region

Expand Down Expand Up @@ -98,7 +100,7 @@ class AzureManagerBuilder {
}

/**
* @param onlyRunningInstances Only Running instances
* @param region the region to filter machines
* @return this builder
*/
AzureManagerBuilder region(Region region){
Expand All @@ -107,14 +109,32 @@ class AzureManagerBuilder {
}

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

/**
* @param tagName the tag to filter machines
* @return this builder
*/
AzureManagerBuilder tagName(String tagName){
this.tagName = tagName
return this
}

/**
* @param tagValue the value of tagName to filter machines
* @return this builder
*/
AzureManagerBuilder tagValue(String tagValue){
this.tagValue = tagValue
return this
}


AzureManager build(){

Expand All @@ -128,6 +148,8 @@ class AzureManagerBuilder {
azure.setResourceGroup(this.resourceGroup)
azure.setOnlyRunningInstances(this.onlyRunningInstances)
azure.setDebug(debug)
azure.setTagName(this.tagName)
azure.setTagValue(this.tagValue)

return azure
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/groovy/com/rundeck/plugins/azure/azure/AzureNode.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,19 @@ class AzureNode {
azureAttributes.vmId = vm.vmId()
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()
azureAttributes."plan:product" = vm.plan().product()
azureAttributes."plan:publisher" = vm.plan().publisher()
}

azureAttributes."size:name" = size.name()
azureAttributes."size:numberOfCores" = size.numberOfCores()
azureAttributes."size:memoryInMB" = size.memoryInMB()
azureAttributes."size:maxDataDiskCount" = size.maxDataDiskCount()
azureAttributes."size:resourceDiskSizeInMB" = size.resourceDiskSizeInMB()
azureAttributes."size:name" = size?.name()
azureAttributes."size:numberOfCores" = size?.numberOfCores()
azureAttributes."size:memoryInMB" = size?.memoryInMB()
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()
Expand Down Expand Up @@ -193,4 +193,4 @@ class AzureNode {

return map
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class AzureResourceModelSource implements ResourceModelSource {
String pfxCertificatePassword=configuration.getProperty(AzureResourceModelSourceFactory.PFX_CERTIFICATE_PASSWORD)
String resourceGroup=configuration.getProperty(AzureResourceModelSourceFactory.RESOURCE_GROUP)
boolean onlyRunningInstances=Boolean.parseBoolean(configuration.getProperty(AzureResourceModelSourceFactory.RUNNING_ONLY))
String tagName=configuration.getProperty(AzureResourceModelSourceFactory.TAG_NAME)
String tagValue=configuration.getProperty(AzureResourceModelSourceFactory.TAG_VALUE)
String extraMapping=configuration.getProperty(AzureResourceModelSourceFactory.EXTRA_MAPPING)

boolean debug=Boolean.parseBoolean(configuration.getProperty(AzureResourceModelSourceFactory.DEBUG))
Expand All @@ -58,6 +60,8 @@ class AzureResourceModelSource implements ResourceModelSource {
.pfxCertificatePassword(pfxCertificatePassword)
.resourceGroup(resourceGroup)
.onlyRunningInstances(onlyRunningInstances)
.tagName(tagName)
.tagValue(tagValue)
.debug(debug)
.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import com.dtolabs.rundeck.core.plugins.configuration.ConfigurationException
import com.dtolabs.rundeck.core.plugins.configuration.Describable
import com.dtolabs.rundeck.core.plugins.configuration.Description
import com.dtolabs.rundeck.core.plugins.configuration.PropertyUtil
import com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants
import com.dtolabs.rundeck.core.resources.ResourceModelSource
import com.dtolabs.rundeck.core.resources.ResourceModelSourceFactory
import com.dtolabs.rundeck.plugins.ServiceNameConstants
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder
import com.rundeck.plugins.azure.util.AzurePluginUtil

/**
* Created by luistoledo on 11/3/17.
*/
Expand Down Expand Up @@ -41,6 +39,8 @@ class AzureResourceModelSourceFactory implements ResourceModelSourceFactory,Desc

//filters
public static final String RESOURCE_GROUP = "resourceGroup"
public static final String TAG_NAME = "tagName"
public static final String TAG_VALUE = "tagValue"
public static final String RUNNING_ONLY = "onlyRunningInstances"

public static final String DEBUG = "debugVm"
Expand Down Expand Up @@ -75,6 +75,10 @@ class AzureResourceModelSourceFactory implements ResourceModelSourceFactory,Desc
"tags.selector=azure_status",null,null, renderingOptionsConfig))
.property(PropertyUtil.string(RESOURCE_GROUP, "Resource Group", "Filter using resource group", false,
null,null,null, renderingOptionsConfig))
.property(PropertyUtil.string(TAG_NAME, "Tag Name", "Filter using tag name (this value will be ignored if either Tag Name or Tag Value is empty)", false,
null,null,null, renderingOptionsConfig))
.property(PropertyUtil.string(TAG_VALUE, "Tag Value", "Filter using tag value (this value will be ignored if either Tag Name or Tag Value is empty)", false,
null,null,null, renderingOptionsConfig))
.property(PropertyUtil.bool(RUNNING_ONLY, "Only Running Instances",
"Include Running state instances only. If false, all instances will be returned that match your " +
"filters.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import com.rundeck.plugins.azure.azure.AzureManager
import com.rundeck.plugins.azure.azure.AzureManagerBuilder
import com.rundeck.plugins.azure.util.AzurePluginUtil
import groovy.json.JsonOutput
import org.apache.commons.collections.map.HashedMap

/**
* Created by luistoledo on 11/21/17.
*/
Expand All @@ -35,6 +33,8 @@ class AzureVmListPlugin implements StepPlugin, Describable {

public static final String VM_RESOURCE_GROUP = "vmResourceGroup"
public static final String VM_REGION = "vmRegion"
public static final String TAG_NAME = "tagName"
public static final String TAG_VALUE = "tagValue"
public static final String RUNNING_ONLY = "onlyRunningInstances"

final static Map<String, Object> renderingOptionsAuthentication = AzurePluginUtil.getRenderOpt("Credentials",false)
Expand All @@ -61,6 +61,10 @@ class AzureVmListPlugin implements StepPlugin, Describable {
null,null,null, renderingOptionsAuthentication))
.property(PropertyUtil.string(VM_REGION, "Region", "Azure Region.", true,
null,null,null, renderingOptionsConfig))
.property(PropertyUtil.string(TAG_NAME, "Tag Name", "Tag Name.", false,
null,null,null, renderingOptionsConfig))
.property(PropertyUtil.string(TAG_VALUE, "Region", "Tag Value.", false,
null,null,null, renderingOptionsConfig))
.property(PropertyUtil.string(VM_RESOURCE_GROUP, "Resource Group", "Azure Resource Group.", true,
null,null,null, renderingOptionsConfig))
.property(PropertyUtil.bool(RUNNING_ONLY, "Only Running Instances",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package com.rundeck.plugins.azure.plugin

import com.dtolabs.rundeck.core.execution.ExecutionContext
import com.dtolabs.rundeck.core.execution.ExecutionListener
import com.dtolabs.rundeck.core.execution.ExecutionLogger
import com.dtolabs.rundeck.plugins.step.PluginStepContext
import com.microsoft.azure.management.Azure
import com.rundeck.plugins.azure.azure.AzureManager
import com.rundeck.plugins.azure.azure.AzureNode
import spock.lang.Specification

/**
* Created by luistoledo on 12/15/17.
*/
Expand Down

0 comments on commit 58dc455

Please sign in to comment.