public static String[] fromDockerLinksToCompositeLinks(String[] links) { if (isNullOrEmpty(links)) { return null; } for (int i = 0; i < links.length; i++) { if (!links[i].contains(":")) { links[i] = links[i] + ":" + links[i]; } } return links; }
private void validateCompositeTemplate(CompositeTemplate compositeTemplate) { assertNotNull(compositeTemplate, "compositeTemplate"); assertNotEmpty(compositeTemplate.name, "name"); assertComponentTypes(compositeTemplate.components); }
@Test public void testConvertCompositeTemplateToDockerCompose() throws IOException { DockerCompose expectedCompose = deserializeDockerCompose( getContent("docker.wordpress.1.yaml")); String expectedComposeYaml = serializeDockerCompose(expectedCompose); CompositeTemplate template = deserializeCompositeTemplate( getContent("composite.wordpress.yaml")); DockerCompose compose = fromCompositeTemplateToDockerCompose(template); String composeYaml = serializeDockerCompose(compose); assertEqualsYamls(expectedComposeYaml, composeYaml); }
private static void sanitizeCompositeTemplate(CompositeTemplate entity, boolean serialize) { assertNotNull(entity, "entity"); entity.id = null; entity.status = null; if (!isNullOrEmpty(entity.components)) { sanitizeContainerComponents(entity, serialize); sanitizeContainerNetworkComponents(entity); sanitizeContainerVolumeComponents(entity); normalizeClosureDescriptions(entity, serialize); } }
public static String serializeCompositeTemplate(CompositeTemplate entity) throws IOException { sanitizeCompositeTemplate(entity, true); Map<String, Object> stringObjectMap = TemplateSerializationUtils.serializeTemplate(entity); if (!isNullOrEmpty(entity.bindings)) { normalizeBindings(stringObjectMap, entity); } return YamlMapper.objectWriter().writeValueAsString(stringObjectMap); }
private static void sanitizeContainerComponents(CompositeTemplate entity, boolean serialize) { for (Entry<String, ComponentTemplate<ContainerDescription>> entry : filterComponentTemplates( entity.components, ContainerDescription.class).entrySet()) { ComponentTemplate<ContainerDescription> component = entry.getValue(); normalizeContainerDescription(component, serialize); if (!entry.getKey().equals(component.data.name)) { Utils.logWarning("Container name '%s' differs from component name '%s' and " + "it will be overridden with the component name!", component.data.name, entry.getKey()); component.data.name = entry.getKey(); } component.data.tenantLinks = null; // this could be a new serializer... HealthConfig hc = component.data.healthConfig; if (hc != null && hc.protocol == null) { component.data.healthConfig = null; } // this could be a new serializer... LogConfig lc = component.data.logConfig; if (lc != null && lc.type == null && isNullOrEmpty(lc.config)) { component.data.logConfig = null; } } }
@Test public void testDeserializeSerializeSimpleDockerComposeWithNetwork() throws IOException { String expectedContent = getContent("docker.simple.network.yaml"); DockerCompose compose = deserializeDockerCompose(expectedContent); String content = serializeDockerCompose(compose); assertEquals(toUnixLineEnding(expectedContent), toUnixLineEnding(content)); CompositeTemplate template = fromDockerComposeToCompositeTemplate(compose); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 2, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template.components); String contentTemplate = serializeCompositeTemplate(template); assertTrue((contentTemplate != null) && (!contentTemplate.isEmpty())); }
@Test public void testDeserializeSerializeComplexCompositeTemplateWithNetwork() throws IOException { String expectedContent = getContent("composite.complex.network.yaml"); CompositeTemplate template = deserializeCompositeTemplate(expectedContent); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 3, template.components); String content = serializeCompositeTemplate(template); expectedContent = expectedContent.replace("h5-name", "h5"); assertEqualsYamls(toUnixLineEnding(getContent("composite.complex.network.expected.yaml")), toUnixLineEnding(content), true); DockerCompose compose = fromCompositeTemplateToDockerCompose(template); String contentCompose = serializeDockerCompose(compose); assertTrue((contentCompose != null) && (!contentCompose.isEmpty())); }
CompositeTemplate expectedTemplate = deserializeCompositeTemplate( getContent("composite.simple.volume.yaml")); String expectedTemplateYaml = serializeCompositeTemplate(expectedTemplate); DockerCompose compose1 = deserializeDockerCompose( getContent("docker.simple.volume.yaml")); CompositeTemplate template1 = fromDockerComposeToCompositeTemplate(compose1); assertComponentTypes(template1.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 2, template1.components); String template1Yaml = serializeCompositeTemplate(template1); expectedTemplate = deserializeCompositeTemplate( getContent("composite.complex.volume.yaml")); expectedTemplateYaml = serializeCompositeTemplate(expectedTemplate); DockerCompose compose2 = deserializeDockerCompose( getContent("docker.complex.volume.yaml")); CompositeTemplate template2 = fromDockerComposeToCompositeTemplate(compose2); assertComponentTypes(template2.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 3, template2.components);
if (isApplicationYamlContent(op.getContentType())) { content = op.getBody(String.class); yamlType = getYamlType(content); switch (yamlType) { case COMPOSITE_TEMPLATE: template = deserializeCompositeTemplate(content); break; case DOCKER_COMPOSE: DockerCompose compose = deserializeDockerCompose(content); template = fromDockerComposeToCompositeTemplate(compose); break; case KUBERNETES_TEMPLATE:
if (!isNullOrEmpty(components)) { compose.services = new LinkedHashMap<>(); for (Entry<String, ComponentTemplate<ContainerDescription>> entry : filterComponentTemplates( components, ContainerDescription.class).entrySet()) { compose.services.put(entry.getKey(), fromCompositeComponentToDockerService(entry.getValue(), components)); for (Entry<String, ComponentTemplate<ContainerNetworkDescription>> entry : filterComponentTemplates( components, ContainerNetworkDescription.class).entrySet()) { if (compose.networks == null) { fromCompositeComponentToDockerNetwork(entry.getValue())); for (Entry<String, ComponentTemplate<ContainerVolumeDescription>> entry : filterComponentTemplates( components, ContainerVolumeDescription.class).entrySet()) { if (compose.volumes == null) { fromCompositeComponentToDockerVolume(entry.getValue()));
@Test public void testSerializeDeserializeEmptyTemplate() throws IOException { CompositeTemplate template = new CompositeTemplate(); template.name = "empty"; String templateSerialized = serializeCompositeTemplate(template); assertNotNull(templateSerialized); assertTrue(templateSerialized.contains("empty")); CompositeTemplate templateDeserialized = deserializeCompositeTemplate(templateSerialized); assertNotNull(templateDeserialized); assertEquals(template.name, templateDeserialized.name); assertTrue(isNullOrEmpty(template.components)); assertTrue(isNullOrEmpty(templateDeserialized.components)); }
if (!isNullOrEmpty(services)) { template.components = new LinkedHashMap<>(); for (Entry<String, DockerComposeService> entry : services.entrySet()) { ComponentTemplate<ResourceState> component = fromDockerServiceToCompositeComponent( entry.getValue()); if (!isNullOrEmpty(networks)) { if (template.components == null) { template.components = new LinkedHashMap<>(); ComponentTemplate<ContainerNetworkDescription> component = fromDockerNetworkToCompositeComponent( entry.getValue()); if (!isNullOrEmpty(volumes)) { if (template.components == null) { template.components = new LinkedHashMap<>(); ComponentTemplate<ContainerVolumeDescription> component = fromDockerVolumeToCompositeComponent( entry.getValue());
private void serializeAndComplete(CompositeTemplate template, boolean returnDocker, boolean returnInline, Operation op) { try { String content; if (returnDocker) { DockerCompose compose = fromCompositeTemplateToDockerCompose(template); content = serializeDockerCompose(compose); } else { content = serializeCompositeTemplate(template); } op.setBody(content); op.setContentType(MEDIA_TYPE_APPLICATION_YAML); String contentDisposition = (returnInline ? CONTENT_DISPOSITION_INLINE : CONTENT_DISPOSITION_ATTACHMENT) + CONTENT_DISPOSITION_FILENAME; op.addResponseHeader(CONTENT_DISPOSITION_HEADER, contentDisposition); op.complete(); } catch (Exception e) { op.fail(e); } }
@Test public void testConvertSmallDockerCompose() throws IOException { // YAML with 'command' as string DockerCompose compose1 = deserializeDockerCompose(getContent("docker.django.yaml")); CompositeTemplate template1 = fromDockerComposeToCompositeTemplate(compose1); String template1Yaml = serializeCompositeTemplate(template1); assertTrue((template1Yaml != null) && (!template1Yaml.isEmpty())); // YAML with 'command' as list DockerCompose compose2 = deserializeDockerCompose(getContent("docker.rails.yaml")); CompositeTemplate template2 = fromDockerComposeToCompositeTemplate(compose2); String template2Yaml = serializeCompositeTemplate(template2); assertTrue((template2Yaml != null) && (!template2Yaml.isEmpty())); }
@Test public void testDeserializeSerializeSimpleCompositeTemplate() throws IOException { String expectedContent = getContent("composite.simple.yaml"); CompositeTemplate template = deserializeCompositeTemplate(expectedContent); assertNull(template.id); assertNull(template.status); assertComponentTypes(template.components); assertContainersComponents(ResourceType.CONTAINER_TYPE.getContentType(), 1, template.components); assertContainersComponents(ResourceType.NETWORK_TYPE.getContentType(), 0, template.components); assertContainersComponents(ResourceType.VOLUME_TYPE.getContentType(), 0, template.components); template.id = "new-id"; template.status = Status.RETIRED; ContainerDescription helloData = (ContainerDescription) template.components .get("hello").data; helloData.healthConfig = new HealthConfig(); helloData.logConfig = new LogConfig(); String content = serializeCompositeTemplate(template); assertNotNull(content); assertFalse(content.contains("new-id")); assertFalse(content.contains(Status.RETIRED.toString())); assertFalse(content.contains("health_config")); assertFalse(content.contains("log_config")); }
@Test public void testSeriaizeDeserializeCompositeTemplateWithHealthCheck() throws IOException { // Assert that healthConfig.ignoreOnProvision is serialized when false String expectedContent = getContent("composite.simple.health.yaml"); CompositeTemplate template = deserializeCompositeTemplate(expectedContent); ContainerDescription data = (ContainerDescription) template.components.get("hello").data; assertNotNull(data.healthConfig); assertEquals(false, data.healthConfig.ignoreOnProvision); // Assert that healthConfig.ignoreOnProvision is serialized when true data.healthConfig.ignoreOnProvision = true; String content = serializeCompositeTemplate(template); template = deserializeCompositeTemplate(content); data = (ContainerDescription) template.components.get("hello").data; assertNotNull(data.healthConfig); assertEquals(true, data.healthConfig.ignoreOnProvision); // Assert that healthConfig.ignoreOnProvision is not serialized when null data.healthConfig = null; content = serializeCompositeTemplate(template); assertFalse(content.contains("health_config")); }
private static ComponentTemplate<ContainerVolumeDescription> fromDockerVolumeToCompositeComponent( DockerComposeVolume volume) { assertNotNull(volume, "volume"); ContainerVolumeDescription description = new ContainerVolumeDescription(); description.driver = volume.driver; description.options = volume.driver_opts; if (volume.external != null) { if (!isNullOrEmpty(volume.external.name)) { description.externalName = volume.external.name; } else if (volume.external.value != null) { description.external = volume.external.value; } } return fromContainerVolumeDescriptionToComponentTemplate(description); }
private static ComponentTemplate<ContainerNetworkDescription> fromDockerNetworkToCompositeComponent( DockerComposeNetwork network) { assertNotNull(network, "network"); ContainerNetworkDescription description = new ContainerNetworkDescription(); description.driver = network.driver; description.customProperties = network.driver_opts; description.ipam = network.ipam; if (network.external != null) { if (!isNullOrEmpty(network.external.name)) { description.externalName = network.external.name; } else if (network.external.value != null) { description.external = network.external.value; } } return fromContainerNetworkDescriptionToComponentTemplate(description); }
public static KubernetesTemplate fromCompositeTemplateToKubernetesTemplate( CompositeTemplate template) { if (template == null) { return null; } KubernetesTemplate kubernetesTemplate = new KubernetesTemplate(); if (!isNullOrEmpty(template.components)) { kubernetesTemplate.deployments = new LinkedHashMap<>(); kubernetesTemplate.services = new LinkedHashMap<>(); Map<String, ComponentTemplate<ContainerDescription>> containerComponents = filterComponentTemplates( template.components, ContainerDescription.class); for (Entry<String, ComponentTemplate<ContainerDescription>> container : containerComponents .entrySet()) { Deployment deployment = fromContainerDescriptionToDeployment( container.getValue().data, template.name); kubernetesTemplate.deployments.put(deployment.metadata.name, deployment); if (!isNullOrEmpty(container.getValue().data.portBindings)) { Service service = fromContainerDescriptionToService(container.getValue().data, template.name); kubernetesTemplate.services.put(service.metadata.name, service); } } } return kubernetesTemplate; }