Class: Pangea::Resources::BaseAttributes

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/pangea/resources/base_attributes.rb

Overview

Base class for all provider resource attribute structs.

Provides:

  • transform_keys(&:to_sym) so hash keys are normalized

  • T constant aliasing Resources::Types for short, unambiguous type references

All provider attribute classes should inherit from this:

class VpcAttributes < Pangea::Resources::BaseAttributes
  attribute :cidr_block, T::CidrBlock
  attribute :tags, T::AwsTags.default({}.freeze)
end

Constant Summary collapse

T =

Short alias for Pangea::Resources::Types — works inside class bodies because it’s a real constant (not const_missing-based)

Pangea::Resources::Types
TERRAFORM_REF_PATTERN =

Terraform reference pattern — matches $… interpolation syntax. Use in self.new validators to skip format checks on values that are terraform references (they will be resolved at plan/apply time).

/\$\{.*\}/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reference_patternObject

Override in subclasses to use a different reference pattern (e.g., Pulumi uses different interpolation syntax).



38
39
40
# File 'lib/pangea/resources/base_attributes.rb', line 38

def self.reference_pattern
  TERRAFORM_REF_PATTERN
end

.terraform_reference?(value) ⇒ Boolean

Returns true if the value contains a terraform/HCL interpolation reference. Works for both class-level (self.terraform_reference?) and instance-level usage. Uses the overridable reference_pattern so providers can customize detection.

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/pangea/resources/base_attributes.rb', line 45

def self.terraform_reference?(value)
  return false unless value.is_a?(String)

  value.match?(reference_pattern)
end

Instance Method Details

#copy_with(changes = {}) ⇒ Object

Create a copy with merged attributes. Uses Dry::Struct’s load method to bypass custom self.new overrides, preventing infinite recursion when copy_with is called inside validators.



69
70
71
72
# File 'lib/pangea/resources/base_attributes.rb', line 69

def copy_with(changes = {})
  merged = to_h.merge(changes.transform_keys(&:to_sym))
  self.class.load(merged)
end

#terraform_ref_or(attr_name) ⇒ Object

Yields the attribute value if it’s NOT a terraform reference, otherwise returns the raw reference string. Useful when a resource needs to compute/transform a value but must pass through $… refs.

terraform_ref_or(:cidr_block) { |v| calculate_something(v) }


61
62
63
64
# File 'lib/pangea/resources/base_attributes.rb', line 61

def terraform_ref_or(attr_name)
  val = public_send(attr_name)
  terraform_reference?(val) ? val : yield(val)
end

#terraform_reference?(value) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/pangea/resources/base_attributes.rb', line 51

def terraform_reference?(value)
  self.class.terraform_reference?(value)
end