Skip to content

Commit

Permalink
Merge pull request #88 from peterbaumert/patch-1
Browse files Browse the repository at this point in the history
Add Section and Vlan import feature
  • Loading branch information
pavel-z1 authored May 15, 2024
2 parents e8e7cb6 + 7b563af commit 118f086
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
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

0 comments on commit 118f086

Please sign in to comment.