|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "fmt" |
| 7 | + "maps" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/config" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" |
| 13 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 15 | + stackitSdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config" |
| 16 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 17 | + "github.com/stackitcloud/stackit-sdk-go/services/git" |
| 18 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 19 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil" |
| 20 | +) |
| 21 | + |
| 22 | +//go:embed testdata/resource.tf |
| 23 | +var resourceConfig string |
| 24 | + |
| 25 | +var name = fmt.Sprintf("git-%s-instance", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) |
| 26 | +var nameUpdated = fmt.Sprintf("git-%s-instance", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)) |
| 27 | + |
| 28 | +var testConfigVars = config.Variables{ |
| 29 | + "project_id": config.StringVariable(testutil.ProjectId), |
| 30 | + "name": config.StringVariable(name), |
| 31 | +} |
| 32 | + |
| 33 | +func testConfigVarsUpdated() config.Variables { |
| 34 | + tempConfig := make(config.Variables, len(testConfigVars)) |
| 35 | + maps.Copy(tempConfig, testConfigVars) |
| 36 | + // update git instance to a new name |
| 37 | + // should trigger creating a new instance |
| 38 | + tempConfig["name"] = config.StringVariable(nameUpdated) |
| 39 | + return tempConfig |
| 40 | +} |
| 41 | + |
| 42 | +func TestGitInstance(t *testing.T) { |
| 43 | + resource.Test(t, resource.TestCase{ |
| 44 | + ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, |
| 45 | + CheckDestroy: testAccCheckGitInstanceDestroy, |
| 46 | + Steps: []resource.TestStep{ |
| 47 | + // Creation |
| 48 | + { |
| 49 | + ConfigVariables: testConfigVars, |
| 50 | + Config: testutil.GitProviderConfig() + resourceConfig, |
| 51 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 52 | + resource.TestCheckResourceAttr("stackit_git.git", "project_id", testutil.ConvertConfigVariable(testConfigVars["project_id"])), |
| 53 | + resource.TestCheckResourceAttr("stackit_git.git", "name", testutil.ConvertConfigVariable(testConfigVars["name"])), |
| 54 | + resource.TestCheckResourceAttrSet("stackit_git.git", "url"), |
| 55 | + resource.TestCheckResourceAttrSet("stackit_git.git", "version"), |
| 56 | + resource.TestCheckResourceAttrSet("stackit_git.git", "instance_id"), |
| 57 | + ), |
| 58 | + }, |
| 59 | + // Data source |
| 60 | + { |
| 61 | + ConfigVariables: testConfigVars, |
| 62 | + Config: fmt.Sprintf(` |
| 63 | + %s |
| 64 | +
|
| 65 | + data "stackit_git" "git" { |
| 66 | + project_id = stackit_git.git.project_id |
| 67 | + instance_id = stackit_git.git.instance_id |
| 68 | + } |
| 69 | + `, testutil.GitProviderConfig()+resourceConfig, |
| 70 | + ), |
| 71 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 72 | + // Instance |
| 73 | + resource.TestCheckResourceAttr("data.stackit_git.git", "project_id", testutil.ConvertConfigVariable(testConfigVars["project_id"])), |
| 74 | + resource.TestCheckResourceAttrPair( |
| 75 | + "stackit_git.git", "project_id", |
| 76 | + "data.stackit_git.git", "project_id", |
| 77 | + ), |
| 78 | + resource.TestCheckResourceAttrPair( |
| 79 | + "stackit_git.git", "instance_id", |
| 80 | + "data.stackit_git.git", "instance_id", |
| 81 | + ), |
| 82 | + resource.TestCheckResourceAttrPair( |
| 83 | + "stackit_git.git", "name", |
| 84 | + "data.stackit_git.git", "name", |
| 85 | + ), |
| 86 | + resource.TestCheckResourceAttrPair( |
| 87 | + "stackit_git.git", "url", |
| 88 | + "data.stackit_git.git", "url", |
| 89 | + ), |
| 90 | + resource.TestCheckResourceAttrPair( |
| 91 | + "stackit_git.git", "version", |
| 92 | + "data.stackit_git.git", "version", |
| 93 | + ), |
| 94 | + ), |
| 95 | + }, |
| 96 | + // Import |
| 97 | + { |
| 98 | + ConfigVariables: testConfigVars, |
| 99 | + ResourceName: "stackit_git.git", |
| 100 | + ImportStateIdFunc: func(s *terraform.State) (string, error) { |
| 101 | + r, ok := s.RootModule().Resources["stackit_git.git"] |
| 102 | + if !ok { |
| 103 | + return "", fmt.Errorf("couldn't find resource stackit_git.git") |
| 104 | + } |
| 105 | + instanceId, ok := r.Primary.Attributes["instance_id"] |
| 106 | + if !ok { |
| 107 | + return "", fmt.Errorf("couldn't find attribute instance_id") |
| 108 | + } |
| 109 | + return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil |
| 110 | + }, |
| 111 | + ImportState: true, |
| 112 | + ImportStateVerify: true, |
| 113 | + }, |
| 114 | + // Update |
| 115 | + { |
| 116 | + ConfigVariables: testConfigVarsUpdated(), |
| 117 | + Config: testutil.GitProviderConfig() + resourceConfig, |
| 118 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 119 | + resource.TestCheckResourceAttr("stackit_git.git", "project_id", testutil.ConvertConfigVariable(testConfigVars["project_id"])), |
| 120 | + resource.TestCheckResourceAttr("stackit_git.git", "name", testutil.ConvertConfigVariable(testConfigVarsUpdated()["name"])), |
| 121 | + resource.TestCheckResourceAttrSet("stackit_git.git", "url"), |
| 122 | + resource.TestCheckResourceAttrSet("stackit_git.git", "version"), |
| 123 | + resource.TestCheckResourceAttrSet("stackit_git.git", "instance_id"), |
| 124 | + ), |
| 125 | + }, |
| 126 | + // Deletion is done by the framework implicitly |
| 127 | + }, |
| 128 | + }) |
| 129 | +} |
| 130 | + |
| 131 | +func testAccCheckGitInstanceDestroy(s *terraform.State) error { |
| 132 | + ctx := context.Background() |
| 133 | + var client *git.APIClient |
| 134 | + var err error |
| 135 | + |
| 136 | + if testutil.GitCustomEndpoint == "" { |
| 137 | + client, err = git.NewAPIClient() |
| 138 | + } else { |
| 139 | + client, err = git.NewAPIClient( |
| 140 | + stackitSdkConfig.WithEndpoint(testutil.GitCustomEndpoint), |
| 141 | + ) |
| 142 | + } |
| 143 | + |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("creating client: %w", err) |
| 146 | + } |
| 147 | + |
| 148 | + var instancesToDestroy []string |
| 149 | + for _, rs := range s.RootModule().Resources { |
| 150 | + if rs.Type != "stackit_git" { |
| 151 | + continue |
| 152 | + } |
| 153 | + instanceId := strings.Split(rs.Primary.ID, core.Separator)[1] |
| 154 | + instancesToDestroy = append(instancesToDestroy, instanceId) |
| 155 | + } |
| 156 | + |
| 157 | + instancesResp, err := client.ListInstances(ctx, testutil.ProjectId).Execute() |
| 158 | + if err != nil { |
| 159 | + return fmt.Errorf("getting git instances: %w", err) |
| 160 | + } |
| 161 | + |
| 162 | + gitInstances := *instancesResp.Instances |
| 163 | + for i := range gitInstances { |
| 164 | + if gitInstances[i].Id == nil { |
| 165 | + continue |
| 166 | + } |
| 167 | + if utils.Contains(instancesToDestroy, *gitInstances[i].Id) { |
| 168 | + err := client.DeleteInstance(ctx, testutil.ProjectId, *gitInstances[i].Id).Execute() |
| 169 | + if err != nil { |
| 170 | + return fmt.Errorf("destroying git instance %s during CheckDestroy: %w", *gitInstances[i].Id, err) |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
0 commit comments