Skip to content

Commit

Permalink
Merge pull request #5 from apigee/Issue4
Browse files Browse the repository at this point in the history
Fix for Issue4
  • Loading branch information
ssvaidyanathan authored Nov 7, 2018
2 parents 4608228 + c95d21b commit f6ec0fd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ mvn install -Pdev -Dapigee.smartdocs.config.options=create
none - No action (default)
create - Creates the model found in the OpenAPI Spec directory
update - Updates the model found in the OpenAPI Spec directory
delete - Deleted all models not found in the OpenAPI Spec directory
delete - Deletes all models not found in the OpenAPI Spec directory
deleteAPIModel - Deletes all models from dev portal found in the OpenAPI Spec directory
render - Renders the smart docs
sync - executes the delete option (mentioned above) and recreates the models found in the OpenAPI Spec directory. This also renders the smart docs as well
Expand Down
2 changes: 1 addition & 1 deletion samples/DevPortal/shared-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<plugin>
<groupId>com.apigee.smartdocs.config</groupId>
<artifactId>apigee-smartdocs-maven-plugin</artifactId>
<version>1.0.1</version>
<version>1.0.4</version>
<!--
portal.model.vocabulary is the machine name of the model vocabulary stored within the Developer Portal.
portal.model.fields represent each field of data from the OpenAPI document that would be pushed to the Developer Portal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,26 @@

package com.apigee.smartdocs.config.mavenplugin;

import com.apigee.smartdocs.config.rest.PortalRestUtil;
import com.apigee.smartdocs.config.utils.ServerProfile;
import com.apigee.smartdocs.config.utils.PortalField;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.apigee.smartdocs.config.rest.PortalRestUtil;
import com.apigee.smartdocs.config.utils.PortalField;
import com.apigee.smartdocs.config.utils.ServerProfile;
import com.google.api.client.util.Key;

import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gson.internal.LinkedTreeMap;
import java.util.Collection;
import java.util.HashMap;

/** ¡¡
* Goal to create API Models in Apigee Developer Portal
Expand All @@ -55,7 +54,7 @@ public class APIModelMojo extends GatewayAbstractMojo {
"************************************************************************";

enum OPTIONS {
none, create, update, delete, sync, render
none, create, update, delete, deleteAPIModel, sync, render
}

OPTIONS buildOption = OPTIONS.none;
Expand Down Expand Up @@ -157,6 +156,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
doDelete();
}

if (buildOption == OPTIONS.deleteAPIModel) {
doDeleteAPIModel();
}

if (buildOption == OPTIONS.render) {
doRender();
}
Expand Down Expand Up @@ -202,18 +205,30 @@ public void doRender() throws MojoExecutionException {
PortalRestUtil.renderAPIModel(serverProfile, file);
}
logger.info("Rendered all models found in the OpenAPI Spec directory.");
if(serverProfile!=null && serverProfile.getPortalCronKey()!=null && !serverProfile.getPortalCronKey().equals("")){
PortalRestUtil.runCron(serverProfile);
logger.info("Cron job run complete");
}else{
logger.info("Cron job is not run, please run cron to see the rendered nodes");
}
doCron();
}
catch (IOException e) {
throw new RuntimeException("Render failure: " + e.getMessage());
}
}

/**
* Run Cron
*/
public void doCron() throws MojoExecutionException {
try {
if(serverProfile!=null && serverProfile.getPortalCronKey()!=null && !serverProfile.getPortalCronKey().equals("")){
PortalRestUtil.runCron(serverProfile);
logger.info("Cron job run complete");
}else{
logger.info("Cron job is not run, please run cron to see the rendered nodes");
}
}
catch (IOException e) {
throw new RuntimeException("Cron failure: " + e.getMessage());
}
}

/**
* Deletes models that exist in the API and not in the file system.
* @throws MojoExecutionException
Expand Down Expand Up @@ -243,6 +258,37 @@ public void doDelete() throws MojoExecutionException {
throw new RuntimeException("Deletion failure: " + e.getMessage());
}
}

/**
* Deletes the APIModel from dev portal
* @throws MojoExecutionException
*/
public void doDeleteAPIModel() throws MojoExecutionException {
try {
// Create a list of all specs we have on the file system.
Set<String> specNames = new HashSet<String>();
if (files != null && files.length > 0) {
for (File file : files) {
logger.info("FilePath: " + file.getPath());
PortalRestUtil.SpecObject spec = PortalRestUtil.parseSpec(serverProfile, file);
specNames.add(spec.getName());
}
}

// Iterate over all models and if exist on the file system, delete it.
PortalRestUtil.ModelObjects modelObjectArray = PortalRestUtil.getAPIModels(serverProfile);
for (PortalRestUtil.ModelObject mo : modelObjectArray.modelObjects) {
if (specNames.contains(mo.name)) {
PortalRestUtil.deleteAPIModel(serverProfile, mo.name);
}
}
logger.info("Deleted all models not found in the OpenAPI Spec directory.");
doCron();
}
catch (IOException e) {
throw new RuntimeException("Deletion failure: " + e.getMessage());
}
}

/**
* Pulls a list of OpenAPI specs frpm a directory to be sent
Expand Down

0 comments on commit f6ec0fd

Please sign in to comment.