|
| 1 | +# Migration from Plugin SDK v2 to Plugin Framework |
| 2 | + |
| 3 | +## Before (SDK v2) - Complex Type Parsing |
| 4 | + |
| 5 | +```go |
| 6 | +// From resource_cluster.go initialiseMinikubeClient function (lines 235-343) |
| 7 | +func initialiseMinikubeClient(d *schema.ResourceData, m interface{}) (lib.ClusterClient, error) { |
| 8 | + // Manual type assertions with runtime panic risk |
| 9 | + driver := d.Get("driver").(string) |
| 10 | + containerRuntime := d.Get("container_runtime").(string) |
| 11 | + |
| 12 | + // Complex null checking and type casting |
| 13 | + addons, ok := d.GetOk("addons") |
| 14 | + if !ok { |
| 15 | + addons = &schema.Set{} |
| 16 | + } |
| 17 | + addonStrings := state_utils.SetToSlice(addons.(*schema.Set)) |
| 18 | + |
| 19 | + // More manual type assertions |
| 20 | + defaultIsos, ok := d.GetOk("iso_url") |
| 21 | + if !ok { |
| 22 | + defaultIsos = []string{defaultIso} |
| 23 | + } |
| 24 | + |
| 25 | + // Repeated patterns for every field |
| 26 | + hyperKitSockPorts, ok := d.GetOk("hyperkit_vsock_ports") |
| 27 | + if !ok { |
| 28 | + hyperKitSockPorts = []string{} |
| 29 | + } |
| 30 | + |
| 31 | + // String conversion with error handling |
| 32 | + memoryStr := d.Get("memory").(string) |
| 33 | + memoryMb, err := state_utils.GetMemory(memoryStr) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + // More of the same... |
| 39 | + cpuStr := d.Get("cpus").(string) |
| 40 | + cpus, err := state_utils.GetCPUs(cpuStr) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + // Set handling with length checks |
| 46 | + apiserverNames := []string{} |
| 47 | + if d.Get("apiserver_names").(*schema.Set).Len() > 0 { |
| 48 | + apiserverNames = state_utils.ReadSliceState(d.Get("apiserver_names")) |
| 49 | + } |
| 50 | + |
| 51 | + // 100+ more lines of similar manual parsing... |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## After (Plugin Framework) - Type-Safe Structured Access |
| 56 | + |
| 57 | +```go |
| 58 | +// From new resource_cluster.go createMinikubeClient function |
| 59 | +func (r *ClusterResource) createMinikubeClient(ctx context.Context, data *ClusterResourceModel) (lib.ClusterClient, error) { |
| 60 | + // Type-safe field access - no casting needed! |
| 61 | + driver := data.Driver.ValueString() |
| 62 | + containerRuntime := data.ContainerRuntime.ValueString() |
| 63 | + |
| 64 | + // Clean null checking and type-safe extraction |
| 65 | + var addons []string |
| 66 | + if !data.Addons.IsNull() { |
| 67 | + data.Addons.ElementsAs(ctx, &addons, false) |
| 68 | + } |
| 69 | + |
| 70 | + // Simple and clean |
| 71 | + var isoURLs []string |
| 72 | + if !data.IsoURL.IsNull() { |
| 73 | + data.IsoURL.ElementsAs(ctx, &isoURLs, false) |
| 74 | + } else { |
| 75 | + isoURLs = []string{defaultIso} |
| 76 | + } |
| 77 | + |
| 78 | + // Type-safe numeric conversions |
| 79 | + memoryMb, err := state_utils.GetMemory(data.Memory.ValueString()) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + cpus, err := state_utils.GetCPUs(data.CPUs.ValueString()) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + // Simple set handling |
| 90 | + var apiServerNames []string |
| 91 | + if !data.APIServerNames.IsNull() { |
| 92 | + data.APIServerNames.ElementsAs(ctx, &apiServerNames, false) |
| 93 | + } |
| 94 | + |
| 95 | + // Much cleaner and less error-prone! |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Key Improvements |
| 100 | + |
| 101 | +### 1. Type Safety |
| 102 | +- **Before**: `d.Get("driver").(string)` - runtime panic risk |
| 103 | +- **After**: `data.Driver.ValueString()` - compile-time safety |
| 104 | + |
| 105 | +### 2. Null Checking |
| 106 | +- **Before**: `addons, ok := d.GetOk("addons"); if !ok { ... }` |
| 107 | +- **After**: `if !data.Addons.IsNull() { ... }` |
| 108 | + |
| 109 | +### 3. Set Handling |
| 110 | +- **Before**: `addons.(*schema.Set)` + manual conversion |
| 111 | +- **After**: `data.Addons.ElementsAs(ctx, &addons, false)` |
| 112 | + |
| 113 | +### 4. Code Reduction |
| 114 | +- **Before**: ~100 lines of repetitive type parsing |
| 115 | +- **After**: ~50 lines of clean, type-safe code |
| 116 | + |
| 117 | +### 5. Error Prevention |
| 118 | +- No more runtime panics from failed type assertions |
| 119 | +- Better validation built into the framework |
| 120 | +- Cleaner error messages for users |
| 121 | + |
| 122 | +This migration successfully addresses the issue's request to eliminate "weird type parsing" and adopt HashiCorp's preferred Plugin Framework. |
0 commit comments