Skip to content

Commit 208a0ac

Browse files
authored
Hashi review 1 (#35)
* Return resource*Read when resources are updated * Remove unusable reverse_dns option from floating ip resource * Prefix provider tests with Hcloud * Add test for ssh key update * Remove reverse_dns attribute from docs * Check backup window in tests * Set test timeout to 30min * Add changelog entries
1 parent 021649e commit 208a0ac

14 files changed

+195
-140
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changelog
22
=========
33

4+
v1.2.0 (Unreleased)
5+
------
6+
7+
* improved test coverage
8+
* resource update methods return the result of the read method
9+
* removed `reverse_dns` property of `hcloud_floating_ip`, because it was not useable, see https://github.com/hetznercloud/terraform-provider-hcloud/issues/32
10+
411
v1.1.0 (March 2, 2018)
512
------
613

GNUmakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test: fmtcheck
6060
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
6161

6262
testacc: fmtcheck
63-
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m
63+
TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 30m
6464

6565
vet:
6666
@echo "go vet ."

docs/hcloud_floating_ip.md

-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ resource "hcloud_floating_ip" "master" {
2323
- `server_id` - (Optional) Server to assign the Floating IP to.
2424
- `home_location` - (Optional) Home location (routing is optimized for that location). Optional if server_id argument is passed.
2525
- `description` - (Optional) Description of the Floating IP.
26-
- `reverse_dns` - (Optional) Set reverse DNS records on the Floating IP.
2726

2827
## Attributes Reference
2928

@@ -32,5 +31,4 @@ resource "hcloud_floating_ip" "master" {
3231
- `server_id` - Server to assign the Floating IP is assigned to.
3332
- `home_location` - Home location.
3433
- `description` - Description of the Floating IP.
35-
- `reverse_dns` - Reverse DNS records of the Floating IP.
3634
- `ip_address` - IP Address of the Floating IP.

hcloud/import_hcloud_floatingip_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import (
77
"github.com/hashicorp/terraform/helper/resource"
88
)
99

10-
func TestAccFloatingIP_importServer(t *testing.T) {
11-
resourceName := "hcloud_floating_ip.foobar"
10+
func TestAccHcloudFloatingIP_importServer(t *testing.T) {
11+
resourceName := "hcloud_floating_ip.floating_ip"
1212
rInt := acctest.RandInt()
1313

1414
resource.Test(t, resource.TestCase{
15-
PreCheck: func() { testAccPreCheck(t) },
15+
PreCheck: func() { testAccHcloudPreCheck(t) },
1616
Providers: testAccProviders,
17-
CheckDestroy: testAccCheckFloatingIPDestroy,
17+
CheckDestroy: testAccHcloudCheckFloatingIPDestroy,
1818
Steps: []resource.TestStep{
1919
{
20-
Config: testAccCheckFloatingIPConfig_server(rInt),
20+
Config: testAccHcloudCheckFloatingIPConfig_server(rInt),
2121
},
2222

2323
{

hcloud/import_hcloud_server_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import (
77
"github.com/hashicorp/terraform/helper/resource"
88
)
99

10-
func TestAccServer_importBasic(t *testing.T) {
10+
func TestAccHcloudServer_importBasic(t *testing.T) {
1111
resourceName := "hcloud_server.foobar"
1212
rInt := acctest.RandInt()
1313

1414
resource.Test(t, resource.TestCase{
15-
PreCheck: func() { testAccPreCheck(t) },
15+
PreCheck: func() { testAccHcloudPreCheck(t) },
1616
Providers: testAccProviders,
17-
CheckDestroy: testAccCheckServerDestroy,
17+
CheckDestroy: testAccHcloudCheckServerDestroy,
1818
Steps: []resource.TestStep{
1919
{
20-
Config: testAccCheckServerConfig_basic(rInt),
20+
Config: testAccHcloudCheckServerConfig_basic(rInt),
2121
},
2222

2323
{

hcloud/import_hcloud_sshkey_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/hashicorp/terraform/helper/resource"
88
)
99

10-
func TestAccSSHKey_importBasic(t *testing.T) {
10+
func TestAccHcloudSSHKey_importBasic(t *testing.T) {
1111
resourceName := "hcloud_ssh_key.foobar"
1212
rInt := acctest.RandInt()
1313
publicKeyMaterial, _, err := acctest.RandSSHKeyPair("hcloud@ssh-acceptance-test")
@@ -16,12 +16,12 @@ func TestAccSSHKey_importBasic(t *testing.T) {
1616
}
1717

1818
resource.Test(t, resource.TestCase{
19-
PreCheck: func() { testAccPreCheck(t) },
19+
PreCheck: func() { testAccHcloudPreCheck(t) },
2020
Providers: testAccProviders,
21-
CheckDestroy: testAccCheckSSHKeyDestroy,
21+
CheckDestroy: testAccHcloudCheckSSHKeyDestroy,
2222
Steps: []resource.TestStep{
2323
{
24-
Config: testAccCheckSSHKeyConfig_basic(rInt, publicKeyMaterial),
24+
Config: testAccHcloudCheckSSHKeyConfig_basic(rInt, publicKeyMaterial),
2525
},
2626

2727
{

hcloud/provider.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ func Provider() terraform.ResourceProvider {
1717
Description: "The API token to access the hetzner cloud.",
1818
},
1919
"endpoint": {
20-
Type: schema.TypeString,
21-
Optional: true,
20+
Type: schema.TypeString,
21+
Optional: true,
22+
DefaultFunc: schema.EnvDefaultFunc("HCLOUD_ENDPOINT", nil),
2223
},
2324
},
2425
ResourcesMap: map[string]*schema.Resource{

hcloud/provider_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestProvider_impl(t *testing.T) {
2828
var _ terraform.ResourceProvider = Provider()
2929
}
3030

31-
func testAccPreCheck(t *testing.T) {
31+
func testAccHcloudPreCheck(t *testing.T) {
3232
if v := os.Getenv("HCLOUD_TOKEN"); v == "" {
3333
t.Fatal("HCLOUD_TOKEN must be set for acceptance tests")
3434
}

hcloud/resource_hcloud_floatingip.go

+1-49
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@ func resourceFloatingIP() *schema.Resource {
4343
Type: schema.TypeString,
4444
Computed: true,
4545
},
46-
"reverse_dns": {
47-
Type: schema.TypeMap,
48-
Computed: true,
49-
Optional: true,
50-
Elem: &schema.Schema{
51-
Type: schema.TypeString,
52-
},
53-
},
5446
},
5547
}
5648
}
@@ -112,7 +104,6 @@ func resourceFloatingIPRead(d *schema.ResourceData, m interface{}) error {
112104
d.Set("server_id", floatingIP.Server.ID)
113105
}
114106
d.Set("ip_address", floatingIP.IP.String())
115-
d.Set("reverse_dns", floatingIP.DNSPtr)
116107

117108
return nil
118109
}
@@ -156,26 +147,9 @@ func resourceFloatingIPUpdate(d *schema.ResourceData, m interface{}) error {
156147
d.SetPartial("server_id")
157148
}
158149

159-
if d.HasChange("reverse_dns") {
160-
oldPtrRaw, newPtrRaw := d.GetChange("reverse_dns")
161-
oldPtr := getFloatingIPDNSPtr(oldPtrRaw)
162-
newPtr := getFloatingIPDNSPtr(newPtrRaw)
163-
164-
for ip := range oldPtr {
165-
if _, ok := newPtr[ip]; !ok {
166-
newPtr[ip] = nil
167-
}
168-
}
169-
170-
if err := updateFloatingIPDNSPtr(ctx, client, floatingIP, newPtr); err != nil {
171-
return err
172-
}
173-
d.SetPartial("reverse_dns")
174-
}
175-
176150
d.Partial(false)
177151

178-
return nil
152+
return resourceFloatingIPRead(d, m)
179153
}
180154

181155
func resourceFloatingIPDelete(d *schema.ResourceData, m interface{}) error {
@@ -192,25 +166,3 @@ func resourceFloatingIPDelete(d *schema.ResourceData, m interface{}) error {
192166

193167
return nil
194168
}
195-
196-
func getFloatingIPDNSPtr(raw interface{}) map[string]*string {
197-
out := map[string]*string{}
198-
for k, v := range raw.(map[string]interface{}) {
199-
out[k] = hcloud.String(v.(string))
200-
}
201-
return out
202-
}
203-
204-
func updateFloatingIPDNSPtr(ctx context.Context, client *hcloud.Client, floatingIP *hcloud.FloatingIP, update map[string]*string) error {
205-
for ip, ptr := range update {
206-
action, _, err := client.FloatingIP.ChangeDNSPtr(ctx, floatingIP, ip, ptr)
207-
if err != nil {
208-
return err
209-
}
210-
_, errCh := client.Action.WatchProgress(ctx, action)
211-
if err := <-errCh; err != nil {
212-
return err
213-
}
214-
}
215-
return nil
216-
}

hcloud/resource_hcloud_floatingip_test.go

+57-19
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,82 @@ func init() {
1919
})
2020
}
2121

22-
func TestAccFloatingIP_Server(t *testing.T) {
22+
func TestAccHcloudFloatingIP_AssignAndUpdateDescription(t *testing.T) {
2323
var floatingIP hcloud.FloatingIP
2424
rInt := acctest.RandInt()
2525

2626
resource.Test(t, resource.TestCase{
27-
PreCheck: func() { testAccPreCheck(t) },
27+
PreCheck: func() { testAccHcloudPreCheck(t) },
2828
Providers: testAccProviders,
29-
CheckDestroy: testAccCheckFloatingIPDestroy,
29+
CheckDestroy: testAccHcloudCheckFloatingIPDestroy,
3030
Steps: []resource.TestStep{
3131
{
32-
Config: testAccCheckFloatingIPConfig_server(rInt),
32+
Config: testAccHcloudCheckFloatingIPConfig_server(rInt),
3333
Check: resource.ComposeTestCheckFunc(
34-
testAccCheckFloatingIPExists("hcloud_floating_ip.foobar", &floatingIP),
34+
testAccHcloudCheckFloatingIPExists("hcloud_floating_ip.floating_ip", &floatingIP),
3535
resource.TestCheckResourceAttr(
36-
"hcloud_floating_ip.foobar", "home_location", "fsn1"),
36+
"hcloud_floating_ip.floating_ip", "home_location", "fsn1"),
37+
resource.TestCheckResourceAttr(
38+
"hcloud_floating_ip.floating_ip", "description", "test"),
39+
),
40+
},
41+
{
42+
Config: testAccHcloudCheckFloatingIPConfig_updateDescription(rInt),
43+
Check: resource.ComposeTestCheckFunc(
44+
testAccHcloudCheckFloatingIPExists("hcloud_floating_ip.floating_ip", &floatingIP),
45+
resource.TestCheckResourceAttr(
46+
"hcloud_floating_ip.floating_ip", "home_location", "fsn1"),
47+
resource.TestCheckResourceAttr(
48+
"hcloud_floating_ip.floating_ip", "description", "updated test"),
3749
),
3850
},
3951
},
4052
})
4153
}
4254

43-
func testAccCheckFloatingIPConfig_server(rInt int) string {
55+
func testAccHcloudCheckFloatingIPConfig_server(rInt int) string {
4456
return fmt.Sprintf(`
45-
resource "hcloud_ssh_key" "foobar" {
46-
name = "foobar-%d"
57+
resource "hcloud_ssh_key" "floating_ip" {
58+
name = "floating-ip-%d"
4759
public_key = "%s"
4860
}
49-
resource "hcloud_server" "foobar" {
50-
name = "foo-%d"
61+
resource "hcloud_server" "floating_ip1" {
62+
name = "floating-ip-1-%d"
5163
server_type = "cx11"
5264
image = "debian-9"
5365
datacenter = "fsn1-dc8"
54-
ssh_keys = ["${hcloud_ssh_key.foobar.id}"]
66+
ssh_keys = ["${hcloud_ssh_key.floating_ip.id}"]
5567
}
5668
57-
resource "hcloud_floating_ip" "foobar" {
58-
server_id = "${hcloud_server.foobar.id}"
59-
type = "ipv4"
69+
resource "hcloud_floating_ip" "floating_ip" {
70+
server_id = "${hcloud_server.floating_ip1.id}"
71+
type = "ipv4"
72+
description = "test"
6073
}`, rInt, testAccSSHPublicKey, rInt)
6174
}
6275

63-
func testAccCheckFloatingIPDestroy(s *terraform.State) error {
76+
func testAccHcloudCheckFloatingIPConfig_updateDescription(rInt int) string {
77+
return fmt.Sprintf(`
78+
resource "hcloud_ssh_key" "floating_ip" {
79+
name = "floating-ip-%d"
80+
public_key = "%s"
81+
}
82+
resource "hcloud_server" "floating_ip1" {
83+
name = "floating-ip-1-%d"
84+
server_type = "cx11"
85+
image = "debian-9"
86+
datacenter = "fsn1-dc8"
87+
ssh_keys = ["${hcloud_ssh_key.floating_ip.id}"]
88+
}
89+
90+
resource "hcloud_floating_ip" "floating_ip" {
91+
server_id = "${hcloud_server.floating_ip1.id}"
92+
type = "ipv4"
93+
description = "updated test"
94+
}`, rInt, testAccSSHPublicKey, rInt)
95+
}
96+
97+
func testAccHcloudCheckFloatingIPDestroy(s *terraform.State) error {
6498
client := testAccProvider.Meta().(*hcloud.Client)
6599

66100
for _, rs := range s.RootModule().Resources {
@@ -72,18 +106,22 @@ func testAccCheckFloatingIPDestroy(s *terraform.State) error {
72106
if err != nil {
73107
return fmt.Errorf("Floating IP id is no int: %v", err)
74108
}
75-
_, _, err = client.FloatingIP.GetByID(context.Background(), id)
109+
var floatingIP *hcloud.FloatingIP
110+
floatingIP, _, err = client.FloatingIP.GetByID(context.Background(), id)
76111
if err != nil {
77112
return fmt.Errorf(
78-
"Error waiting for floating ip (%s) to be destroyed: %v",
113+
"Error checking if floating ip (%s) is deleted: %v",
79114
rs.Primary.ID, err)
80115
}
116+
if floatingIP != nil {
117+
return fmt.Errorf("Floating ip (%s) has not been deleted", rs.Primary.ID)
118+
}
81119
}
82120

83121
return nil
84122
}
85123

86-
func testAccCheckFloatingIPExists(n string, floatingIP *hcloud.FloatingIP) resource.TestCheckFunc {
124+
func testAccHcloudCheckFloatingIPExists(n string, floatingIP *hcloud.FloatingIP) resource.TestCheckFunc {
87125
return func(s *terraform.State) error {
88126
rs, ok := s.RootModule().Resources[n]
89127
if !ok {

hcloud/resource_hcloud_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func resourceServerUpdate(d *schema.ResourceData, m interface{}) error {
297297
}
298298

299299
d.Partial(false)
300-
return nil
300+
return resourceServerRead(d, m)
301301
}
302302

303303
func resourceServerDelete(d *schema.ResourceData, m interface{}) error {

0 commit comments

Comments
 (0)