Skip to content

Modified code in utils.go to ignore error when error text contains 'cannot be deleted.' and the error code is 1 (DCNE-380) #1354

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion internal/provider/data_source_aci_rest_managed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestAccDataSourceAciRestManaged_tenant(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down
90 changes: 71 additions & 19 deletions internal/provider/resource_aci_rest_managed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccAciRestManaged_tenant(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestAccAciRestManaged_tenant(t *testing.T) {

func TestAccAciRestManaged_connPref(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -79,7 +79,7 @@ func TestAccAciRestManaged_connPref(t *testing.T) {

func TestAccAciRestManaged_escapeHtml(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -98,7 +98,7 @@ func TestAccAciRestManaged_escapeHtml(t *testing.T) {

func TestAccAciRestManaged_escapeHtmlTrue(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -118,7 +118,7 @@ func TestAccAciRestManaged_escapeHtmlTrue(t *testing.T) {

func TestAccAciRestManaged_noContent(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -136,7 +136,7 @@ func TestAccAciRestManaged_tenantVrf(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -159,7 +159,7 @@ func TestAccAciRestManaged_import(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestAccAciRestManaged_importWithIpv6(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestAccAciRestManaged_importWithBracket(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -504,7 +504,7 @@ func TestAccAciRestManaged_tagTag(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -527,7 +527,7 @@ func TestAccAciRestManaged_tenantChildren(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -568,7 +568,7 @@ func TestAccAciRestManaged_globalAllowExistingOnCreate(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -584,7 +584,7 @@ func TestAccAciRestManaged_globalAllowExistingOnCreate(t *testing.T) {

setEnvVariable(t, "ACI_ALLOW_EXISTING_ON_CREATE", "false")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -596,7 +596,7 @@ func TestAccAciRestManaged_globalAllowExistingOnCreate(t *testing.T) {

setEnvVariable(t, "ACI_ALLOW_EXISTING_ON_CREATE", "true")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -615,7 +615,7 @@ func TestAccAciRestManaged_globalAnnotation(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -633,7 +633,7 @@ func TestAccAciRestManaged_globalAnnotation(t *testing.T) {

setEnvVariable(t, "ACI_ANNOTATION", "orchestrator:from_env")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -698,7 +698,7 @@ func TestAccAciRestManaged_globalAnnotation(t *testing.T) {
})
setEnvVariable(t, "ACI_ANNOTATION", "")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand All @@ -718,7 +718,7 @@ func TestAccAciRestManaged_globalAnnotation(t *testing.T) {

func TestAccAciRestManaged_undeletableClass(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -752,7 +752,7 @@ func TestAccAciRestManaged_explicitNull(t *testing.T) {
name := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)") },
PreCheck: func() { testAccPreCheck(t, "both", "5.2(7g)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -804,6 +804,35 @@ func TestAccAciRestManaged_explicitNull(t *testing.T) {

}

func TestAccAciRestManaged_undeletableObject(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, "both", "2.0(1m)-") },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccAciRestManagedConfig_undeletableObject(),
ExpectNonEmptyPlan: false,
Check: resource.ComposeTestCheckFunc(
// Validate the attributes for fvFabricExtConnP
resource.TestCheckResourceAttr("aci_rest_managed.fvFabricExtConnP", "dn", "uni/tn-infra/fabricExtConnP-1"),
resource.TestCheckResourceAttr("aci_rest_managed.fvFabricExtConnP", "class_name", "fvFabricExtConnP"),
resource.TestCheckResourceAttr("aci_rest_managed.fvFabricExtConnP", "content.id", "1"),
resource.TestCheckResourceAttr("aci_rest_managed.fvFabricExtConnP", "content.name", "IPN"),
resource.TestCheckResourceAttr("aci_rest_managed.fvFabricExtConnP", "content.rt", "extended:as2-nn4:5:16"),
resource.TestCheckResourceAttr("aci_rest_managed.fvFabricExtConnP", "content.siteId", "1"),
resource.TestCheckResourceAttr("aci_rest_managed.fvFabricExtConnP", "content.%", "4"),

// Validate the attributes for fvPeeringP
resource.TestCheckResourceAttr("aci_rest_managed.fvPeeringP", "dn", "uni/tn-infra/fabricExtConnP-1/peeringP"),
resource.TestCheckResourceAttr("aci_rest_managed.fvPeeringP", "class_name", "fvPeeringP"),
resource.TestCheckResourceAttr("aci_rest_managed.fvPeeringP", "content.type", "automatic_with_rr"),
resource.TestCheckResourceAttr("aci_rest_managed.fvPeeringP", "content.%", "1"),
),
},
},
})
}

func testAccAciRestManagedConfig_tenant(name string, description string) string {
return fmt.Sprintf(`
resource "aci_rest_managed" "fvTenant" {
Expand Down Expand Up @@ -1229,3 +1258,26 @@ func testAccAciRestManagedConfig_escapeHtmlTrue() string {
}
`
}

func testAccAciRestManagedConfig_undeletableObject() string {
return `
resource "aci_rest_managed" "fvFabricExtConnP" {
dn = "uni/tn-infra/fabricExtConnP-1"
class_name = "fvFabricExtConnP"
content = {
id = "1"
name = "IPN"
rt = "extended:as2-nn4:5:16"
siteId = "1"
}
}
resource "aci_rest_managed" "fvPeeringP" {
dn = "${aci_rest_managed.fvFabricExtConnP.dn}/peeringP"
class_name = "fvPeeringP"
escape_html = false
content = {
type = "automatic_with_rr"
}
}
`
}
4 changes: 2 additions & 2 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func DoRestRequestEscapeHtml(ctx context.Context, diags *diag.Diagnostics, aciCl
if restResponse != nil && cont.Data() != nil && restResponse.StatusCode != 200 {
errCode := models.StripQuotes(models.StripSquareBrackets(cont.Search("imdata", "error", "attributes", "code").String()))
errText := models.StripQuotes(models.StripSquareBrackets(cont.Search("imdata", "error", "attributes", "text").String()))
// Ignore errors of type "Cannot create object", "Cannot delete object", "Request in progress" and error text containing "can not be deleted." when the error code is 120
if errCode == "103" || errCode == "107" || errCode == "202" || (errCode == "120" && strings.HasSuffix(errText, "can not be deleted.")) {
// Ignore errors of type "Cannot create object", "Cannot delete object", "Request in progress", error text containing "can not be deleted." when the error code is 120 and error text containing "cannot be deleted." when the error code is 1
if errCode == "103" || errCode == "107" || errCode == "202" || (errCode == "120" && strings.HasSuffix(errText, "can not be deleted.")) || (errCode == "1" && strings.HasSuffix(errText, "cannot be deleted.")) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a . dot always present at the end of an error message?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. I think a few messages returned by the API have a dot but a few don't

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, shouldn't you remove the dot at the end since you're using the HasSuffix? it should then pass whenever or not the error message has a dot at the end

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gmicol I was referring to all the error messages not just the one that has "cannot be deleted."

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would using string.Contains(errText, "cannot be deleted") be possible to use instead? This way it will handle a full stop at the end or not. It may also handle the same error if additional message text is added to the error in future versions.

tflog.Debug(ctx, fmt.Sprintf("Exiting from error: Code: %s, Message: %s", errCode, errText))
return nil
} else if (errText == "" && errCode == "403") || errCode == "401" {
Expand Down
Loading