Skip to content

Commit

Permalink
feat: add import via address for ip resources (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikadix authored Jan 9, 2025
1 parent 6e30909 commit 921caf7
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pdzsvmnkeyne
begv003ejaf9
21 changes: 20 additions & 1 deletion internal/v6provider/resource_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,26 @@ func (r *IPResource) ImportState(
req resource.ImportStateRequest,
resp *resource.ImportStateResponse,
) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
if strings.HasPrefix(req.ID, "ip_") {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
return
}

res, err := r.M.Core.GetIpAddressWithResponse(ctx, &core.GetIpAddressParams{
IpAddressAddress: &req.ID,
})
if err != nil {
resp.Diagnostics.AddError("IP Address Import Error", err.Error())
return
}

resp.Diagnostics.Append(
resp.State.SetAttribute(
ctx,
path.Root("id"),
*res.JSON200.IpAddress.Id,
)...,
)
}

func unflattenIPVersion(ver int64) core.IPAddressVersionEnum {
Expand Down
13 changes: 13 additions & 0 deletions internal/v6provider/resource_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ func TestAccKatapultIP_minimal(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "katapult_ip.web",
ImportStateIdFunc: func(s *terraform.State) (string, error) {
rs := s.RootModule().Resources["katapult_ip.web"]
if rs == nil {
return "", fmt.Errorf("resource not found")
}

return rs.Primary.Attributes["address"], nil
},
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down
Loading

0 comments on commit 921caf7

Please sign in to comment.