|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 10 | + |
| 11 | + "github.com/terraform-providers/terraform-provider-datadog/datadog/fwprovider" |
| 12 | +) |
| 13 | + |
| 14 | +// Create a policies_list and update the name and priority of its policy |
| 15 | +func TestAccCSMThreatsPoliciesList_CreateAndUpdate(t *testing.T) { |
| 16 | + _, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t) |
| 17 | + |
| 18 | + resourceName := "datadog_csm_threats_policies_list.all" |
| 19 | + |
| 20 | + resource.Test(t, resource.TestCase{ |
| 21 | + PreCheck: func() { testAccPreCheck(t) }, |
| 22 | + ProtoV5ProviderFactories: accProviders, |
| 23 | + CheckDestroy: testAccCheckCSMThreatsPoliciesListDestroy(providers.frameworkProvider), |
| 24 | + Steps: []resource.TestStep{ |
| 25 | + { |
| 26 | + Config: testAccCSMThreatsPoliciesListConfigBasic(), |
| 27 | + Check: resource.ComposeTestCheckFunc( |
| 28 | + testAccCheckCSMThreatsPoliciesListExists(providers.frameworkProvider, resourceName), |
| 29 | + resource.TestCheckResourceAttr(resourceName, "entries.#", "2"), |
| 30 | + resource.TestCheckResourceAttr(resourceName, "entries.0.name", "TERRAFORM_POLICY1"), |
| 31 | + resource.TestCheckResourceAttr(resourceName, "entries.0.priority", "2"), |
| 32 | + resource.TestCheckResourceAttr(resourceName, "entries.1.name", "TERRAFORM_POLICY2"), |
| 33 | + resource.TestCheckResourceAttr(resourceName, "entries.1.priority", "3"), |
| 34 | + ), |
| 35 | + }, |
| 36 | + { |
| 37 | + Config: testAccCSMThreatsPoliciesListConfigUpdate(), |
| 38 | + Check: resource.ComposeTestCheckFunc( |
| 39 | + testAccCheckCSMThreatsPoliciesListExists(providers.frameworkProvider, resourceName), |
| 40 | + resource.TestCheckResourceAttr(resourceName, "entries.#", "2"), |
| 41 | + resource.TestCheckResourceAttr(resourceName, "entries.0.name", "TERRAFORM_POLICY1"), |
| 42 | + resource.TestCheckResourceAttr(resourceName, "entries.0.priority", "2"), |
| 43 | + resource.TestCheckResourceAttr(resourceName, "entries.1.name", "TERRAFORM_POLICY2 UPDATED"), |
| 44 | + resource.TestCheckResourceAttr(resourceName, "entries.1.priority", "5"), |
| 45 | + ), |
| 46 | + }, |
| 47 | + }, |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func testAccCheckCSMThreatsPoliciesListExists(accProvider *fwprovider.FrameworkProvider, resourceName string) resource.TestCheckFunc { |
| 52 | + return func(s *terraform.State) error { |
| 53 | + rs, ok := s.RootModule().Resources[resourceName] |
| 54 | + if !ok { |
| 55 | + return fmt.Errorf("resource '%s' not found in state", resourceName) |
| 56 | + } |
| 57 | + if rs.Type != "datadog_csm_threats_policies_list" { |
| 58 | + return fmt.Errorf( |
| 59 | + "resource %s is not a datadog_csm_threats_policies_list, got: %s", |
| 60 | + resourceName, |
| 61 | + rs.Type, |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + if rs.Primary.ID != "policies_list" { |
| 66 | + return fmt.Errorf("expected resource ID to be 'policies_list', got %s", rs.Primary.ID) |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func testAccCheckCSMThreatsPoliciesListDestroy(accProvider *fwprovider.FrameworkProvider) resource.TestCheckFunc { |
| 74 | + return func(s *terraform.State) error { |
| 75 | + apiInstances := accProvider.DatadogApiInstances |
| 76 | + auth := accProvider.Auth |
| 77 | + |
| 78 | + for _, r := range s.RootModule().Resources { |
| 79 | + if r.Type != "datadog_csm_threats_policies_list" { |
| 80 | + continue |
| 81 | + } |
| 82 | + |
| 83 | + resp, httpResponse, err := apiInstances.GetCSMThreatsApiV2().ListCSMThreatsAgentPolicies(auth) |
| 84 | + if err != nil { |
| 85 | + if httpResponse != nil && httpResponse.StatusCode == 404 { |
| 86 | + return nil |
| 87 | + } |
| 88 | + return fmt.Errorf("Received an error while listing the policies: %s", err) |
| 89 | + } |
| 90 | + |
| 91 | + if len(resp.GetData()) > 1 { // CWS_DD is always present |
| 92 | + return fmt.Errorf("Policies list not empty, some policies are still present") |
| 93 | + } |
| 94 | + } |
| 95 | + return nil |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func testAccCSMThreatsPoliciesListConfigBasic() string { |
| 100 | + return ` |
| 101 | + resource "datadog_csm_threats_policy" "policy1" { |
| 102 | + description = "created with terraform" |
| 103 | + enabled = false |
| 104 | + tags = [] |
| 105 | + } |
| 106 | +
|
| 107 | + resource "datadog_csm_threats_policy" "policy2" { |
| 108 | + description = "created with terraform 2" |
| 109 | + enabled = true |
| 110 | + tags = ["env:staging"] |
| 111 | + } |
| 112 | +
|
| 113 | + resource "datadog_csm_threats_policies_list" "all" { |
| 114 | + entries { |
| 115 | + policy_id = datadog_csm_threats_policy.policy1.id |
| 116 | + name = "TERRAFORM_POLICY1" |
| 117 | + priority = 2 |
| 118 | + } |
| 119 | + entries { |
| 120 | + policy_id = datadog_csm_threats_policy.policy2.id |
| 121 | + name = "TERRAFORM_POLICY2" |
| 122 | + priority = 3 |
| 123 | + } |
| 124 | + } |
| 125 | + ` |
| 126 | +} |
| 127 | + |
| 128 | +func testAccCSMThreatsPoliciesListConfigUpdate() string { |
| 129 | + return ` |
| 130 | + resource "datadog_csm_threats_policy" "policy1" { |
| 131 | + description = "created with terraform" |
| 132 | + enabled = false |
| 133 | + tags = [] |
| 134 | + } |
| 135 | +
|
| 136 | + resource "datadog_csm_threats_policy" "policy2" { |
| 137 | + description = "created with terraform 2" |
| 138 | + enabled = true |
| 139 | + tags = ["env:staging"] |
| 140 | + } |
| 141 | +
|
| 142 | + resource "datadog_csm_threats_policies_list" "all" { |
| 143 | + entries { |
| 144 | + policy_id = datadog_csm_threats_policy.policy1.id |
| 145 | + name = "TERRAFORM_POLICY1" |
| 146 | + priority = 2 |
| 147 | + } |
| 148 | + entries { |
| 149 | + policy_id = datadog_csm_threats_policy.policy2.id |
| 150 | + name = "TERRAFORM_POLICY2 UPDATED" |
| 151 | + priority = 5 |
| 152 | + } |
| 153 | + } |
| 154 | + ` |
| 155 | +} |
0 commit comments