|
| 1 | +package tfbuilder |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-go/tfprotov6" |
| 5 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 6 | + "github.com/hashicorp/terraform-plugin-testing/knownvalue" |
| 7 | + "github.com/hashicorp/terraform-plugin-testing/plancheck" |
| 8 | + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" |
| 9 | +) |
| 10 | + |
| 11 | +func CreateMeshAndModifyFieldsOnIt( |
| 12 | + providerFactory map[string]func() (tfprotov6.ProviderServer, error), |
| 13 | + builder ModifyMeshBuilder, |
| 14 | + mesh *MeshBuilder, |
| 15 | +) resource.TestCase { |
| 16 | + return resource.TestCase{ |
| 17 | + ProtoV6ProviderFactories: providerFactory, |
| 18 | + Steps: []resource.TestStep{ |
| 19 | + { |
| 20 | + Config: builder.AddMesh(mesh).Build(), |
| 21 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 22 | + PreApply: []plancheck.PlanCheck{ |
| 23 | + plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionCreate), |
| 24 | + }, |
| 25 | + }, |
| 26 | + }, |
| 27 | + CheckReapplyPlanEmpty(builder), |
| 28 | + { |
| 29 | + Config: builder.AddMesh(mesh.AddToSpec("at the end", ` |
| 30 | + constraints = { |
| 31 | + dataplane_proxy = { |
| 32 | + requirements = [ { tags = { key = "a" } } ] |
| 33 | + restrictions = [] |
| 34 | + } |
| 35 | + } |
| 36 | + routing = { |
| 37 | + default_forbid_mesh_external_service_access = true |
| 38 | + } |
| 39 | +`)).Build(), |
| 40 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 41 | + PreApply: []plancheck.PlanCheck{ |
| 42 | + plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionUpdate), |
| 43 | + plancheck.ExpectKnownValue(builder.ResourceAddress("mesh", mesh.ResourceName), tfjsonpath.New("routing").AtMapKey("default_forbid_mesh_external_service_access"), knownvalue.Bool(true)), |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + { |
| 48 | + Config: builder.AddMesh(mesh.RemoveFromSpec(`default_forbid_mesh_external_service_access = true`)).Build(), |
| 49 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 50 | + PreApply: []plancheck.PlanCheck{ |
| 51 | + plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionUpdate), |
| 52 | + plancheck.ExpectKnownValue(builder.ResourceAddress("mesh", mesh.ResourceName), tfjsonpath.New("routing").AtMapKey("default_forbid_mesh_external_service_access"), knownvalue.Null()), |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + CheckReapplyPlanEmpty(builder), |
| 57 | + { |
| 58 | + Config: builder.AddMesh(mesh.UpdateSpec(`requirements = [ { tags = { key = "a" } } ]`, `requirements = []`)).Build(), |
| 59 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 60 | + PreApply: []plancheck.PlanCheck{ |
| 61 | + plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionUpdate), |
| 62 | + plancheck.ExpectKnownValue(builder.ResourceAddress("mesh", mesh.ResourceName), tfjsonpath.New("constraints").AtMapKey("dataplane_proxy").AtMapKey("requirements"), knownvalue.ListExact([]knownvalue.Check{})), |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + CheckReapplyPlanEmpty(builder), |
| 67 | + { |
| 68 | + Config: builder.RemoveMesh(mesh.MeshName).Build(), |
| 69 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 70 | + PreApply: []plancheck.PlanCheck{ |
| 71 | + plancheck.ExpectResourceAction(builder.ResourceAddress("mesh", mesh.ResourceName), plancheck.ResourceActionDestroy), |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + CheckReapplyPlanEmpty(builder), |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func CreatePolicyAndModifyFieldsOnIt( |
| 81 | + providerFactory map[string]func() (tfprotov6.ProviderServer, error), |
| 82 | + builder ModifyPolicyBuilder, |
| 83 | + mtp *PolicyBuilder, |
| 84 | +) resource.TestCase { |
| 85 | + mtp.WithSpec(AllowAllTrafficPermissionSpec) |
| 86 | + |
| 87 | + return resource.TestCase{ |
| 88 | + ProtoV6ProviderFactories: providerFactory, |
| 89 | + Steps: []resource.TestStep{ |
| 90 | + { |
| 91 | + Config: builder.AddPolicy(mtp).Build(), |
| 92 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 93 | + PreApply: []plancheck.PlanCheck{ |
| 94 | + plancheck.ExpectResourceAction(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), plancheck.ResourceActionCreate), |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + CheckReapplyPlanEmpty(builder), |
| 99 | + { |
| 100 | + Config: builder.AddPolicy(mtp.AddToSpec(`kind = "Mesh"`, `proxy_types = ["Sidecar"]`)).Build(), |
| 101 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 102 | + PreApply: []plancheck.PlanCheck{ |
| 103 | + plancheck.ExpectResourceAction(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), plancheck.ResourceActionUpdate), |
| 104 | + plancheck.ExpectKnownValue(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), tfjsonpath.New("spec").AtMapKey("from").AtSliceIndex(0).AtMapKey("target_ref").AtMapKey("proxy_types"), knownvalue.ListExact([]knownvalue.Check{knownvalue.StringExact("Sidecar")})), |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + CheckReapplyPlanEmpty(builder), |
| 109 | + { |
| 110 | + Config: builder.AddPolicy(mtp.UpdateSpec(`proxy_types = ["Sidecar"]`, `proxy_types = []`)).Build(), |
| 111 | + ConfigPlanChecks: resource.ConfigPlanChecks{ |
| 112 | + PreApply: []plancheck.PlanCheck{ |
| 113 | + plancheck.ExpectResourceAction(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), plancheck.ResourceActionUpdate), |
| 114 | + plancheck.ExpectKnownValue(builder.ResourceAddress(mtp.ResourceType, mtp.ResourceName), tfjsonpath.New("spec").AtMapKey("from").AtSliceIndex(0).AtMapKey("target_ref").AtMapKey("proxy_types"), knownvalue.ListExact([]knownvalue.Check{})), |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + CheckReapplyPlanEmpty(builder), |
| 119 | + }, |
| 120 | + } |
| 121 | +} |
0 commit comments