diff --git a/coordinator/build.gradle b/coordinator/build.gradle index df5441a..0d9489b 100644 --- a/coordinator/build.gradle +++ b/coordinator/build.gradle @@ -1,5 +1,6 @@ plugins { id "application" + id "org.xtext.xtend" } repositories { @@ -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" @@ -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' +} diff --git a/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/TerraformRepository.java b/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/TerraformRepository.java index f155d74..b902a0b 100644 --- a/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/TerraformRepository.java +++ b/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/TerraformRepository.java @@ -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; @@ -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; @@ -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. diff --git a/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/templates/CamTemplates.xtend b/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/templates/CamTemplates.xtend new file mode 100644 index 0000000..5e34ef3 --- /dev/null +++ b/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/templates/CamTemplates.xtend @@ -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 (miguel@uvic.ca) + * @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 + } + +} diff --git a/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/templates/package-info.java b/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/templates/package-info.java new file mode 100644 index 0000000..4ecb172 --- /dev/null +++ b/coordinator/src/main/java/com/rigiresearch/middleware/coordinator/templates/package-info.java @@ -0,0 +1,7 @@ +/** + * Contains templates for generating IBM CAM's JSON files. + * @author Miguel Jimenez (miguel@uvic.ca) + * @version $Id$ + * @since 0.1.0 + */ +package com.rigiresearch.middleware.coordinator.templates;