Skip to content

Commit

Permalink
internal/network: Add network peer import
Browse files Browse the repository at this point in the history
Signed-off-by: Din Music <[email protected]>
  • Loading branch information
MusicDin committed Aug 2, 2024
1 parent 2baa007 commit 654a68d
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions internal/network/resource_network_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/canonical/lxd/shared/api"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
Expand Down Expand Up @@ -361,3 +362,30 @@ func (r NetworkPeerResource) SyncState(ctx context.Context, tfState *tfsdk.State

return tfState.Set(ctx, &m)
}

func (r NetworkPeerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
meta := common.ImportMetadata{
ResourceName: "network_peer",
RequiredFields: []string{
"name",
"source_project",
"source_network",
"target_project",
"target_network",
},
}

fields, diag := meta.ParseImportID(req.ID)
if diag != nil {
resp.Diagnostics.Append(diag)
return
}

// Remove project field because we are extracting source and target
// projects as required fields.
delete(fields, "project")

for k, v := range fields {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root(k), v)...)
}
}

0 comments on commit 654a68d

Please sign in to comment.