|
| 1 | +//go:build integration || tag |
| 2 | + |
| 3 | +package tag_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" |
| 12 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 13 | + "github.com/linode/linodego" |
| 14 | + "github.com/linode/terraform-provider-linode/v3/linode/acceptance" |
| 15 | + "github.com/linode/terraform-provider-linode/v3/linode/tag/tmpl" |
| 16 | +) |
| 17 | + |
| 18 | +func init() { |
| 19 | + resource.AddTestSweepers("linode_tag", &resource.Sweeper{ |
| 20 | + Name: "linode_tag", |
| 21 | + F: sweep, |
| 22 | + }) |
| 23 | +} |
| 24 | + |
| 25 | +func sweep(prefix string) error { |
| 26 | + client, err := acceptance.GetTestClient() |
| 27 | + if err != nil { |
| 28 | + return fmt.Errorf("Error getting client: %s", err) |
| 29 | + } |
| 30 | + |
| 31 | + tags, err := client.ListTags(context.Background(), nil) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("Error getting tags: %s", err) |
| 34 | + } |
| 35 | + |
| 36 | + for _, tag := range tags { |
| 37 | + if !acceptance.ShouldSweep(prefix, tag.Label) { |
| 38 | + continue |
| 39 | + } |
| 40 | + if err := client.DeleteTag(context.Background(), tag.Label); err != nil { |
| 41 | + return fmt.Errorf("Error destroying tag %q during sweep: %s", tag.Label, err) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return nil |
| 46 | +} |
| 47 | + |
| 48 | +func TestAccDataSourceTag_basic(t *testing.T) { |
| 49 | + t.Parallel() |
| 50 | + |
| 51 | + dsName := "data.linode_tag.test" |
| 52 | + tagLabel := acctest.RandomWithPrefix("tf_test") |
| 53 | + |
| 54 | + resource.Test(t, resource.TestCase{ |
| 55 | + PreCheck: func() { acceptance.PreCheck(t) }, |
| 56 | + ProtoV6ProviderFactories: acceptance.ProtoV6ProviderFactories, |
| 57 | + Steps: []resource.TestStep{ |
| 58 | + { |
| 59 | + PreConfig: func() { |
| 60 | + client, err := acceptance.GetTestClient() |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("Error getting client: %s", err) |
| 63 | + } |
| 64 | + t.Cleanup(func() { |
| 65 | + _ = client.DeleteTag(context.Background(), tagLabel) |
| 66 | + }) |
| 67 | + if _, err := client.CreateTag(context.Background(), linodego.TagCreateOptions{Label: tagLabel}); err != nil { |
| 68 | + t.Fatalf("Error creating tag: %s", err) |
| 69 | + } |
| 70 | + }, |
| 71 | + Config: tmpl.DataSource(t, tagLabel), |
| 72 | + Check: resource.ComposeTestCheckFunc( |
| 73 | + resource.TestCheckResourceAttr(dsName, "label", tagLabel), |
| 74 | + resource.TestCheckResourceAttr(dsName, "id", tagLabel), |
| 75 | + ), |
| 76 | + }, |
| 77 | + }, |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +func TestAccDataSourceTag_reservedIP(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + |
| 84 | + dsName := "data.linode_tag.test" |
| 85 | + tagLabel := acctest.RandomWithPrefix("tf_test") |
| 86 | + var reservedIPAddress string |
| 87 | + |
| 88 | + resource.Test(t, resource.TestCase{ |
| 89 | + PreCheck: func() { acceptance.PreCheck(t) }, |
| 90 | + ProtoV6ProviderFactories: acceptance.ProtoV6ProviderFactories, |
| 91 | + Steps: []resource.TestStep{ |
| 92 | + { |
| 93 | + PreConfig: func() { |
| 94 | + region, err := acceptance.GetRandomRegionWithCaps(nil, "core") |
| 95 | + if err != nil { |
| 96 | + t.Fatalf("Error finding region: %s", err) |
| 97 | + } |
| 98 | + client, err := acceptance.GetTestClient() |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("Error getting client: %s", err) |
| 101 | + } |
| 102 | + ip, err := client.AllocateReserveIP(context.Background(), linodego.AllocateReserveIPOptions{ |
| 103 | + Type: "ipv4", |
| 104 | + Public: true, |
| 105 | + Reserved: true, |
| 106 | + Region: region, |
| 107 | + }) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("Error reserving IP: %s", err) |
| 110 | + } |
| 111 | + reservedIPAddress = ip.Address |
| 112 | + t.Cleanup(func() { |
| 113 | + _ = client.DeleteReservedIPAddress(context.Background(), ip.Address) |
| 114 | + _ = client.DeleteTag(context.Background(), tagLabel) |
| 115 | + }) |
| 116 | + if _, err := client.CreateTag(context.Background(), linodego.TagCreateOptions{ |
| 117 | + Label: tagLabel, |
| 118 | + ReservedIPv4Addresses: []string{ip.Address}, |
| 119 | + }); err != nil { |
| 120 | + t.Fatalf("Error creating tag: %s", err) |
| 121 | + } |
| 122 | + |
| 123 | + // Poll until the reserved IP association is visible in the API |
| 124 | + // before letting Terraform proceed (eventual consistency). |
| 125 | + // Skip if the API environment does not support reserved_ipv4_addresses |
| 126 | + // in POST /tags (feature may not be rolled out yet). |
| 127 | + deadline := time.Now().Add(30 * time.Second) |
| 128 | + visible := false |
| 129 | + for time.Now().Before(deadline) { |
| 130 | + objects, err := client.ListTaggedObjects(context.Background(), tagLabel, nil) |
| 131 | + if err == nil && len(objects) > 0 { |
| 132 | + visible = true |
| 133 | + break |
| 134 | + } |
| 135 | + time.Sleep(2 * time.Second) |
| 136 | + } |
| 137 | + if !visible { |
| 138 | + t.Skip("reserved_ipv4_addresses tag association not visible via API; skipping (feature may not be available in this environment)") |
| 139 | + } |
| 140 | + }, |
| 141 | + Config: tmpl.DataSource(t, tagLabel), |
| 142 | + Check: resource.ComposeTestCheckFunc( |
| 143 | + resource.TestCheckResourceAttr(dsName, "label", tagLabel), |
| 144 | + resource.TestCheckResourceAttr(dsName, "id", tagLabel), |
| 145 | + resource.TestCheckResourceAttr(dsName, "objects.#", "1"), |
| 146 | + resource.TestCheckResourceAttr(dsName, "objects.0.type", "reserved_ipv4_address"), |
| 147 | + resource.TestCheckResourceAttrWith(dsName, "objects.0.id", func(val string) error { |
| 148 | + if val != reservedIPAddress { |
| 149 | + return fmt.Errorf("expected objects.0.id to be %q, got %q", reservedIPAddress, val) |
| 150 | + } |
| 151 | + return nil |
| 152 | + }), |
| 153 | + ), |
| 154 | + }, |
| 155 | + }, |
| 156 | + }) |
| 157 | +} |
0 commit comments