Skip to content

Commit eded436

Browse files
authored
cognitive_account_rai_blocklist_resource - support tags property and add validation for name property (#31871)
[ENHANCEMENT] * `cognitive_account_rai_blocklist_resource` - add support for the `tags` property
1 parent 29fde31 commit eded436

File tree

5 files changed

+161
-7
lines changed

5 files changed

+161
-7
lines changed

internal/services/cognitive/cognitive_account_rai_blocklist_resource.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ import (
1010

1111
"github.com/hashicorp/go-azure-helpers/lang/pointer"
1212
"github.com/hashicorp/go-azure-helpers/lang/response"
13+
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
1314
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2025-06-01/raiblocklists"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1516
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
1617
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
18+
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cognitive/validate"
1719
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
18-
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
1920
)
2021

2122
type cognitiveRaiBlocklistModel struct {
22-
Name string `tfschema:"name"`
23-
CognitiveAccountId string `tfschema:"cognitive_account_id"`
24-
Description string `tfschema:"description"`
23+
Name string `tfschema:"name"`
24+
CognitiveAccountId string `tfschema:"cognitive_account_id"`
25+
Description string `tfschema:"description"`
26+
Tags map[string]string `tfschema:"tags"`
2527
}
2628

2729
type CognitiveRaiBlocklistResource struct{}
@@ -34,7 +36,7 @@ func (c CognitiveRaiBlocklistResource) Arguments() map[string]*schema.Schema {
3436
Type: pluginsdk.TypeString,
3537
Required: true,
3638
ForceNew: true,
37-
ValidateFunc: validation.StringIsNotEmpty,
39+
ValidateFunc: validate.RaiBlocklistName(),
3840
},
3941

4042
"cognitive_account_id": {
@@ -48,6 +50,7 @@ func (c CognitiveRaiBlocklistResource) Arguments() map[string]*schema.Schema {
4850
Type: pluginsdk.TypeString,
4951
Optional: true,
5052
},
53+
"tags": commonschema.Tags(),
5154
}
5255
}
5356

@@ -86,6 +89,7 @@ func (c CognitiveRaiBlocklistResource) Create() sdk.ResourceFunc {
8689

8790
payload := &raiblocklists.RaiBlocklist{
8891
Properties: &raiblocklists.RaiBlocklistProperties{},
92+
Tags: pointer.To(model.Tags),
8993
}
9094

9195
if model.Description != "" {
@@ -144,6 +148,10 @@ func (c CognitiveRaiBlocklistResource) Update() sdk.ResourceFunc {
144148
payload.Properties.Description = pointer.To(model.Description)
145149
}
146150

151+
if metadata.ResourceData.HasChange("tags") {
152+
payload.Tags = pointer.To(model.Tags)
153+
}
154+
147155
if _, err := client.CreateOrUpdate(ctx, *id, *payload); err != nil {
148156
return fmt.Errorf("updating %s: %+v", id, err)
149157
}
@@ -215,6 +223,7 @@ func (c CognitiveRaiBlocklistResource) Read() sdk.ResourceFunc {
215223
if props := model.Properties; props != nil {
216224
state.Description = pointer.From(props.Description)
217225
}
226+
state.Tags = pointer.From(model.Tags)
218227
}
219228

220229
return metadata.Encode(&state)

internal/services/cognitive/cognitive_account_rai_blocklist_resource_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ resource "azurerm_cognitive_account_rai_blocklist" "test" {
162162
name = "acctest-crb-%d"
163163
cognitive_account_id = azurerm_cognitive_account.test.id
164164
description = "Acceptance test data new azurerm resource"
165+
tags = {
166+
Acceptance = "Test"
167+
Environment = "Dev"
168+
}
165169
}
166170
`, template, data.RandomInteger)
167171
}
@@ -174,6 +178,11 @@ resource "azurerm_cognitive_account_rai_blocklist" "test" {
174178
name = "acctest-crb-%d"
175179
cognitive_account_id = azurerm_cognitive_account.test.id
176180
description = "Acceptance test data new azurerm resource updated"
181+
tags = {
182+
Acceptance = "TestUpdated"
183+
Environment = "Dev"
184+
Updated = "true"
185+
}
177186
}
178187
`, template, data.RandomInteger)
179188
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright IBM Corp. 2014, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package validate
5+
6+
import (
7+
"regexp"
8+
9+
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
10+
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
11+
)
12+
13+
func RaiBlocklistName() pluginsdk.SchemaValidateFunc {
14+
return validation.StringMatch(
15+
regexp.MustCompile("^[a-zA-Z0-9_-]{2,64}$"),
16+
"The RAI Blocklist Name must be between 2 and 64 characters long, contain only alphanumeric characters, hyphens(-) or underscores(_).",
17+
)
18+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright IBM Corp. 2014, 2026
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package validate
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func TestValidateCognitiveServicesRaiBlocklistName(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
input string
14+
valid bool
15+
}{
16+
{
17+
name: "empty name",
18+
input: "",
19+
valid: false,
20+
},
21+
{
22+
name: "Invalid single character",
23+
input: "a",
24+
valid: false,
25+
},
26+
{
27+
name: "Valid two characters",
28+
input: "ab",
29+
valid: true,
30+
},
31+
{
32+
name: "Valid alphanumeric",
33+
input: "hello1",
34+
valid: true,
35+
},
36+
{
37+
name: "Valid contains underscore",
38+
input: "hello_world",
39+
valid: true,
40+
},
41+
{
42+
name: "Valid contains hyphen",
43+
input: "hello-world",
44+
valid: true,
45+
},
46+
{
47+
name: "Valid starts with digit",
48+
input: "1hello",
49+
valid: true,
50+
},
51+
{
52+
name: "Valid starts with underscore",
53+
input: "_hello",
54+
valid: true,
55+
},
56+
{
57+
name: "Valid starts with hyphen",
58+
input: "-hello",
59+
valid: true,
60+
},
61+
{
62+
name: "Valid ends with underscore",
63+
input: "hello_",
64+
valid: true,
65+
},
66+
{
67+
name: "Valid ends with hyphen",
68+
input: "hello-",
69+
valid: true,
70+
},
71+
{
72+
name: "Invalid contains period",
73+
input: "a.bc",
74+
valid: false,
75+
},
76+
{
77+
name: "Invalid contains whitespace",
78+
input: "a bc",
79+
valid: false,
80+
},
81+
{
82+
name: "Invalid contains special character",
83+
input: "a@bc",
84+
valid: false,
85+
},
86+
{
87+
name: "contains slash",
88+
input: "a/bc",
89+
valid: false,
90+
},
91+
{
92+
name: "Valid 64 characters",
93+
input: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789012",
94+
valid: true,
95+
},
96+
{
97+
name: "Invalid starts with period",
98+
input: ".heyo",
99+
valid: false,
100+
},
101+
{
102+
name: "Invalid 65 characters",
103+
input: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890123",
104+
valid: false,
105+
},
106+
}
107+
for _, tt := range tests {
108+
t.Run(tt.name, func(t *testing.T) {
109+
_, err := RaiBlocklistName()(tt.input, "")
110+
valid := err == nil
111+
if valid != tt.valid {
112+
t.Errorf("Expected valid status %t but got %t for input %s", tt.valid, valid, tt.input)
113+
}
114+
})
115+
}
116+
}

website/docs/r/cognitive_account_rai_blocklist.html.markdown

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ subcategory: "Cognitive Services"
33
layout: "azurerm"
44
page_title: "Azure Resource Manager: azurerm_cognitive_account_rai_blocklist"
55
description: |-
6-
Manages a Cognitive Account Rai Blocklist.
6+
Manages a Microsoft Foundry "Guardrails + Controls" blocklist. Microsoft Foundry was previously known as "Cognitive Account".
77
---
88

99
# azurerm_cognitive_account_rai_blocklist
1010

11-
Manages a Cognitive Account Rai Blocklist.
11+
Manages a Microsoft Foundry "Guardrails + Controls" blocklist. Microsoft Foundry was previously known as "Cognitive Account".
1212

1313
## Example Usage
1414

@@ -41,6 +41,8 @@ The following arguments are supported:
4141

4242
* `cognitive_account_id` - (Required) The ID of the Cognitive Services Account. Changing this forces a new Cognitive Account Rai Blocklist to be created.
4343

44+
* `tags` - (Optional) A mapping of tags assigned to the resource.
45+
4446
---
4547

4648
* `description` - (Optional) A short description for the Cognitive Account Rai Blocklist.

0 commit comments

Comments
 (0)