|
| 1 | +package gcore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/G-Core/gcorelabs-storage-sdk-go/swagger/client/storage" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func TestStorageS3DataSource(t *testing.T) { |
| 14 | + random := time.Now().Nanosecond() |
| 15 | + resourceName := fmt.Sprintf("gcore_storage_s3.terraformtest%d_s3", random) |
| 16 | + dataSourceName := fmt.Sprintf("data.gcore_storage_s3.terraformtest%d_s3_data", random) |
| 17 | + |
| 18 | + templateCreate := func() string { |
| 19 | + return fmt.Sprintf(` |
| 20 | +resource "gcore_storage_s3" "terraformtest%d_s3" { |
| 21 | + name = "terraformtest%d" |
| 22 | + location = "s-ed1" |
| 23 | +} |
| 24 | + `, random, random) |
| 25 | + } |
| 26 | + |
| 27 | + templateRead := func() string { |
| 28 | + return fmt.Sprintf(` |
| 29 | +data "gcore_storage_s3" "terraformtest%d_s3_data" { |
| 30 | + name = "terraformtest%d" |
| 31 | +} |
| 32 | + `, random, random) |
| 33 | + } |
| 34 | + |
| 35 | + resource.Test(t, resource.TestCase{ |
| 36 | + PreCheck: func() { |
| 37 | + testAccPreCheckVars(t, GCORE_USERNAME_VAR, GCORE_PASSWORD_VAR, GCORE_STORAGE_URL_VAR) |
| 38 | + }, |
| 39 | + CheckDestroy: func(s *terraform.State) error { |
| 40 | + config := testAccProvider.Meta().(*Config) |
| 41 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 42 | + defer cancel() |
| 43 | + |
| 44 | + for _, rs := range s.RootModule().Resources { |
| 45 | + if rs.Type != "gcore_storage_s3" { |
| 46 | + continue |
| 47 | + } |
| 48 | + opts := []func(opt *storage.StorageListHTTPV2Params){ |
| 49 | + func(opt *storage.StorageListHTTPV2Params) { opt.Context = ctx }, |
| 50 | + func(opt *storage.StorageListHTTPV2Params) { opt.ID = &rs.Primary.ID }, |
| 51 | + } |
| 52 | + storages, err := config.StorageClient.StoragesList(opts...) |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("find storage: %w", err) |
| 55 | + } |
| 56 | + if len(storages) == 0 { |
| 57 | + return nil |
| 58 | + } |
| 59 | + if storages[0].ProvisioningStatus == "ok" { |
| 60 | + return fmt.Errorf("storage #%s wasn't deleted correctrly", rs.Primary.ID) |
| 61 | + } |
| 62 | + } |
| 63 | + return nil |
| 64 | + }, |
| 65 | + ProviderFactories: testAccProviders, |
| 66 | + Steps: []resource.TestStep{ |
| 67 | + { |
| 68 | + Config: templateCreate(), |
| 69 | + Check: resource.ComposeTestCheckFunc( |
| 70 | + testAccCheckResourceExists(resourceName), |
| 71 | + resource.TestCheckResourceAttr(resourceName, StorageSchemaLocation, "s-ed1"), |
| 72 | + ), |
| 73 | + }, |
| 74 | + { |
| 75 | + Config: templateRead(), |
| 76 | + Check: resource.ComposeTestCheckFunc( |
| 77 | + testAccCheckResourceExists(dataSourceName), |
| 78 | + resource.TestCheckResourceAttr(dataSourceName, StorageSchemaLocation, "s-ed1"), |
| 79 | + ), |
| 80 | + }, |
| 81 | + }, |
| 82 | + }) |
| 83 | +} |
0 commit comments