|
| 1 | +package scaleway |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 9 | + accountV2 "github.com/scaleway/scaleway-sdk-go/api/account/v2" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + if !terraformBetaEnabled { |
| 15 | + return |
| 16 | + } |
| 17 | + resource.AddTestSweepers("scaleway_account_project", &resource.Sweeper{ |
| 18 | + Name: "scaleway_account_project", |
| 19 | + F: testSweepAccountproject, |
| 20 | + }) |
| 21 | +} |
| 22 | + |
| 23 | +func testSweepAccountproject(_ string) error { |
| 24 | + return sweep(func(scwClient *scw.Client) error { |
| 25 | + accountAPI := accountV2.NewAPI(scwClient) |
| 26 | + |
| 27 | + l.Debugf("sweeper: destroying the project") |
| 28 | + |
| 29 | + listProjects, err := accountAPI.ListProjects(&accountV2.ListProjectsRequest{}) |
| 30 | + if err != nil { |
| 31 | + return fmt.Errorf("failed to list projects: %w", err) |
| 32 | + } |
| 33 | + for _, project := range listProjects.Projects { |
| 34 | + if project.Name == "default" { |
| 35 | + continue |
| 36 | + } |
| 37 | + err = accountAPI.DeleteProject(&accountV2.DeleteProjectRequest{ |
| 38 | + ProjectID: project.ID, |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + return fmt.Errorf("failed to delete project: %w", err) |
| 42 | + } |
| 43 | + } |
| 44 | + return nil |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +func TestAccScalewayAccountProject_Basic(t *testing.T) { |
| 49 | + SkipBetaTest(t) |
| 50 | + tt := NewTestTools(t) |
| 51 | + defer tt.Cleanup() |
| 52 | + resource.ParallelTest(t, resource.TestCase{ |
| 53 | + ProviderFactories: tt.ProviderFactories, |
| 54 | + CheckDestroy: testAccCheckScalewayAccountProjectDestroy(tt), |
| 55 | + Steps: []resource.TestStep{ |
| 56 | + { |
| 57 | + Config: ` |
| 58 | + resource "scaleway_account_project" "main" { |
| 59 | + name = "tf_tests_project_basic" |
| 60 | + description = "a description" |
| 61 | + } |
| 62 | + `, |
| 63 | + Check: resource.ComposeTestCheckFunc( |
| 64 | + testAccCheckScalewayAccountProjectExists(tt, "scaleway_account_project.main"), |
| 65 | + resource.TestCheckResourceAttr("scaleway_account_project.main", "name", "tf_tests_project_basic"), |
| 66 | + resource.TestCheckResourceAttr("scaleway_account_project.main", "description", "a description"), |
| 67 | + ), |
| 68 | + }, |
| 69 | + { |
| 70 | + Config: ` |
| 71 | + resource "scaleway_account_project" "main" { |
| 72 | + name = "tf_tests_project_basic_rename" |
| 73 | + description = "another description" |
| 74 | + } |
| 75 | + `, |
| 76 | + Check: resource.ComposeTestCheckFunc( |
| 77 | + testAccCheckScalewayAccountProjectExists(tt, "scaleway_account_project.main"), |
| 78 | + resource.TestCheckResourceAttr("scaleway_account_project.main", "name", "tf_tests_project_basic_rename"), |
| 79 | + resource.TestCheckResourceAttr("scaleway_account_project.main", "description", "another description"), |
| 80 | + ), |
| 81 | + }, |
| 82 | + }, |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func TestAccScalewayAccountProject_NoUpdate(t *testing.T) { |
| 87 | + SkipBetaTest(t) |
| 88 | + tt := NewTestTools(t) |
| 89 | + defer tt.Cleanup() |
| 90 | + resource.ParallelTest(t, resource.TestCase{ |
| 91 | + ProviderFactories: tt.ProviderFactories, |
| 92 | + CheckDestroy: testAccCheckScalewayAccountProjectDestroy(tt), |
| 93 | + Steps: []resource.TestStep{ |
| 94 | + { |
| 95 | + Config: ` |
| 96 | + resource "scaleway_account_project" "main" { |
| 97 | + name = "tf_tests_project_noupdate" |
| 98 | + } |
| 99 | + `, |
| 100 | + Check: resource.ComposeTestCheckFunc( |
| 101 | + testAccCheckScalewayAccountProjectExists(tt, "scaleway_account_project.main"), |
| 102 | + ), |
| 103 | + }, |
| 104 | + { |
| 105 | + Config: ` |
| 106 | + resource "scaleway_account_project" "main" { |
| 107 | + name = "tf_tests_project_noupdate" |
| 108 | + } |
| 109 | + `, |
| 110 | + Check: resource.ComposeTestCheckFunc( |
| 111 | + testAccCheckScalewayAccountProjectExists(tt, "scaleway_account_project.main"), |
| 112 | + ), |
| 113 | + }, |
| 114 | + }, |
| 115 | + }) |
| 116 | +} |
| 117 | + |
| 118 | +func testAccCheckScalewayAccountProjectExists(tt *TestTools, name string) resource.TestCheckFunc { |
| 119 | + return func(s *terraform.State) error { |
| 120 | + rs, ok := s.RootModule().Resources[name] |
| 121 | + if !ok { |
| 122 | + return fmt.Errorf("resource not found: %s", name) |
| 123 | + } |
| 124 | + |
| 125 | + accountAPI := accountV2API(tt.Meta) |
| 126 | + |
| 127 | + _, err := accountAPI.GetProject(&accountV2.GetProjectRequest{ |
| 128 | + ProjectID: rs.Primary.ID, |
| 129 | + }) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("could not find project: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +func testAccCheckScalewayAccountProjectDestroy(tt *TestTools) resource.TestCheckFunc { |
| 139 | + return func(s *terraform.State) error { |
| 140 | + for _, rs := range s.RootModule().Resources { |
| 141 | + if rs.Type != "scaleway_account_project" { |
| 142 | + continue |
| 143 | + } |
| 144 | + |
| 145 | + accountAPI := accountV2API(tt.Meta) |
| 146 | + |
| 147 | + _, err := accountAPI.GetProject(&accountV2.GetProjectRequest{ |
| 148 | + ProjectID: rs.Primary.ID, |
| 149 | + }) |
| 150 | + |
| 151 | + // If no error resource still exist |
| 152 | + if err == nil { |
| 153 | + return fmt.Errorf("resource %s(%s) still exist", rs.Type, rs.Primary.ID) |
| 154 | + } |
| 155 | + |
| 156 | + // Unexpected api error we return it |
| 157 | + if !is404Error(err) { |
| 158 | + return err |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + return nil |
| 163 | + } |
| 164 | +} |
0 commit comments