Skip to content

Commit

Permalink
Adds user-context to remote_subnet4_resource
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-silvas committed Aug 31, 2023
1 parent 0dbb442 commit b31811e
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 29 deletions.
17 changes: 6 additions & 11 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
*.dll
*.exe
.DS_Store
*/**/.DS_Store
example.tf
terraform.tfplan
terraform.tfstate
bin/
dist/
modules-dev/
/pkg/
website/.vagrant
website/.bundle
website/build
website/node_modules
.vagrant/
*.backup
./*.tfstate
.terraform/
Expand All @@ -23,12 +18,12 @@ website/node_modules
.idea
*.iml
*.test
*.iml

examples/**/terraform.tfstate
examples/**/terraform.tfstate.backup
examples/**/.terraform
examples/provider-install-verification/*
# Ignore all .tfstate files
*/**/.terraform.lock.hcl
*/**/terraform.tfstate
*/**/terraform.tfstate.backdup
*/**/.terraform

# Ignore binary files
develop/terraform-provider-kea
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ testacc: .env ## Run acceptance tests on all sub-packages within docker
# BUILD: Build the provider
# -------------------------------------------------------------------------------------------
build: ## Build the provider for local development
@echo "Building the provider..."
@echo "🛠️ Building the provider..."
@go build -o develop/terraform-provider-kea
@echo "Completed building the provider."
@echo "🏁 Complete! Binary is located in develop/terraform-provider-kea."
.PHONY: build

terraform-plan: ## Run terraform plan to test the provider
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.2
v0.1.3
38 changes: 30 additions & 8 deletions internal/provider/remote_subnet4_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ type (
// remoteSubnet4DataSourceSchema describes the data source data model.
// Maps to the source schema data.
remoteSubnet4DataSourceSchema struct {
Prefix types.String `tfsdk:"prefix"`
SubnetID types.Int64 `tfsdk:"subnet_id"`
Hostname types.String `tfsdk:"hostname"`
ID types.Int64 `tfsdk:"id"`
OptionData []remoteSubnet4DataSourceOptionModel `tfsdk:"option_data"`
Pools types.List `tfsdk:"pools"`
Relay types.List `tfsdk:"relay"`
Subnet types.String `tfsdk:"subnet"`
Prefix types.String `tfsdk:"prefix"`
SubnetID types.Int64 `tfsdk:"subnet_id"`
Hostname types.String `tfsdk:"hostname"`
ID types.Int64 `tfsdk:"id"`
OptionData []remoteSubnet4DataSourceOptionModel `tfsdk:"option_data"`
Pools types.List `tfsdk:"pools"`
Relay types.List `tfsdk:"relay"`
Subnet types.String `tfsdk:"subnet"`
UserContext types.Map `tfsdk:"user_context"`
}

// optionDataModel : Represents a single option-data entry in Kea.
Expand Down Expand Up @@ -94,6 +95,11 @@ func (d *remoteSubnet4DataSource) Schema(_ context.Context, _ datasource.SchemaR
},
},
},
"user_context": schema.MapAttribute{
MarkdownDescription: "Arbitrary string data to tie to the subnet. e.g. `{site = \"AUS\", name = \"Austin, Tx\"}`",
ElementType: types.StringType,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -221,6 +227,22 @@ func (d *remoteSubnet4DataSource) Read(ctx context.Context, req datasource.ReadR
return retVal
}()
config.Subnet = types.StringValue(respData.Subnet)
if respData.UserContext != nil {
config.UserContext = func() types.Map {
fr := make(map[string]attr.Value)
for k, v := range respData.UserContext {
fr[k] = types.StringValue(fmt.Sprintf("%v", v))
}
mv, diags := types.MapValue(types.StringType, fr)
resp.Diagnostics.Append(diags...)
return mv
}()
}

// If there are any diagnostics errors, stop here.
if resp.Diagnostics.HasError() {
return
}

// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
Expand Down
50 changes: 43 additions & 7 deletions internal/provider/remote_subnet4_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -34,12 +35,13 @@ type (

// remoteSubnet4ResourceSchema describes the resource data model.
remoteSubnet4ResourceSchema struct {
Hostname types.String `tfsdk:"hostname"`
ID types.Int64 `tfsdk:"id"`
OptionData []remoteSubnet4OptionResourceModel `tfsdk:"option_data"`
Pools []remoteSubnet4PoolResourceModel `tfsdk:"pools"`
Relay []remoteSubnet4RelayResourceModel `tfsdk:"relay"`
Subnet types.String `tfsdk:"subnet"`
Hostname types.String `tfsdk:"hostname"`
ID types.Int64 `tfsdk:"id"`
OptionData []remoteSubnet4OptionResourceModel `tfsdk:"option_data"`
Pools []remoteSubnet4PoolResourceModel `tfsdk:"pools"`
Relay []remoteSubnet4RelayResourceModel `tfsdk:"relay"`
Subnet types.String `tfsdk:"subnet"`
UserContext types.Map `tfsdk:"user_context"`
}

// remoteSubnet4OptionResourceModel : Represents a single option-data entry in Kea.
Expand Down Expand Up @@ -108,10 +110,15 @@ func (r *remoteSubnet4Resource) Schema(_ context.Context, _ resource.SchemaReque
"code": schema.Int64Attribute{Required: true},
"name": schema.StringAttribute{Required: true},
"data": schema.StringAttribute{Required: true},
"always_send": schema.BoolAttribute{Optional: true},
"always_send": schema.BoolAttribute{Required: true},
},
},
},
"user_context": schema.MapAttribute{
MarkdownDescription: "Arbitrary string data to tie to the subnet. e.g. `{site = \"AUS\", name = \"Austin, Tx\"}`",
ElementType: types.StringType,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -201,6 +208,13 @@ func (r *remoteSubnet4Resource) Create(ctx context.Context, req resource.CreateR
}
return fr
}(),
UserContext: func() map[string]string {
fr := make(map[string]string)
for k, v := range config.UserContext.Elements() {
fr[k] = v.String()
}
return fr
}(),
}

// nolint: contextcheck
Expand Down Expand Up @@ -307,6 +321,22 @@ func (r *remoteSubnet4Resource) Read(ctx context.Context, req resource.ReadReque
return fr
}()
config.Subnet = types.StringValue(respData.Subnet)
if respData.UserContext != nil {
config.UserContext = func() types.Map {
fr := make(map[string]attr.Value)
for k, v := range respData.UserContext {
fr[k] = types.StringValue(fmt.Sprintf("%v", v))
}
mv, diags := types.MapValue(types.StringType, fr)
resp.Diagnostics.Append(diags...)
return mv
}()
}

// If there are any diagnostics errors, stop here.
if resp.Diagnostics.HasError() {
return
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &config)...)
Expand Down Expand Up @@ -381,6 +411,12 @@ func (r *remoteSubnet4Resource) Update(ctx context.Context, req resource.UpdateR
}
return fr
}(),
UserContext: func() map[string]string {
elements := make(map[string]string, len(config.UserContext.Elements()))
diags := config.UserContext.ElementsAs(ctx, &elements, false)
resp.Diagnostics.Append(diags...)
return elements
}(),
},
},
)
Expand Down

0 comments on commit b31811e

Please sign in to comment.