Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support partial mode to avoid writing diff to state while Update error #257

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/services/azapi_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ func resourceAzApiResourceCreateUpdate(d *schema.ResourceData, meta interface{})
ctx, cancel := tf.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

if !d.IsNewResource() {
d.Partial(true)
}

config := d.GetRawConfig()
var resourceName string

Expand Down Expand Up @@ -374,6 +378,10 @@ func resourceAzApiResourceCreateUpdate(d *schema.ResourceData, meta interface{})

d.SetId(id.ID())

if !d.IsNewResource() {
d.Partial(false)
}

return resourceAzApiResourceRead(d, meta)
}

Expand Down
8 changes: 8 additions & 0 deletions internal/services/azapi_resource_action_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func resourceResourceActionCreateUpdate(d *schema.ResourceData, meta interface{}
ctx, cancel := tf.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

if !d.IsNewResource() {
d.Partial(true)
}

id, err := parse.ResourceIDWithResourceType(d.Get("resource_id").(string), d.Get("type").(string))
if err != nil {
return err
Expand Down Expand Up @@ -151,6 +155,10 @@ func resourceResourceActionCreateUpdate(d *schema.ResourceData, meta interface{}
// #nosec G104
d.Set("output", flattenOutput(responseBody, d.Get("response_export_values").([]interface{})))

if !d.IsNewResource() {
d.Partial(false)
}

return resourceResourceActionRead(d, meta)
}

Expand Down
50 changes: 50 additions & 0 deletions internal/services/azapi_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/Azure/terraform-provider-azapi/internal/acceptance"
Expand Down Expand Up @@ -43,6 +44,30 @@ func TestAccGenericResource_basic(t *testing.T) {
})
}

func TestAccGenericResource_invalidVersionUpdate(t *testing.T) {
data := acceptance.BuildTestData(t, "azapi_resource", "test")
r := GenericResource{}

data.ResourceTest(t, r, []resource.TestStep{
{
Config: r.basic(data),
Check: resource.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(defaultIgnores()...),
{
Config: r.basicInvalidVersion(data),
ExpectError: regexp.MustCompile("400 Bad Request"),
},
data.ImportStep(defaultIgnores()...),
{
Config: r.basic(data),
PlanOnly: true,
},
})
}

func TestAccGenericResource_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azapi_resource", "test")
r := GenericResource{}
Expand Down Expand Up @@ -446,6 +471,31 @@ resource "azapi_resource" "test" {
`, r.template(data), data.RandomString, testCertBase64)
}

func (r GenericResource) basicInvalidVersion(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_automation_account" "test" {
name = "acctest%[2]s"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_name = "Basic"
}

resource "azapi_resource" "test" {
type = "Microsoft.Automation/automationAccounts/certificates@1999-01-01"
name = "acctest%[2]s"
parent_id = azurerm_automation_account.test.id
schema_validation_enabled = false
body = jsonencode({
properties = {
base64Value = "%[3]s"
}
})
}
`, r.template(data), data.RandomString, testCertBase64)
}

func (r GenericResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
Expand Down
8 changes: 8 additions & 0 deletions internal/services/azapi_update_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ func resourceAzApiUpdateResourceCreateUpdate(d *schema.ResourceData, meta interf
ctx, cancel := tf.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

if !d.IsNewResource() {
d.Partial(true)
}

var id parse.ResourceId
if name := d.Get("name").(string); len(name) != 0 {
buildId, err := parse.NewResourceID(d.Get("name").(string), d.Get("parent_id").(string), d.Get("type").(string))
Expand Down Expand Up @@ -194,6 +198,10 @@ func resourceAzApiUpdateResourceCreateUpdate(d *schema.ResourceData, meta interf

d.SetId(id.ID())

if !d.IsNewResource() {
d.Partial(false)
}

return resourceAzApiUpdateResourceRead(d, meta)
}

Expand Down