|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/knownvalue" |
| 13 | + "github.com/hashicorp/terraform-plugin-testing/statecheck" |
| 14 | + "github.com/hashicorp/terraform-plugin-testing/tfjsonpath" |
| 15 | + "github.com/jianyuan/go-utils/ptr" |
| 16 | + "github.com/rootlyhq/terraform-provider-rootly/v2/internal/acctest" |
| 17 | + rootly "github.com/rootlyhq/terraform-provider-rootly/v2/schema" |
| 18 | +) |
| 19 | + |
| 20 | +func init() { |
| 21 | + resource.AddTestSweepers("rootly_alert_urgency", &resource.Sweeper{ |
| 22 | + Name: "rootly_alert_urgency", |
| 23 | + F: func(region string) error { |
| 24 | + ctx := context.Background() |
| 25 | + |
| 26 | + params := &rootly.ListAlertUrgenciesParams{ |
| 27 | + PageNumber: ptr.Ptr(1), |
| 28 | + } |
| 29 | + |
| 30 | + for { |
| 31 | + httpResp, err := acctest.SharedClient.ListAlertUrgenciesWithResponse(ctx, params) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("Error getting alert urgencies, got error: %s", err) |
| 34 | + } else if httpResp.StatusCode() != http.StatusOK { |
| 35 | + return fmt.Errorf("Error getting alert urgencies, got status code: %d", httpResp.StatusCode()) |
| 36 | + } else if httpResp.ApplicationvndApiJSON200 == nil { |
| 37 | + return fmt.Errorf("Error getting alert urgencies, got empty response") |
| 38 | + } |
| 39 | + |
| 40 | + for _, alertUrgency := range httpResp.ApplicationvndApiJSON200.Data { |
| 41 | + if strings.HasPrefix(alertUrgency.Attributes.Name, "tf-") { |
| 42 | + httpResp, err := acctest.SharedClient.DeleteAlertUrgencyWithResponse(ctx, alertUrgency.Id) |
| 43 | + if err != nil { |
| 44 | + return fmt.Errorf("Error deleting alert urgency: %s", err) |
| 45 | + } else if httpResp.StatusCode() != http.StatusOK { |
| 46 | + return fmt.Errorf("Error deleting alert urgency, got status code: %d", httpResp.StatusCode()) |
| 47 | + } |
| 48 | + |
| 49 | + log.Printf("[INFO] Deleted alert urgency %s", alertUrgency.Attributes.Name) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if httpResp.ApplicationvndApiJSON200.Links.Next == nil { |
| 54 | + break |
| 55 | + } |
| 56 | + |
| 57 | + params.PageNumber = ptr.Ptr(ptr.Value(params.PageNumber) + 1) |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | + }, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +func TestAccResourceAlertUrgency(t *testing.T) { |
| 66 | + resName := "rootly_alert_urgency.test" |
| 67 | + alertUrgencyName := acctest.RandomWithPrefix("tf-alert-urgency") |
| 68 | + |
| 69 | + configStateChecks := []statecheck.StateCheck{ |
| 70 | + statecheck.ExpectKnownValue(resName, tfjsonpath.New("id"), knownvalue.NotNull()), |
| 71 | + statecheck.ExpectKnownValue(resName, tfjsonpath.New("position"), knownvalue.NotNull()), |
| 72 | + } |
| 73 | + |
| 74 | + resource.UnitTest(t, resource.TestCase{ |
| 75 | + PreCheck: func() { testAccPreCheck(t) }, |
| 76 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 77 | + Steps: []resource.TestStep{ |
| 78 | + { |
| 79 | + Config: testAccResourceAlertUrgency(alertUrgencyName, "description"), |
| 80 | + ConfigStateChecks: append( |
| 81 | + configStateChecks, |
| 82 | + statecheck.ExpectKnownValue(resName, tfjsonpath.New("name"), knownvalue.StringExact(alertUrgencyName)), |
| 83 | + statecheck.ExpectKnownValue(resName, tfjsonpath.New("description"), knownvalue.StringExact("description")), |
| 84 | + ), |
| 85 | + }, |
| 86 | + { |
| 87 | + Config: testAccResourceAlertUrgency(alertUrgencyName+"-updated", "updated description"), |
| 88 | + ConfigStateChecks: append( |
| 89 | + configStateChecks, |
| 90 | + statecheck.ExpectKnownValue(resName, tfjsonpath.New("name"), knownvalue.StringExact(alertUrgencyName+"-updated")), |
| 91 | + statecheck.ExpectKnownValue(resName, tfjsonpath.New("description"), knownvalue.StringExact("updated description")), |
| 92 | + ), |
| 93 | + }, |
| 94 | + }, |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func testAccResourceAlertUrgency(alertUrgencyName, alertUrgencyDescription string) string { |
| 99 | + return fmt.Sprintf(` |
| 100 | +resource "rootly_alert_urgency" "test" { |
| 101 | + name = "%s" |
| 102 | + description = "%s" |
| 103 | +} |
| 104 | +`, alertUrgencyName, alertUrgencyDescription) |
| 105 | +} |
0 commit comments