Skip to content

Commit

Permalink
fix: requested adjustments and acceptence test formated using terrafm…
Browse files Browse the repository at this point in the history
…t tool
  • Loading branch information
feliperezende-barbosa committed Dec 11, 2024
1 parent 94a21ec commit b0dc132
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ func (c CognitiveRaiBlocklistResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Cognitive.RaiBlocklistsClient

var model cognitiveRaiBlocklistModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

client := metadata.Client.Cognitive.RaiBlocklistsClient
accountId, err := raiblocklists.ParseAccountID(model.CognitiveAccountId)
if err != nil {
return err
Expand All @@ -80,15 +81,15 @@ func (c CognitiveRaiBlocklistResource) Create() sdk.ResourceFunc {
return metadata.ResourceRequiresImport(c.ResourceType(), id)
}

properties := &raiblocklists.RaiBlocklist{
payload := &raiblocklists.RaiBlocklist{
Properties: &raiblocklists.RaiBlocklistProperties{},
}

if model.Description != "" {
properties.Properties.Description = &model.Description
payload.Properties.Description = &model.Description
}

if _, err := client.CreateOrUpdate(ctx, id, *properties); err != nil {
if _, err := client.CreateOrUpdate(ctx, id, *payload); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}

Expand All @@ -102,12 +103,13 @@ func (c CognitiveRaiBlocklistResource) Update() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
client := metadata.Client.Cognitive.RaiBlocklistsClient

var model cognitiveRaiBlocklistModel
if err := metadata.Decode(&model); err != nil {
return fmt.Errorf("decoding: %+v", err)
}

client := metadata.Client.Cognitive.RaiBlocklistsClient
accountId, err := raiblocklists.ParseAccountID(model.CognitiveAccountId)
if err != nil {
return err
Expand All @@ -122,17 +124,25 @@ func (c CognitiveRaiBlocklistResource) Update() sdk.ResourceFunc {
}
existing, err := client.Get(ctx, *id)
if err != nil {
return err
return fmt.Errorf("retrieving %s: %+v", id, err)
}

if existing.Model == nil {
return fmt.Errorf("retrieving %s: `model` was nil", id)
}

if existing.Model.Properties == nil {
return fmt.Errorf("retrieving %s: `properties` was nil", id)
}

properties := existing.Model
payload := existing.Model

if metadata.ResourceData.HasChange("description") {
properties.Properties.Description = pointer.To(model.Description)
payload.Properties.Description = pointer.To(model.Description)
}

if _, err := client.CreateOrUpdate(ctx, *id, *properties); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
if _, err := client.CreateOrUpdate(ctx, *id, *payload); err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

metadata.SetID(id)
Expand Down Expand Up @@ -193,19 +203,17 @@ func (c CognitiveRaiBlocklistResource) Read() sdk.ResourceFunc {
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

model := existing.Model
if model == nil {
return fmt.Errorf("retrieving %s: model was nil", id)
}

state := cognitiveRaiBlocklistModel{
Name: id.RaiBlocklistName,
CognitiveAccountId: raiblocklists.NewAccountID(id.SubscriptionId, id.ResourceGroupName, id.AccountName).ID(),
}

if properties := model.Properties; properties != nil {
state.Description = pointer.From(properties.Description)
if model := existing.Model; model != nil {
if props := model.Properties; props != nil {
state.Description = pointer.From(props.Description)
}
}

return metadata.Encode(&state)
},
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/cognitive/2024-10-01/raiblocklists"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type CognitiveRaiBlocklistTestResource struct{}
Expand All @@ -29,13 +28,13 @@ func TestAccCognitiveRaiBlocklistSequential(t *testing.T) {

func TestAccCognitiveRaiBlocklist_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_account_rai_blocklist", "test")
tRaiBlocklist := CognitiveRaiBlocklistTestResource{}
r := CognitiveRaiBlocklistTestResource{}

data.ResourceSequentialTest(t, tRaiBlocklist, []acceptance.TestStep{
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: tRaiBlocklist.basicConfig(data),
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(tRaiBlocklist),
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
Expand All @@ -44,28 +43,28 @@ func TestAccCognitiveRaiBlocklist_basic(t *testing.T) {

func TestAccCognitiveRaiBlocklist_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_account_rai_blocklist", "test")
tRaiBlocklist := CognitiveRaiBlocklistTestResource{}
r := CognitiveRaiBlocklistTestResource{}

data.ResourceSequentialTest(t, tRaiBlocklist, []acceptance.TestStep{
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: tRaiBlocklist.basicConfig(data),
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(tRaiBlocklist),
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(tRaiBlocklist.requiresImportConfig),
data.RequiresImportErrorStep(r.requiresImport),
})
}

func TestAccCognitiveRaiBlocklist_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_account_rai_blocklist", "test")
tRaiBlocklist := CognitiveRaiBlocklistTestResource{}
r := CognitiveRaiBlocklistTestResource{}

data.ResourceSequentialTest(t, tRaiBlocklist, []acceptance.TestStep{
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: tRaiBlocklist.completeConfig(data),
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(tRaiBlocklist),
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
Expand All @@ -74,20 +73,20 @@ func TestAccCognitiveRaiBlocklist_complete(t *testing.T) {

func TestAccCognitiveRaiBlocklist_update(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cognitive_account_rai_blocklist", "test")
tRaiBlocklist := CognitiveRaiBlocklistTestResource{}
r := CognitiveRaiBlocklistTestResource{}

data.ResourceSequentialTest(t, tRaiBlocklist, []acceptance.TestStep{
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: tRaiBlocklist.basicConfig(data),
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(tRaiBlocklist),
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: tRaiBlocklist.completeConfig(data),
Config: r.update(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(tRaiBlocklist),
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
Expand All @@ -100,70 +99,78 @@ func (c CognitiveRaiBlocklistTestResource) Exists(ctx context.Context, clients *
return nil, err
}

client := clients.Cognitive.RaiBlocklistsClient
existing, err := client.Get(ctx, *id)
existing, err := clients.Cognitive.RaiBlocklistsClient.Get(ctx, *id)
if err != nil {
if response.WasNotFound(existing.HttpResponse) {
return utils.Bool(false), nil
}
return nil, fmt.Errorf("retrieving %s: %+v", id, err)
}
return utils.Bool(existing.Model != nil), nil
return pointer.To(existing.Model != nil), nil
}

func (c CognitiveRaiBlocklistTestResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctest-rg-%d"
location = "%s"
}
resource "azurerm_cognitive_account" "test" {
name = "acctest-ca-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "OpenAI"
sku_name = "S0"
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctest-rg-%d"
location = "%s"
}
resource "azurerm_cognitive_account" "test" {
name = "acctest-ca-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
kind = "OpenAI"
sku_name = "S0"
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (c CognitiveRaiBlocklistTestResource) basicConfig(data acceptance.TestData) string {
func (c CognitiveRaiBlocklistTestResource) basic(data acceptance.TestData) string {
template := c.template(data)
return fmt.Sprintf(`
%s
resource "azurerm_cognitive_account_rai_blocklist" "test" {
name = "acctest-crb-%d"
cognitive_account_id = azurerm_cognitive_account.test.id
description = "Acceptance test data new azurerm resource"
}
resource "azurerm_cognitive_account_rai_blocklist" "test" {
name = "acctest-crb-%d"
cognitive_account_id = azurerm_cognitive_account.test.id
description = "Acceptance test data new azurerm resource"
}
`, template, data.RandomInteger)
}

func (c CognitiveRaiBlocklistTestResource) requiresImportConfig(data acceptance.TestData) string {
config := c.basicConfig(data)
func (c CognitiveRaiBlocklistTestResource) requiresImport(data acceptance.TestData) string {
config := c.basic(data)
return fmt.Sprintf(`
%s
resource "azurerm_cognitive_account_rai_blocklist" "import" {
name = azurerm_cognitive_account_rai_blocklist.test.name
cognitive_account_id = azurerm_cognitive_account.test.id
description = "Acceptance test data new azurerm resource"
}
resource "azurerm_cognitive_account_rai_blocklist" "import" {
name = azurerm_cognitive_account_rai_blocklist.test.name
cognitive_account_id = azurerm_cognitive_account.test.id
description = "Acceptance test data new azurerm resource"
}
`, config)
}

func (c CognitiveRaiBlocklistTestResource) completeConfig(data acceptance.TestData) string {
func (c CognitiveRaiBlocklistTestResource) complete(data acceptance.TestData) string {
template := c.template(data)
return fmt.Sprintf(`
%s
resource "azurerm_cognitive_account_rai_blocklist" "test" {
name = "acctest-crb-%d"
cognitive_account_id = azurerm_cognitive_account.test.id
description = "Acceptance test data new azurerm resource"
}
resource "azurerm_cognitive_account_rai_blocklist" "test" {
name = "acctest-crb-%d"
cognitive_account_id = azurerm_cognitive_account.test.id
description = "Acceptance test data new azurerm resource"
}
`, template, data.RandomInteger)
}

func (c CognitiveRaiBlocklistTestResource) update(data acceptance.TestData) string {
template := c.template(data)
return fmt.Sprintf(`
%s
resource "azurerm_cognitive_account_rai_blocklist" "test" {
name = "acctest-crb-%d"
cognitive_account_id = azurerm_cognitive_account.test.id
description = "Acceptance test data new azurerm resource updated"
}
`, template, data.RandomInteger)
}

0 comments on commit b0dc132

Please sign in to comment.