Skip to content

Commit

Permalink
Update govultr and add support for reserved IP label updates (#268)
Browse files Browse the repository at this point in the history
* Add support for label update on reserved IP

* Bump govultr to v2.17.2

* Move label test to separate function

* Fix documentation

Reported in issues #267 & #266
  • Loading branch information
optik-aper committed Jun 14, 2022
1 parent 6bd19bd commit 243f1a5
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.17.0
github.com/vultr/govultr/v2 v2.17.1
github.com/vultr/govultr/v2 v2.17.2
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -220,8 +220,8 @@ github.com/vmihailenco/msgpack/v4 v4.3.12 h1:07s4sz9IReOgdikxLTKNbBdqDMLsjPKXwvC
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1 h1:quXMXlA39OCbd2wAdTsGDlK9RkOk6Wuw+x37wVyIuWY=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/vultr/govultr/v2 v2.17.1 h1:UBmotwA0mkGtyJMakUF9jhLH/W3mN5wfGRn543i/BCA=
github.com/vultr/govultr/v2 v2.17.1/go.mod h1:ZFOKGWmgjytfyjeyAdhQlSWwTjh2ig+X49cAp50dzXI=
github.com/vultr/govultr/v2 v2.17.2 h1:gej/rwr91Puc/tgh+j33p/BLR16UrIPnSr+AIwYWZQs=
github.com/vultr/govultr/v2 v2.17.2/go.mod h1:ZFOKGWmgjytfyjeyAdhQlSWwTjh2ig+X49cAp50dzXI=
github.com/xanzy/ssh-agent v0.3.0 h1:wUMzuKtKilRgBAD1sUb8gOwwRr2FGoBVumcjoOACClI=
github.com/xanzy/ssh-agent v0.3.0/go.mod h1:3s9xbODqPuuhK9JV1R321M/FlMZSBvE5aY6eAcqrDh0=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
5 changes: 5 additions & 0 deletions vendor/github.com/vultr/govultr/v2/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/github.com/vultr/govultr/v2/govultr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/vultr/govultr/v2/reserved_ip.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Expand Up @@ -186,7 +186,7 @@ github.com/vmihailenco/msgpack/v4/codes
github.com/vmihailenco/tagparser
github.com/vmihailenco/tagparser/internal
github.com/vmihailenco/tagparser/internal/parser
# github.com/vultr/govultr/v2 v2.17.1
# github.com/vultr/govultr/v2 v2.17.2
## explicit; go 1.17
github.com/vultr/govultr/v2
# github.com/zclconf/go-cty v1.10.0
Expand Down
19 changes: 15 additions & 4 deletions vultr/resource_vultr_reserved_ip.go
Expand Up @@ -35,7 +35,6 @@ func resourceVultrReservedIP() *schema.Resource {
"label": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "",
},
"instance_id": {
Expand Down Expand Up @@ -106,10 +105,10 @@ func resourceVultrReservedIPRead(ctx context.Context, d *schema.ResourceData, me
}

func resourceVultrReservedIPUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChange("instance_id") {
client := meta.(*Client).govultrClient()
client := meta.(*Client).govultrClient()

log.Printf("[INFO] Updating Reserved IP: %s", d.Id())
if d.HasChange("instance_id") {
log.Printf("[INFO] Updating Reserved IP instance: %s", d.Id())

old, newVal := d.GetChange("instance_id")

Expand All @@ -125,6 +124,18 @@ func resourceVultrReservedIPUpdate(ctx context.Context, d *schema.ResourceData,
}
}

if d.HasChange("label") {
log.Printf("[INFO] Updating Reserved IP label: %s", d.Id())

req := &govultr.ReservedIPUpdateReq{
Label: govultr.StringToStringPtr(d.Get("label").(string)),
}

if _, err := client.ReservedIP.Update(ctx, d.Id(), req); err != nil {
return diag.Errorf("error updating reserved IP %s : %s", d.Id(), err.Error())
}
}

return resourceVultrReservedIPRead(ctx, d, meta)
}

Expand Down
42 changes: 40 additions & 2 deletions vultr/resource_vultr_reserved_ip_test.go
Expand Up @@ -13,6 +13,7 @@ import (
func TestAccVultrReservedIPIPv4(t *testing.T) {
rServerLabel := acctest.RandomWithPrefix("tf-vps-rip4")
rLabel := acctest.RandomWithPrefix("tf-rip4-rs")
rLabelUpdated := rLabel + "_updated"
ipType := "v4"

resource.Test(t, resource.TestCase{
Expand All @@ -32,10 +33,10 @@ func TestAccVultrReservedIPIPv4(t *testing.T) {
),
},
{
Config: testAccVultrReservedIPConfigAttach(rServerLabel, rLabel, ipType),
Config: testAccVultrReservedIPConfigAttach(rServerLabel, rLabelUpdated, ipType),
Check: resource.ComposeTestCheckFunc(
testAccCheckVultrReservedIPExists("vultr_reserved_ip.foo"),
resource.TestCheckResourceAttr("vultr_reserved_ip.foo", "label", rLabel),
resource.TestCheckResourceAttr("vultr_reserved_ip.foo", "label", rLabelUpdated),
resource.TestCheckResourceAttr("vultr_reserved_ip.foo", "ip_type", ipType),
resource.TestCheckResourceAttrSet("vultr_reserved_ip.foo", "region"),
resource.TestCheckResourceAttrSet("vultr_reserved_ip.foo", "subnet"),
Expand Down Expand Up @@ -108,6 +109,33 @@ func TestAccVultrReservedIPIPv6(t *testing.T) {
})
}

func TestAccVultrReservedIPLabelUpdate(t *testing.T) {
rLabel := acctest.RandomWithPrefix("tf-rip-rs")
rLabelUpdated := rLabel + "_updated"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVultrReservedIPDestroy,
Steps: []resource.TestStep{
{
Config: testAccVultrReservedIPConfigLabel(rLabel),
Check: resource.ComposeTestCheckFunc(
testAccCheckVultrReservedIPExists("vultr_reserved_ip.foo"),
resource.TestCheckResourceAttr("vultr_reserved_ip.foo", "label", rLabel),
),
},
{
Config: testAccVultrReservedIPConfigLabel(rLabelUpdated),
Check: resource.ComposeTestCheckFunc(
testAccCheckVultrReservedIPExists("vultr_reserved_ip.foo"),
resource.TestCheckResourceAttr("vultr_reserved_ip.foo", "label", rLabelUpdated),
),
},
},
})
}

func testAccCheckVultrReservedIPDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "vultr_reserved_ip" {
Expand Down Expand Up @@ -147,6 +175,16 @@ func testAccCheckVultrReservedIPExists(n string) resource.TestCheckFunc {
}
}

func testAccVultrReservedIPConfigLabel(label string) string {
return fmt.Sprintf(`
resource "vultr_reserved_ip" "foo" {
label = "%s"
region = "ewr"
ip_type = "v4"
}`, label)
}

func testAccVultrReservedIPConfig(rServerLabel, label, ipType string) string {
return fmt.Sprintf(`
resource "vultr_instance" "ip" {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/instance.html.markdown
Expand Up @@ -75,7 +75,7 @@ The following arguments are supported:

`backups_schedule` supports the following:

* `type` - Type of backup schedule Possible values are `daily`, `weekly`, `monthly`, `daily_alt_event`, or `daily_alt_odd`.
* `type` - Type of backup schedule Possible values are `daily`, `weekly`, `monthly`, `daily_alt_even`, or `daily_alt_odd`.
* `hour` - (Optional) Hour of day to run in UTC.
* `dow` - (Optional) Day of week to run. `1 = Sunday`, `2 = Monday`, `3 = Tuesday`, `4 = Wednesday`, `5 = Thursday`, `6 = Friday`, `7 = Saturday`
* `dom` - (Optional) Day of month to run. Use values between 1 and 28.
Expand Down
4 changes: 2 additions & 2 deletions website/docs/r/reverse_ipv6.html.markdown
Expand Up @@ -25,8 +25,8 @@ resource "vultr_instance" "my_server" {
}
resource "vultr_reverse_ipv6" "my_reverse_ipv6" {
instance_id = "${vultr_server.my_server.id}"
ip = "${vultr_server.my_server.v6_networks[0].v6_main_ip}"
instance_id = "${vultr_instance.my_server.id}"
ip = "${vultr_instance.my_server.v6_networks[0].v6_main_ip}"
reverse = "host.example.com"
}
```
Expand Down

0 comments on commit 243f1a5

Please sign in to comment.