Skip to content

Commit

Permalink
Implementation of resource for DTC Pool and DTC Server and Datasource…
Browse files Browse the repository at this point in the history
… for DTC Server (#424)

* Implementation of resource for  DTC Pool and DTC Server and Datasource for DTC Server

* adding acceptance tests for lbdn

---------

Co-authored-by: Aish-sp <[email protected]>
Co-authored-by: Jeenitkumar Khatri <[email protected]>
  • Loading branch information
3 people authored Jan 10, 2025
1 parent 2d97661 commit d46fd5a
Show file tree
Hide file tree
Showing 5 changed files with 1,647 additions and 0 deletions.
182 changes: 182 additions & 0 deletions infoblox/datasource_infoblox_dtc_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
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 dataSourceDtcServer() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDtcServerRead,
Schema: map[string]*schema.Schema{
"filters": {
Type: schema.TypeMap,
Required: true,
},
"results": {
Type: schema.TypeList,
Computed: true,
Description: "List of A records matching filters",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"auto_create_host_record": {
Type: schema.TypeBool,
Optional: true,
Description: "Enabling this option will auto-create a single read-only A/AAAA/CNAME record corresponding to the configured hostname and update it if the hostname changes.\n\n",
},
"comment": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Description of the Dtc server.",
},
"disable": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Determines if the zone is disabled or not.",
},
"ext_attrs": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Extensible attributes of the Dtc Server to be added/updated, as a map in JSON format",
},
"host": {
Type: schema.TypeString,
Required: true,
Description: "The address or FQDN of the server.",
},
"monitors": {
Type: schema.TypeList,
Optional: true,
Description: "List of IP/FQDN and monitor pairs to be used for additional monitoring.\n\n",
Elem: &schema.Resource{
//check the required part once
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Required: true,
Description: "IP address or FQDN of the server used for monitoring.",
},
"monitor_name": {
Type: schema.TypeString,
Required: true,
Description: "The monitor name related to server.",
},
"monitor_type": {
Type: schema.TypeString,
Required: true,
Description: "The monitor type related to server.",
},
},
},
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The DTC Server display name.",
},
"sni_hostname": {
Type: schema.TypeString,
Optional: true,
Description: "The hostname for Server Name Indication (SNI) in FQDN format.",
},
"use_sni_hostname": {
Type: schema.TypeBool,
Optional: true,
Description: "Use flag for: sni_hostname",
},
},
},
},
},
}
}
func dataSourceDtcServerRead(ctx 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.GetAllDtcServer(qp)
if err != nil {
return diag.FromErr(fmt.Errorf("failed to get Dtc servers: %w", err))
}
if res == nil {
return diag.FromErr(fmt.Errorf("API returns a nil/empty ID for zone forward"))
}
// 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 {
dsFlat, err := flattenDtcServer(r, connector)
if err != nil {
return diag.FromErr(fmt.Errorf("failed to flatten dtc server : %w", err))
}
results = append(results, dsFlat)
}
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 flattenDtcServer(dtcServer ibclient.DtcServer, connector ibclient.IBConnector) (map[string]interface{}, error) {
var eaMap map[string]interface{}
if dtcServer.Ea != nil && len(dtcServer.Ea) > 0 {
eaMap = dtcServer.Ea
} else {
eaMap = make(map[string]interface{})
}

ea, err := json.Marshal(eaMap)
if err != nil {
return nil, err
}

res := map[string]interface{}{
"id": dtcServer.Ref,
"ext_attrs": string(ea),
"name": *dtcServer.Name,
"host": *dtcServer.Host,
}
if dtcServer.AutoCreateHostRecord != nil {
res["auto_create_host_record"] = *dtcServer.AutoCreateHostRecord
}
if dtcServer.Comment != nil {
res["comment"] = *dtcServer.Comment
}
if dtcServer.Disable != nil {
res["disable"] = *dtcServer.Disable
}
if dtcServer.Monitors != nil {
monitorInterface := convertDtcServerMonitorsToInterface(dtcServer.Monitors, connector)
res["monitors"] = monitorInterface
}
if dtcServer.SniHostname != nil {
res["sni_hostname"] = *dtcServer.SniHostname
}
if dtcServer.UseSniHostname != nil {
res["use_sni_hostname"] = *dtcServer.UseSniHostname
}
return res, nil
}
3 changes: 3 additions & 0 deletions infoblox/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ func Provider() *schema.Provider {
"infoblox_zone_auth": resourceZoneAuth(),
"infoblox_zone_forward": resourceZoneForward(),
"infoblox_dtc_lbdn": resourceDtcLbdnRecord(),
"infoblox_dtc_pool": resourceDtcPool(),
"infoblox_dtc_server": resourceDtcServer(),
},
DataSourcesMap: map[string]*schema.Resource{
"infoblox_ipv4_network": dataSourceIPv4Network(),
Expand All @@ -233,6 +235,7 @@ func Provider() *schema.Provider {
"infoblox_zone_forward": dataSourceZoneForward(),
"infoblox_dtc_lbdn": dataSourceDtcLbdnRecord(),
"infoblox_dtc_pool": datasourceDtcPool(),
"infoblox_dtc_server": dataSourceDtcServer(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
Loading

0 comments on commit d46fd5a

Please sign in to comment.