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

fix linux vm shared image acctests. added support for shared_image_id variants #5640

Merged
merged 2 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ func resourceLinuxVirtualMachine() *schema.Resource {
"secret": linuxSecretSchema(),

"source_image_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: computeValidate.ImageID,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.Any(
computeValidate.ImageID,
computeValidate.SharedImageID,
computeValidate.SharedImageVersionID,
),
},

"source_image_reference": sourceImageReferenceSchema(true),
Expand Down
38 changes: 38 additions & 0 deletions azurerm/internal/services/compute/parse/shared_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type SharedImageId struct {
ResourceGroup string
Gallery string
Name string
}

func SharedImageID(input string) (*SharedImageId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Image ID %q: %+v", input, err)
}

set := SharedImageId{
ResourceGroup: id.ResourceGroup,
}

if set.Gallery, err = id.PopSegment("galleries"); err != nil {
return nil, err
}

if set.Name, err = id.PopSegment("images"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &set, nil
}
81 changes: 81 additions & 0 deletions azurerm/internal/services/compute/parse/shared_image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package parse

import (
"testing"
)

func TestSharedImageID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *SharedImageId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Error: true,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Error: true,
},
{
Name: "Missing galleries segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/images/image1",
Error: true,
},
{
Name: "Missing image Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/galleries/gallery1/images",
Error: true,
},
{
Name: "Image ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Compute/galleries/gallery1/images/image1",
Error: false,
Expect: &SharedImageId{
ResourceGroup: "mygroup1",
Gallery: "gallery1",
Name: "image1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Compute/galleries/gallery1/Images/image1",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := SharedImageID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expect.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name)
}

if actual.ResourceGroup != v.Expect.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup)
}
}
}
43 changes: 43 additions & 0 deletions azurerm/internal/services/compute/parse/shared_image_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type SharedImageVersionId struct {
ResourceGroup string
Version string
Gallery string
Name string
}

func SharedImageVersionID(input string) (*SharedImageVersionId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse Image ID %q: %+v", input, err)
}

set := SharedImageVersionId{
ResourceGroup: id.ResourceGroup,
}

if set.Gallery, err = id.PopSegment("galleries"); err != nil {
return nil, err
}

if set.Name, err = id.PopSegment("images"); err != nil {
return nil, err
}

if set.Version, err = id.PopSegment("versions"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &set, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package parse

import (
"testing"
)

func TestSharedImageVersionID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *SharedImageVersionId
}{
{
Name: "Empty",
Input: "",
Error: true,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Error: true,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Error: true,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Error: true,
},
{
Name: "Missing galleries segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/images/image1/versions/1.0.0",
Error: true,
},
{
Name: "Missing image segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.Compute/galleries/gallery1/versions/1.0.0",
Error: true,
},
{
Name: "Image ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Compute/galleries/gallery1/images/image1/versions/1.0.0",
Error: false,
Expect: &SharedImageVersionId{
ResourceGroup: "mygroup1",
Gallery: "gallery1",
Name: "image1",
Version: "1.0.0",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.Compute/galleries/gallery1/images/image1/Versions/1.0.0",
Error: true,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := SharedImageVersionID(v.Input)
if err != nil {
if v.Error {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expect.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name)
}

if actual.ResourceGroup != v.Expect.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup)
}
}
}
Loading