|
| 1 | +package protocolspec |
| 2 | + |
| 3 | +// CPS stands for Current Pulumi Schema |
| 4 | +// PPS stands for Previous Pulumi Schema |
| 5 | +// CTFS stands for Current TF Schema |
| 6 | +// PTFS stands for Previous TF Schema |
| 7 | +type Bridge[CPS PulumiSchema, PPS PulumiSchema, CTFS TFSchema, PTFS TFSchema] struct { |
| 8 | + TFProvider TFProviderWithUpgradeState[CTFS, PTFS] |
| 9 | +} |
| 10 | + |
| 11 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) getCurrentBridge() Bridge[CPS, CPS, CTFS, CTFS] { |
| 12 | + return Bridge[CPS, CPS, CTFS, CTFS]{ |
| 13 | + TFProvider: b.TFProvider.GetCurrentProvider(), |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) tfStateToPulumiOutput(TFState[CTFS]) PulumiOutput[CPS] { |
| 18 | + return PulumiOutput[CPS]{} |
| 19 | +} |
| 20 | + |
| 21 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) tfPlannedStateToPulumiPreviewOutput( |
| 22 | + TFPlannedState[CTFS], |
| 23 | +) PulumiPreviewOutput[CPS] { |
| 24 | + return PulumiPreviewOutput[CPS]{} |
| 25 | +} |
| 26 | + |
| 27 | +// This was recently replaced by tfRawStateFromPulumiOutput |
| 28 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) pulumiOutputToTFState(PulumiOutput[CPS]) TFState[CTFS] { |
| 29 | + return TFState[CTFS]{} |
| 30 | +} |
| 31 | + |
| 32 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) pulumiCheckedInputToTFConfig(PulumiCheckedInput[CPS]) TFConfig[CTFS] { |
| 33 | + return TFConfig[CTFS]{} |
| 34 | +} |
| 35 | + |
| 36 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) tfRawStateFromPulumiOutput(PulumiOutput[PPS]) TFState[PTFS] { |
| 37 | + return TFState[PTFS]{} |
| 38 | +} |
| 39 | + |
| 40 | +// extractInputsFromOutputs is not precise and has some assumptions and approximations |
| 41 | +// Especially when there are no past inputs |
| 42 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) extractInputsFromOutputs( |
| 43 | + oldInputs PulumiCheckedInput[PPS], |
| 44 | + outputs PulumiOutput[CPS], |
| 45 | +) PulumiInput[CPS] { |
| 46 | + return PulumiInput[CPS]{} |
| 47 | +} |
| 48 | + |
| 49 | +var _ PulumiProvider[PulumiSchema, PulumiSchema] = &Bridge[PulumiSchema, PulumiSchema, TFSchema, TFSchema]{} |
| 50 | + |
| 51 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) Check( |
| 52 | + news PulumiInput[CPS], olds PulumiCheckedInput[PPS], |
| 53 | +) PulumiCheckedInput[CPS] { |
| 54 | + return PulumiCheckedInput[CPS]{} |
| 55 | +} |
| 56 | + |
| 57 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) CreatePreview(checkedInputs PulumiCheckedInput[CPS]) PulumiPreviewOutput[CPS] { |
| 58 | + tfConfig := b.pulumiCheckedInputToTFConfig(checkedInputs) |
| 59 | + emptyState := TFState[CTFS]{} |
| 60 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, emptyState) |
| 61 | + return b.tfPlannedStateToPulumiPreviewOutput(tfPlannedState) |
| 62 | +} |
| 63 | + |
| 64 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) Create(checkedInputs PulumiCheckedInput[CPS]) PulumiOutput[CPS] { |
| 65 | + tfConfig := b.pulumiCheckedInputToTFConfig(checkedInputs) |
| 66 | + emptyState := TFState[CTFS]{} |
| 67 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, emptyState) |
| 68 | + tfState := b.TFProvider.ApplyResourceChange(tfConfig, emptyState, tfPlannedState) |
| 69 | + return b.tfStateToPulumiOutput(tfState) |
| 70 | +} |
| 71 | + |
| 72 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) UpdatePreview( |
| 73 | + checkedInputs PulumiCheckedInput[CPS], |
| 74 | + state PulumiOutput[PPS], |
| 75 | + // oldInputs is unused |
| 76 | + _oldInputs PulumiCheckedInput[PPS], |
| 77 | +) PulumiPreviewOutput[CPS] { |
| 78 | + tfConfig := b.pulumiCheckedInputToTFConfig(checkedInputs) |
| 79 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 80 | + tfUpgradedState := b.TFProvider.UpgradeState(tfState) |
| 81 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, tfUpgradedState) |
| 82 | + return b.tfPlannedStateToPulumiPreviewOutput(tfPlannedState) |
| 83 | +} |
| 84 | + |
| 85 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) Update( |
| 86 | + checkedInputs PulumiCheckedInput[CPS], |
| 87 | + state PulumiOutput[PPS], |
| 88 | + // oldInputs is unused |
| 89 | + _oldInputs PulumiCheckedInput[PPS], |
| 90 | +) PulumiOutput[CPS] { |
| 91 | + tfConfig := b.pulumiCheckedInputToTFConfig(checkedInputs) |
| 92 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 93 | + tfUpgradedState := b.TFProvider.UpgradeState(tfState) |
| 94 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, tfUpgradedState) |
| 95 | + tfNewState := b.TFProvider.ApplyResourceChange(tfConfig, tfUpgradedState, tfPlannedState) |
| 96 | + return b.tfStateToPulumiOutput(tfNewState) |
| 97 | +} |
| 98 | + |
| 99 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) DeletePreview(state PulumiOutput[PPS]) { |
| 100 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 101 | + upgradedState := b.TFProvider.UpgradeState(tfState) |
| 102 | + emptyConfig := TFConfig[CTFS]{} |
| 103 | + b.TFProvider.PlanResourceChange(emptyConfig, upgradedState) |
| 104 | +} |
| 105 | + |
| 106 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) Delete(state PulumiOutput[PPS]) { |
| 107 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 108 | + upgradedState := b.TFProvider.UpgradeState(tfState) |
| 109 | + emptyConfig := TFConfig[CTFS]{} |
| 110 | + b.TFProvider.ApplyResourceChange(emptyConfig, upgradedState, TFPlannedState[CTFS]{}) |
| 111 | +} |
| 112 | + |
| 113 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) Diff( |
| 114 | + oldState PulumiOutput[PPS], |
| 115 | + oldInputs PulumiCheckedInput[PPS], |
| 116 | + newCheckedInputs PulumiCheckedInput[CPS], |
| 117 | +) (PulumiDiff[CPS], PulumiDetailedDiff[CPS, PPS]) { |
| 118 | + tfState := b.tfRawStateFromPulumiOutput(oldState) |
| 119 | + tfUpgradedState := b.TFProvider.UpgradeState(tfState) |
| 120 | + tfConfig := b.pulumiCheckedInputToTFConfig(newCheckedInputs) |
| 121 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, tfUpgradedState) |
| 122 | + plannedState := b.tfPlannedStateToPulumiPreviewOutput(tfPlannedState) |
| 123 | + |
| 124 | + tfDiff := b.TFProvider.Diff(tfUpgradedState, tfPlannedState) |
| 125 | + |
| 126 | + translateTFDiffToPulumiDiff := func(TFDiff[CTFS]) PulumiDiff[CPS] { |
| 127 | + return PulumiDiff[CPS]{} |
| 128 | + } |
| 129 | + |
| 130 | + producePulumiDetailedDiff := func( |
| 131 | + _ TFDiff[CTFS], |
| 132 | + oldState PulumiOutput[PPS], |
| 133 | + newCheckedInputs PulumiCheckedInput[CPS], |
| 134 | + _ PulumiPreviewOutput[CPS], |
| 135 | + ) PulumiDetailedDiff[CPS, PPS] { |
| 136 | + // This is not easy as we first need to diff PulumiOutput[PPS] against PulumiPreviewOutput[CPS] |
| 137 | + // and then we need to map this back to the PulumiCheckedInput[CPS] |
| 138 | + // PulumiPreviewOutput and PulumiCheckedInput are not directly comparable |
| 139 | + return PulumiDetailedDiff[CPS, PPS]{ |
| 140 | + oldState: oldState, |
| 141 | + newCheckedInputs: newCheckedInputs, |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + pulumiDiff := translateTFDiffToPulumiDiff(tfDiff) |
| 146 | + pulumiDetailedDiff := producePulumiDetailedDiff(tfDiff, oldState, newCheckedInputs, plannedState) |
| 147 | + |
| 148 | + return pulumiDiff, pulumiDetailedDiff |
| 149 | +} |
| 150 | + |
| 151 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) ReadForImport(id string) (PulumiOutput[CPS], PulumiInput[CPS]) { |
| 152 | + tfState := b.TFProvider.Importer(id) |
| 153 | + state := b.tfStateToPulumiOutput(tfState) |
| 154 | + inputs := b.extractInputsFromOutputs(PulumiCheckedInput[PPS]{}, state) |
| 155 | + return state, inputs |
| 156 | +} |
| 157 | + |
| 158 | +func (b *Bridge[CPS, PPS, CTFS, PTFS]) ReadForRefresh( |
| 159 | + inputs PulumiCheckedInput[PPS], state PulumiOutput[PPS], |
| 160 | +) (PulumiInput[CPS], PulumiOutput[CPS]) { |
| 161 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 162 | + tfUpgradedState := b.TFProvider.UpgradeState(tfState) |
| 163 | + |
| 164 | + tfNewState := b.TFProvider.ReadResource(tfUpgradedState) |
| 165 | + |
| 166 | + newPulumiState := b.tfStateToPulumiOutput(tfNewState) |
| 167 | + |
| 168 | + newPulumiInputs := b.extractInputsFromOutputs(inputs, newPulumiState) |
| 169 | + return newPulumiInputs, newPulumiState |
| 170 | +} |
0 commit comments