|
| 1 | +package provider_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-testing/config" |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/knownvalue" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/statecheck" |
| 13 | + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" |
| 14 | +) |
| 15 | + |
| 16 | +// TestAccPanosVsys_ConflictWithTemplateDefaultVsys verifies that creating a |
| 17 | +// panos_vsys resource for "vsys1" fails when the template already has |
| 18 | +// default_vsys = "vsys1" (because the template's PostCreate hook already |
| 19 | +// created that vsys entry). |
| 20 | +func TestAccPanosVsys_ConflictWithTemplateDefaultVsys(t *testing.T) { |
| 21 | + t.Parallel() |
| 22 | + |
| 23 | + nameSuffix := acctest.RandStringFromCharSet(6, acctest.CharSetAlphaNum) |
| 24 | + prefix := fmt.Sprintf("test-acc-%s", nameSuffix) |
| 25 | + |
| 26 | + location := config.ObjectVariable(map[string]config.Variable{ |
| 27 | + "panorama": config.ObjectVariable(map[string]config.Variable{}), |
| 28 | + }) |
| 29 | + |
| 30 | + configVars := map[string]config.Variable{ |
| 31 | + "prefix": config.StringVariable(prefix), |
| 32 | + "location": location, |
| 33 | + } |
| 34 | + |
| 35 | + resource.Test(t, resource.TestCase{ |
| 36 | + PreCheck: func() { testAccPreCheck(t) }, |
| 37 | + ProtoV6ProviderFactories: testAccProviders, |
| 38 | + Steps: []resource.TestStep{ |
| 39 | + { |
| 40 | + Config: vsys_ConflictWithTemplateDefaultVsys_Tmpl, |
| 41 | + ConfigVariables: configVars, |
| 42 | + ExpectError: regexp.MustCompile(`.`), |
| 43 | + }, |
| 44 | + }, |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +const vsys_ConflictWithTemplateDefaultVsys_Tmpl = ` |
| 49 | +variable "prefix" { type = string } |
| 50 | +variable "location" { type = any } |
| 51 | +
|
| 52 | +resource "panos_template" "test" { |
| 53 | + location = var.location |
| 54 | + name = "${var.prefix}-template" |
| 55 | + default_vsys = "vsys1" |
| 56 | +} |
| 57 | +
|
| 58 | +resource "panos_vsys" "vsys1" { |
| 59 | + location = { |
| 60 | + template = { |
| 61 | + name = panos_template.test.name |
| 62 | + } |
| 63 | + } |
| 64 | + name = "vsys1" |
| 65 | +} |
| 66 | +` |
| 67 | + |
| 68 | +// TestAccPanosVsys_WithTemplate verifies that creating a panos_vsys resource |
| 69 | +// for "vsys2" succeeds when the template has default_vsys = "vsys1" (a |
| 70 | +// different vsys name, so no conflict). |
| 71 | +func TestAccPanosVsys_WithTemplate(t *testing.T) { |
| 72 | + t.Parallel() |
| 73 | + |
| 74 | + nameSuffix := acctest.RandStringFromCharSet(6, acctest.CharSetAlphaNum) |
| 75 | + prefix := fmt.Sprintf("test-acc-%s", nameSuffix) |
| 76 | + |
| 77 | + location := config.ObjectVariable(map[string]config.Variable{ |
| 78 | + "panorama": config.ObjectVariable(map[string]config.Variable{}), |
| 79 | + }) |
| 80 | + |
| 81 | + configVars := map[string]config.Variable{ |
| 82 | + "prefix": config.StringVariable(prefix), |
| 83 | + "location": location, |
| 84 | + } |
| 85 | + |
| 86 | + resource.Test(t, resource.TestCase{ |
| 87 | + PreCheck: func() { testAccPreCheck(t) }, |
| 88 | + ProtoV6ProviderFactories: testAccProviders, |
| 89 | + Steps: []resource.TestStep{ |
| 90 | + { |
| 91 | + Config: vsys_WithTemplate_Tmpl, |
| 92 | + ConfigVariables: configVars, |
| 93 | + ConfigStateChecks: []statecheck.StateCheck{ |
| 94 | + statecheck.ExpectKnownValue( |
| 95 | + "panos_vsys.vsys2", |
| 96 | + tfjsonpath.New("name"), |
| 97 | + knownvalue.StringExact("vsys2"), |
| 98 | + ), |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }) |
| 103 | +} |
| 104 | + |
| 105 | +const vsys_WithTemplate_Tmpl = ` |
| 106 | +variable "prefix" { type = string } |
| 107 | +variable "location" { type = any } |
| 108 | +
|
| 109 | +resource "panos_template" "test" { |
| 110 | + location = var.location |
| 111 | + name = "${var.prefix}-template" |
| 112 | + default_vsys = "vsys1" |
| 113 | +} |
| 114 | +
|
| 115 | +resource "panos_vsys" "vsys2" { |
| 116 | + location = { |
| 117 | + template = { |
| 118 | + name = panos_template.test.name |
| 119 | + } |
| 120 | + } |
| 121 | + name = "vsys2" |
| 122 | +} |
| 123 | +` |
0 commit comments