Skip to content

Commit b71711b

Browse files
finish handling getinput function calls
1 parent 217693c commit b71711b

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

plugins/iac/nimble/src/main/java/org/apache/cloudstack/tosca/orchestrator/ToscaOrchestrator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,14 @@ protected void resolveServiceTemplateInputs(ToscaServiceTemplate serviceTemplate
126126

127127
serviceTemplate.getUnresolvedPropertiesByGetInput().forEach((nodeTemplate, unresolvedProperties) -> {
128128
unresolvedProperties.forEach(property -> {
129-
handleGetInputFunctionCall(nodeTemplate, property, serviceTemplate, inputs);
129+
handleGetInputFunctionCalls(nodeTemplate, property, serviceTemplate, inputs);
130130
});
131131
});
132132
}
133133

134-
private void handleGetInputFunctionCall(String nodeTemplateName, ToscaProperty property, ToscaServiceTemplate serviceTemplate, Map<String, String> inputs) {
134+
private void handleGetInputFunctionCalls(String nodeTemplateName, ToscaProperty property, ToscaServiceTemplate serviceTemplate, Map<String, String> inputs) {
135135
String propertyName = property.getDefinition().getName();
136-
logger.debug("Resolving the [{}] function call triggered by the [{}] property of the [{}] node template.", ToscaConstants.GET_INPUT_FUNCTION, propertyName, nodeTemplateName);
136+
logger.debug("Resolving the [{}] function calls triggered by the [{}] property of the [{}] node template.", ToscaConstants.GET_INPUT_FUNCTION, propertyName, nodeTemplateName);
137137

138138
Object evaluatedValue = getPropertyEvaluatedValue(property.getRawValue(), serviceTemplate, nodeTemplateName, inputs);
139139
ToscaFunction.ToscaBooleanFunction propertyValidationFunction = property.getDefinition().getValidation();

plugins/iac/nimble/src/main/java/org/apache/cloudstack/tosca/parser/ToscaServiceTemplateParser.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,6 @@ private Pair<Map<String, Set<ToscaGetterFunctionCallContext>>, Boolean> getAllTo
286286
}
287287

288288
private void getAllToscaFunctionCallsRecursive(ToscaProperty property, Object propertyBody, ToscaTypeDefinition type, Pair<Map<String, Set<ToscaGetterFunctionCallContext>>, Boolean> functionCalls, ToscaServiceTemplateParsingContext context) {
289-
if (!functionCalls.second()) {
290-
return;
291-
}
292-
293289
if (propertyBody instanceof List) {
294290
ToscaTypeDefinition entrySchema = ObjectUtils.defaultIfNull(type.getEntrySchema(), type);
295291
ToscaYamlHelper.asList(propertyBody).forEach(value -> getAllToscaFunctionCallsRecursive(property, value, entrySchema, functionCalls, context));

plugins/iac/nimble/src/test/java/org/apache/cloudstack/tosca/orchestrator/ToscaOrchestratorTest.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,31 @@ public void resolveServiceTemplateInputsTestThrowExceptionWhenInputIsNotDefinedA
135135
toscaOrchestratorSpy.resolveServiceTemplateInputs(serviceTemplate, Map.of());
136136
}
137137

138+
@Test
139+
public void resolveServiceTemplateInputsTestCorrectlyResolveAllGetInputFunctionCallsInsideOfCollections() {
140+
String serviceTemplateYaml = "{service_template: {inputs: {first-ip: {type: string}, second-ip: {type: string}, first-detail: {type: string}, second-detail: {type: string}}, node_templates: {instance: {type: Vm, properties: {type: SSVM, vcpus: 2, ip-addresses: [{$get_input: first-ip}, {$get_input: second-ip}], offering-details: {detail1: {$get_input: first-detail}, detail2: {$get_input: second-detail}}, name-value-mapping: [{name: {$get_input: first-detail}, value: {$get_input: first-ip}}, {name: {$get_input: second-detail}, value: {$get_input: second-ip}}]}}}}}";
141+
ToscaServiceTemplate serviceTemplate = toscaParser.parseServiceTemplate(serviceTemplateYaml, ToscaFixtures.getToscaProfileForTests(), null);
142+
143+
toscaOrchestratorSpy.resolveServiceTemplateInputs(serviceTemplate, Map.of("first-ip", "10.0.0.1", "second-ip", "10.0.0.2", "first-detail", "1st detail", "second-detail", "2nd detail"));
144+
145+
List<?> instanceIpAddresses = ToscaYamlHelper.asList(serviceTemplate.getNodeTemplates().get("instance").getProperty("ip-addresses").getEvaluatedValue());
146+
Assert.assertEquals(2, instanceIpAddresses.size());
147+
Assert.assertEquals("10.0.0.1", instanceIpAddresses.get(0));
148+
Assert.assertEquals("10.0.0.2", instanceIpAddresses.get(1));
149+
150+
Map<String, Object> offeringDetails = ToscaYamlHelper.asMap(serviceTemplate.getNodeTemplates().get("instance").getProperty("offering-details").getEvaluatedValue());
151+
Assert.assertEquals(2, offeringDetails.size());
152+
Assert.assertEquals("1st detail", offeringDetails.get("detail1"));
153+
Assert.assertEquals("2nd detail", offeringDetails.get("detail2"));
154+
155+
List<?> nameValueMappings = ToscaYamlHelper.asList(serviceTemplate.getNodeTemplates().get("instance").getProperty("name-value-mapping").getEvaluatedValue());
156+
Assert.assertEquals(2, nameValueMappings.size());
157+
Assert.assertEquals("1st detail", ToscaYamlHelper.asMap(nameValueMappings.get(0)).get("name"));
158+
Assert.assertEquals("10.0.0.1", ToscaYamlHelper.asMap(nameValueMappings.get(0)).get("value"));
159+
Assert.assertEquals("2nd detail", ToscaYamlHelper.asMap(nameValueMappings.get(1)).get("name"));
160+
Assert.assertEquals("10.0.0.2", ToscaYamlHelper.asMap(nameValueMappings.get(1)).get("value"));
161+
}
162+
138163
@Test
139164
public void populateNodeTemplateAttributesTestSuccessfullyPopulateNodeAttributes() {
140165
String serviceTemplateYaml = "{service_template: {node_templates: {instance: {type: Vm, properties: {type: VR, ssh-key-pair-name: {$get_property: [pair, name]}, vcpus: 2, ip-addresses: [10.0.0.1, 10.0.0.2]}}, other-instance: {type: Vm, properties: {type: SSVM, ssh-key-pair-name: {$get_attribute: [pair, uuid]}, vcpus: 1, ip-addresses: [10.0.0.1]}}, pair: {type: SshPair, properties: {name: Pair, public-key: Public Key}}}}}";
@@ -174,7 +199,7 @@ public void executeGetAttributeAndGetPropertyFunctionCallsTestSuccessfullyExecut
174199

175200
@Test
176201
public void executeGetAttributeAndGetPropertyFunctionCallsTestSuccessfullyExecuteFunctionCallsContainedInsideOfCollections() {
177-
String serviceTemplateYaml = "{service_template: {node_templates: {instance: {type: Vm, properties: {type: VR, vcpus: 2, ip-addresses: [{$get_property: [other-instance, type]}, {$get_attribute: [other-instance, uuid]}]}}, other-instance: {type: Vm, properties: {type: {$get_property: [pair, name]}, vcpus: 1, offering-details: {detail1: {$get_attribute: [pair, uuid]}, detail2: {$get_property: [pair, public-key]}}}}, pair: {type: SshPair, properties: {name: SSVM, public-key: Public Key}}}}}";
202+
String serviceTemplateYaml = "{service_template: {node_templates: {instance: {type: Vm, properties: {type: VR, vcpus: 2, name-value-mapping: [{name: {$get_property: [pair, name]}, value: {$get_attribute: [pair, uuid]}}, {name: {$get_property: [other-instance, type]}, value: {$get_attribute: [other-instance, uuid]}}], ip-addresses: [{$get_property: [other-instance, type]}, {$get_attribute: [other-instance, uuid]}]}}, other-instance: {type: Vm, properties: {type: {$get_property: [pair, name]}, vcpus: 1, offering-details: {detail1: {$get_attribute: [pair, uuid]}, detail2: {$get_property: [pair, public-key]}}}}, pair: {type: SshPair, properties: {name: SSVM, public-key: Public Key}}}}}";
178203
ToscaServiceTemplate serviceTemplate = toscaParser.parseServiceTemplate(serviceTemplateYaml, ToscaFixtures.getToscaProfileForTests(), null);
179204
serviceTemplate.getNodeTemplates().get("other-instance").addAttribute("uuid", "otherinstanceuuid");
180205
serviceTemplate.getNodeTemplates().get("pair").addAttribute("uuid", "pairuuid");
@@ -186,7 +211,15 @@ public void executeGetAttributeAndGetPropertyFunctionCallsTestSuccessfullyExecut
186211
Assert.assertEquals(serviceTemplate.getNodeTemplates().get("pair").getProperty("public-key").getEvaluatedValue(), offeringDetails.get("detail2"));
187212

188213
toscaOrchestratorSpy.executeGetAttributeAndGetPropertyFunctionCalls(serviceTemplate.getNodeTemplates().get("instance"), serviceTemplate);
214+
List<?> nameValueMappings = ToscaYamlHelper.asList(serviceTemplate.getNodeTemplates().get("instance").getProperty("name-value-mapping").getEvaluatedValue());
215+
Assert.assertEquals(2, nameValueMappings.size());
216+
Assert.assertEquals(serviceTemplate.getNodeTemplates().get("pair").getProperty("name").getEvaluatedValue(), ToscaYamlHelper.asMap(nameValueMappings.get(0)).get("name"));
217+
Assert.assertEquals(serviceTemplate.getNodeTemplates().get("pair").getAttribute("uuid"), ToscaYamlHelper.asMap(nameValueMappings.get(0)).get("value"));
218+
Assert.assertEquals(serviceTemplate.getNodeTemplates().get("other-instance").getProperty("type").getEvaluatedValue(), ToscaYamlHelper.asMap(nameValueMappings.get(1)).get("name"));
219+
Assert.assertEquals(serviceTemplate.getNodeTemplates().get("other-instance").getAttribute("uuid"), ToscaYamlHelper.asMap(nameValueMappings.get(1)).get("value"));
220+
189221
List<?> instanceIpAddresses = ToscaYamlHelper.asList(serviceTemplate.getNodeTemplates().get("instance").getProperty("ip-addresses").getEvaluatedValue());
222+
Assert.assertEquals(2, offeringDetails.size());
190223
Assert.assertEquals(serviceTemplate.getNodeTemplates().get("other-instance").getProperty("type").getEvaluatedValue(), instanceIpAddresses.get(0));
191224
Assert.assertEquals(serviceTemplate.getNodeTemplates().get("other-instance").getAttribute("uuid"), instanceIpAddresses.get(1));
192225
}

0 commit comments

Comments
 (0)