|
| 1 | +package protocolspec |
| 2 | + |
| 3 | +type BridgeV2[CPS PulumiSchema, CTFS TFSchema] struct { |
| 4 | + TFProvider TFProvider[CTFS] |
| 5 | +} |
| 6 | + |
| 7 | +var _ PulumiProviderV2[PulumiSchema] = &BridgeV2[PulumiSchema, TFSchema]{} |
| 8 | + |
| 9 | +func (b *BridgeV2[CPS, CTFS]) tfStateToPulumiOutput(TFState[CTFS]) PulumiOutput[CPS] { |
| 10 | + return PulumiOutput[CPS]{} |
| 11 | +} |
| 12 | + |
| 13 | +func (b *BridgeV2[CPS, CTFS]) tfPlannedStateToPulumiPreviewOutput( |
| 14 | + TFPlannedState[CTFS], |
| 15 | +) PulumiPreviewOutput[CPS] { |
| 16 | + return PulumiPreviewOutput[CPS]{} |
| 17 | +} |
| 18 | + |
| 19 | +// This is new. |
| 20 | +func (b *BridgeV2[CPS, CTFS]) pulumiPreviewOutputToTFPlannedState(PulumiPreviewOutput[CPS]) TFPlannedState[CTFS] { |
| 21 | + return TFPlannedState[CTFS]{} |
| 22 | +} |
| 23 | + |
| 24 | +func (b *BridgeV2[CPS, CTFS]) pulumiInputToTFConfig(PulumiInput[CPS]) TFConfig[CTFS] { |
| 25 | + return TFConfig[CTFS]{} |
| 26 | +} |
| 27 | + |
| 28 | +func (b *BridgeV2[CPS, CTFS]) tfRawStateFromPulumiOutput(PulumiOutput[CPS]) TFState[CTFS] { |
| 29 | + return TFState[CTFS]{} |
| 30 | +} |
| 31 | + |
| 32 | +func (b *BridgeV2[CPS, CTFS]) Check(news PulumiInput[CPS]) PulumiInput[CPS] { |
| 33 | + return PulumiInput[CPS]{} |
| 34 | +} |
| 35 | + |
| 36 | +func (b *BridgeV2[CPS, CTFS]) CreatePreview(inputs PulumiInput[CPS]) PulumiPreviewOutput[CPS] { |
| 37 | + tfConfig := b.pulumiInputToTFConfig(inputs) |
| 38 | + emptyState := TFState[CTFS]{} |
| 39 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, emptyState) |
| 40 | + return b.tfPlannedStateToPulumiPreviewOutput(tfPlannedState) |
| 41 | +} |
| 42 | + |
| 43 | +func (b *BridgeV2[CPS, CTFS]) Create(inputs PulumiInput[CPS]) PulumiOutput[CPS] { |
| 44 | + tfConfig := b.pulumiInputToTFConfig(inputs) |
| 45 | + emptyState := TFState[CTFS]{} |
| 46 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, emptyState) |
| 47 | + tfState := b.TFProvider.ApplyResourceChange(tfConfig, emptyState, tfPlannedState) |
| 48 | + return b.tfStateToPulumiOutput(tfState) |
| 49 | +} |
| 50 | + |
| 51 | +func (b *BridgeV2[CPS, CTFS]) UpdatePreview(inputs PulumiInput[CPS], state PulumiOutput[CPS]) PulumiPreviewOutput[CPS] { |
| 52 | + tfConfig := b.pulumiInputToTFConfig(inputs) |
| 53 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 54 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, tfState) |
| 55 | + return b.tfPlannedStateToPulumiPreviewOutput(tfPlannedState) |
| 56 | +} |
| 57 | + |
| 58 | +func (b *BridgeV2[CPS, CTFS]) Update(inputs PulumiInput[CPS], state PulumiOutput[CPS]) PulumiOutput[CPS] { |
| 59 | + tfConfig := b.pulumiInputToTFConfig(inputs) |
| 60 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 61 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, tfState) |
| 62 | + tfState = b.TFProvider.ApplyResourceChange(tfConfig, tfState, tfPlannedState) |
| 63 | + return b.tfStateToPulumiOutput(tfState) |
| 64 | +} |
| 65 | + |
| 66 | +func (b *BridgeV2[CPS, CTFS]) DeletePreview(state PulumiOutput[CPS]) PulumiPreviewOutput[CPS] { |
| 67 | + tfConfig := TFConfig[CTFS]{} |
| 68 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 69 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, tfState) |
| 70 | + return b.tfPlannedStateToPulumiPreviewOutput(tfPlannedState) |
| 71 | +} |
| 72 | + |
| 73 | +func (b *BridgeV2[CPS, CTFS]) Delete(state PulumiOutput[CPS]) PulumiOutput[CPS] { |
| 74 | + tfConfig := TFConfig[CTFS]{} |
| 75 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 76 | + tfPlannedState := b.TFProvider.PlanResourceChange(tfConfig, tfState) |
| 77 | + tfState = b.TFProvider.ApplyResourceChange(tfConfig, tfState, tfPlannedState) |
| 78 | + return b.tfStateToPulumiOutput(tfState) |
| 79 | +} |
| 80 | + |
| 81 | +func (b *BridgeV2[CPS, CTFS]) Diff(oldState PulumiOutput[CPS], plannedState PulumiPreviewOutput[CPS]) (PulumiDiff[CPS], PulumiDetailedDiffV2[CPS]) { |
| 82 | + tfState := b.tfRawStateFromPulumiOutput(oldState) |
| 83 | + tfPlannedState := b.pulumiPreviewOutputToTFPlannedState(plannedState) |
| 84 | + tfDiff := b.TFProvider.Diff(tfState, tfPlannedState) |
| 85 | + |
| 86 | + translateTFDiffToPulumiDiff := func(TFDiff[CTFS]) PulumiDiff[CPS] { |
| 87 | + return PulumiDiff[CPS]{} |
| 88 | + } |
| 89 | + |
| 90 | + producePulumiDetailedDiff := func( |
| 91 | + oldState PulumiOutput[CPS], |
| 92 | + plannedState PulumiPreviewOutput[CPS], |
| 93 | + ) PulumiDetailedDiffV2[CPS] { |
| 94 | + // The values here are in the same plane - much easier to produce a diff. |
| 95 | + return PulumiDetailedDiffV2[CPS]{ |
| 96 | + OldState: oldState, |
| 97 | + plannedState: plannedState, |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + pulumiDiff := translateTFDiffToPulumiDiff(tfDiff) |
| 102 | + pulumiDetailedDiff := producePulumiDetailedDiff(oldState, plannedState) |
| 103 | + |
| 104 | + return pulumiDiff, pulumiDetailedDiff |
| 105 | +} |
| 106 | + |
| 107 | +// extractInputsFromOutputs is not precise and has some assumptions and approximations |
| 108 | +// Especially when there are no past inputs |
| 109 | +func (b *BridgeV2[CPS, CTFS]) extractInputsFromOutputsNoInputs( |
| 110 | + outputs PulumiOutput[CPS], |
| 111 | +) PulumiInput[CPS] { |
| 112 | + // guess |
| 113 | + return PulumiInput[CPS]{} |
| 114 | +} |
| 115 | + |
| 116 | +func (b *BridgeV2[CPS, CTFS]) ReadForImport(id string) (PulumiOutput[CPS], PulumiInput[CPS]) { |
| 117 | + tfState := b.TFProvider.Importer(id) |
| 118 | + state := b.tfStateToPulumiOutput(tfState) |
| 119 | + inputs := b.extractInputsFromOutputsNoInputs(state) |
| 120 | + return state, inputs |
| 121 | +} |
| 122 | + |
| 123 | +// The only method which needs to deal with old schema is UpgradeState. |
| 124 | +type BridgeV2WithUpgrade[CPS PulumiSchema, PS PulumiSchema, CTFS TFSchema, PTS TFSchema] struct { |
| 125 | + BridgeV2[CPS, CTFS] |
| 126 | + TFProvider TFProviderWithUpgradeState[CTFS, PTS] |
| 127 | +} |
| 128 | + |
| 129 | +var _ PulumiProviderV2WithUpgrade[PulumiSchema, PulumiSchema] = &BridgeV2WithUpgrade[PulumiSchema, PulumiSchema, TFSchema, TFSchema]{} |
| 130 | + |
| 131 | +func (b *BridgeV2WithUpgrade[CPS, PS, CTFS, PTS]) UpgradeState(state PulumiOutput[PS]) PulumiOutput[CPS] { |
| 132 | + return PulumiOutput[CPS]{} |
| 133 | +} |
| 134 | + |
| 135 | +// extractInputsFromOutputs is not precise and has some assumptions and approximations |
| 136 | +// Especially when there are no past inputs |
| 137 | +func (b *BridgeV2WithUpgrade[CPS, PS, CTFS, PTS]) extractInputsFromOutputs( |
| 138 | + oldInputs PulumiInput[PS], |
| 139 | + outputs PulumiOutput[CPS], |
| 140 | +) PulumiInput[CPS] { |
| 141 | + return PulumiInput[CPS]{} |
| 142 | +} |
| 143 | + |
| 144 | +func (b *BridgeV2WithUpgrade[CPS, PS, CTFS, PTS]) ReadForRefresh(inputs PulumiInput[PS], state PulumiOutput[CPS]) (PulumiInput[CPS], PulumiOutput[CPS]) { |
| 145 | + tfState := b.tfRawStateFromPulumiOutput(state) |
| 146 | + tfNewState := b.TFProvider.ReadResource(tfState) |
| 147 | + newState := b.tfStateToPulumiOutput(tfNewState) |
| 148 | + newInputs := b.extractInputsFromOutputs(inputs, newState) |
| 149 | + return newInputs, newState |
| 150 | +} |
0 commit comments