-
Notifications
You must be signed in to change notification settings - Fork 52
Add zone metadata resource #83
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
selfuryon
wants to merge
10
commits into
pan-net:master
Choose a base branch
from
selfuryon:add_meta
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 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0f991b0
Add resource_powerdns_zone_metadata
selfuryon c427979
Fix linting and add basic tests
selfuryon 0363bd3
Change ForceNew to true in all situation
selfuryon 68b8d61
Add skips
selfuryon cd81ddd
Add tests
selfuryon 1d9a2cf
Add documentation
selfuryon 895dc0d
Merge branch 'pan-net:master' into add_meta
selfuryon 22c8b04
Remove ImportStateVerifyIgnore in tests and refactor StatusCode for
selfuryon 74f0d78
Merge branch 'master' into add_meta
selfuryon 8f1266a
Fix trim and format string
selfuryon 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
terraform-provider-dns | ||
terraform-provider-powerdns | ||
|
||
*.dll | ||
*.exe | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version: "3" | ||
version: "3.7" | ||
services: | ||
nginx: | ||
image: nginx:1.17.2 | ||
|
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
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,127 @@ | ||
package powerdns | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourcePDNSZoneMetadata() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourcePDNSZoneMetadataCreate, | ||
Read: resourcePDNSZoneMetadataRead, | ||
Delete: resourcePDNSZoneMetadataDelete, | ||
Exists: resourcePDNSZoneMetadataExists, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"zone": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"kind": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"metadata": { | ||
Type: schema.TypeSet, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourcePDNSZoneMetadataCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
zone := d.Get("zone").(string) | ||
mtdata := d.Get("metadata").(*schema.Set).List() | ||
|
||
for _, mt := range mtdata { | ||
if len(strings.Trim(mt.(string), " ")) == 0 { | ||
log.Printf("[WARN] One or more values in 'metadata' contain empty '' value(s)") | ||
} | ||
} | ||
if !(len(mtdata) > 0) { | ||
return fmt.Errorf("'metadata' must not be empty") | ||
} | ||
|
||
metadata := make([]string, 0, len(mtdata)) | ||
for _, mt := range mtdata { | ||
metadata = append(metadata, mt.(string)) | ||
} | ||
|
||
zoneMetadata := ResourceZoneMetadata{ | ||
Kind: d.Get("kind").(string), | ||
Metadata: metadata, | ||
} | ||
|
||
log.Printf("[DEBUG] Creating PowerDNS Zone Metadata: %#v", zoneMetadata) | ||
|
||
metaid, err := client.UpdateZoneMetadata(zone, zoneMetadata) | ||
if err != nil { | ||
return fmt.Errorf("Failed to create PowerDNS Zone Metadata: %s", err) | ||
} | ||
|
||
d.SetId(metaid) | ||
log.Printf("[INFO] Created PowerDNS Zone Metadata with ID: %s", d.Id()) | ||
|
||
return nil | ||
} | ||
|
||
func resourcePDNSZoneMetadataRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
log.Printf("[DEBUG] Reading PowerDNS Zone Metadata: %s", d.Id()) | ||
record, err := client.GetZoneMetadata(d.Id()) | ||
if err != nil { | ||
return fmt.Errorf("Couldn't fetch PowerDNS Zone Metadata: %s", err) | ||
} | ||
|
||
zone, _, err := parseID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(d.Id()) | ||
d.Set("kind", record.Kind) | ||
d.Set("metadata", record.Metadata) | ||
d.Set("zone", zone) | ||
|
||
return nil | ||
} | ||
|
||
func resourcePDNSZoneMetadataDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*Client) | ||
|
||
log.Printf("[INFO] Deleting PowerDNS Zone Metadata: %s", d.Id()) | ||
err := client.DeleteZoneMetadata(d.Id()) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error deleting PowerDNS Zone Metadata: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourcePDNSZoneMetadataExists(d *schema.ResourceData, meta interface{}) (bool, error) { | ||
log.Printf("[INFO] Checking existence of PowerDNS Zone Metadata: %s", d.Id()) | ||
|
||
client := meta.(*Client) | ||
exists, err := client.ZoneMetadataExists(d.Id()) | ||
|
||
if err != nil { | ||
return false, fmt.Errorf("Error checking PowerDNS Zone Metadata: %s", err) | ||
} | ||
return exists, nil | ||
} |
Oops, something went wrong.
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.
I assume here was something in single quotes but you deleted it but quotes are left.
Plus you don't break this loop if you find empty metadata.
In case we will have N objects in list empty - it will through the message N times.
I would suggest add a break after this log.
Uh oh!
There was an error while loading. Please reload this page.
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.
And also there is a function TrimSpace in strings package. We can use it instead of regular trim :)
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.
Fixed it, thanks!