Skip to content

Commit

Permalink
Transform a Specification into camtemplate.json and camvariables.json #9
Browse files Browse the repository at this point in the history
  • Loading branch information
jachinte committed Oct 21, 2019
1 parent c8b2441 commit fc2102e
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
28 changes: 28 additions & 0 deletions coordinator/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id "application"
id "org.xtext.xtend"
}

repositories {
Expand Down Expand Up @@ -27,6 +28,7 @@ dependencies {
implementation "org.apache.commons:commons-configuration2:2.5"
implementation "org.eclipse.jgit:org.eclipse.jgit:5.5.1.201910021850-r"
implementation "com.fasterxml.jackson.core:jackson-databind:2.9.9"
implementation "org.eclipse.xtend:org.eclipse.xtend.lib:2.18.0"
runtime "commons-beanutils:commons-beanutils:1.9.3"
runtime "org.apache.logging.log4j:log4j-core:2.12.0"
runtime "org.apache.logging.log4j:log4j-jcl:2.12.0"
Expand All @@ -35,3 +37,29 @@ dependencies {
application {
mainClassName = "com.rigiresearch.middleware.coordinator.Application"
}

xtend {
generator {
//whether to generate @SuppressWarnings("all"), enabled by default
suppressWarningsAnnotation = false
//whether to generate the @Generated annotation, disabled by default
generatedAnnotation {
active = true
comment = "Copyright University of Victoria"
includeDate = true
}
}
debugger {
sourceInstaller = 'SMAP'
hideSyntheticVariables = true
}
validator {
// Available levels are error, warning, info and ignore
error 'org.eclipse.xtend.core.validation.IssueCodes.unused_private_member'
}
}

sourceSets {
main.xtendOutputDir = 'src/main/xtend-gen'
test.xtendOutputDir = 'test/xtend-gen'
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rigiresearch.middleware.coordinator;

import com.rigiresearch.middleware.coordinator.templates.CamTemplates;
import com.rigiresearch.middleware.metamodels.hcl.HclMergeStrategy;
import com.rigiresearch.middleware.metamodels.hcl.Specification;
import com.rigiresearch.middleware.metamodels.hcl.SpecificationSet;
Expand Down Expand Up @@ -144,6 +145,10 @@ public void update(final Specification specification)
try (Git git = Git.open(this.repository.getDirectory())) {
this.prepareBranch(git);
this.updateTemplates(specification);
this.updateCamTemplate(
git.getRepository().getDirectory().getParentFile(),
specification
);
if (git.status().call().isClean()) {
TerraformRepository.LOGGER.info("The repository is already up to date");
return;
Expand Down Expand Up @@ -175,6 +180,34 @@ public void update(final Specification specification)
}
}

/**
* Updates or creates IBM CAM's templates.
* @param directory The repository directory
* @param specification The latest specification
* @throws IOException If there's an I/O error
*/
private void updateCamTemplate(final File directory,
final Specification specification) throws IOException {
final CamTemplates templates = new CamTemplates();
final File template = new File(directory, "camtemplate.json");
if (!template.exists()) {
Files.write(
template.toPath(),
templates.template().getBytes(),
StandardOpenOption.CREATE_NEW
);
}
final File variables = new File(directory, "camvariables.json");
if (!variables.exists()) {
variables.createNewFile();
}
Files.write(
variables.toPath(),
templates.variables(specification).getBytes(),
StandardOpenOption.TRUNCATE_EXISTING
);
}

/**
* Prepares the repository and the branch to use depending on the state of
* the repository and the remote branches.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.rigiresearch.middleware.coordinator.templates

import com.rigiresearch.middleware.metamodels.hcl.Dictionary
import com.rigiresearch.middleware.metamodels.hcl.Resource
import com.rigiresearch.middleware.metamodels.hcl.Specification
import com.rigiresearch.middleware.metamodels.hcl.Text

/**
* JSON templates for generating CAM's camtemplate.json and camvariables.json.
* @author Miguel Jimenez ([email protected])
* @version $Id$
* @since 0.1.0
*/
class CamTemplates {

/**
* Generates the content for camtemplate.json.
*/
def String template() '''
{
"name": "ImportedVmwareResources",
"description": "Imported resources from VMware vSphere",
"type": "userCreated",
"version": "1.0",
"manifest": {
"template_type": "Terraform",
"template_format": "HCL",
"template_provider": "VMware vSphere",
"template": {
"templateOutput": "",
"templateVariables": "",
"templateData": ""
},
"template_source": {
"githubRepoUrl": "",
"githubAccessToken": "",
"relativePathToTemplateFolder": "",
"templateFileName": "main.tf"
}
},
"metadata": {
"displayName": "ImportedVmwareResources",
"longDescription": "Imported resources from VMware vSphere",
"bullets": [
{
"title": "Clouds",
"description": "VMware"
}
]
}
}
'''
/**
* Generates the content for camvariables.json.
*/
def String variables(Specification specification) '''
{
"input_datatypes": [
],
"output_datatype": "content_template_output",
"input_groups": [
{
"name": "all",
"label": "All variables"
}
],
"output_groups": [
{
"name": "content_template_output",
"label": "Outputs"
}
],
"template_input_params": [
«FOR input : specification.resources.filter[it.specifier.equals("variable")]»
{
"name": "«input.name»",
"type": "«input.attr("type"",
"description": "«input.attr("description"",
"group_name": "all"
}
«ENDFOR»
]
}
'''
def private String attr(Resource resource, String name) {
((resource.value as Dictionary).elements
.findFirst[it.name.equals(name)]
?.value as Text)?.value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Contains templates for generating IBM CAM's JSON files.
* @author Miguel Jimenez ([email protected])
* @version $Id$
* @since 0.1.0
*/
package com.rigiresearch.middleware.coordinator.templates;

0 comments on commit fc2102e

Please sign in to comment.