🚀 Enhancements
Add multiple inheritance and mixins for components. Add `metadata.component` and `metadata.inherits` settings @aknysh (#101)
what
- Add multiple inheritance (and multiple inheritance chains) and mixins for components
- Add
metadata.component
andmetadata.inherits
settings
why
-
Allow a component to inherit from many base components or mixins, each base component having its own inheritance chain, effectively making it an inheritance matrix. It uses a method similar to Method Resolution Order (MRO), which is how Python supports multiple inheritance.
-
metadata.component
points to the component implementation (e.g. incomponents/terraform
folder).
It does not specify inheritance. It overrides the deprecated top-levelcomponent
attribute, which performed two roles: 1) point to a component implementation; 2) specify a base component to inherit from (only one) . -
metadata.inherits
is a list of component or mixins names from which the current component inherits. In the case of multiple base components, it is processed left to right, in the order by which it was declared.
For example:metadata.inherit: [componentA, componentB]
will deep-merge all the base components ofcomponentA
(each component overriding its base), then all the base components ofcomponentB
(each component overriding its base), then the two results are deep-merged together (componentB
inheritance chain will override values from `componentA' inheritance chain).
related
- In object-oriented programming languages, a mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes
test
Given this component config
import:
- catalog/terraform/mixins/test-*.*
components:
terraform:
"test/test-component-override-3":
settings:
spacelift:
workspace_enabled: false
vars: {}
env:
TEST_ENV_VAR1: "val1-override-3"
TEST_ENV_VAR2: "val2-override-3"
TEST_ENV_VAR3: "val3-override-3"
TEST_ENV_VAR4: "val4-override-3"
metadata:
# `real` is implicit, you don't need to specify it; `abstract` makes the component protected from being deployed
type: real
# Terraform component. Must exist in `components/terraform` folder.
# If not specified, it's assumed that this component `test/test-component-override-3` is also a Terraform component
# in `components/terraform/test/test-component-override-3` folder
component: "test/test-component"
# Multiple inheritance. It's a down-top/left-right matrix (similar to Method Resolution Order (MRO), which is how Python supports multiple inheritance).
# All base components and mixins are processed and deep-merged in the order they are specified in the `inherits` list:
# 1. `test/test-component-override-2` overrides `test/test-component-override` and its base components (all the way up its inheritance chain).
# 2. `mixin/test-1` overrides `test/test-component-override-2` and its base components (all the way up its inheritance chain).
# 3. `mixin/test-2` overrides `mixin/test-1` and its base components (all the way up its inheritance chain).
# 4. This `test/test-component-override-3` component overrides `mixin/test-2` and its base components (all the way up its inheritance chain).
# Inheritance: test/test-component-override-3 -> mixin/test-2 -> mixin/test-1 -> test/test-component-override-2 -> test/test-component-override -> test/test-component
inherits:
- "test/test-component-override"
- "test/test-component-override-2"
- "mixin/test-1"
- "mixin/test-2"
atmos terraform plan test/test-component-override-3 -s=tenant1-ue2-dev
Results in
Command info:
Terraform binary: terraform
Terraform command: plan
Arguments and flags: []
Component: test/test-component-override-3
Terraform component: test/test-component
Inheritance: test/test-component-override-3 -> mixin/test-2 -> mixin/test-1 -> test/test-component-override-2 -> test/test-component-override -> test/test-component
Stack: tenant1/ue2/dev
Working dir: examples/complete/components/terraform/test/test-component
Executing command:
/usr/local/bin/terraform plan -var-file tenant1-ue2-dev-test-component-override-3.terraform.tfvars.json -out tenant1-ue2-dev-test-component-override-3.planfile
Changes to Outputs:
+ service_1_id = "eg-ue2-dev-mixin-2"
+ service_2_id = "eg-ue2-dev-service-2-override-2"