|
| 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 | + iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func init() { |
| 14 | + resource.AddTestSweepers("scaleway_iam_application", &resource.Sweeper{ |
| 15 | + Name: "scaleway_iam_application", |
| 16 | + F: testSweepIamApplication, |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +func testSweepIamApplication(_ string) error { |
| 21 | + return sweep(func(scwClient *scw.Client) error { |
| 22 | + api := iam.NewAPI(scwClient) |
| 23 | + |
| 24 | + listApps, err := api.ListApplications(&iam.ListApplicationsRequest{}) |
| 25 | + if err != nil { |
| 26 | + return fmt.Errorf("failed to list applications: %w", err) |
| 27 | + } |
| 28 | + for _, app := range listApps.Applications { |
| 29 | + err = api.DeleteApplication(&iam.DeleteApplicationRequest{ |
| 30 | + ApplicationID: app.ID, |
| 31 | + }) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("failed to delete application: %w", err) |
| 34 | + } |
| 35 | + } |
| 36 | + return nil |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +func TestAccScalewayIamApplication_Basic(t *testing.T) { |
| 41 | + SkipBetaTest(t) |
| 42 | + tt := NewTestTools(t) |
| 43 | + defer tt.Cleanup() |
| 44 | + resource.ParallelTest(t, resource.TestCase{ |
| 45 | + ProviderFactories: tt.ProviderFactories, |
| 46 | + CheckDestroy: testAccCheckScalewayIamApplicationDestroy(tt), |
| 47 | + Steps: []resource.TestStep{ |
| 48 | + { |
| 49 | + Config: ` |
| 50 | + resource "scaleway_iam_application" "main" { |
| 51 | + name = "tf_tests_app_basic" |
| 52 | + description = "a description" |
| 53 | + } |
| 54 | + `, |
| 55 | + Check: resource.ComposeTestCheckFunc( |
| 56 | + testAccCheckScalewayIamApplicationExists(tt, "scaleway_iam_application.main"), |
| 57 | + resource.TestCheckResourceAttr("scaleway_iam_application.main", "name", "tf_tests_app_basic"), |
| 58 | + resource.TestCheckResourceAttr("scaleway_iam_application.main", "description", "a description"), |
| 59 | + ), |
| 60 | + }, |
| 61 | + { |
| 62 | + Config: ` |
| 63 | + resource "scaleway_iam_application" "main" { |
| 64 | + name = "tf_tests_app_basic_rename" |
| 65 | + description = "another description" |
| 66 | + } |
| 67 | + `, |
| 68 | + Check: resource.ComposeTestCheckFunc( |
| 69 | + testAccCheckScalewayIamApplicationExists(tt, "scaleway_iam_application.main"), |
| 70 | + resource.TestCheckResourceAttr("scaleway_iam_application.main", "name", "tf_tests_app_basic_rename"), |
| 71 | + resource.TestCheckResourceAttr("scaleway_iam_application.main", "description", "another description"), |
| 72 | + ), |
| 73 | + }, |
| 74 | + }, |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +func TestAccScalewayIamApplication_NoUpdate(t *testing.T) { |
| 79 | + SkipBetaTest(t) |
| 80 | + tt := NewTestTools(t) |
| 81 | + defer tt.Cleanup() |
| 82 | + |
| 83 | + resource.ParallelTest(t, resource.TestCase{ |
| 84 | + ProviderFactories: tt.ProviderFactories, |
| 85 | + CheckDestroy: testAccCheckScalewayIamApplicationDestroy(tt), |
| 86 | + Steps: []resource.TestStep{ |
| 87 | + { |
| 88 | + Config: ` |
| 89 | + resource "scaleway_iam_application" "main" { |
| 90 | + name = "tf_tests_app_noupdate" |
| 91 | + } |
| 92 | + `, |
| 93 | + Check: resource.ComposeTestCheckFunc( |
| 94 | + testAccCheckScalewayIamApplicationExists(tt, "scaleway_iam_application.main"), |
| 95 | + ), |
| 96 | + }, |
| 97 | + { |
| 98 | + Config: ` |
| 99 | + resource "scaleway_iam_application" "main" { |
| 100 | + name = "tf_tests_app_noupdate" |
| 101 | + } |
| 102 | + `, |
| 103 | + Check: resource.ComposeTestCheckFunc( |
| 104 | + testAccCheckScalewayIamApplicationExists(tt, "scaleway_iam_application.main"), |
| 105 | + ), |
| 106 | + }, |
| 107 | + }, |
| 108 | + }) |
| 109 | +} |
| 110 | + |
| 111 | +func testAccCheckScalewayIamApplicationExists(tt *TestTools, name string) resource.TestCheckFunc { |
| 112 | + return func(s *terraform.State) error { |
| 113 | + rs, ok := s.RootModule().Resources[name] |
| 114 | + if !ok { |
| 115 | + return fmt.Errorf("resource not found: %s", name) |
| 116 | + } |
| 117 | + |
| 118 | + iamAPI := iamAPI(tt.Meta) |
| 119 | + |
| 120 | + _, err := iamAPI.GetApplication(&iam.GetApplicationRequest{ |
| 121 | + ApplicationID: rs.Primary.ID, |
| 122 | + }) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("could not find application: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + return nil |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func testAccCheckScalewayIamApplicationDestroy(tt *TestTools) resource.TestCheckFunc { |
| 132 | + return func(s *terraform.State) error { |
| 133 | + for _, rs := range s.RootModule().Resources { |
| 134 | + if rs.Type != "scaleway_iam_application" { |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + iamAPI := iamAPI(tt.Meta) |
| 139 | + |
| 140 | + _, err := iamAPI.GetApplication(&iam.GetApplicationRequest{ |
| 141 | + ApplicationID: rs.Primary.ID, |
| 142 | + }) |
| 143 | + |
| 144 | + // If no error resource still exist |
| 145 | + if err == nil { |
| 146 | + return fmt.Errorf("resource %s(%s) still exist", rs.Type, rs.Primary.ID) |
| 147 | + } |
| 148 | + |
| 149 | + // Unexpected api error we return it |
| 150 | + if !is404Error(err) { |
| 151 | + return err |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | + } |
| 157 | +} |
0 commit comments