Skip to content

Commit

Permalink
Docs update and custom image options update (#538)
Browse files Browse the repository at this point in the history
* Docs update and custom image options update support

Signed-off-by: Noel Georgi <[email protected]>

* Apply review suggestion.

* Add an acceptance test for updates.

Co-authored-by: Andrew Starr-Bochicchio <[email protected]>
  • Loading branch information
frezbo and andrewsomething committed Apr 26, 2021
1 parent 0af71d2 commit 25aaaca
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
17 changes: 11 additions & 6 deletions digitalocean/resource_digitalocean_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,20 @@ func resourceDigitalOceanCustomImageUpdate(ctx context.Context, d *schema.Resour
return diag.Errorf("Error converting id %s to string: %s", imageID, err)
}

if d.HasChange("name") {
name := d.Get("name").(string)
_, _, err := client.Images.Update(ctx, id, &godo.ImageUpdateRequest{
Name: name,
})
if d.HasChanges("name", "description", "distribution") {
imageName := d.Get("name").(string)
imageUpdateRequest := &godo.ImageUpdateRequest{
Name: imageName,
Distribution: d.Get("distribution").(string),
Description: d.Get("description").(string),
}

_, _, err := client.Images.Update(ctx, id, imageUpdateRequest)
if err != nil {
return diag.Errorf("Error updating image %s, name %s: %s", imageID, name, err)
return diag.Errorf("Error updating image %s, name %s: %s", imageID, imageName, err)
}
}

return resourceDigitalOceanCustomImageRead(ctx, d, meta)
}

Expand Down
22 changes: 16 additions & 6 deletions digitalocean/resource_digitalocean_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
func TestAccDigitalOceanCustomImageFull(t *testing.T) {
rString := randomTestName()
name := fmt.Sprintf("digitalocean_custom_image.%s", rString)
updatedString := randomTestName()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanCustomImageDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckDigitalOceanCustomImageConfig(rString),
Config: testAccCheckDigitalOceanCustomImageConfig(rString, rString, "Unknown"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", fmt.Sprintf("%s-name", rString)),
resource.TestCheckResourceAttr(name, "description", fmt.Sprintf("%s-description", rString)),
Expand All @@ -37,22 +38,31 @@ func TestAccDigitalOceanCustomImageFull(t *testing.T) {
resource.TestCheckResourceAttrSet(name, "size_gigabytes"),
),
},
{
Config: testAccCheckDigitalOceanCustomImageConfig(rString, updatedString, "CoreOS"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", fmt.Sprintf("%s-name", updatedString)),
resource.TestCheckResourceAttr(name, "description", fmt.Sprintf("%s-description", updatedString)),
resource.TestCheckResourceAttr(name, "distribution", "CoreOS"),
),
},
},
})
}

func testAccCheckDigitalOceanCustomImageConfig(name string) string {
func testAccCheckDigitalOceanCustomImageConfig(rName string, name string, distro string) string {
return fmt.Sprintf(`
resource "digitalocean_custom_image" "%s" {
name = "%s-name"
url = "https://stable.release.flatcar-linux.net/amd64-usr/2605.7.0/flatcar_production_digitalocean_image.bin.bz2"
regions = ["nyc3"]
description = "%s-description"
url = "https://stable.release.flatcar-linux.net/amd64-usr/2605.7.0/flatcar_production_digitalocean_image.bin.bz2"
regions = ["nyc3"]
description = "%s-description"
distribution = "%s"
tags = [
"flatcar"
]
}
`, name, name, name)
`, rName, name, name, distro)
}

func testAccCheckDigitalOceanCustomImageDestroy(s *terraform.State) error {
Expand Down
5 changes: 4 additions & 1 deletion docs/resources/custom_image.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ The following arguments are supported:

* `name` - (Required) A name for the Custom Image.
* `url` - (Required) A URL from which the custom Linux virtual machine image may be retrieved.
* `regions` - (Required) A list of regions. (Currently only one is supported)
* `regions` - (Required) A list of regions. (Currently only one is supported).
* `description` - An optional description for the image.
* `distribution` - An optional distribution name for the image. Valid values are documented [here](https://developers.digitalocean.com/documentation/v2/#create-a-custom-image)
* `tags` - A list of optional tags for the image.

## Attributes Reference

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/digitalocean/terraform-provider-digitalocean

require (
github.com/aws/aws-sdk-go v1.25.4
github.com/digitalocean/godo v1.52.0
github.com/digitalocean/godo v1.54.0
github.com/hashicorp/go-version v1.2.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.0.3
github.com/mitchellh/go-homedir v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/digitalocean/godo v1.52.0 h1:1QSUC0w5T1wS1d/1uvPtG8GLeD0p/4zhx1Q+Fxtna+k=
github.com/digitalocean/godo v1.52.0/go.mod h1:p7dOjjtSBqCTUksqtA5Fd3uaKs9kyTq2xcz76ulEJRU=
github.com/digitalocean/godo v1.54.0 h1:KP0Nv87pgViR8k/7De3VrmflCL5pJqXbNnkcw0bwG10=
github.com/digitalocean/godo v1.54.0/go.mod h1:p7dOjjtSBqCTUksqtA5Fd3uaKs9kyTq2xcz76ulEJRU=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand Down

0 comments on commit 25aaaca

Please sign in to comment.