Skip to content

Provider-Agnostic Resource Introspection

The ResourceIntrospector class analyzes resource classes at runtime to tell which arguments a resource must be written with and which it can leave out. Phased synthesis uses it to decide whether a reference that closes a cycle can be left out of the first phase. It is useful for resource validation too.

Overview

The introspector reads the signature of a resource class's __init__ with Python's inspect module. It needs no knowledge of a provider: a generated class takes the schema's required arguments as keyword parameters without a default, and every optional argument has a default.

Key Features

1. Attribute Analysis

get_required_optional_attributes takes a resource class or instance and returns the required and the optional arguments:

from nf.core import App, Stack
from nf.core.phases.resource_introspection import ResourceIntrospector
from nf.providers.hashicorp.aws import Subnet

app = App(nf_id="app")
stack = Stack(nf_scope=app, nf_id="network")
subnet = Subnet(nf_scope=stack, nf_id="subnet", vpc_id="vpc-123", cidr_block="10.0.1.0/24")

required, optional = ResourceIntrospector.get_required_optional_attributes(subnet)
assert required == {"vpc_id"}
assert "cidr_block" in optional and "tags" in optional

# The same from the class
assert ResourceIntrospector.get_required_optional_attributes(Subnet)[0] == required

The module's required_arguments(cls) gives the required arguments of a class by their Terraform names (type_ is type), which is what the phase planner compares references against:

from nf.core.phases.resource_introspection import required_arguments
from nf.providers.hashicorp.aws import Subnet

assert required_arguments(Subnet) == {"vpc_id"}

2. Safe Omission Detection

can_create_without_attributes says whether a resource can be written without some arguments: it can when none of them is required.

from nf.core import App, Stack
from nf.core.phases.resource_introspection import ResourceIntrospector
from nf.providers.hashicorp.aws import Subnet

app = App(nf_id="app")
stack = Stack(nf_scope=app, nf_id="network")
subnet = Subnet(nf_scope=stack, nf_id="subnet", vpc_id="vpc-123", cidr_block="10.0.1.0/24")

assert ResourceIntrospector.can_create_without_attributes(subnet, {"cidr_block"})
assert not ResourceIntrospector.can_create_without_attributes(subnet, {"vpc_id"})

can_omit_resource is the opposite question, and its name is the reverse of what it checks: it returns True when any of the arguments is required, that is, when leaving them out means leaving the whole resource out of the phase.

from nf.core import App, Stack
from nf.core.phases.resource_introspection import ResourceIntrospector
from nf.providers.hashicorp.aws import Subnet

app = App(nf_id="app")
stack = Stack(nf_scope=app, nf_id="network")
subnet = Subnet(nf_scope=stack, nf_id="subnet", vpc_id="vpc-123", cidr_block="10.0.1.0/24")

assert ResourceIntrospector.can_omit_resource(subnet, {"vpc_id"})
assert not ResourceIntrospector.can_omit_resource(subnet, {"cidr_block"})

3. Circular Attributes

get_circular_attributes_for_resource returns the arguments of a resource that refer to any of a set of resource ids (the nf_ids of the resources in a cycle):

from nf.core import App, Stack
from nf.core.phases.resource_introspection import ResourceIntrospector
from nf.providers.hashicorp.aws import Subnet, Vpc

app = App(nf_id="app")
stack = Stack(nf_scope=app, nf_id="network")
vpc = Vpc(nf_scope=stack, nf_id="vpc", cidr_block="10.0.0.0/16")
subnet = Subnet(nf_scope=stack, nf_id="subnet", vpc_id=vpc.ref_id_, cidr_block="10.0.1.0/24")

assert ResourceIntrospector.get_circular_attributes_for_resource(subnet, {"vpc"}) == {"vpc_id"}
assert ResourceIntrospector.get_circular_attributes_for_resource(subnet, {"other"}) == set()

Implementation Details

How It Works

  1. Signature analysis: examines the __init__ signature of the class.
  2. Default detection: a parameter with a default is optional, one without is required. self, scope, nf_id and provider are skipped, as are *args and **kwargs.
  3. References: a reference to a resource is found in a value, in a list or dict, and in a ${type.id.attribute} string.

Provider Agnostic Design

The introspector works with any resource class. A resource built from nf.core.Resource takes its arguments as **config, so it tells none as required:

from nf.core import App, Resource, Stack
from nf.core.phases.resource_introspection import ResourceIntrospector

app = App(nf_id="app")
stack = Stack(nf_scope=app, nf_id="custom")
custom = Resource(nf_scope=stack, nf_id="custom", nf_type="custom_resource", config={"tier": "gold"})

required, _ = ResourceIntrospector.get_required_optional_attributes(custom)
assert required == set()

Use Cases

1. Circular Dependency Resolution

The phase planner leaves a reference out of the first phase only when the argument it is in is optional (see Circular Dependency Resolution).

2. Resource Validation

Check that the arguments a resource is built with include every required one:

from nf.core import App, Stack
from nf.core.phases.resource_introspection import ResourceIntrospector
from nf.providers.hashicorp.aws import Subnet

app = App(nf_id="app")
stack = Stack(nf_scope=app, nf_id="network")


def missing_arguments(resource_class: type, provided: set[str]) -> set[str]:
    required, _ = ResourceIntrospector.get_required_optional_attributes(resource_class)
    return required - provided


assert missing_arguments(Subnet, {"cidr_block"}) == {"vpc_id"}
assert missing_arguments(Subnet, {"vpc_id", "cidr_block"}) == set()

3. Dynamic Configuration

Build a configuration from the requirements of a class:

from nf.core.phases.resource_introspection import ResourceIntrospector
from nf.providers.hashicorp.aws import Subnet


def minimal_config(resource_class: type, values: dict[str, object]) -> dict[str, object]:
    required, _ = ResourceIntrospector.get_required_optional_attributes(resource_class)
    return {name: values[name] for name in required}


assert minimal_config(Subnet, {"vpc_id": "vpc-123", "cidr_block": "10.0.1.0/24"}) == {"vpc_id": "vpc-123"}

Caching Results

required_arguments is cached per class. To cache a wider analysis, key it by class, not by instance:

from functools import lru_cache

from nf.core.phases.resource_introspection import ResourceIntrospector
from nf.providers.hashicorp.aws import Subnet, Vpc


@lru_cache(maxsize=128)
def cached_introspection(resource_class: type) -> tuple[frozenset[str], frozenset[str]]:
    required, optional = ResourceIntrospector.get_required_optional_attributes(resource_class)
    return frozenset(required), frozenset(optional)


assert cached_introspection(Subnet) is cached_introspection(Subnet)
assert "cidr_block" in cached_introspection(Vpc)[1]

Limitations

  1. Constructor analysis: it reads the class, so a class taking **config tells no required argument.
  2. Dynamic attributes: attributes added after initialization are not detected.
  3. Complex defaults: a default that depends on another argument is not detected.