Skip to content

Commit

Permalink
Support tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino committed Jul 20, 2024
1 parent 437252c commit cfcf186
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
12 changes: 9 additions & 3 deletions internal/provider/bgpsession_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ type SessionResourceModel struct {
PrefixListInID types.Int64 `tfsdk:"prefix_list_in_id"`
PrefixListOutID types.Int64 `tfsdk:"prefix_list_out_id"`

// todo: tags
Tags types.List `tfsdk:"tags"`

// todo: custom fields
}

Expand Down Expand Up @@ -120,7 +121,9 @@ func (m *SessionResourceModel) ToAPIModel(ctx context.Context, diags diag.Diagno
p.PrefixListOut = toIntPointer(m.PrefixListOutID.ValueInt64())
}

// todo: tags and custom fields
p.Tags = TagsForAPIModel(ctx, m.Tags, diags)

// todo: custom fields

return p
}
Expand Down Expand Up @@ -178,7 +181,9 @@ func (m *SessionResourceModel) FillFromAPIModel(ctx context.Context, resp *clien
m.TenantID = types.Int64Value(int64(*resp.Tenant.Id))
}

// todo: tags and custom fields
m.Tags = TagsFromAPI(ctx, resp.Tags, diags)

// todo: custom fields
}

func (r *SessionResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
Expand Down Expand Up @@ -255,6 +260,7 @@ func (r *SessionResource) Schema(ctx context.Context, req resource.SchemaRequest
"prefix_list_out_id": schema.Int64Attribute{
Optional: true,
},
TagFieldName: TagSchema,
},
}
}
Expand Down
21 changes: 21 additions & 0 deletions internal/provider/bgpsession_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
)

func testName(t *testing.T) string {
Expand Down Expand Up @@ -108,6 +111,13 @@ func TestAccSessionResource(t *testing.T) {
},
{
Config: fmt.Sprintf(`%s
resource "netbox_tag" "test_a" {
name = "Integration Test"
}
resource "netbox_tag" "test_b" {
name = "temporary"
}
resource "netboxbgp_session" "test" {
name = "My session changed"
status = "active"
Expand All @@ -117,8 +127,19 @@ func TestAccSessionResource(t *testing.T) {
local_as_id = netbox_asn.test.id
remote_as_id = netbox_asn.test.id
site_id = netbox_site.test.id
tags = ["Integration Test", "temporary"]
}
`, baseResources(t)),
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(
"netboxbgp_session.test",
tfjsonpath.New("tags"),
knownvalue.ListExact([]knownvalue.Check{
knownvalue.StringExact("Integration Test"),
knownvalue.StringExact("temporary"),
}),
),
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("netboxbgp_session.test", "name", "My session changed"),
resource.TestCheckResourceAttrPair("netboxbgp_session.test", "site_id", "netbox_site.test", "id"),
Expand Down
23 changes: 23 additions & 0 deletions internal/provider/slugs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package provider

import (
"regexp"
"strings"
)

var (
// \w = word characters (== [0-9A-Za-z_])
// \s = whitespace (== [\t\n\f\r ])

Check failure on line 10 in internal/provider/slugs.go

View workflow job for this annotation

GitHub Actions / Build

Comment should end in a period (godot)
matchSpecial = regexp.MustCompile(`[^\w\s-]`)
matchMultiWhitespacesAndDashes = regexp.MustCompile(`[\s-]+`)
)

func slugify(name string) string {
var result string
// Special chars are stripped
result = matchSpecial.ReplaceAllString(name, "")
// Blocks of multiple whitespaces and dashes will be replaced by a single dash
result = matchMultiWhitespacesAndDashes.ReplaceAllString(result, "-")
result = strings.Trim(result, "-")
return strings.ToLower(result)
}
55 changes: 55 additions & 0 deletions internal/provider/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package provider

import (
"context"

"github.com/ffddorf/terraform-provider-netbox-bgp/client"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
TagFieldName = "tags"
TagSchema = schema.ListAttribute{
ElementType: types.StringType,
Optional: true,
}
)

func TagsForAPIModel(ctx context.Context, l types.List, diags diag.Diagnostics) *[]client.NestedTagRequest {
if l.IsNull() {
return nil
}

var tags []string
if diags := l.ElementsAs(ctx, &tags, true); diags.HasError() {
diags.Append(diags...)
return nil
}

reqs := make([]client.NestedTagRequest, 0, len(tags))
for _, t := range tags {
reqs = append(reqs, client.NestedTagRequest{
Name: t,
Slug: slugify(t),
})
}

return &reqs
}

func TagsFromAPI(ctx context.Context, tags *[]client.NestedTag, diags diag.Diagnostics) types.List {
if tags == nil || len(*tags) == 0 {
return types.ListNull(types.StringType)
}

tagNames := make([]string, 0, len(*tags))
for _, t := range *tags {
tagNames = append(tagNames, t.Name)
}

l, d := types.ListValueFrom(ctx, types.StringType, tagNames)
diags.Append(d...)
return l
}

0 comments on commit cfcf186

Please sign in to comment.