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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,3 @@ Apigee customers should use [formal support channels](https://cloud.google.com/a

## Disclaimer
This is not an officially supported Google product.

3 changes: 2 additions & 1 deletion samples/EdgeConfig/resources/edge/org/developers.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"email": "[email protected]",
"firstName": "Hugh",
"lastName": "Jack",
"userName": "hughexample"
"userName": "hughexample",
"status": "active"
}
]
2 changes: 1 addition & 1 deletion samples/EdgeConfig/shared-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<plugin>
<groupId>com.apigee.edge.config</groupId>
<artifactId>apigee-config-maven-plugin</artifactId>
<version>2.9.3</version>
<version>2.9.4-SNAPSHOT</version>
<executions>
<execution>
<id>create-config-targetserver</id>
Expand Down
104 changes: 94 additions & 10 deletions src/main/java/com/apigee/edge/config/mavenplugin/DeveloperMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.List;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.maven.plugin.MojoExecutionException;
Expand All @@ -35,6 +36,8 @@
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.util.Key;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import com.google.gson.JsonParseException;

/** ¡¡
Expand Down Expand Up @@ -250,15 +253,47 @@ public static String updateDeveloper(ServerProfile profile,
developerId,
developer);
try {
String initialUpdateResponsePayload = response.parseAsString();
//if (response.isSuccessStatusCode())
//logger.info("Update Success.");
//logger.debug("output " + response.getContentType());

JSONParser parser = new JSONParser();
JSONObject devPayloadFromServer = (JSONObject)parser.parse(initialUpdateResponsePayload);
JSONObject devJsonFromConfig = (JSONObject)parser.parse(developer);

String finalPayloadToLog = initialUpdateResponsePayload; // Default to the initial response

Object statusFromServerObj = devPayloadFromServer.get("status");
Object statusFromConfigObj = devJsonFromConfig.get("status");

String statusFromServer = (statusFromServerObj == null) ? null : statusFromServerObj.toString();
String statusFromConfig = (statusFromConfigObj == null) ? null : statusFromConfigObj.toString();

// Check if status needs to be updated
if (statusFromConfig != null && !statusFromConfig.trim().isEmpty()) {
if (!statusFromConfig.equals(statusFromServer)) {
logger.info("Developer " + developerId + ": status differs. Server: \"" + statusFromServer + "\", Config: \"" + statusFromConfig + "\". Updating status.");
// setDeveloperStatus returns the response payload of the status update call
/* String statusUpdateResponse = */ setDeveloperStatus(profile, (String) devPayloadFromServer.get("developerId"), (String) devJsonFromConfig.get("status"));
// Update our local representation of the developer payload to reflect the change.
devPayloadFromServer.put("status", statusFromConfig);
finalPayloadToLog = new GsonBuilder().setPrettyPrinting().create().toJson(devPayloadFromServer);
} else {
logger.info("Developer " + developerId + ": status in config matches server status (\"" + statusFromServer + "\"). No status change needed.");
}
} else {
logger.warn("Developer " + developerId + ": 'status' field is missing, null, or empty in the configuration. Skipping status update. Current server status: \"" + statusFromServer + "\"");
}
logger.info("Response " + response.getContentType() + "\n" +
response.parseAsString());
if (response.isSuccessStatusCode())
logger.info("Update Success.");

finalPayloadToLog);
logger.info("Update Success.");
} catch (HttpResponseException e) {
logger.error("Developer update error " + e.getMessage());
throw new IOException(e.getMessage());
} catch (ParseException pe){
logger.error("Get Developer parse error " + pe.getMessage());
throw new IOException(pe.getMessage());
}

return "";
Expand Down Expand Up @@ -286,10 +321,43 @@ public static String deleteDeveloper(ServerProfile profile,
return "";
}

public static String setDeveloperStatus(ServerProfile profile,
String developerId,
String action)
throws IOException {
RestUtil restUtil = new RestUtil(profile);



HttpResponse response = restUtil.updateDeveloperStatus(profile,
"developers",
developerId,
action);
try {
String statusUpdateResponsePayload = response.parseAsString(); // Parse only once
logger.info("Setting developer status to: " + action);
logger.info("Response " + response.getContentType() + "\n" +
statusUpdateResponsePayload); // Use parsed payload
if (response.isSuccessStatusCode()) {
logger.info("Developer status successfully updated to: " + action);
} else {
// Use the already parsed payload for the error message
logger.error("Developer status update failed with status code " + response.getStatusCode() + ": " + statusUpdateResponsePayload);
throw new IOException("Failed to update developer status. Status: " + response.getStatusCode() + ", Message: " + statusUpdateResponsePayload);
}
} catch (HttpResponseException e) {
logger.error("Developer status update error " + e.getMessage(), e);
throw new IOException("Developer status update failed: " + e.getMessage(), e);
}
// Return the response from the status update call
return "";
}


public static List getDeveloper(ServerProfile profile)
throws IOException {
RestUtil restUtil = new RestUtil(profile);
HttpResponse response = restUtil.getOrgConfig(profile, "developers");
HttpResponse response = restUtil.getOrgConfig(profile, "developers"); // Fetches list of developers
if(response == null) return new ArrayList();
JSONArray developers = new JSONArray();
try {
Expand All @@ -314,13 +382,33 @@ public static List getDeveloper(ServerProfile profile)

return developers;
}

// Helper method to get the latest details of a specific developer
public static String getDeveloperDetails(ServerProfile profile, String developerEmail)
throws IOException {
RestUtil restUtil = new RestUtil(profile);
logger.info("Fetching details for developer - " + developerEmail);
HttpResponse response = restUtil.getOrgConfig(profile, "developers/" + URLEncoder.encode(developerEmail, "UTF-8"));
if (response == null) {
logger.warn("No response from server when fetching details for developer " + developerEmail);
return null;
}
try {
String payload = response.parseAsString();
logger.debug("Developer details payload for " + developerEmail + ": " + payload);
return payload;
} catch (HttpResponseException e) {
logger.error("Get Developer details error for " + developerEmail + ": " + e.getMessage());
throw new IOException("Failed to get developer details for " + developerEmail + ": " + e.getMessage(), e);
}
}

public static boolean doesDeveloperExist(ServerProfile profile, String developerEmail)
throws IOException {
try {
RestUtil restUtil = new RestUtil(profile);
logger.info("Checking if developer - " +developerEmail + " exist");
HttpResponse response = restUtil.getOrgConfig(profile, "developers/"+URLEncoder.encode(developerEmail, "UTF-8"));
HttpResponse response = restUtil.getOrgConfig(profile, "developers/" + URLEncoder.encode(developerEmail, "UTF-8"));
if(response == null)
return false;
} catch (HttpResponseException e) {
Expand All @@ -330,7 +418,3 @@ public static boolean doesDeveloperExist(ServerProfile profile, String developer
return true;
}
}




23 changes: 22 additions & 1 deletion src/main/java/com/apigee/edge/config/rest/RestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ public HttpResponse createEnvConfig(ServerProfile profile,
return executeAPIPost(profile, payload, importCmd);
}

public HttpResponse updateDeveloperStatus(ServerProfile profile,
String resource,
String developerId,
String action)
throws IOException {
String cmd = profile.getHostUrl() + "/"
+ profile.getApi_version() + "/organizations/"
+ profile.getOrg() + "/developers/"
+ URLEncoder.encode(developerId, "UTF-8") + "?action=" + action;

return executeAPIPost(profile, "", cmd, "application/octet-stream");
}

public HttpResponse createEnvConfigWithParameters(ServerProfile profile, String resource, String resourceId, String subResource, Map<String, String> parameters,
String payload) throws IOException {

Expand Down Expand Up @@ -1249,7 +1262,15 @@ private HttpResponse executeAPIPost(ServerProfile profile, String payload,
String importCmd)
throws IOException {

ByteArrayContent content = new ByteArrayContent("application/json",

return executeAPIPost(profile, payload, importCmd, "application/json");
}

private HttpResponse executeAPIPost(ServerProfile profile, String payload,
String importCmd, String contentType)
throws IOException {

ByteArrayContent content = new ByteArrayContent(contentType,
payload.getBytes());

HttpRequest restRequest = REQUEST_FACTORY
Expand Down