-
Notifications
You must be signed in to change notification settings - Fork 53
Introducing data source for PTR record generation #94
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
Open
johannwagner
wants to merge
1
commit into
pan-net:master
Choose a base branch
from
johannwagner:feature/powerdns-ptr-datasource
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package powerdns | ||
|
||
import ( | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"net" | ||
"strings" | ||
) | ||
|
||
func expandIPv6Address(ip net.IP) string { | ||
b := make([]byte, 0, len(ip)) | ||
|
||
// Print with possible :: in place of run of zeros | ||
for i := 0; i < len(ip); i += 2 { | ||
if i > 0 { | ||
b = append(b, ':') | ||
} | ||
s := (uint32(ip[i]) << 8) | uint32(ip[i+1]) | ||
bHex := fmt.Sprintf("%04x", s) | ||
b = append(b, bHex...) | ||
} | ||
return string(b) | ||
} | ||
|
||
func resourcePDNSPTR() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: resourcePDNSPTRRead, | ||
Schema: map[string]*schema.Schema{ | ||
"ip_address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"ptr_address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourcePDNSPTRRead(d *schema.ResourceData, meta interface{}) error { | ||
ipAddressStr := d.Get("ip_address").(string) | ||
|
||
ipAddress := net.ParseIP(ipAddressStr) | ||
if ipAddress == nil { | ||
return fmt.Errorf("%v is not a valid IP address", ipAddressStr) | ||
} | ||
|
||
d.SetId(ipAddressStr) | ||
|
||
ipAddress4 := ipAddress.To4() | ||
|
||
if ipAddress4 != nil { | ||
// IPv4 | ||
addressStringSplitted := strings.Split(ipAddress4.String(), ".") | ||
reverseAddressParts := make([]string, 0) | ||
for i := len(addressStringSplitted) - 1; i >= 0; i-- { | ||
reverseAddressParts = append(reverseAddressParts, addressStringSplitted[i]) | ||
} | ||
reverseAddress := strings.Join(reverseAddressParts, ".") | ||
|
||
ptrRecord := fmt.Sprintf("%v.in-addr.arpa.", reverseAddress) | ||
return d.Set("ptr_address", ptrRecord) | ||
} | ||
|
||
expandedAddress := expandIPv6Address(ipAddress) | ||
|
||
addressStringSplitted := strings.Split(strings.ReplaceAll(expandedAddress, ":", ""), "") | ||
reverseAddressParts := []string{} | ||
for i := len(addressStringSplitted) - 1; i >= 0; i-- { | ||
reverseAddressParts = append(reverseAddressParts, addressStringSplitted[i]) | ||
} | ||
reverseAddress := strings.Join(reverseAddressParts, ".") | ||
|
||
ptrRecord := fmt.Sprintf("%v.ip6.arpa.", reverseAddress) | ||
return d.Set("ptr_address", ptrRecord) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package powerdns | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccPDNSPTR_v4(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testPDNSPtrConfig4, | ||
Check: resource.TestCheckResourceAttr("data.powerdns_ptr.test4", "ptr_address", "4.3.2.1.in-addr.arpa."), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccPDNSPTR_v6(t *testing.T) { | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testPDNSPtrConfig6, | ||
Check: resource.TestCheckResourceAttr("data.powerdns_ptr.test6", "ptr_address", "1.1.b.6.f.a.d.1.5.e.6.7.c.0.6.d.5.9.2.0.0.c.a.8.8.0.1.8.2.0.a.2.ip6.arpa."), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testPDNSPtrConfig4 = ` | ||
data "powerdns_ptr" "test4" { | ||
ip_address = "1.2.3.4" | ||
}` | ||
|
||
const testPDNSPtrConfig6 = ` | ||
data "powerdns_ptr" "test6" { | ||
ip_address = "2a02:8108:8ac0:295:d60c:76e5:1daf:6b11" | ||
}` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not need to use append. We know what is the length of ipv4 and ipv6 address (I would not even assert on that, as you can trust net.Parse to work if there was no error), so array can be preallocated with exact size, and reversing done without append. (there is more you can do, but that might be overkill here).
Also in the IPv4 you use
make
, but in IPv6 you don't.