Skip to content

Commit

Permalink
Merge pull request #32 from rundeck-plugins/rse-109-enh-azure-node-so…
Browse files Browse the repository at this point in the history
…urce-filter-many-resource-groups

RSE-109: Enh: Allow Azure resource model to filter by many resourceGroups
  • Loading branch information
ltamaster authored Sep 26, 2022
2 parents ed9a90f + 02a09b9 commit 0ae7115
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Settings related to the Azure connection
Mapping and filter settings

* **Mapping Params**: Custom mapping settings. Property mapping definitions. Specify multiple mappings in the form "attributeName.selector=selector" or "attributeName.default=value", separated by ";"
* **Resource Group**: Filter using resource group
* **Resource Groups**: Filter using a list of allowed resource groups separated by semicolons (`;`). Eg: `RG1; RG2; RG3`
* **Only Running Instances**: Filter for the "Running" instances. If false, all instances will be returned.

### Mapping
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.rundeck.plugins.azure.azure

import com.dtolabs.rundeck.core.resources.ResourceModelSourceException
import com.microsoft.azure.AzureEnvironment
import com.microsoft.azure.CloudException
import com.microsoft.azure.credentials.ApplicationTokenCredentials
import com.microsoft.azure.management.Azure
import com.microsoft.azure.management.compute.VirtualMachine
import com.microsoft.azure.management.compute.VirtualMachineSize
import com.microsoft.azure.management.resources.fluentcore.arm.Region
import com.microsoft.azure.management.resources.fluentcore.utils.SdkContext
Expand All @@ -19,7 +22,7 @@ class AzureManager {
String pfxCertificatePath
String pfxCertificatePassword

String resourceGroup
List<String> resourceGroups
String tagName
String tagValue
Region region
Expand Down Expand Up @@ -58,12 +61,23 @@ class AzureManager {
this.connect()

def vms = azure.virtualMachines()
def list
List<VirtualMachine> list = new LinkedList<>()

if(resourceGroup!=null){
list = vms.listByResourceGroup(resourceGroup)
}else{
if(resourceGroups.isEmpty()){
list = vms.list()
}else{
StringBuilder errorMsgs = new StringBuilder()
for(String rg : resourceGroups)
try{
list.addAll(vms.listByResourceGroup(rg))
}catch(CloudException requestError){
errorMsgs.append("\n" + requestError.getLocalizedMessage())
if(debug){
println("Couldn't load machines for resource group '${rg}': " + requestError.getLocalizedMessage())
}
}
if (list.size() < 1 && errorMsgs.length() > 0)
throw new ResourceModelSourceException("COULDN'T LOAD ANY VIRTUAL MACHINES. LISTING ERRORS: " + errorMsgs)
}

if(onlyRunningInstances){
Expand Down Expand Up @@ -111,9 +125,9 @@ class AzureManager {
def vms = azure.virtualMachines()

if(async){
vms.startAsync(resourceGroup,name).await()
vms.startAsync(resourceGroups[0],name).await()
}else{
vms.start(resourceGroup,name)
vms.start(resourceGroups[0],name)
}

}
Expand All @@ -124,10 +138,10 @@ class AzureManager {
def vms = azure.virtualMachines()

if(async) {
vms.powerOffAsync(resourceGroup, name).await()
vms.powerOffAsync(resourceGroups[0], name).await()

}else{
vms.powerOff(resourceGroup, name)
vms.powerOff(resourceGroups[0], name)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AzureManagerBuilder {
String pfxCertificatePath
String pfxCertificatePassword

String resourceGroup
List<String> resourceGroups
String tagName
String tagValue
boolean onlyRunningInstances
Expand Down Expand Up @@ -86,8 +86,8 @@ class AzureManagerBuilder {
* @param resourceGroup Azure Resource Group
* @return this builder
*/
AzureManagerBuilder resourceGroup(String resourceGroup){
this.resourceGroup = resourceGroup
AzureManagerBuilder resourceGroups(List resourceGroups){
this.resourceGroups = resourceGroups
return this
}

Expand Down Expand Up @@ -155,7 +155,7 @@ class AzureManagerBuilder {
azure.setKey(this.key)
azure.setPfxCertificatePath(this.pfxCertificatePath)
azure.setPfxCertificatePassword(this.pfxCertificatePassword)
azure.setResourceGroup(this.resourceGroup)
azure.setResourceGroups(this.resourceGroups)
azure.setOnlyRunningInstances(this.onlyRunningInstances)
azure.setDebug(debug)
azure.setTagName(this.tagName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ class AzureResourceModelSource implements ResourceModelSource {
String key=configuration.getProperty(AzureResourceModelSourceFactory.KEY)
String pfxCertificatePath=configuration.getProperty(AzureResourceModelSourceFactory.PFX_CERTIFICATE_PATH)
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 useAzureTags=Boolean.parseBoolean(configuration.getProperty(AzureResourceModelSourceFactory.USE_AZURE_TAGS))
String keyStoragePath=configuration.getProperty(AzureResourceModelSourceFactory.KEY_STORAGE_PATH)

List<String> resourceGroups = []
String [] rawResourceGroupStrs = configuration.getProperty(AzureResourceModelSourceFactory.RESOURCE_GROUPS)?.split(AzureResourceModelSourceFactory.RESOURCE_GROUP_SEPARATOR)
if(rawResourceGroupStrs)
for( int i = 0 ; i < rawResourceGroupStrs.size() ; i++)
resourceGroups << rawResourceGroupStrs[i].trim()


if(keyStoragePath){
KeyStorageTree keyStorage = services.getService(KeyStorageTree.class)
key = AzurePluginUtil.getPasswordFromKeyStorage(keyStoragePath, keyStorage)
Expand All @@ -69,7 +75,7 @@ class AzureResourceModelSource implements ResourceModelSource {
.key(key)
.pfxCertificatePath(pfxCertificatePath)
.pfxCertificatePassword(pfxCertificatePassword)
.resourceGroup(resourceGroup)
.resourceGroups(resourceGroups)
.onlyRunningInstances(onlyRunningInstances)
.tagName(tagName)
.tagValue(tagValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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.ValidationException
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
import org.rundeck.app.spi.Services
import java.util.regex.Pattern

/**
* Created by luistoledo on 11/3/17.
Expand All @@ -37,12 +39,13 @@ class AzureResourceModelSourceFactory implements ResourceModelSourceFactory,Desc

public static final String PFX_CERTIFICATE_PATH = "pfxCertificatePath"
public static final String PFX_CERTIFICATE_PASSWORD = "pfxCertificatePassword"
public static final String RESOURCE_GROUP_SEPARATOR = ";"

//mapping
public static final String EXTRA_MAPPING = "extraMapping"

//filters
public static final String RESOURCE_GROUP = "resourceGroup"
public static final String RESOURCE_GROUPS = "resourceGroup"
public static final String TAG_NAME = "tagName"
public static final String TAG_VALUE = "tagValue"
public static final String RUNNING_ONLY = "onlyRunningInstances"
Expand Down Expand Up @@ -82,8 +85,12 @@ class AzureResourceModelSourceFactory implements ResourceModelSourceFactory,Desc
null,null,null, renderingOptionsAuthentication))
.property(PropertyUtil.string(EXTRA_MAPPING, "Mapping Params", "Property mapping definitions. Specify multiple mappings in the form \"attributeName.selector=selector\" or \"attributeName.default=value\", separated by \";\"", false,
"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(RESOURCE_GROUPS, "Resource Groups", "Filter using one or more resource group separated by '" + RESOURCE_GROUP_SEPARATOR + "' (empty for all resource groups).", false,
null, { String rgStr ->
if(Pattern.compile("^[a-zA-Z0-9_-]*(\\s?;\\s?[a-zA-Z0-9_-]+)*\$").matcher(rgStr).matches())
return true
throw new ValidationException( rgStr + "Expected: rg1;rg2;...;rgN OR adding one space on each side of the ';'");
},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,
Expand Down Expand Up @@ -114,7 +121,6 @@ class AzureResourceModelSourceFactory implements ResourceModelSourceFactory,Desc
@Override
ResourceModelSource createResourceModelSource(Services services, Properties configuration) throws ConfigurationException {
final AzureResourceModelSource resource = new AzureResourceModelSource(configuration, services)

return resource
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class AzureVmStartPlugin implements StepPlugin, Describable {
.key(key)
.pfxCertificatePath(pfxCertificatePath)
.pfxCertificatePassword(pfxCertificatePassword)
.resourceGroup(resourceGroup)
.resourceGroups([resourceGroup])
.build()

Map<String, String> meta = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AzureVmStopPlugin implements StepPlugin, Describable {
.key(key)
.pfxCertificatePath(pfxCertificatePath)
.pfxCertificatePassword(pfxCertificatePassword)
.resourceGroup(resourceGroup)
.resourceGroups([resourceGroup])
.build()

Map<String, String> meta = new HashMap<>();
Expand Down

0 comments on commit 0ae7115

Please sign in to comment.