Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple inheritance and mixins for components. Add metadata.component and metadata.inherits settings #101

Merged
merged 10 commits into from Jan 12, 2022

Conversation

aknysh
Copy link
Member

@aknysh aknysh commented Jan 12, 2022

what

  • Add multiple inheritance (and multiple inheritance chains) and mixins for components
  • Add metadata.component and metadata.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. in components/terraform folder).
    It does not specify inheritance. It overrides the deprecated top-level component 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 of componentA (each component overriding its base), then all the base components of componentB (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. `catalog/test/test-component-override-2` overrides `catalog/test/test-component-override` and its base components (all the way up its inheritance chain).
        # 2. `catalog/mixin/test-1` overrides `catalog/test/test-component-override-2` and its base components (all the way up its inheritance chain).
        # 3. `catalog/mixin/test-2` overrides `catalog/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:
          - "catalog/test/test-component-override"
          - "catalog/test/test-component-override-2"
          - "catalog/mixin/test-1"
          - "catalog/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"

@aknysh aknysh added the patch A minor, backward compatible change label Jan 12, 2022
@aknysh aknysh self-assigned this Jan 12, 2022
@aknysh aknysh requested review from a team as code owners January 12, 2022 04:12
nitrocode
nitrocode previously approved these changes Jan 12, 2022
osterman
osterman previously approved these changes Jan 12, 2022
@aknysh aknysh dismissed stale reviews from osterman and nitrocode via 66e0d9c January 12, 2022 18:25
@aknysh aknysh changed the title Add multiple inheritance and mixins for components. Add metadata.component and metadata.inherit settings Add multiple inheritance and mixins for components. Add metadata.component and metadata.inherits settings Jan 12, 2022
@aknysh aknysh merged commit 9e7add8 into master Jan 12, 2022
@aknysh aknysh deleted the add-multiple-inheritance branch January 12, 2022 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch A minor, backward compatible change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants