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_monitor_metric_alert supports multiple scopes and different criteria #7159

Merged
merged 9 commits into from
Jul 16, 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
@@ -0,0 +1,59 @@
package parse

import (
"fmt"

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

type ApplicationInsightsId struct {
ResourceGroup string
Name string
}

func ApplicationInsightsID(input string) (*ApplicationInsightsId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Application Insights ID %q: %+v", input, err)
}

appId := ApplicationInsightsId{
ResourceGroup: id.ResourceGroup,
}

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

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

return &appId, nil
}

type ApplicationInsightsWebTestId struct {
ResourceGroup string
Name string
}

func ApplicationInsightsWebTestID(input string) (*ApplicationInsightsWebTestId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Application Insights Web Test ID %q: %+v", input, err)
}

testid := ApplicationInsightsWebTestId{
ResourceGroup: id.ResourceGroup,
}

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

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

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

import (
"testing"
)

func TestApplicationInsightsID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *ApplicationInsightsId
}{
{
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: "No Provider",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers",
Error: true,
},
{
Name: "No component",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/microsoft.insights/components",
Error: true,
},
{
Name: "Correct",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/microsoft.insights/components/appinsights1",
Expect: &ApplicationInsightsId{
ResourceGroup: "group1",
Name: "appinsights1",
},
},
}

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

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

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get")
}

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

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

func TestApplicationInsightsWebTestID(t *testing.T) {
testData := []struct {
Name string
Input string
Error bool
Expect *ApplicationInsightsWebTestId
}{
{
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: "No Provider",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers",
Error: true,
},
{
Name: "No webtest",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/microsoft.insights/webtests",
Error: true,
},
{
Name: "Correct",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/microsoft.insights/webtests/test1",
Expect: &ApplicationInsightsWebTestId{
ResourceGroup: "group1",
Name: "test1",
},
},
}

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

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

t.Fatalf("Expect a value but got an error: %s", err)
}
if v.Error {
t.Fatal("Expect an error but didn't get")
}

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

if actual.Name != v.Expect.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expect.Name, actual.Name)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package validate

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/applicationinsights/parse"
)

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

if _, err := parse.ApplicationInsightsID(v); err != nil {
errors = append(errors, fmt.Errorf("parsing %q as a resource id: %v", k, err))
return
}

return warnings, errors
}

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

if _, err := parse.ApplicationInsightsWebTestID(v); err != nil {
errors = append(errors, fmt.Errorf("parsing %q as a resource id: %v", k, err))
return
}

return warnings, errors
}
Loading