Skip to content

Commit

Permalink
Merge pull request #197 from apigee/Issue192
Browse files Browse the repository at this point in the history
KVM update
  • Loading branch information
ssvaidyanathan authored Feb 13, 2024
2 parents d4f1fb0 + 8aecbd9 commit 6c705d2
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<groupId>com.apigee.edge.config</groupId>
<artifactId>apigee-config-maven-plugin</artifactId>
<version>2.6.1-SNAPSHOT</version>
<version>2.7.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>apigee-config-maven-plugin</name>
<description>Plugin to manage configuration in Apigee</description>
Expand Down
2 changes: 1 addition & 1 deletion samples/APIandConfig/shared-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<plugin>
<groupId>com.apigee.edge.config</groupId>
<artifactId>apigee-config-maven-plugin</artifactId>
<version>2.6.0</version>
<version>2.7.0</version>
<executions>
<execution>
<id>create-config-targetserver</id>
Expand Down
28 changes: 28 additions & 0 deletions samples/EdgeConfig/resources/edge/env/dev/aliases.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"alias":"testSelfSignedCert",
"keystorename": "testKeyStorename",
"format": "selfsignedcert",
"keySize":"2048",
"sigAlg":"SHA256withRSA",
"subject":{
"commonName":"testcommonName"
},
"certValidityInDays":"90"
},
{
"alias":"testAliasKeyCertFileAndKey",
"keystorename": "testKeyStorename",
"format": "keycertfile",
"certFilePath":"/tmp/certs/keystore.pem",
"keyFilePath":"/tmp/certs/keystore.key",
"password":"dummy"
},
{
"alias":"testAliasPKCS12",
"keystorename": "testKeyStorename",
"format": "pkcs12",
"filePath":"/tmp/certs/myKeystore.p12",
"password":"dummy"
}
]
6 changes: 6 additions & 0 deletions samples/EdgeConfig/resources/edge/env/dev/flowhooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"flowHookPoint":"PreTargetFlowHook",
"sharedFlow":"test"
}
]
5 changes: 5 additions & 0 deletions samples/EdgeConfig/resources/edge/env/dev/keystores.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"name" : "testKeyStorename"
}
]
15 changes: 15 additions & 0 deletions samples/EdgeConfig/resources/edge/env/dev/kvms.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"entry": [
{
"name": "COMPANY",
"value": "example.com"
},
{
"name": "PREFIX",
"value": "EXMPL"
}
],
"name": "backend_account_config"
}
]
7 changes: 7 additions & 0 deletions samples/EdgeConfig/resources/edge/env/dev/references.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"name" : "sampleReference",
"refers": "testKeyStorename",
"resourceType": "KeyStore"
}
]
12 changes: 12 additions & 0 deletions samples/EdgeConfig/resources/edge/env/dev/resourcefiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"name": "test",
"type": "jsc",
"file": "./resourceFiles/jsc/test.js"
},
{
"name": "foobar",
"type": "properties",
"file": "./resourceFiles/properties/foobar.properties"
}
]
22 changes: 22 additions & 0 deletions samples/EdgeConfig/resources/edge/env/dev/targetServers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"name": "Enterprisetarget",
"host": "example.com",
"isEnabled": true,
"port": 8080
},
{
"name": "ESBTarget",
"host": "enterprise.com",
"isEnabled": true,
"port": 8080,
"sSLInfo": {
"clientAuthEnabled": "false",
"enabled": "true",
"ignoreValidationErrors": "false",
"keyAlias": "key_alias",
"keyStore": "keystore_name",
"trustStore": "truststore_name"
}
}
]
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.6.0</version>
<version>2.7.0</version>
<executions>
<execution>
<id>create-config-targetserver</id>
Expand Down
72 changes: 47 additions & 25 deletions src/main/java/com/apigee/edge/config/mavenplugin/KVMMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ enum OPTIONS {
public static class KVM {
@Key
public String name;
@Key
public List<KVMEntry> entry;
}
public static class KVMEntry {
@Key
public String name;
@Key
public String value;
}

public KVMMojo() {
Expand Down Expand Up @@ -114,6 +122,26 @@ protected String getKVMName(String payload) throws MojoFailureException {
throw new MojoFailureException(e.getMessage());
}
}

//Apigee API does not allow "/" in the key name, so need to check before create/update operation
protected void checkForInvalidKey(List<String> kvms) throws MojoFailureException {
if (buildOption == OPTIONS.delete) //This check is not needed for delete option
return;
Gson gson = new Gson();
try {
for (String kvmString : kvms) {
KVM kvm = gson.fromJson(kvmString, KVM.class);
if(kvm!=null && kvm.entry!=null && kvm.entry.size()>0) {
for (KVMEntry kvmEntry : kvm.entry) {
if(kvmEntry!=null && kvmEntry.name!=null && kvmEntry.name.contains("/"))
throw new MojoFailureException("KVM key: "+ kvmEntry.name +" is invalid. Keys cannot contain \"/\" character ");
}
}
}
} catch (JsonParseException e) {
throw new MojoFailureException(e.getMessage());
}
}

protected void doOrgUpdate(List<String> kvms, String scope)
throws MojoFailureException {
Expand All @@ -135,6 +163,11 @@ protected void doOrgUpdate(List<String> kvms, String scope)

if (existingKVM.contains(kvmName)) {
switch (buildOption) {
case update:
logger.info("Org KVM \"" + kvmName +
"\" exists. Updating.");
kvmOrg.update(new KvmValueObject(serverProfile, kvmName, kvm));
break;
case create:
logger.info("Org KVM \"" + kvmName +
"\" already exists. Skipping.");
Expand All @@ -144,14 +177,6 @@ protected void doOrgUpdate(List<String> kvms, String scope)
"\" already exists. Deleting.");
deleteOrgKVM(serverProfile, kvmName);
break;
case update:
logger.info("Org KVM \"" + kvmName +
"\" exists. Updating.");
//deleting the KVM and re-creating the KVM and entries
deleteOrgKVM(serverProfile, kvmName);
createOrgKVM(serverProfile, kvmName);
kvmOrg.update(new KvmValueObject(serverProfile, kvmName, kvm));
break;
case sync:
logger.info("Org KVM \"" + kvmName +
"\" already exists. Deleting and recreating.");
Expand Down Expand Up @@ -206,6 +231,11 @@ protected void doEnvUpdate(List<String> kvms, String scope)

if (existingKVM.contains(kvmName)) {
switch (buildOption) {
case update:
logger.info("Env KVM \"" + kvmName +
"\" exists. Updating.");
kvmEnv.update(new KvmValueObject(serverProfile, kvmName, kvm));
break;
case create:
logger.info("Env KVM \"" + kvmName +
"\" already exists. Skipping.");
Expand All @@ -215,14 +245,6 @@ protected void doEnvUpdate(List<String> kvms, String scope)
"\" already exists. Deleting.");
deleteEnvKVM(serverProfile, kvmName);
break;
case update:
logger.info("Env KVM \"" + kvmName +
"\" exists. Updating.");
//deleting the KVM and re-creating the KVM and entries
deleteEnvKVM(serverProfile, kvmName);
createEnvKVM(serverProfile, kvmName);
kvmEnv.update(new KvmValueObject(serverProfile, kvmName, kvm));
break;
case sync:
logger.info("Env KVM \"" + kvmName +
"\" already exists. Deleting and recreating.");
Expand Down Expand Up @@ -277,6 +299,11 @@ protected void doAPIUpdate(String api, List<String> kvms)

if (existingKVM.contains(kvmName)) {
switch (buildOption) {
case update:
logger.info("API KVM \"" + kvmName +
"\" exists. Updating.");
kvmApi.update(new KvmValueObject(serverProfile, api, kvmName, kvm));
break;
case create:
logger.info("API KVM \"" + kvmName +
"\" already exists. Skipping.");
Expand All @@ -285,15 +312,6 @@ protected void doAPIUpdate(String api, List<String> kvms)
logger.info("API KVM \"" + kvmName +
"\" already exists. Deleting.");
deleteAPIKVM(serverProfile, api, kvmName);
break;
case update:
logger.info("API KVM \"" + kvmName +
"\" exists. Updating.");
//deleting the KVM and re-creating the KVM and entries
deleteAPIKVM(serverProfile, api, kvmName);
createAPIKVM(serverProfile, api, kvmName);
kvmApi.update(new KvmValueObject(serverProfile, api, kvmName, kvm));

break;
case sync:
logger.info("API KVM \"" + kvmName +
Expand Down Expand Up @@ -361,6 +379,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (kvms == null || kvms.size() == 0) {
logger.info("No org scoped KVM config found.");
} else {
checkForInvalidKey(kvms);
doOrgUpdate(kvms, scope);
}

Expand All @@ -369,6 +388,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (kvms == null || kvms.size() == 0) {
logger.info("No env scoped KVM config found.");
} else {
checkForInvalidKey(kvms);
doEnvUpdate(kvms, scope);
}

Expand All @@ -385,6 +405,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
logger.info(
"No API scoped KVM config found for " + api);
} else {
checkForInvalidKey(kvms);
doAPIUpdate(api, kvms);
}
}
Expand Down Expand Up @@ -633,3 +654,4 @@ public static List getAPIKVM(ServerProfile profile, String api)




24 changes: 22 additions & 2 deletions src/main/java/com/apigee/edge/config/mavenplugin/kvm/KvmApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class KvmApi extends KvmOperations implements Kvm {
@Override
public HttpResponse getKvm(KvmValueObject kvmValueObject) throws IOException {
RestUtil restUtil = new RestUtil(kvmValueObject.getProfile());
return restUtil.getAPIConfig(kvmValueObject.getProfile(),
return restUtil.getAPIConfig(kvmValueObject.getProfile(),
kvmValueObject.getApi(),
"keyvaluemaps/"+kvmValueObject.getKvmName());
}
Expand Down Expand Up @@ -39,20 +39,40 @@ public HttpResponse updateKvmEntries(KvmValueObject kvmValueObject, String kvmEn
kvmEntryValue);
}

@Override
public HttpResponse updateKvmEntriesForNonCpsOrg(KvmValueObject kvmValueObject) throws IOException {
RestUtil restUtil = new RestUtil(kvmValueObject.getProfile());
return restUtil.updateAPIConfig(kvmValueObject.getProfile(),
kvmValueObject.getApi(),
"keyvaluemaps",
kvmValueObject.getKvmName(),
kvmValueObject.getKvm());
}

@Override
public HttpResponse createKvmEntries(KvmValueObject kvmValueObject, String kvmEntryValue) throws IOException {
RestUtil restUtil = new RestUtil(kvmValueObject.getProfile());
return restUtil.createAPIConfig(kvmValueObject.getProfile(),
return restUtil.createAPIConfig(kvmValueObject.getProfile(),
kvmValueObject.getApi(),
"keyvaluemaps",
kvmValueObject.getKvmName(),
"entries",
kvmEntryValue);
}

@Override
public HttpResponse deleteKvmEntries(KvmValueObject kvmValueObject, String kvmEntryValue) throws IOException {
RestUtil restUtil = new RestUtil(kvmValueObject.getProfile());
return restUtil.deleteAPIConfig(kvmValueObject.getProfile(),
kvmValueObject.getApi(),
"keyvaluemaps",
kvmValueObject.getKvmName()+ "/entries/"+kvmEntryValue);
}

@Override
public void update(KvmValueObject kvmValueObject)
throws IOException, MojoFailureException {
super.update(kvmValueObject);
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/apigee/edge/config/mavenplugin/kvm/KvmEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ public HttpResponse updateKvmEntries(KvmValueObject kvmValueObject, String kvmEn
kvmEntryValue);
}

@Override
public HttpResponse updateKvmEntriesForNonCpsOrg(KvmValueObject kvmValueObject) throws IOException {
RestUtil restUtil = new RestUtil(kvmValueObject.getProfile());
return restUtil.updateEnvConfig(kvmValueObject.getProfile(),
"keyvaluemaps",
kvmValueObject.getKvmName(),
kvmValueObject.getKvm());
}

@Override
public HttpResponse createKvmEntries(KvmValueObject kvmValueObject, String kvmEntryValue) throws IOException {
RestUtil restUtil = new RestUtil(kvmValueObject.getProfile());
Expand All @@ -45,6 +54,15 @@ public HttpResponse createKvmEntries(KvmValueObject kvmValueObject, String kvmEn
"entries",
kvmEntryValue);
}

@Override
public HttpResponse deleteKvmEntries(KvmValueObject kvmValueObject, String kvmEntryValue) throws IOException {
RestUtil restUtil = new RestUtil(kvmValueObject.getProfile());
return restUtil.deleteEnvConfig(kvmValueObject.getProfile(),
"keyvaluemaps",
kvmValueObject.getKvmName()+"/entries/"+kvmEntryValue);
}


@Override
public void update(KvmValueObject kvmValueObject)
Expand Down
Loading

0 comments on commit 6c705d2

Please sign in to comment.