Skip to content

Commit

Permalink
priorize rules depending on componen inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
milesstoetzner committed Nov 20, 2024
1 parent 21db0e6 commit f6137a8
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 233 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/graph/inheritance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ export default class Inheritance {
return this.getTypes<RelationshipType>('relationship_types')[name]
}

collectNodeTypes(name: string): {name: string; type: NodeType}[] {
const found: {name: string; type: NodeType}[] = []

const walker = this.Walker(name, 'node_types', NODE_TYPE_ROOT)
while (walker.has()) {
found.push(walker.walk())
}

return found
}

private getTypes<T extends EntityType>(key: keyof EntityTypes) {
return (this.graph.serviceTemplate[key] ?? {}) as {[key: string]: T}
}
Expand Down
4 changes: 4 additions & 0 deletions src/graph/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export default class Type extends Element {
throw new Error(`${this.Display} does not support checking type inheritance`)
}

getInheritance() {
// TODO: return inheritance
}

getDefinition() {
if (this.container.isArtifact()) {
return this.graph.inheritance.getArtifactType(this.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Generator extends GeneratorAbstract {
technology = 'ansible'
artifact = 'tar.archive'
hosting = ['*', 'local.machine']
weight = 0.5
weight = 1
reason =
'While this is a primary use case due to the specialization of Ansible, we must rely on scripts. More specialized types should be used, e.g., service.application.'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Generator extends GeneratorAbstract {
technology = 'ansible'
artifact = 'tar.archive'
hosting = ['*', 'remote.machine']
weight = 0.5
weight = 1
reason =
'While this is a primary use case due to the specialization of Ansible, we must rely on scripts. More specialized types should be used, e.g., "service.application".'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Generator extends GeneratorAbstract {
technology = 'ansible'
artifact = 'zip.archive'
hosting = ['*', 'local.machine']
weight = 0.5
weight = 1
reason =
'While this is a primary use case due to the specialization of Ansible, we must rely on scripts. More specialized types should be used, e.g., service.application.'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Generator extends GeneratorAbstract {
technology = 'ansible'
artifact = 'zip.archive'
hosting = ['*', 'remote.machine']
weight = 0.5
weight = 1
reason =
'While this is a primary use case due to the specialization of Ansible, we must rely on scripts. More specialized types should be used, e.g., service.application.'

Expand Down
26 changes: 19 additions & 7 deletions src/technologies/plugins/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ export class TechnologyRulePlugin implements TechnologyPlugin {

getRules() {
const rules = this.graph.serviceTemplate.topology_template?.variability?.technology_rules
if (check.isUndefined(rules)) return rules
if (check.isUndefined(rules)) return []
assert.isObject(rules, 'Rules not loaded')
return rules
}

hasRules() {
return check.isDefined(this.getRules())
return utils.isPopulated(this.getRules())
}

implement(name: string, type: NodeType): NodeTypeMap {
const rules = this.getRules()
if (check.isUndefined(rules)) return {}
if (utils.isEmpty(rules)) return {}

const types: NodeTypeMap = {}

Expand Down Expand Up @@ -94,18 +94,30 @@ export class TechnologyRulePlugin implements TechnologyPlugin {
return types
}

// TODO: rework this function
assign(node: Node): TechnologyTemplateMap[] {
const maps: TechnologyTemplateMap[] = []

const rules = this.getRules()
if (check.isUndefined(rules)) return maps
let rules = this.getRules()
if (utils.isEmpty(rules)) return maps

// Check if rule matches component
rules = rules.filter(rule => node.getType().isA(rule.component))

// Check if another rule is more specific, i.e., if rule.component is within the inheritance (but not the root) of another rule
rules = rules.filter(rule =>
check.isUndefined(
rules.find(other => {
const parents = this.graph.inheritance.collectNodeTypes(other.component).slice(1)
return check.isDefined(parents.find(type => type.name === rule.component))
})
)
)

for (const entry of rules) {
const rule = utils.copy(entry)
assert.isDefined(rule.hosting)

if (!node.getType().isA(rule.component)) continue

let artifactCondition: LogicExpression | undefined
if (check.isDefined(rule.artifact)) {
// Check for artifact in template
Expand Down
14 changes: 14 additions & 0 deletions tests/conformance/technologies/optimization-prio/expected.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tosca_definitions_version: tosca_simple_yaml_1_3

node_types:
grandparent:
derived_from: tosca.nodes.Root
parent:
derived_from: grandparent
container:
derived_from: parent

topology_template:
node_templates:
container:
type: container~parent::ansible
25 changes: 25 additions & 0 deletions tests/conformance/technologies/optimization-prio/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tosca_definitions_version: tosca_variability_1_0_rc_3

node_types:
grandparent:
derived_from: tosca.nodes.Root
parent:
derived_from: grandparent
container:
derived_from: parent

topology_template:
variability:
technology_rules:
- component: parent
technology: ansible
weight: 0

- component: grandparent
technology: terraform
weight: 1

node_templates:
container:
type: container
persistent: true
1 change: 1 addition & 0 deletions tests/conformance/technologies/optimization-prio/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
description: technology rule for grandparent is not considered since technology rule for parent is stricter

0 comments on commit f6137a8

Please sign in to comment.