Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 43 additions & 26 deletions src/main/java/com/oneops/inv/Inventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ private void gatherInstanceMapsByComponentName(Operation operation, CiResource e

// Ugly - have to parse the name of the component to get the instance number.
String instanceNum = extractInstanceNum(componentInstance);
instancesByInstanceNum.put(instanceNum, componentInstance);
if (instanceNum != null) {
instancesByInstanceNum.put(instanceNum, componentInstance);
}

}
} catch(OneOpsClientAPIException e) {
Expand Down Expand Up @@ -528,7 +530,9 @@ private void generateHostvars(JSONObject meta) throws InventoryException {
for( CiResource host : allHosts ) {
JSONObject hostObj = new JSONObject();
String publicIp = generateHostJson(host, hostObj);
hostvars.put( publicIp, hostObj);
if (publicIp != null) {
hostvars.put( publicIp, hostObj);
}
}
}

Expand Down Expand Up @@ -565,16 +569,18 @@ private String generateHostJson(CiResource host, JSONObject hostObj) throws Inve
* something else, but it's painful. We should look at this.
*/
String metadataStr = (String) ciAddlProps.get("metadata");
JSONObject metadata = new JSONObject( metadataStr );

hostObj.put("oo_organization", metadata.get("organization"));
hostObj.put("oo_assembly", metadata.get("assembly"));
hostObj.put("oo_environment", metadata.get("environment"));
hostObj.put("oo_platform", metadata.get("platform"));
hostObj.put("oo_owner", metadata.get("owner"));
hostObj.put("oo_mgmt_url", metadata.get("mgmt_url"));
hostObj.put("oo_component", metadata.get("component"));
hostObj.put("oo_instance", metadata.get("instance"));
// GCP instances return raw GCP instance metadata here (not OneOps metadata),
// which won't have the expected OneOps keys. Use has()/get() to avoid JSONException.
JSONObject metadata = (metadataStr != null) ? new JSONObject(metadataStr) : new JSONObject();

hostObj.put("oo_organization", metadata.has("organization") ? metadata.get("organization") : JSONObject.NULL);
hostObj.put("oo_assembly", metadata.has("assembly") ? metadata.get("assembly") : JSONObject.NULL);
hostObj.put("oo_environment", metadata.has("environment") ? metadata.get("environment") : JSONObject.NULL);
hostObj.put("oo_platform", metadata.has("platform") ? metadata.get("platform") : JSONObject.NULL);
hostObj.put("oo_owner", metadata.has("owner") ? metadata.get("owner") : JSONObject.NULL);
hostObj.put("oo_mgmt_url", metadata.has("mgmt_url") ? metadata.get("mgmt_url") : JSONObject.NULL);
hostObj.put("oo_component", metadata.has("component") ? metadata.get("component") : JSONObject.NULL);
hostObj.put("oo_instance", metadata.has("instance") ? metadata.get("instance") : JSONObject.NULL);
/**
* END I HATE EVERYTHING ABOUT THIS
*/
Expand All @@ -584,7 +590,8 @@ private String generateHostJson(CiResource host, JSONObject hostObj) throws Inve
hostObj.put("oo_component_name", computeComponent.get( host ).getCiName());

// Adds the name of the cloud for this compute
hostObj.put("oo_cloud", ((Map<String,String>) host.getAdditionalProperties().get("deployedTo")).get("ciName"));
Map deployedTo = (Map) host.getAdditionalProperties().get("deployedTo");
hostObj.put("oo_cloud", deployedTo != null ? deployedTo.get("ciName") : JSONObject.NULL);

hostObj.put("oo_host_id", ciAddlProps.get("host_id"));
hostObj.put("oo_hypervisor", ciAddlProps.get("hypervisor"));
Expand All @@ -600,12 +607,14 @@ private String generateHostJson(CiResource host, JSONObject hostObj) throws Inve
// Get the instance number for this computer - we need this to jump to sibling instances for
// hostname and os
String instanceNum = extractInstanceNum(host);
String environmentName = metadata.get("environment").toString();
String platformName = metadata.get("platform").toString();
// metadata may not contain OneOps keys for non-Azure clouds (e.g. GCP returns raw GCP metadata)
String environmentName = metadata.has("environment") ? metadata.get("environment").toString() : null;
String platformName = metadata.has("platform") ? metadata.get("platform").toString() : null;

String compositeCiNameHostname = environmentName + ":" + platformName + ":hostname";
String compositeCiNameHostname = instanceNum != null && environmentName != null && platformName != null
? environmentName + ":" + platformName + ":hostname" : null;
// If there is a hostname component, grab the hostnames for this compute instance.
if( instanceMapsByComponentName.containsKey(compositeCiNameHostname)) {
if( compositeCiNameHostname != null && instanceMapsByComponentName.containsKey(compositeCiNameHostname)) {

Map<String,CiResource> hostnamesByInstanceNum = instanceMapsByComponentName.get(compositeCiNameHostname);
// Retrieve related hostname component (if there is one)
Expand All @@ -621,9 +630,10 @@ private String generateHostJson(CiResource host, JSONObject hostObj) throws Inve

}

String compositeCiNameOs = environmentName + ":" + platformName + ":os";
String compositeCiNameOs = instanceNum != null && environmentName != null && platformName != null
? environmentName + ":" + platformName + ":os" : null;
// If there is an OS, grab the OS name and type for this compute instance.
if( instanceMapsByComponentName.containsKey(compositeCiNameOs)) {
if( compositeCiNameOs != null && instanceMapsByComponentName.containsKey(compositeCiNameOs)) {

Map<String,CiResource> osByInstanceNum = instanceMapsByComponentName.get(compositeCiNameOs);
// Retrieve related os component (if there is one)
Expand Down Expand Up @@ -653,7 +663,14 @@ private String generateHostJson(CiResource host, JSONObject hostObj) throws Inve
*/
private String extractInstanceNum(CiResource ciResource) {
String ciName = ciResource.getCiName();
return ciName.substring( ciName.indexOf('-') );
if (ciName == null) {
return null; // GCP assemblies may not populate ciName
}
int dashIdx = ciName.indexOf('-');
if (dashIdx == -1) {
return null; // ciName present but has no dash-based instance numbering
}
return ciName.substring(dashIdx);
}

/**
Expand All @@ -678,15 +695,15 @@ private String computeHostId(CiResource compute) throws InventoryException {
// hostname and os
String instanceNum = extractInstanceNum(compute);


String metadataStr = (String) ciAddlProps.get("metadata");
JSONObject metadata = new JSONObject( metadataStr );
String environment = metadata.get("environment").toString();
String platform = metadata.get("platform").toString();
JSONObject metadata = (metadataStr != null) ? new JSONObject(metadataStr) : new JSONObject();
String environment = metadata.has("environment") ? metadata.get("environment").toString() : null;
String platform = metadata.has("platform") ? metadata.get("platform").toString() : null;

String compositeCiNameHostname = environment + ":" + platform + ":hostname";
String compositeCiNameHostname = instanceNum != null && environment != null && platform != null
? environment + ":" + platform + ":hostname" : null;
// If there is a hostname component, grab the hostnames for this compute instance.
if( instanceMapsByComponentName.containsKey(compositeCiNameHostname)) {
if( compositeCiNameHostname != null && instanceMapsByComponentName.containsKey(compositeCiNameHostname)) {
Map<String,CiResource> hostnamesByInstanceNum =
instanceMapsByComponentName.get(compositeCiNameHostname);
// Retrieve related hostname component (if there is one)
Expand Down
Loading