
com.oneops.api.resource.Design Maven / Gradle / Ivy
package com.oneops.api.resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jayway.restassured.path.json.JsonPath;
import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
import com.oneops.api.APIClient;
import com.oneops.api.OOInstance;
import com.oneops.api.ResourceObject;
import com.oneops.api.exception.OneOpsClientAPIException;
import com.oneops.api.resource.model.AttrProps;
import com.oneops.api.resource.model.CiAttributes;
import com.oneops.api.resource.model.CiResource;
import com.oneops.api.resource.model.Release;
import com.oneops.api.util.IConstants;
import com.oneops.api.util.JsonUtil;
public class Design extends APIClient {
private String designReleaseURI;
private String designURI;
public Design(OOInstance instance, String assemblyName) throws OneOpsClientAPIException {
super(instance);
if(assemblyName == null || assemblyName.length() == 0) {
String msg = "Missing assembly name";
throw new OneOpsClientAPIException(msg);
}
designReleaseURI = IConstants.ASSEMBLY_URI + assemblyName + IConstants.DESIGN_URI + IConstants.RELEASES_URI;
designURI = IConstants.ASSEMBLY_URI + assemblyName + IConstants.DESIGN_URI;
}
/**
* Fetches specific platform details
*
* @param platformName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource getPlatform(String platformName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to fetch details";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.PLATFORM_URI + platformName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get platform with name %s due to %s", platformName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to get platform with name %s due to null response", platformName);
throw new OneOpsClientAPIException(msg);
}
/**
* Lists all the platforms
*
* @return
* @throws OneOpsClientAPIException
*/
public List listPlatforms() throws OneOpsClientAPIException {
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.PLATFORM_URI);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return JsonUtil.toObject(response.getBody().asString(), new TypeReference>(){});
} else {
String msg = String.format("Failed to get list of platforms due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to get list of platforms due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Creates platform within the given assembly
*
* @param platformName {mandatory}
* @param packname {mandatory}
* @param packversion {mandatory}
* @param packsource {mandatory}
* @param comments
* @param description
* @return
* @throws OneOpsClientAPIException
*/
public CiResource createPlatform(String platformName, String packname, String packversion,
String packsource, String comments, String description) throws OneOpsClientAPIException {
ResourceObject ro = new ResourceObject();
Map attributes = new HashMap();
Map properties= new HashMap();
if(platformName != null && platformName.length() > 0) {
properties.put("ciName", platformName);
} else {
String msg = "Missing platform name to create platform";
throw new OneOpsClientAPIException(msg);
}
if(packname != null && packname.length() > 0) {
attributes.put("pack", packname);
} else {
String msg = "Missing pack name to create platform";
throw new OneOpsClientAPIException(msg);
}
if(packversion != null && packversion.length() > 0) {
attributes.put("version", packversion);
} else {
String msg = "Missing pack version to create platform";
throw new OneOpsClientAPIException(msg);
}
if(packsource != null && packsource.length() > 0) {
attributes.put("source", packsource);
} else {
String msg = "Missing platform name to create platform";
throw new OneOpsClientAPIException(msg);
}
attributes.put("major_version", "1");
properties.put("comments", comments);
ro.setProperties(properties);
attributes.put("description", description);
ro.setAttributes(attributes);
Map ownerProps = Maps.newHashMap();
ownerProps.put("description", "");
ro.setOwnerProps(ownerProps );
RequestSpecification request = createRequest();
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
Response response = request.body(jsonObject.toString()).post(designURI + IConstants.PLATFORM_URI);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to create platform with name %s due to %s", platformName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to create platform with name %s due to null response", platformName);
throw new OneOpsClientAPIException(msg);
}
/**
* Commits design open releases
*
* @return
* @throws OneOpsClientAPIException
*/
public Release commitDesign() throws OneOpsClientAPIException {
RequestSpecification request = createRequest();
Response response = request.get(designReleaseURI + "latest");
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
String releaseState = response.getBody().jsonPath().get("releaseState");
if("open".equals(releaseState)) {
long releaseId = response.getBody().jsonPath().get("releaseId");
response = request.post(designReleaseURI + releaseId + "/commit");
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(Release.class);
} else {
String msg = String.format("Failed to commit design due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
} else {
String msg = String.format("No open release found to perform design commit");
throw new OneOpsClientAPIException(msg);
}
} else {
String msg = String.format("Failed to get latest release details due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to commit design due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Fetches latest release for the given assembly/environment
*
* @param environmentName
* @return
* @throws OneOpsClientAPIException
*/
public Release getLatestRelease() throws OneOpsClientAPIException {
RequestSpecification request = createRequest();
Response response = request.get(designReleaseURI + "latest" );
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(Release.class);
} else {
String msg = String.format("Failed to get latest releases due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to get latest releases due to null response");
throw new OneOpsClientAPIException(msg);
}
/**
* Commits specific platform with open release
*
* @return
* @throws OneOpsClientAPIException
*/
public JsonPath commitPlatform(String platformName) throws OneOpsClientAPIException {
RequestSpecification request = createRequest();
CiResource platform = getPlatform(platformName);
if(platform != null) {
Long platformId = platform.getCiId();
Response response = request.post(designURI + IConstants.PLATFORM_URI + platformId + "/commit");
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().jsonPath();
} else {
String msg = String.format("Failed to commit %s platform due to %s", platformName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to commit %s due to null response", platformName);
throw new OneOpsClientAPIException(msg);
}
/**
* Update links between platforms
*
* @param fromPlatformName
* @param toPlatformNames
* @return
* @throws OneOpsClientAPIException
*/
public CiResource updatePlatformLinks(String fromPlatformName, List toPlatformNames) throws OneOpsClientAPIException {
if(fromPlatformName == null || fromPlatformName.length() == 0) {
String msg = "Missing from platform name to link";
throw new OneOpsClientAPIException(msg);
}
if(toPlatformNames == null || toPlatformNames.size() == 0) {
String msg = "Missing to platform name to link";
throw new OneOpsClientAPIException(msg);
}
List toIds = new ArrayList();
for(String toPlatformName : toPlatformNames) {
CiResource toPlatform = getPlatform(toPlatformName);
toIds.add(toPlatform.getCiId());
}
CiResource fromPlatform = getPlatform(fromPlatformName);
ResourceObject ro = new ResourceObject();
Map properties= new HashMap();
properties.put("ciId", fromPlatform.getCiId() + "");
ro.setProperties(properties);
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
jsonObject.put("links_to", toIds);
RequestSpecification request = createRequest();
Response response = request.body(jsonObject.toString()).put(designURI + IConstants.PLATFORM_URI + fromPlatform.getCiId());
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to update platform link to %s from %s due to %s response", toPlatformNames, fromPlatformName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to update platform link to %s from %s due to null response", toPlatformNames, fromPlatformName);
throw new OneOpsClientAPIException(msg);
}
/**
* Deletes the given platform
*
* @param platformName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource deletePlatform(String platformName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to delete";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.delete(designURI + IConstants.PLATFORM_URI + platformName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to delete platform with name %s due to %s", platformName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to delete platform with name %s due to null response", platformName);
throw new OneOpsClientAPIException(msg);
}
/**
* List platform components for a given assembly/design/platform
*
* @param environmentName
* @param platformName
* @return
* @throws OneOpsClientAPIException
*/
public List listPlatformComponents(String platformName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to list enviornment platform components";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return JsonUtil.toObject(response.getBody().asString(), new TypeReference>(){});
} else {
String msg = String.format("Failed to get list of platforms components due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to get list of platforms components due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Get platform component details for a given assembly/design/platform
*
* @param platformName
* @param componentName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource getPlatformComponent(String platformName, String componentName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to get platform component details";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to get platform component details";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + componentName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get platform component details due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to get platform component details due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Add component to a given assembly/design/platform
*
* @param platformName
* @param componentName
* @param attributes
* @return
* @throws OneOpsClientAPIException
*/
public CiResource addPlatformComponent(String platformName, String componentName, String uniqueName, Map attributes) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to add component";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to add component";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response newComponentResponse = request.queryParam("template_name", componentName).get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + "new.json");
if(newComponentResponse != null) {
ResourceObject ro = new ResourceObject();
Map properties = Maps.newHashMap();
properties.put("ciName", uniqueName);
properties.put("rfcAction", "add");
JsonPath componentDetails = newComponentResponse.getBody().jsonPath();
Map attr = componentDetails.getMap("ciAttributes");
if(attr == null) {
attr = Maps.newHashMap();
}
List dependsOnComponents = Lists.newArrayList();
if(attributes != null && attributes.size() > 0) {
if(attributes.containsKey("sibling_depends_on")) {
String dependsOn = attributes.get("sibling_depends_on");
JSONArray dependsOnArray = new JSONArray(dependsOn);
List dependsOnList = Lists.newArrayList();
for (Object object : dependsOnArray) {
dependsOnList.add(String.valueOf(object));
}
dependsOnComponents = getDependsOnSiblingIds(platformName, componentName, componentDetails.getString("ciClassName"), dependsOnList);
attributes.remove("sibling_depends_on");
}
attr.putAll(attributes);
Map ownerProps = componentDetails.getMap("ciAttrProps.owner");
if(ownerProps == null) {
ownerProps = Maps.newHashMap();
}
for(Entry entry : attributes.entrySet()) {
ownerProps.put(entry.getKey(), "design");
}
ro.setOwnerProps(ownerProps);
}
ro.setAttributes(attr);
ro.setProperties(properties);
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
jsonObject.put("template_name", componentName);
if(dependsOnComponents != null && dependsOnComponents.size() > 0) {
jsonObject.put("sibling_depends_on", dependsOnComponents);
}
Response response = request.body(jsonObject.toString()).post(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI );
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get add component %s due to %s", componentName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
}
String msg = String.format("Failed to get add component %s due to null response", componentName);
throw new OneOpsClientAPIException(msg);
}
/**
* Update component attributes for a given assembly/design/platform/component
*
* @param platformName
* @param componentName
* @param attributes
* @return
* @throws OneOpsClientAPIException
*/
public CiResource updatePlatformComponent(String platformName, String componentName, Map attributes) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to update component attributes";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to update component attributes";
throw new OneOpsClientAPIException(msg);
}
if(attributes == null || attributes.size() == 0) {
String msg = "Missing attributes list to be updated";
throw new OneOpsClientAPIException(msg);
}
CiResource componentDetails = getPlatformComponent(platformName, componentName);
if(componentDetails != null) {
ResourceObject ro = new ResourceObject();
Long ciId = componentDetails.getCiId();
RequestSpecification request = createRequest();
Map attr = Maps.newHashMap();
List dependsOnList = Lists.newArrayList();
//Add ciAttributes to be updated
if(attributes != null && attributes.size() > 0) {
if(attributes.containsKey("sibling_depends_on")) {
String dependsOn = attributes.get("sibling_depends_on");
JSONArray dependsOnArray = new JSONArray(dependsOn);
for (Object object : dependsOnArray) {
dependsOnList.add(String.valueOf(object));
}
attributes.remove("sibling_depends_on");
}
attr.putAll(attributes);
}
Map ownerProps = Maps.newHashMap();
//Add existing attrProps to retain locking of attributes
AttrProps attrProps = componentDetails.getAttrProps();
if(attrProps != null && attrProps.getAdditionalProperties() != null && attrProps.getAdditionalProperties().size() > 0 && attrProps.getAdditionalProperties().get("owner") != null) {
@SuppressWarnings("unchecked")
Map ownersMap = (Map) attrProps.getAdditionalProperties().get("owner");
for(Entry entry : ownersMap.entrySet()) {
ownerProps.put(entry.getKey(), entry.getValue());
}
}
//Add updated attributes to attrProps to lock them
for(Entry entry : attributes.entrySet()) {
ownerProps.put(entry.getKey(), "design");
}
ro.setAttributes(attr);
ro.setOwnerProps(ownerProps);
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
Response response = request.body(jsonObject.toString()).put(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + ciId);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
if(dependsOnList != null && dependsOnList.size() > 0) {
updatePlatformComponentDependency(platformName, componentName, dependsOnList);
}
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get update component %s due to %s", componentName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
}
String msg = String.format("Failed to get update component %s due to null response", componentName);
throw new OneOpsClientAPIException(msg);
}
/**
* Update component dependency to sibling componenets of same type
* @param platformName
* @param componentName
* @param dependsOnComponentName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource updatePlatformComponentDependency(String platformName, String componentName, List dependsOnComponentNames) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to update component";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to update component dependency";
throw new OneOpsClientAPIException(msg);
}
if(dependsOnComponentNames == null || dependsOnComponentNames.size() == 0) {
String msg = "Missing dependsOn component to be linked";
throw new OneOpsClientAPIException(msg);
}
CiResource componentDetails = getPlatformComponent(platformName, componentName);
if(componentDetails == null) {
String msg = String.format("Failed to get component %s due to not found", componentName);
throw new OneOpsClientAPIException(msg);
}
List dependsOnCiIds = getDependsOnSiblingIds(platformName, componentName, componentDetails.getCiClassName(), dependsOnComponentNames);
Long ciId = componentDetails.getCiId();
RequestSpecification request = createRequest();
JSONObject jsonObject = new JSONObject();
jsonObject.put("cms_dj_ci", new JSONObject());
jsonObject.put("sibling_depends_on", dependsOnCiIds);
Response response = request.body(jsonObject.toString()).put(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + ciId);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get update component %s due to %s", componentName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to get update component %s due to null response", componentName);
throw new OneOpsClientAPIException(msg);
}
private List getDependsOnSiblingIds(String platformName, String componentName, String componentClassName,
List dependsOnComponentNames) throws OneOpsClientAPIException {
List dependsOnCiIds = new ArrayList();
for(String dependsOnComponentName : dependsOnComponentNames) {
CiResource dependsOnComponentDetails = getPlatformComponent(platformName, dependsOnComponentName);
if(dependsOnComponentDetails != null) {
if(dependsOnComponentDetails.getCiClassName().equals(componentClassName)) {
dependsOnCiIds.add(dependsOnComponentDetails.getCiId());
} else {
String msg = String.format("component %s is not same type as component %s class -> %s", dependsOnComponentName, componentName, componentClassName);
throw new OneOpsClientAPIException(msg);
}
} else {
String msg = String.format("Failed to get component %s due to be linked", dependsOnComponentName);
throw new OneOpsClientAPIException(msg);
}
}
return dependsOnCiIds;
}
/**
* Deletes the given platform component
*
* @param platformName
* @param componentName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource deletePlatformComponent(String platformName, String componentName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to delete component";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to delete it";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.delete(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + componentName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to delete component '%s' of platform '%s' due to %s: %s", componentName, platformName, response.getStatusLine(), response.getBody().asString());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to delete component '%s' of platform '%s' due to null response", componentName, platformName);
throw new OneOpsClientAPIException(msg);
}
/**
* List attachments for a given assembly/design/platform/component
*
* @param platformName
* @return
* @throws OneOpsClientAPIException
*/
public List listPlatformComponentAttachments(String platformName, String componentName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to list platform attachments";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + componentName + IConstants.ATTACHMENTS_URI);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return JsonUtil.toObject(response.getBody().asString(), new TypeReference>(){});
} else {
String msg = String.format("Failed to get list of design platforms attachments due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to get list of design platforms attachments due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Get platform component attachment details
*
* @param platformName
* @param componentName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource getPlatformComponentAttachment(String platformName, String componentName, String attachmentName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to get platform component attachment details";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to get platform component attachment details";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + componentName + IConstants.ATTACHMENTS_URI + attachmentName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get platform component attachment details due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to get platform component attachment details due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Update attachment
*
* @param platformName
* @param componentName
* @param attachmentName
* @param attributes
*
* Sample request for new attachment attributes
* attributes.put("content", "content");
attributes.put("source", "source");
attributes.put("path", "/tmp/my.sh");
attributes.put("exec_cmd", "exec_cmd");
attributes.put("run_on", "after-add,after-replace,after-update");
attributes.put("run_on_action", "[\"after-restart\"]");
* @return
* @throws OneOpsClientAPIException
*/
public CiResource updatePlatformComponentAttachment(String platformName, String componentName, String attachmentName, Map attributes) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to update attachment attributes";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to update attachment attributes";
throw new OneOpsClientAPIException(msg);
}
if(attachmentName == null || attachmentName.length() == 0) {
String msg = "Missing attachment name to update attachment attributes";
throw new OneOpsClientAPIException(msg);
}
if(attributes == null || attributes.size() == 0) {
//nothing to be updated
return null;
}
CiResource attachmentDetails = getPlatformComponentAttachment(platformName, componentName, attachmentName);
if(attachmentDetails != null) {
ResourceObject ro = new ResourceObject();
Long ciId = attachmentDetails.getCiId();
RequestSpecification request = createRequest();
//Add existing ciAttributes
CiAttributes ciAttributes = attachmentDetails.getCiAttributes();
Map attr = Maps.newHashMap();
if(ciAttributes != null && ciAttributes.getAdditionalProperties() != null && ciAttributes.getAdditionalProperties().size() > 0) {
for(Entry entry : ciAttributes.getAdditionalProperties().entrySet()) {
attr.put(entry.getKey(), String.valueOf(entry.getValue()));
}
}
//Add ciAttributes to be updated
if(attributes != null && attributes.size() > 0)
attr.putAll(attributes);
Map ownerProps = Maps.newHashMap();
//Add existing attrProps to retain locking of attributes
AttrProps attrProps = attachmentDetails.getAttrProps();
if(attrProps != null && attrProps.getAdditionalProperties() != null && attrProps.getAdditionalProperties().size() > 0 && attrProps.getAdditionalProperties().get("owner") != null) {
Map ownersMap = (Map) attrProps.getAdditionalProperties().get("owner");
for(Entry entry : ownersMap.entrySet()) {
ownerProps.put(entry.getKey(), entry.getValue());
}
}
//Add updated attributes to attrProps to lock them
for(Entry entry : attributes.entrySet()) {
ownerProps.put(entry.getKey(), "design");
}
ro.setOwnerProps(ownerProps);
ro.setAttributes(attr);
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
Response response = request.body(jsonObject.toString()).put(designURI + IConstants.PLATFORM_URI + platformName
+ IConstants.COMPONENT_URI + componentName + IConstants.ATTACHMENTS_URI + ciId);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get update attachment %s due to %s", componentName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
}
String msg = String.format("Failed to get update attachment %s due to null response", componentName);
throw new OneOpsClientAPIException(msg);
}
/**
* Add attachment to a given assembly/design/platform/component
*
* @param platformName
* @param componentName
* @param attributes
*
* Sample request for new attachment attributes
* attributes.put("content", "content");
attributes.put("source", "source");
attributes.put("path", "/tmp/my.sh");
attributes.put("exec_cmd", "exec_cmd");
attributes.put("run_on", "after-add,after-replace,after-update");
attributes.put("run_on_action", "[\"after-restart\"]");
* @return
* @throws OneOpsClientAPIException
*/
public CiResource addNewAttachment(String platformName, String componentName, String uniqueName, Map attributes) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to add attachment";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to add attachment";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response newAttachmentResponse = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + componentName + IConstants.ATTACHMENTS_URI + "new.json");
if(newAttachmentResponse != null) {
ResourceObject ro = new ResourceObject();
Map properties = Maps.newHashMap();
properties.put("ciName", uniqueName);
properties.put("rfcAction", "add");
JsonPath attachmentDetails = newAttachmentResponse.getBody().jsonPath();
Map attr = attachmentDetails.getMap("ciAttributes");
if(attr == null) {
attr = Maps.newHashMap();
}
if(attributes != null && attributes.size() > 0) {
attr.putAll(attributes);
Map ownerProps = attachmentDetails.getMap("ciAttrProps.owner");
if(ownerProps == null) {
ownerProps = Maps.newHashMap();
}
for(Entry entry : attributes.entrySet()) {
ownerProps.put(entry.getKey(), "design");
}
ro.setOwnerProps(ownerProps);
}
ro.setAttributes(attr);
ro.setProperties(properties);
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
Response response = request.body(jsonObject.toString()).post(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + componentName + IConstants.ATTACHMENTS_URI);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get add attachment to %s due to %s", componentName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
}
String msg = String.format("Failed to get add attachment to %s due to null response", componentName);
throw new OneOpsClientAPIException(msg);
}
/**
* Delete attachment from a given component/platform
*
* @param platformName
* @param componentName
* @param attachmentName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource deleteAttachment(String platformName, String componentName, String attachmentName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to delete attachment";
throw new OneOpsClientAPIException(msg);
}
if(componentName == null || componentName.length() == 0) {
String msg = "Missing component name to delete attachment";
throw new OneOpsClientAPIException(msg);
}
if(attachmentName == null || attachmentName.length() == 0) {
String msg = "Missing component name to delete attachment";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.delete(designURI + IConstants.PLATFORM_URI + platformName + IConstants.COMPONENT_URI + componentName + IConstants.ATTACHMENTS_URI + attachmentName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to delete attachment %s on component %s due to %s", attachmentName, componentName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to delete attachment %s on component %s due to null response", attachmentName, componentName);
throw new OneOpsClientAPIException(msg);
}
/**
* List local variables for a given assembly/design/platform
*
* @param platformName
* @return
* @throws OneOpsClientAPIException
*/
public List listPlatformVariables(String platformName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to list platform variables";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return JsonUtil.toObject(response.getBody().asString(), new TypeReference>(){});
} else {
String msg = String.format("Failed to get list of design platforms variables due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to get list of design platforms variables due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Get local variable details
*
* @param platformName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource getPlatformVariable(String platformName, String variableName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to list platform variables";
throw new OneOpsClientAPIException(msg);
}
if(variableName == null || variableName.length() == 0) {
String msg = "Missing variable name to fetch";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI + variableName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get design platform variable %s due to %s", variableName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to get design platform variable % due to null response", variableName);
throw new OneOpsClientAPIException(msg);
}
/**
* Add platform variables for a given assembly/design
*
* @param environmentName
* @param variables
* @return
* @throws OneOpsClientAPIException
*/
public CiResource addPlatformVariable(String platformName, String variableName, String variableValue, boolean isSecure) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to add variables";
throw new OneOpsClientAPIException(msg);
}
if(variableName == null ) {
String msg = "Missing variable name to be added";
throw new OneOpsClientAPIException(msg);
}
if(variableValue == null ) {
String msg = "Missing variable value to be added";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response variable = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI + variableName);
if(variable != null && variable.getBody() != null && variable.getBody().jsonPath().getString("ciId") != null) {
String msg = String.format("Global variables %s already exists", variableName);
throw new OneOpsClientAPIException(msg);
}
ResourceObject ro = new ResourceObject();
Response newVarResponse = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI + "new.json");
if(newVarResponse != null) {
JsonPath newVarJsonPath = newVarResponse.getBody().jsonPath();
if(newVarJsonPath != null) {
Map attr = newVarJsonPath.getMap("ciAttributes");
Map properties = Maps.newHashMap();
Map ownerProps = Maps.newHashMap();
if(attr == null) {
attr = Maps.newHashMap();
}
if(isSecure) {
attr.put("secure", "true");
attr.put("encrypted_value", variableValue);
ownerProps.put("secure", "design");
ownerProps.put("encrypted_value", "design");
} else {
attr.put("secure", "false");
attr.put("value", variableValue);
ownerProps.put("value", "design");
}
properties.put("ciName", variableName);
ro.setProperties(properties);
ro.setAttributes(attr);
ro.setOwnerProps(ownerProps);
}
}
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
Response response = request.body(jsonObject.toString()).post(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI );
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to add platform variable %s due to %s", variableName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to add new variables %s due to null response", variableName);
throw new OneOpsClientAPIException(msg);
}
/**
* Update platform local variables for a given assembly/design/platform
*
* @param platformName
* @param variables
* @return
* @throws OneOpsClientAPIException
*/
public Boolean updatePlatformVariable(String platformName, String variableName, String variableValue, boolean isSecure) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to update variables";
throw new OneOpsClientAPIException(msg);
}
if(variableName == null ) {
String msg = "Missing variable name to be added";
throw new OneOpsClientAPIException(msg);
}
if(variableValue == null ) {
String msg = "Missing variable value to be added";
throw new OneOpsClientAPIException(msg);
}
Boolean success = false;
RequestSpecification request = createRequest();
Response variable = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI + variableName);
if(variable == null || variable.getStatusCode() != 200 || variable.getBody() == null) {
String msg = String.format("Failed to find local variables %s for platform %s", variableName, platformName);
throw new OneOpsClientAPIException(msg);
}
JsonPath variableDetails = variable.getBody().jsonPath();
String ciId = variableDetails.getString("ciId");
Map attr = variableDetails.getMap("ciAttributes");
if(attr == null) {
attr = new HashMap ();
}
if(isSecure) {
attr.put("secure", "true");
attr.put("encrypted_value", variableValue);
} else {
attr.put("secure", "false");
attr.put("value", variableValue);
}
ResourceObject ro = new ResourceObject();
ro.setAttributes(attr);
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
Response response = request.body(jsonObject.toString()).put(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI + ciId);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
//return response.getBody().jsonPath();
success = true;
} else {
String msg = String.format("Failed to get update variables %s due to %s", variableName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
return success;
}
/**
* Update platform local variables for a given assembly/design/platform
*
* @param platformName
* @param variables
* @return
* @throws OneOpsClientAPIException
*/
public Boolean updateOrAddPlatformVariables(String platformName, String variableName, String variableValue, boolean isSecure) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to update variables";
throw new OneOpsClientAPIException(msg);
}
if(variableName == null ) {
String msg = "Missing variable name to be added";
throw new OneOpsClientAPIException(msg);
}
if(variableValue == null ) {
String msg = "Missing variable value to be added";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response variable = request.get(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI + variableName);
if(variable == null || variable.getStatusCode() != 200 || variable.getBody() == null) {
return addPlatformVariable(platformName, variableName, variableValue, isSecure) == null ? false : true;
} else {
return updatePlatformVariable(platformName, variableName, variableValue, isSecure) == null ? false : true;
}
}
/**
* Deletes the given platform variable
*
* @param platformName
* @param variableName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource deletePlatformVariable(String platformName, String variableName) throws OneOpsClientAPIException {
if(platformName == null || platformName.length() == 0) {
String msg = "Missing platform name to delete variable";
throw new OneOpsClientAPIException(msg);
}
if(variableName == null || variableName.length() == 0) {
String msg = "Missing variable name to delete it";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.delete(designURI + IConstants.PLATFORM_URI + platformName + IConstants.VARIABLES_URI + variableName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to delete platform %s variable %s due to %s", platformName, variableName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to delete platform %s variable %s due to null response", platformName, variableName);
throw new OneOpsClientAPIException(msg);
}
/**
* Deletes the given global variable
*
* @param platformName
* @param variableName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource deleteGlobalVariable(String variableName) throws OneOpsClientAPIException {
if(variableName == null || variableName.length() == 0) {
String msg = "Missing variable name to delete it";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.delete(designURI + IConstants.VARIABLES_URI + variableName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to delete global with name %s due to %s", variableName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to delete global with name %s due to null response", variableName);
throw new OneOpsClientAPIException(msg);
}
/**
* List global variables for a given assembly/design
*
* @return
* @throws OneOpsClientAPIException
*/
public List listGlobalVariables() throws OneOpsClientAPIException {
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.VARIABLES_URI);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return JsonUtil.toObject(response.getBody().asString(), new TypeReference>(){});
} else {
String msg = String.format("Failed to get list of design variables due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to get list of design variables due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Get global variable details
*
* @param platformName
* @return
* @throws OneOpsClientAPIException
*/
public CiResource getGlobalVariable(String variableName) throws OneOpsClientAPIException {
if(variableName == null || variableName.length() == 0) {
String msg = "Missing variable name to fetch";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response response = request.get(designURI + IConstants.VARIABLES_URI + variableName);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get global variable %s due to %s", variableName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to get global variable % due to null response", variableName);
throw new OneOpsClientAPIException(msg);
}
/**
* Add global variables for a given assembly/design
*
* @param environmentName
* @param variables
* @return
* @throws OneOpsClientAPIException
*/
public CiResource addGlobalVariable(String variableName, String variableValue, boolean isSecure) throws OneOpsClientAPIException {
if(variableName == null ) {
String msg = "Missing variable name to be added";
throw new OneOpsClientAPIException(msg);
}
if(variableValue == null ) {
String msg = "Missing variable value to be added";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
Response variable = request.get(designURI + IConstants.VARIABLES_URI + variableName);
if(variable != null && variable.getBody() != null && variable.getBody().jsonPath().getString("ciId") != null) {
String msg = String.format("Global variables %s already exists", variableName);
throw new OneOpsClientAPIException(msg);
}
ResourceObject ro = new ResourceObject();
Response newVarResponse = request.get(designURI + IConstants.VARIABLES_URI + "new.json");
if(newVarResponse != null) {
JsonPath newVarJsonPath = newVarResponse.getBody().jsonPath();
if(newVarJsonPath != null) {
Map attr = newVarJsonPath.getMap("ciAttributes");
Map properties = Maps.newHashMap();
Map ownerProps = Maps.newHashMap();
if(attr == null) {
attr = Maps.newHashMap();
}
if(isSecure) {
attr.put("secure", "true");
attr.put("encrypted_value", variableValue);
ownerProps.put("secure", "design");
ownerProps.put("encrypted_value", "design");
} else {
attr.put("secure", "false");
attr.put("value", variableValue);
ownerProps.put("value", "design");
}
properties.put("ciName", variableName);
ro.setProperties(properties);
ro.setAttributes(attr);
}
}
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
Response response = request.body(jsonObject.toString()).post(designURI + IConstants.VARIABLES_URI );
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().as(CiResource.class);
} else {
String msg = String.format("Failed to get new global variable %s due to %s", variableName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = String.format("Failed to add new variables %s due to null response", variableName);
throw new OneOpsClientAPIException(msg);
}
/**
* Update global variables for a given assembly/design
*
* @param environmentName
* @param variables
* @return
* @throws OneOpsClientAPIException
*/
public Boolean updateGlobalVariable(String variableName, String variableValue, boolean isSecure) throws OneOpsClientAPIException {
if(variableName == null ) {
String msg = "Missing variable name to be added";
throw new OneOpsClientAPIException(msg);
}
if(variableValue == null ) {
String msg = "Missing variable value to be added";
throw new OneOpsClientAPIException(msg);
}
Boolean success = false;
RequestSpecification request = createRequest();
Response variable = request.get(designURI + IConstants.VARIABLES_URI + variableName);
if(variable == null || variable.getBody() == null) {
String msg = String.format("Failed to find global variables %s", variableName);
throw new OneOpsClientAPIException(msg);
}
JsonPath variableDetails = variable.getBody().jsonPath();
String ciId = variableDetails.getString("ciId");
Map attr = variableDetails.getMap("ciAttributes");
if(attr == null) {
attr = new HashMap ();
}
if(isSecure) {
attr.put("secure", "true");
attr.put("encrypted_value", variableValue);
} else {
attr.put("secure", "false");
attr.put("value", variableValue);
}
ResourceObject ro = new ResourceObject();
ro.setAttributes(attr);
JSONObject jsonObject = JsonUtil.createJsonObject(ro , "cms_dj_ci");
Response response = request.body(jsonObject.toString()).put(designURI + IConstants.VARIABLES_URI + ciId);
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
success = true;
} else {
String msg = String.format("Failed to get update global variable %s due to %s", variableName, response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
return success;
}
/**
* Fetches specific platform details in Yaml format
*
* @return
* @throws OneOpsClientAPIException
*/
public JsonPath extractYaml() throws OneOpsClientAPIException {
RequestSpecification request = createRequest();
Response response = request.get(designURI + "/extract.yaml");
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().jsonPath();
} else {
String msg = String.format("Failed to extract yaml content due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to extract yaml content due to null response";
throw new OneOpsClientAPIException(msg);
}
/**
* Adds specific platform from Yaml/Json file input
*
* @param platformName
* @return
* @throws OneOpsClientAPIException
*/
public JsonPath loadFile(String filecontent) throws OneOpsClientAPIException {
if(filecontent == null || filecontent.length() == 0) {
String msg = "Missing input file content";
throw new OneOpsClientAPIException(msg);
}
RequestSpecification request = createRequest();
request.header("Content-Type", "multipart/text");
JSONObject jo = new JSONObject();
jo.put("data", filecontent);
Response response = request.parameter("data", filecontent).put(designURI + "/load" );
if(response != null) {
if(response.getStatusCode() == 200 || response.getStatusCode() == 302) {
return response.getBody().jsonPath();
} else {
String msg = String.format("Failed to load yaml content due to %s", response.getStatusLine());
throw new OneOpsClientAPIException(msg);
}
}
String msg = "Failed to load yaml content due to null response";
throw new OneOpsClientAPIException(msg);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy