Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Section and Vlan import feature #88

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion plugin/providers/phpipam/data_source_phpipam_section.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion plugin/providers/phpipam/data_source_phpipam_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down