|
7 | 7 |
|
8 | 8 | petname "github.com/dustinkirkland/golang-petname"
|
9 | 9 | "github.com/hashicorp/terraform-plugin-framework/diag"
|
| 10 | + "github.com/hashicorp/terraform-plugin-framework/path" |
10 | 11 | "github.com/hashicorp/terraform-plugin-framework/resource"
|
11 | 12 | "github.com/hashicorp/terraform-plugin-framework/tfsdk"
|
12 | 13 | "github.com/hashicorp/terraform-plugin-framework/types"
|
@@ -148,6 +149,31 @@ func (r *petResource) Update(ctx context.Context, req resource.UpdateRequest, re
|
148 | 149 | func (r *petResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
149 | 150 | }
|
150 | 151 |
|
| 152 | +func (r *petResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 153 | + idParts := strings.Split(req.ID, ",") |
| 154 | + if len(idParts) != 3 { |
| 155 | + resp.Diagnostics.AddError( |
| 156 | + "Unexpected Import Identifier", |
| 157 | + fmt.Sprintf("Expected import identifier with format: pet_name,separator,prefix. Got: %q", req.ID), |
| 158 | + ) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + id, separator, prefix := idParts[0], idParts[1], idParts[2] |
| 163 | + |
| 164 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), id)...) |
| 165 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("length"), int64(len(id)))...) |
| 166 | + |
| 167 | + if separator == "" { |
| 168 | + separator = "-" |
| 169 | + } |
| 170 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("separator"), separator)...) |
| 171 | + |
| 172 | + if prefix != "" { |
| 173 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("prefix"), prefix)...) |
| 174 | + } |
| 175 | +} |
| 176 | + |
151 | 177 | type petModelV0 struct {
|
152 | 178 | ID types.String `tfsdk:"id"`
|
153 | 179 | Keepers types.Map `tfsdk:"keepers"`
|
|
0 commit comments