Skip to content

Commit

Permalink
Merge pull request #4 from chriskuchin/main
Browse files Browse the repository at this point in the history
SearchDomain Support
  • Loading branch information
tylerjl authored Nov 20, 2023
2 parents f0312dd + 22d85e5 commit af11706
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
1 change: 1 addition & 0 deletions internal/bowtie/client/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type DNS struct {
IsLog bool `json:"is_log"`
IsDropA bool `json:"is_drop_a"`
IsDropAll bool `json:"is_drop_all"`
IsSearchDomain bool `json:"is_search_domain"`
DNS64Exclude map[string]DNSExclude `json:"dns64_exclude"`
}

Expand Down
7 changes: 4 additions & 3 deletions internal/bowtie/client/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
"github.com/google/uuid"
)

func (c *Client) CreateDNS(name string, serverAddrs []Server, includeOnlySites []string, isDNS64, isCounted, isLog, isDropA, isDropAll bool, exlude []DNSExclude) (string, error) {
func (c *Client) CreateDNS(name string, serverAddrs []Server, includeOnlySites []string, isDNS64, isCounted, isLog, isDropA, isDropAll, isSearchDomain bool, exlude []DNSExclude) (string, error) {
id := uuid.NewString()
return id, c.UpsertDNS(id, name, serverAddrs, includeOnlySites, isDNS64, isCounted, isLog, isDropA, isDropAll, exlude)
return id, c.UpsertDNS(id, name, serverAddrs, includeOnlySites, isDNS64, isCounted, isLog, isDropA, isDropAll, isSearchDomain, exlude)
}

func (c *Client) UpsertDNS(id, name string, serverAddrs []Server, includeOnlySites []string, isDNS64, isCounted, isLog, isDropA, isDropAll bool, exlude []DNSExclude) error {
func (c *Client) UpsertDNS(id, name string, serverAddrs []Server, includeOnlySites []string, isDNS64, isCounted, isLog, isDropA, isDropAll, isSearchDomain bool, exlude []DNSExclude) error {
var servers map[string]Server = map[string]Server{}
for _, addr := range serverAddrs {
servers[addr.ID] = addr
Expand All @@ -34,6 +34,7 @@ func (c *Client) UpsertDNS(id, name string, serverAddrs []Server, includeOnlySit
IsLog: isLog,
IsDropA: isDropA,
IsDropAll: isDropAll,
IsSearchDomain: isSearchDomain,
DNS64Exclude: dnsExclude,
}

Expand Down
87 changes: 67 additions & 20 deletions internal/bowtie/resources/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// Ensure provider defined types fully satisfy framework interfaces.
Expand All @@ -29,12 +30,13 @@ type dnsResourceModel struct {
LastUpdated types.String `tfsdk:"last_updated"`
Name types.String `tfsdk:"name"`
Servers []dnsServersResourceModel `tfsdk:"servers"`
IncludeOnlySites []types.String `tfsdk:"include_only_sites"`
IncludeOnlySites types.List `tfsdk:"include_only_sites"`
IsCounted types.Bool `tfsdk:"is_counted"`
IsDNS64 types.Bool `tfsdk:"is_dns64"`
IsLog types.Bool `tfsdk:"is_log"`
IsDropA types.Bool `tfsdk:"is_drop_a"`
IsDropAll types.Bool `tfsdk:"is_drop_all"`
IsSearchDomain types.Bool `tfsdk:"is_search_domain"`
DNS64Exclude []dnsExcludeResourceModel `tfsdk:"excludes"`
}

Expand Down Expand Up @@ -96,6 +98,9 @@ func (d *dnsResource) Schema(ctx context.Context, req resource.SchemaRequest, re
"order": schema.Int64Attribute{
MarkdownDescription: "The order for this dns server",
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
},
},
Expand All @@ -110,21 +115,35 @@ func (d *dnsResource) Schema(ctx context.Context, req resource.SchemaRequest, re
MarkdownDescription: "Is Counted var",
},
"is_counted": schema.BoolAttribute{
Default: booldefault.StaticBool(true),
Optional: true,
Computed: true,
MarkdownDescription: "Is Counted var",
},
"is_log": schema.BoolAttribute{
Default: booldefault.StaticBool(false),
Optional: true,
Computed: true,
MarkdownDescription: "Is Log Var",
},
"is_drop_a": schema.BoolAttribute{
Default: booldefault.StaticBool(true),
Optional: true,
MarkdownDescription: "Should all records be dropped",
Computed: true,
MarkdownDescription: "Whether to drop the A record or not",
},
"is_drop_all": schema.BoolAttribute{
Default: booldefault.StaticBool(false),
Optional: true,
Computed: true,
MarkdownDescription: "Should all records be dropped",
},
"is_search_domain": schema.BoolAttribute{
Default: booldefault.StaticBool(false),
Optional: true,
Computed: true,
MarkdownDescription: "should be treated as a search domain",
},
"excludes": schema.ListNestedAttribute{
MarkdownDescription: "Provider Metadata storing extra API information about the exclude settings",
Optional: true,
Expand All @@ -144,6 +163,9 @@ func (d *dnsResource) Schema(ctx context.Context, req resource.SchemaRequest, re
"order": schema.Int64Attribute{
MarkdownDescription: "",
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
},
},
},
Expand Down Expand Up @@ -184,9 +206,10 @@ func (d *dnsResource) Create(ctx context.Context, req resource.CreateRequest, re
})
}

includeSites := []string{}
for _, site := range plan.IncludeOnlySites {
includeSites = append(includeSites, site.ValueString())
var includeSites []string
resp.Diagnostics.Append(plan.IncludeOnlySites.ElementsAs(ctx, &includeSites, false)...)
if resp.Diagnostics.HasError() {
return
}

excludes := []client.DNSExclude{}
Expand All @@ -198,7 +221,7 @@ func (d *dnsResource) Create(ctx context.Context, req resource.CreateRequest, re
})
}

id, err := d.client.CreateDNS(plan.Name.ValueString(), servers, includeSites, plan.IsDNS64.ValueBool(), plan.IsCounted.ValueBool(), plan.IsLog.ValueBool(), plan.IsDropA.ValueBool(), plan.IsDropAll.ValueBool(), excludes)
id, err := d.client.CreateDNS(plan.Name.ValueString(), servers, includeSites, plan.IsDNS64.ValueBool(), plan.IsCounted.ValueBool(), plan.IsLog.ValueBool(), plan.IsDropA.ValueBool(), plan.IsDropAll.ValueBool(), plan.IsSearchDomain.ValueBool(), excludes)
if err != nil {
resp.Diagnostics.AddError(
"Failed talking to bowtie server",
Expand Down Expand Up @@ -246,16 +269,38 @@ func (d *dnsResource) Read(ctx context.Context, req resource.ReadRequest, resp *
return
}

tflog.Debug(ctx, fmt.Sprintf("!!!!!!!!!! %+v", dns))
state.Servers = []dnsServersResourceModel{}
for _, v := range dns.Servers {
state.Servers = append(state.Servers, dnsServersResourceModel{
ID: types.StringValue(v.ID),
Addr: types.StringValue(v.Addr),
Order: types.Int64Value(v.Order),
})
}

state.DNS64Exclude = []dnsExcludeResourceModel{}
for _, v := range dns.DNS64Exclude {
state.DNS64Exclude = append(state.DNS64Exclude, dnsExcludeResourceModel{
ID: types.StringValue(v.ID),
Name: types.StringValue(v.Name),
Order: types.Int64Value(v.Order),
})
}

includeSites, diags := types.ListValueFrom(ctx, types.StringType, dns.IncludeOnlySites)
if diags.HasError() {
return
}

state.IncludeOnlySites = includeSites

state.Name = types.StringValue(dns.Name)

// state.Servers = []dnsServersResourceModel{}
// for _, v := range dns.Servers {
// state.Servers[v.Order] = dnsServersResourceModel{
// ID: types.StringValue(v.ID),
// Addr: types.StringValue(v.Addr),
// Order: types.Int64Value(v.Order),
// }
// }
state.IsCounted = types.BoolValue(dns.IsCounted)
state.IsDropA = types.BoolValue(dns.IsDropA)
state.IsDropAll = types.BoolValue(dns.IsDropAll)
state.IsLog = types.BoolValue(dns.IsLog)
state.IsSearchDomain = types.BoolValue(dns.IsSearchDomain)

resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Expand All @@ -267,10 +312,12 @@ func (d *dnsResource) Update(ctx context.Context, req resource.UpdateRequest, re
return
}

var includes []string = []string{}
for _, include := range plan.IncludeOnlySites {
includes = append(includes, include.ValueString())
var includes []string
resp.Diagnostics.Append(plan.IncludeOnlySites.ElementsAs(ctx, &includes, false)...)
if resp.Diagnostics.HasError() {
return
}

var servers []client.Server = []client.Server{}
for _, server := range plan.Servers {
id := server.ID.ValueString()
Expand All @@ -293,7 +340,7 @@ func (d *dnsResource) Update(ctx context.Context, req resource.UpdateRequest, re
})
}

err := d.client.UpsertDNS(plan.ID.ValueString(), plan.Name.ValueString(), servers, includes, plan.IsDNS64.ValueBool(), plan.IsCounted.ValueBool(), plan.IsLog.ValueBool(), plan.IsDropA.ValueBool(), plan.IsDropAll.ValueBool(), excludes)
err := d.client.UpsertDNS(plan.ID.ValueString(), plan.Name.ValueString(), servers, includes, plan.IsDNS64.ValueBool(), plan.IsCounted.ValueBool(), plan.IsLog.ValueBool(), plan.IsDropA.ValueBool(), plan.IsDropAll.ValueBool(), plan.IsSearchDomain.ValueBool(), excludes)
if err != nil {
resp.Diagnostics.AddError(
"Failed updating the dns settings",
Expand Down

0 comments on commit af11706

Please sign in to comment.