|
1 | 1 | package mongodb_test |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | | - "strings" |
| 4 | + "context" |
| 5 | + "fmt" |
6 | 6 | "testing" |
7 | 7 |
|
8 | 8 | "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
9 | 9 | "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 10 | + mongodbSDK "github.com/scaleway/scaleway-sdk-go/api/mongodb/v1" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
10 | 12 | "github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest" |
| 13 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" |
11 | 14 | ) |
12 | 15 |
|
13 | 16 | func TestAccActionMongoDBInstanceSnapshot_Basic(t *testing.T) { |
@@ -75,35 +78,44 @@ func TestAccActionMongoDBInstanceSnapshot_Basic(t *testing.T) { |
75 | 78 | wait = true |
76 | 79 | } |
77 | 80 | } |
78 | | -
|
79 | | - data "scaleway_audit_trail_event" "mongodb" { |
80 | | - resource_type = "mongodb_instance" |
81 | | - resource_id = scaleway_mongodb_instance.main.id |
82 | | - method_name = "CreateSnapshot" |
83 | | - } |
84 | 81 | `, |
85 | 82 | Check: resource.ComposeTestCheckFunc( |
86 | | - resource.TestCheckResourceAttrSet("data.scaleway_audit_trail_event.mongodb", "events.#"), |
87 | | - func(state *terraform.State) error { |
88 | | - rs, ok := state.RootModule().Resources["data.scaleway_audit_trail_event.mongodb"] |
89 | | - if !ok { |
90 | | - return errors.New("not found: data.scaleway_audit_trail_event.mongodb") |
91 | | - } |
92 | | - |
93 | | - for key, value := range rs.Primary.Attributes { |
94 | | - if !strings.Contains(key, "request_body") { |
95 | | - continue |
96 | | - } |
97 | | - |
98 | | - if strings.Contains(value, "tf-acc-mongodb-instance-snapshot-action") { |
99 | | - return nil |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - return errors.New("did not find the CreateSnapshot event") |
104 | | - }, |
| 83 | + isSnapshotCreated(tt, "scaleway_mongodb_instance.main", "tf-acc-mongodb-instance-snapshot-action"), |
105 | 84 | ), |
106 | 85 | }, |
107 | 86 | }, |
108 | 87 | }) |
109 | 88 | } |
| 89 | + |
| 90 | +func isSnapshotCreated(tt *acctest.TestTools, instanceResourceName, snapshotName string) resource.TestCheckFunc { |
| 91 | + return func(state *terraform.State) error { |
| 92 | + rs, ok := state.RootModule().Resources[instanceResourceName] |
| 93 | + if !ok { |
| 94 | + return fmt.Errorf("resource not found: %s", instanceResourceName) |
| 95 | + } |
| 96 | + |
| 97 | + instanceID := rs.Primary.ID |
| 98 | + region, id, err := regional.ParseID(instanceID) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to parse instance ID: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | + api := mongodbSDK.NewAPI(tt.Meta.ScwClient()) |
| 104 | + |
| 105 | + snapshots, err := api.ListSnapshots(&mongodbSDK.ListSnapshotsRequest{ |
| 106 | + Region: region, |
| 107 | + InstanceID: &id, |
| 108 | + }, scw.WithAllPages(), scw.WithContext(context.Background())) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("failed to list snapshots: %w", err) |
| 111 | + } |
| 112 | + |
| 113 | + for _, snapshot := range snapshots.Snapshots { |
| 114 | + if snapshot.Name == snapshotName { |
| 115 | + return nil |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return fmt.Errorf("snapshot with name %q not found for instance %s", snapshotName, instanceID) |
| 120 | + } |
| 121 | +} |
0 commit comments