Class: Pangea::Magma::Migration::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/pangea/magma/migration.rb

Overview

Typed migration plan — returned by Migration#plan; consumed by ‘magma migrate` subprocess + dry-run inspection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migration:, actions:, would_recreate: [], warnings: []) ⇒ Plan

Returns a new instance of Plan.



51
52
53
54
55
56
# File 'lib/pangea/magma/migration.rb', line 51

def initialize(migration:, actions:, would_recreate: [], warnings: [])
  @migration      = migration
  @actions        = actions
  @would_recreate = would_recreate
  @warnings       = warnings
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



49
50
51
# File 'lib/pangea/magma/migration.rb', line 49

def actions
  @actions
end

#migrationObject (readonly)

Returns the value of attribute migration.



49
50
51
# File 'lib/pangea/magma/migration.rb', line 49

def migration
  @migration
end

#warningsObject (readonly)

Returns the value of attribute warnings.



49
50
51
# File 'lib/pangea/magma/migration.rb', line 49

def warnings
  @warnings
end

#would_recreateObject (readonly)

Returns the value of attribute would_recreate.



49
50
51
# File 'lib/pangea/magma/migration.rb', line 49

def would_recreate
  @would_recreate
end

Instance Method Details

#apply!Object

Apply the typed Plan via ‘magma migrate <plan.json>`. Returns the parsed `MigrationReceipt` Hash on success (with BLAKE3 state-file hashes pre/post per theory/PANGEA-MAGMA-ORCHESTRATION.md §V). Raises on either invariant violation or magma exit≠0.

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pangea/magma/migration.rb', line 76

def apply!
  raise InvariantViolation,
        "would recreate: #{@would_recreate.inspect}" unless invariants_satisfied?

  unless @migration.from_state_path
    raise ArgumentError,
          "Migration#apply! requires from_state_path — declare with " \
          "`from_state_path: 'workspaces/<src>/terraform.tfstate'` " \
          "(see theory/PANGEA-MAGMA-ORCHESTRATION.md §V)"
  end
  unless @migration.to_state_path
    raise ArgumentError,
          "Migration#apply! requires to_state_path — declare with " \
          "`to_state_path: 'workspaces/<dst>/terraform.tfstate'`"
  end

  # Render typed MigrationPlan JSON that `magma migrate` consumes
  # directly (the on-disk shape of magma_migrate::MigrationPlan)
  # and dispatch through the shared Runner. The Runner owns
  # tempfile cleanup + error capture.
  Runner.invoke('migrate', json_arg: {
    from: { name: @migration.from.to_s, state_path: @migration.from_state_path },
    to:   { name: @migration.to.to_s,   state_path: @migration.to_state_path   },
    moves: @actions.map { |a| { source_address: a.address, target_address: a.new_address } },
    preserve: {
      resource_identity:    @migration.preserve.include?(:resource_identity),
      tags:                 @migration.preserve.include?(:tags),
      dependent_resources:  @migration.preserve.include?(:dependent_resources),
    },
    dry_run: @migration.dry_run,
  })
end

#invariants_satisfied?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/pangea/magma/migration.rb', line 58

def invariants_satisfied?
  @would_recreate.empty?
end

#summaryObject



62
63
64
65
66
67
68
69
# File 'lib/pangea/magma/migration.rb', line 62

def summary
  lines = []
  lines << "Migration: #{@migration.from}#{@migration.to}"
  lines << "  #{@actions.size} action(s)"
  lines << "  recreates: #{@would_recreate.size}" if @would_recreate.any?
  lines << "  warnings: #{@warnings.size}" if @warnings.any?
  lines.join("\n")
end

#to_hObject



109
110
111
112
113
114
115
116
# File 'lib/pangea/magma/migration.rb', line 109

def to_h
  {
    migration: @migration.to_h,
    actions:   @actions.map(&:to_h),
    would_recreate: @would_recreate,
    warnings:  @warnings,
  }
end

#to_json(*args) ⇒ Object



118
119
120
# File 'lib/pangea/magma/migration.rb', line 118

def to_json(*args)
  to_h.to_json(*args)
end