|
| 1 | +package appsv1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-codegen-kubernetes/autocrud" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 13 | +) |
| 14 | + |
| 15 | +func (r *DaemonSet) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 16 | + var dataModel DaemonSetModel |
| 17 | + |
| 18 | + diag := req.Config.Get(ctx, &dataModel) |
| 19 | + resp.Diagnostics.Append(diag...) |
| 20 | + if diag.HasError() { |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + defaultTimeout, err := time.ParseDuration("20m") |
| 25 | + if err != nil { |
| 26 | + resp.Diagnostics.AddError("Error parsing timeout", err.Error()) |
| 27 | + return |
| 28 | + } |
| 29 | + timeout, diag := dataModel.Timeouts.Create(ctx, defaultTimeout) |
| 30 | + resp.Diagnostics.Append(diag...) |
| 31 | + if diag.HasError() { |
| 32 | + return |
| 33 | + } |
| 34 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 35 | + defer cancel() |
| 36 | + |
| 37 | + err = autocrud.Create(ctx, r.clientGetter, r.APIVersion, r.Kind, &dataModel) |
| 38 | + if err != nil { |
| 39 | + resp.Diagnostics.AddError("Error creating resource", err.Error()) |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + diags := resp.State.Set(ctx, &dataModel) |
| 44 | + resp.Diagnostics.Append(diags...) |
| 45 | + if resp.Diagnostics.HasError() { |
| 46 | + return |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func (r *DaemonSet) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 51 | + var dataModel DaemonSetModel |
| 52 | + |
| 53 | + diag := req.State.Get(ctx, &dataModel) |
| 54 | + resp.Diagnostics.Append(diag...) |
| 55 | + if diag.HasError() { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + defaultTimeout, err := time.ParseDuration("20m") |
| 60 | + if err != nil { |
| 61 | + resp.Diagnostics.AddError("Error parsing timeout", err.Error()) |
| 62 | + return |
| 63 | + } |
| 64 | + timeout, diag := dataModel.Timeouts.Read(ctx, defaultTimeout) |
| 65 | + resp.Diagnostics.Append(diag...) |
| 66 | + if diag.HasError() { |
| 67 | + return |
| 68 | + } |
| 69 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 70 | + defer cancel() |
| 71 | + |
| 72 | + var id string |
| 73 | + req.State.GetAttribute(ctx, path.Root("id"), &id) |
| 74 | + err = autocrud.Read(ctx, r.clientGetter, r.Kind, r.APIVersion, id, &dataModel) |
| 75 | + if err != nil { |
| 76 | + resp.Diagnostics.AddError("Error reading resource", err.Error()) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + diags := resp.State.Set(ctx, &dataModel) |
| 81 | + resp.Diagnostics.Append(diags...) |
| 82 | + if resp.Diagnostics.HasError() { |
| 83 | + return |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func (r *DaemonSet) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 88 | + var dataModel DaemonSetModel |
| 89 | + |
| 90 | + diag := req.Config.Get(ctx, &dataModel) |
| 91 | + resp.Diagnostics.Append(diag...) |
| 92 | + if diag.HasError() { |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + defaultTimeout, err := time.ParseDuration("20m") |
| 97 | + if err != nil { |
| 98 | + resp.Diagnostics.AddError("Error parsing timeout", err.Error()) |
| 99 | + return |
| 100 | + } |
| 101 | + timeout, diag := dataModel.Timeouts.Update(ctx, defaultTimeout) |
| 102 | + resp.Diagnostics.Append(diag...) |
| 103 | + if diag.HasError() { |
| 104 | + return |
| 105 | + } |
| 106 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 107 | + defer cancel() |
| 108 | + |
| 109 | + err = autocrud.Update(ctx, r.clientGetter, r.Kind, r.APIVersion, &dataModel) |
| 110 | + if err != nil { |
| 111 | + resp.Diagnostics.AddError("Error updating resource", err.Error()) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + diags := resp.State.Set(ctx, &dataModel) |
| 116 | + resp.Diagnostics.Append(diags...) |
| 117 | + if resp.Diagnostics.HasError() { |
| 118 | + return |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +func (r *DaemonSet) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 123 | + waitForDeletion := true |
| 124 | + |
| 125 | + var dataModel DaemonSetModel |
| 126 | + |
| 127 | + diag := req.State.Get(ctx, &dataModel) |
| 128 | + resp.Diagnostics.Append(diag...) |
| 129 | + if diag.HasError() { |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + defaultTimeout, err := time.ParseDuration("20m") |
| 134 | + if err != nil { |
| 135 | + resp.Diagnostics.AddError("Error parsing timeout", err.Error()) |
| 136 | + return |
| 137 | + } |
| 138 | + timeout, diag := dataModel.Timeouts.Delete(ctx, defaultTimeout) |
| 139 | + resp.Diagnostics.Append(diag...) |
| 140 | + if diag.HasError() { |
| 141 | + return |
| 142 | + } |
| 143 | + ctx, cancel := context.WithTimeout(ctx, timeout) |
| 144 | + defer cancel() |
| 145 | + |
| 146 | + err = autocrud.Delete(ctx, r.clientGetter, r.Kind, r.APIVersion, req, waitForDeletion) |
| 147 | + if err != nil { |
| 148 | + resp.Diagnostics.AddError("Error deleting resource", err.Error()) |
| 149 | + return |
| 150 | + } |
| 151 | + |
| 152 | +} |
| 153 | + |
| 154 | +func (r *DaemonSet) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 155 | + var dataModel DaemonSetModel |
| 156 | + |
| 157 | + err := autocrud.Read(ctx, r.clientGetter, r.Kind, r.APIVersion, req.ID, &dataModel) |
| 158 | + if err != nil { |
| 159 | + resp.Diagnostics.AddError("Error importing resource", err.Error()) |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + // awkward timeouts/types.Object issue https://github.com/hashicorp/terraform-plugin-framework-timeouts/issues/46 & https://github.com/hashicorp/terraform-plugin-framework/issues/716 |
| 164 | + dataModel.Timeouts = timeouts.Value{ |
| 165 | + Object: types.ObjectNull(map[string]attr.Type{ |
| 166 | + "create": types.StringType, |
| 167 | + "delete": types.StringType, |
| 168 | + "read": types.StringType, |
| 169 | + "update": types.StringType, |
| 170 | + }), |
| 171 | + } |
| 172 | + |
| 173 | + resp.Diagnostics.Append(resp.State.Set(ctx, &dataModel)...) |
| 174 | +} |
0 commit comments