Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

azurerm_image - change os_disk property to a list #1443

Merged
merged 4 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions azurerm/helpers/validate/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package validate

import (
"fmt"
"net/url"

"github.com/hashicorp/terraform/helper/schema"
"strings"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you move this up to the first group of imports?

)

func UrlIsHttpOrHttps(i interface{}, k string) (_ []string, errors []error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may have been a precedent here you are following, but traditionally initialisms in Go are capitalized: https://github.com/golang/go/wiki/CodeReviewComments#initialisms

URLIsHTTPOrHTTPS I think? I may be misreading this thing from Go, may want to run the linter on it.

return UrlWithScheme([]string{"http", "https"})(i, k)
}

func UrlWithScheme(validSchemes []string) schema.SchemaValidateFunc {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment on initialisms.

return func(i interface{}, k string) (_ []string, errors []error) {
v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %q to be string", k))
return
}

if v == "" {
errors = append(errors, fmt.Errorf("expected %q url to not be empty", k))
return
}

url, err := url.Parse(v)
if err != nil {
errors = append(errors, fmt.Errorf("%q url is in an invalid format: %q (%+v)", k, v, err))
return
}

if url.Host == "" {
errors = append(errors, fmt.Errorf("%q url has no host: %q", k, v))
return
}

for _, s := range validSchemes {
if url.Scheme == s {
return //last check so just return
}
}

errors = append(errors, fmt.Errorf("expected %q url %q to have a schema of: %q", k, v, strings.Join(validSchemes, ",")))
return
}
}
45 changes: 45 additions & 0 deletions azurerm/helpers/validate/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package validate

import (
"testing"
)

func TestHelper_Validate_UrlIsHttpOrHttps(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just call this Test<function name> and if its one of many methods Test<function name>_<description>

cases := []struct {
Url string
Errors int
}{
{
Url: "",
Errors: 1,
},
{
Url: "this is not a url",
Errors: 1,
},
{
Url: "www.example.com",
Errors: 1,
},
{
Url: "ftp://www.example.com",
Errors: 1,
},
{
Url: "http://www.example.com",
Errors: 0,
},
{
Url: "https://www.example.com",
Errors: 0,
},
}

for _, tc := range cases {
_, errors := UrlIsHttpOrHttps(tc.Url, "test")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these cases should be inside a t.Run


if len(errors) != tc.Errors {
t.Fatalf("Expected UrlIsHttpOrHttps to have an error for %q", tc.Url)
}
}
}
81 changes: 46 additions & 35 deletions azurerm/resource_arm_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"log"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-12-01/compute"

"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -32,66 +35,71 @@ func resourceArmImage() *schema.Resource {
"resource_group_name": resourceGroupNameSchema(),

"source_virtual_machine_id": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceId,
},

"os_disk": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"os_type": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(compute.Linux),
string(compute.Windows),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"os_state": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(compute.Generalized),
string(compute.Specialized),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"managed_disk_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Type: schema.TypeString,
Computed: true,
Optional: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: azure.ValidateResourceId,
},

"blob_uri": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.UrlIsHttpOrHttps,
},

"caching": {
Type: schema.TypeString,
Optional: true,
Default: string(compute.None),
Type: schema.TypeString,
Optional: true,
Default: string(compute.None),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(compute.CachingTypesNone),
string(compute.CachingTypesReadOnly),
string(compute.CachingTypesReadWrite),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"size_gb": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Type: schema.TypeInt,
Computed: true,
Optional: true,
ValidateFunc: validation.NoZeroValues,
},
},
},
Expand All @@ -109,15 +117,17 @@ func resourceArmImage() *schema.Resource {
},

"managed_disk_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceId,
},

"blob_uri": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.UrlIsHttpOrHttps,
},

"caching": {
Expand All @@ -133,9 +143,10 @@ func resourceArmImage() *schema.Resource {
},

"size_gb": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.NoZeroValues,
},
},
},
Expand All @@ -155,8 +166,8 @@ func resourceArmImageCreateUpdate(d *schema.ResourceData, meta interface{}) erro
name := d.Get("name").(string)
location := azureRMNormalizeLocation(d.Get("location").(string))
resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)
expandedTags := expandTags(d.Get("tags").(map[string]interface{}))

properties := compute.ImageProperties{}

osDisk, err := expandAzureRmImageOsDisk(d)
Expand Down Expand Up @@ -349,7 +360,7 @@ func flattenAzureRmImageDataDisks(diskImages *[]compute.ImageDataDisk) []interfa

func expandAzureRmImageOsDisk(d *schema.ResourceData) (*compute.ImageOSDisk, error) {
osDisk := &compute.ImageOSDisk{}
disks := d.Get("os_disk").(*schema.Set).List()
disks := d.Get("os_disk").([]interface{})

if len(disks) > 0 {
config := disks[0].(map[string]interface{})
Expand Down