-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for ZoneDelegated object (#379)
--------- Co-authored-by: Neil Garratt <[email protected]> Co-authored-by: Piper Dougherty <[email protected]>
- Loading branch information
1 parent
05ca000
commit 91a36f4
Showing
11 changed files
with
1,202 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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 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,40 @@ | ||
# Resource Zone Delegated | ||
|
||
A Zone Delegated resource creates NS records for a subdomain, pointing to one or more external authoritative name servers. The `infoblox_zone_delegated` resource allow managing such delegations. The parent zone must already exist | ||
|
||
The following list describes the parameters you can define in the `infoblox_zone_delegated` resource block: | ||
|
||
## Argument Reference | ||
* `fqdn`: (Required) The subdomain name to be delegated | ||
* `delegate_to`: (Required) Nested block(s)s for the delegated name servers | ||
* `name`: (Required) The FQDN of the name server | ||
* `ext_attrs`: (Optional) A set of NIOS extensible attributes that are attached to the record, using jsonencode. Currently only "Tenant ID" is supported | ||
|
||
## Attribute Reference | ||
* `delegate_to`: | ||
* `address`: The computed IP address for each delegated name server | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "infoblox_zone_delegated" "subdomain" { | ||
fqdn = "subdomain.test.com" | ||
delegate_to { | ||
name = "ns-1488.awsdns-58.org" | ||
} | ||
delegate_to { | ||
name = "ns-2034.awsdns-62.co.uk" | ||
} | ||
} | ||
``` | ||
|
||
## Import | ||
Zone Delegated resources can be imported by using either the object reference or the subdomain fqdn, for example: | ||
```shell script | ||
# terraform import infoblox_zone_delegated.subdomain zone_delegated/ZG5zLnpvbmUkLl9kZWZhdWx0LmNvbS5jb2xsZWdlY2hvaWNldHJhbnNpdGlvbi5nc2xi:subdomain.test.com/default | ||
# terraform import infoblox_zone_delegated.subdomain subdomain.test.com | ||
``` |
This file contains 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,13 @@ | ||
# Zone Delegated | ||
|
||
resource "infoblox_zone_delegated" "subdomain" { | ||
fqdn = "subdomain.example.com" | ||
delegate_to { | ||
name = "ns-1488.awsdns-58.org" | ||
address = "10.1.1.1" | ||
} | ||
delegate_to { | ||
name = "ns-2034.awsdns-62.co.uk" | ||
address = "10.10.1.1" | ||
} | ||
} |
This file contains 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,201 @@ | ||
package infoblox | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
ibclient "github.com/infobloxopen/infoblox-go-client/v2" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
func dataSourceZoneDelegated() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceZoneDelegatedRead, | ||
Schema: map[string]*schema.Schema{ | ||
"filters": { | ||
Type: schema.TypeMap, | ||
Required: true, | ||
}, | ||
"results": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "List of Forward Zones matching filters", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"fqdn": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The FQDN of the delegated zone.", | ||
}, | ||
"delegate_to": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Description: "The Infoblox appliance redirects queries for data for the delegated zone to this remote name server.", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"address": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The IPv4 Address or IPv6 Address of the server.", | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "A resolvable domain name for the external DNS server.", | ||
}, | ||
}, | ||
}, | ||
}, | ||
"comment": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "A descriptive comment.", | ||
}, | ||
"disable": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Description: "Determines if the zone is disabled or not.", | ||
}, | ||
"locked": { | ||
Type: schema.TypeBool, | ||
Optional: true, | ||
Default: false, | ||
Description: "If you enable this flag, other administrators cannot make conflicting changes. This is for administration purposes only. " + | ||
"The zone will continue to serve DNS data even when it is locked.", | ||
}, | ||
"ns_group": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "The delegation NS group bound with delegated zone.", | ||
}, | ||
"delegated_ttl": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: ttlUndef, | ||
Description: "TTL value for zone-delegated.", | ||
}, | ||
"ext_attrs": { | ||
Type: schema.TypeString, | ||
Default: "", | ||
Optional: true, | ||
Description: "Extensible attributes, as a map in JSON format", | ||
}, | ||
"view": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "default", | ||
Description: "The DNS view in which the zone is created.", | ||
}, | ||
"zone_format": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Default: "FORWARD", | ||
Description: "The format of the zone. Valid values are: FORWARD, IPV4, IPV6.", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceZoneDelegatedRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
connector := m.(ibclient.IBConnector) | ||
|
||
var diags diag.Diagnostics | ||
|
||
filters := filterFromMap(d.Get("filters").(map[string]interface{})) | ||
|
||
objMgr := ibclient.NewObjectManager(connector, "Terraform", "") | ||
|
||
qp := ibclient.NewQueryParams(false, filters) | ||
res, err := objMgr.GetZoneDelegatedByFilters(qp) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("failed to get zone delegated records: %w", err)) | ||
} | ||
|
||
if res == nil { | ||
return diag.FromErr(fmt.Errorf("API returns a nil/empty ID for zone delegated")) | ||
} | ||
// TODO: temporary scaffold, need to rework marshalling/unmarshalling of EAs | ||
// (avoiding additional layer of keys ("value" key) | ||
results := make([]interface{}, 0, len(res)) | ||
for _, r := range res { | ||
zoneDelegatedFlat, err := flattenZoneDelegated(r) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("failed to flatten zone delegated : %w", err)) | ||
} | ||
results = append(results, zoneDelegatedFlat) | ||
} | ||
|
||
err = d.Set("results", results) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
// always run | ||
d.SetId(strconv.FormatInt(time.Now().Unix(), 10)) | ||
|
||
return diags | ||
|
||
} | ||
|
||
func flattenZoneDelegated(zoneDelegated ibclient.ZoneDelegated) (map[string]interface{}, error) { | ||
var eaMap map[string]interface{} | ||
if zoneDelegated.Ea != nil && len(zoneDelegated.Ea) > 0 { | ||
eaMap = zoneDelegated.Ea | ||
} else { | ||
eaMap = make(map[string]interface{}) | ||
} | ||
|
||
ea, err := json.Marshal(eaMap) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := map[string]interface{}{ | ||
"id": zoneDelegated.Ref, | ||
"fqdn": zoneDelegated.Fqdn, | ||
"ext_attrs": string(ea), | ||
"zone_format": zoneDelegated.ZoneFormat, | ||
"view": *zoneDelegated.View, | ||
} | ||
if zoneDelegated.Comment != nil { | ||
res["comment"] = *zoneDelegated.Comment | ||
} | ||
if zoneDelegated.Disable != nil { | ||
res["disable"] = *zoneDelegated.Disable | ||
} | ||
if zoneDelegated.Locked != nil { | ||
res["locked"] = *zoneDelegated.Locked | ||
} | ||
if zoneDelegated.NsGroup != nil { | ||
res["ns_group"] = *zoneDelegated.NsGroup | ||
} | ||
if zoneDelegated.UseDelegatedTtl != nil { | ||
if !*zoneDelegated.UseDelegatedTtl { | ||
res["delegated_ttl"] = ttlUndef | ||
} | ||
} | ||
|
||
if zoneDelegated.DelegatedTtl != nil && *zoneDelegated.DelegatedTtl > 0 { | ||
res["delegated_ttl"] = *zoneDelegated.DelegatedTtl | ||
} else { | ||
res["delegated_ttl"] = ttlUndef | ||
} | ||
|
||
if zoneDelegated.DelegateTo.IsNull == false { | ||
nsInterface := convertNullableNameServersToInterface(zoneDelegated.DelegateTo) | ||
res["delegate_to"] = nsInterface | ||
} | ||
|
||
return res, nil | ||
} |
Oops, something went wrong.