diff --git a/plugin/providers/phpipam/data_source_phpipam_section.go b/plugin/providers/phpipam/data_source_phpipam_section.go index ec03acd6..5c9674dc 100644 --- a/plugin/providers/phpipam/data_source_phpipam_section.go +++ b/plugin/providers/phpipam/data_source_phpipam_section.go @@ -4,6 +4,7 @@ import ( "errors" "log" "strings" + "strconv" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/pavel-z1/phpipam-sdk-go/controllers/sections" @@ -42,7 +43,21 @@ func dataSourcePHPIPAMSectionRead(d *schema.ResourceData, meta interface{}) erro return err } default: - return errors.New("section_id or name not defined, cannot proceed with reading data") + // We need to ensure imported resources are not recreated when terraform apply is ran + // imported resources only have an Id which we need to map back to section_id + id := d.Id() + if len(id) > 0 { + section_id, err := strconv.Atoi(id) + if err != nil { + return err + } + out, err = c.GetSectionByID(section_id) + if err != nil { + return err + } + } else { + return errors.New("section_id or name not defined, cannot proceed with reading data") + } } flattenSection(out, d) return nil diff --git a/plugin/providers/phpipam/data_source_phpipam_vlan.go b/plugin/providers/phpipam/data_source_phpipam_vlan.go index abeb5f1e..23685932 100644 --- a/plugin/providers/phpipam/data_source_phpipam_vlan.go +++ b/plugin/providers/phpipam/data_source_phpipam_vlan.go @@ -2,6 +2,7 @@ package phpipam import ( "errors" + "strconv" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/pavel-z1/phpipam-sdk-go/controllers/vlans" @@ -47,7 +48,21 @@ func dataSourcePHPIPAMVLANRead(d *schema.ResourceData, meta interface{}) error { } out = v[0] default: - return errors.New("vlan_id or number not defined, cannot proceed with reading data") + // We need to ensure imported resources are not recreated when terraform apply is ran + // imported resources only have an Id which we need to map back to vlan_id + id := d.Id() + if len(id) > 0 { + vlan_id, err := strconv.Atoi(id) + if err != nil { + return err + } + out, err = c.GetVLANByID(vlan_id) + if err != nil { + return err + } + } else { + return errors.New("vlan_id or number not defined, cannot proceed with reading data") + } } if checkVlansCustomFiledsExists(d, c) {