generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
3 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
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,23 @@ | ||
package provider | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var ( | ||
// \w = word characters (== [0-9A-Za-z_]) | ||
// \s = whitespace (== [\t\n\f\r ]) | ||
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) | ||
} |
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,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 | ||
} |