Skip to content

Commit

Permalink
Automatically rename vSphere platforms to ensure name is unique.
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen committed Jun 24, 2010
1 parent e2d22dd commit 296d38c
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions hqu/hqapi1/app/ResourceController.groovy
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -577,6 +577,10 @@ class ResourceController extends ApiController {
} }
return config.size() == 0; return config.size() == 0;
} }

private generateVSpherePlatformName(name, fqdn) {
return name + " (" + fqdn + ")"
}


private syncResource(xmlResource, parent) { private syncResource(xmlResource, parent) {


Expand Down Expand Up @@ -605,6 +609,20 @@ class ResourceController extends ApiController {
"Resource name not given") "Resource name not given")
} }


def xmlPrototype = xmlResource['ResourcePrototype']
if (!xmlPrototype) {
return getFailureXML(ErrorCode.INVALID_PARAMETERS ,
"Resource prototype not given for " + name)
}

def prototype = resourceHelper.find(prototype: xmlPrototype.'@name')

if (!prototype) {
return getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"No ResourcePrototype found for " +
name)
}

def resource = null def resource = null
if (id) { if (id) {
resource = getResource(id) resource = getResource(id)
Expand All @@ -626,26 +644,25 @@ class ResourceController extends ApiController {
def fqdn = xmlResource['ResourceInfo'].find { it.'@key' == PROP_FQDN } def fqdn = xmlResource['ResourceInfo'].find { it.'@key' == PROP_FQDN }
if (fqdn) { if (fqdn) {
resource = resourceHelper.find('byFqdn':fqdn.'@value') resource = resourceHelper.find('byFqdn':fqdn.'@value')

// Automatically rename vSphere platforms to ensure uniqueness
if (!resource && prototype.isVSpherePlatformPrototype()) {
// check to see if the platform name is already used
def anotherPlatformWithSameName = resourceHelper.find('platform':name)

if (anotherPlatformWithSameName) {
// rename platform using this convention: name (fqdn)
def uniqueName = generateVSpherePlatformName(name, fqdn.'@value')
name = uniqueName
config.name = uniqueName
}
}
} else { } else {
resource = resourceHelper.find('platform':name) resource = resourceHelper.find('platform':name)
} }
} }
} }


def xmlPrototype = xmlResource['ResourcePrototype']
if (!xmlPrototype) {
return getFailureXML(ErrorCode.INVALID_PARAMETERS ,
"Resource prototype not given for " + name)
}

def prototype = resourceHelper.find(prototype: xmlPrototype.'@name')

if (!prototype) {
return getFailureXML(ErrorCode.OBJECT_NOT_FOUND,
"No ResourcePrototype found for " +
name)
}

if (resource) { if (resource) {
// Add special configurations from ResourceInfo // Add special configurations from ResourceInfo
if (prototype.isPlatformPrototype()) { if (prototype.isPlatformPrototype()) {
Expand All @@ -657,6 +674,17 @@ class ResourceController extends ApiController {
config.put(PROP_FQDN, fqdn.'@value') config.put(PROP_FQDN, fqdn.'@value')
} }


// Automatically rename vSphere platforms to ensure uniqueness
if (prototype.isVSpherePlatformPrototype()) {
def uniqueName = generateVSpherePlatformName(name, fqdn.'@value')
if (resource.name.equals(uniqueName)) {
// platform was previously automatically renamed,
// so keep using that name
name = uniqueName
config.name = uniqueName
}
}

// Add agent info // Add agent info
def xmlAgent = xmlResource['Agent'] def xmlAgent = xmlResource['Agent']
if (xmlAgent) { if (xmlAgent) {
Expand Down

0 comments on commit 296d38c

Please sign in to comment.