|
| 1 | +package iaas_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" |
| 10 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/terraform" |
| 12 | + "github.com/stackitcloud/stackit-sdk-go/core/config" |
| 13 | + "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 14 | + "github.com/stackitcloud/stackit-sdk-go/services/iaas" |
| 15 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 16 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil" |
| 17 | +) |
| 18 | + |
| 19 | +// Network resource data |
| 20 | +var networkResource = map[string]string{ |
| 21 | + "project_id": testutil.ProjectId, |
| 22 | + "name": fmt.Sprintf("acc-test-%s", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)), |
| 23 | + "ipv4_prefix_length": "24", |
| 24 | + "nameserver0": "1.2.3.4", |
| 25 | + "nameserver1": "5.6.7.8", |
| 26 | +} |
| 27 | + |
| 28 | +func resourceConfig(name, nameservers string) string { |
| 29 | + return fmt.Sprintf(` |
| 30 | + %s |
| 31 | +
|
| 32 | + resource "stackit_network" "network" { |
| 33 | + project_id = "%s" |
| 34 | + name = "%s" |
| 35 | + ipv4_prefix_length = "%s" |
| 36 | + nameservers = %s |
| 37 | + } |
| 38 | + `, |
| 39 | + testutil.IaaSProviderConfig(), |
| 40 | + networkResource["project_id"], |
| 41 | + name, |
| 42 | + networkResource["ipv4_prefix_length"], |
| 43 | + nameservers, |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +func TestAccIaaS(t *testing.T) { |
| 48 | + resource.Test(t, resource.TestCase{ |
| 49 | + ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, |
| 50 | + CheckDestroy: testAccCheckIaaSDestroy, |
| 51 | + Steps: []resource.TestStep{ |
| 52 | + |
| 53 | + // Creation |
| 54 | + { |
| 55 | + Config: resourceConfig( |
| 56 | + networkResource["name"], |
| 57 | + fmt.Sprintf( |
| 58 | + "[%q]", |
| 59 | + networkResource["nameserver0"], |
| 60 | + ), |
| 61 | + ), |
| 62 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 63 | + // Instance |
| 64 | + resource.TestCheckResourceAttr("stackit_network.network", "project_id", networkResource["project_id"]), |
| 65 | + resource.TestCheckResourceAttrSet("stackit_network.network", "network_id"), |
| 66 | + resource.TestCheckResourceAttr("stackit_network.network", "name", networkResource["name"]), |
| 67 | + resource.TestCheckResourceAttr("stackit_network.network", "nameservers.#", "1"), |
| 68 | + resource.TestCheckResourceAttr("stackit_network.network", "nameservers.0", networkResource["nameserver0"]), |
| 69 | + ), |
| 70 | + }, |
| 71 | + // Data source |
| 72 | + { |
| 73 | + Config: fmt.Sprintf(` |
| 74 | + %s |
| 75 | +
|
| 76 | + data "stackit_network" "network" { |
| 77 | + project_id = stackit_network.network.project_id |
| 78 | + network_id = stackit_network.network.network_id |
| 79 | + }`, |
| 80 | + resourceConfig( |
| 81 | + networkResource["name"], |
| 82 | + fmt.Sprintf( |
| 83 | + "[%q]", |
| 84 | + networkResource["nameserver0"], |
| 85 | + ), |
| 86 | + ), |
| 87 | + ), |
| 88 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 89 | + // Instance |
| 90 | + resource.TestCheckResourceAttr("data.stackit_network.network", "project_id", networkResource["project_id"]), |
| 91 | + resource.TestCheckResourceAttrPair( |
| 92 | + "stackit_network.network", "network_id", |
| 93 | + "data.stackit_network.network", "network_id", |
| 94 | + ), |
| 95 | + resource.TestCheckResourceAttr("data.stackit_network.network", "name", networkResource["name"]), |
| 96 | + resource.TestCheckResourceAttr("data.stackit_network.network", "nameservers.0", networkResource["nameserver0"]), |
| 97 | + ), |
| 98 | + }, |
| 99 | + // Import |
| 100 | + { |
| 101 | + ResourceName: "stackit_network.network", |
| 102 | + ImportStateIdFunc: func(s *terraform.State) (string, error) { |
| 103 | + r, ok := s.RootModule().Resources["stackit_network.network"] |
| 104 | + if !ok { |
| 105 | + return "", fmt.Errorf("couldn't find resource stackit_network.network") |
| 106 | + } |
| 107 | + networkId, ok := r.Primary.Attributes["network_id"] |
| 108 | + if !ok { |
| 109 | + return "", fmt.Errorf("couldn't find attribute network_id") |
| 110 | + } |
| 111 | + return fmt.Sprintf("%s,%s", testutil.ProjectId, networkId), nil |
| 112 | + }, |
| 113 | + ImportState: true, |
| 114 | + ImportStateVerify: true, |
| 115 | + ImportStateVerifyIgnore: []string{"ipv4_prefix_length"}, // Field is not returned by the API |
| 116 | + }, |
| 117 | + // Update |
| 118 | + { |
| 119 | + Config: resourceConfig( |
| 120 | + fmt.Sprintf("%s-updated", networkResource["name"]), |
| 121 | + fmt.Sprintf( |
| 122 | + "[%q, %q]", |
| 123 | + networkResource["nameserver0"], |
| 124 | + networkResource["nameserver1"], |
| 125 | + ), |
| 126 | + ), |
| 127 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 128 | + // Instance |
| 129 | + resource.TestCheckResourceAttr("stackit_network.network", "project_id", networkResource["project_id"]), |
| 130 | + resource.TestCheckResourceAttrSet("stackit_network.network", "network_id"), |
| 131 | + resource.TestCheckResourceAttr("stackit_network.network", "name", fmt.Sprintf("%s-updated", networkResource["name"])), |
| 132 | + resource.TestCheckResourceAttr("stackit_network.network", "nameservers.#", "2"), |
| 133 | + resource.TestCheckResourceAttr("stackit_network.network", "nameservers.0", networkResource["nameserver0"]), |
| 134 | + resource.TestCheckResourceAttr("stackit_network.network", "nameservers.1", networkResource["nameserver1"]), |
| 135 | + ), |
| 136 | + }, |
| 137 | + // Deletion is done by the framework implicitly |
| 138 | + }, |
| 139 | + }) |
| 140 | +} |
| 141 | + |
| 142 | +func testAccCheckIaaSDestroy(s *terraform.State) error { |
| 143 | + ctx := context.Background() |
| 144 | + var client *iaas.APIClient |
| 145 | + var err error |
| 146 | + if testutil.IaaSCustomEndpoint == "" { |
| 147 | + client, err = iaas.NewAPIClient( |
| 148 | + config.WithRegion("eu01"), |
| 149 | + ) |
| 150 | + } else { |
| 151 | + client, err = iaas.NewAPIClient( |
| 152 | + config.WithEndpoint(testutil.IaaSCustomEndpoint), |
| 153 | + ) |
| 154 | + } |
| 155 | + if err != nil { |
| 156 | + return fmt.Errorf("creating client: %w", err) |
| 157 | + } |
| 158 | + |
| 159 | + networksToDestroy := []string{} |
| 160 | + for _, rs := range s.RootModule().Resources { |
| 161 | + if rs.Type != "stackit_network" { |
| 162 | + continue |
| 163 | + } |
| 164 | + // network terraform ID: "[project_id],[network_id]" |
| 165 | + networkId := strings.Split(rs.Primary.ID, core.Separator)[1] |
| 166 | + networksToDestroy = append(networksToDestroy, networkId) |
| 167 | + } |
| 168 | + |
| 169 | + networksResp, err := client.ListNetworksExecute(ctx, testutil.ProjectId) |
| 170 | + if err != nil { |
| 171 | + return fmt.Errorf("getting networksResp: %w", err) |
| 172 | + } |
| 173 | + |
| 174 | + networks := *networksResp.Items |
| 175 | + for i := range networks { |
| 176 | + if networks[i].NetworkId == nil { |
| 177 | + continue |
| 178 | + } |
| 179 | + if utils.Contains(networksToDestroy, *networks[i].NetworkId) { |
| 180 | + err := client.DeleteNetworkExecute(ctx, testutil.ProjectId, *networks[i].NetworkId) |
| 181 | + if err != nil { |
| 182 | + return fmt.Errorf("destroying network %s during CheckDestroy: %w", *networks[i].NetworkId, err) |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + return nil |
| 187 | +} |
0 commit comments