2022-01-21 06:48:59 +03:00
|
|
|
package utils_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-04 05:43:09 +03:00
|
|
|
"github.com/Azure/terraform-provider-azapi/internal/azure/types"
|
|
|
|
"github.com/Azure/terraform-provider-azapi/utils"
|
2022-01-21 06:48:59 +03:00
|
|
|
)
|
|
|
|
|
2022-01-24 12:12:16 +03:00
|
|
|
func Test_GetParentId(t *testing.T) {
|
2022-01-21 06:48:59 +03:00
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
{
|
2022-01-30 05:48:22 +03:00
|
|
|
Input: "",
|
|
|
|
Output: "",
|
2022-01-21 06:48:59 +03:00
|
|
|
},
|
|
|
|
{
|
2022-01-30 05:48:22 +03:00
|
|
|
Input: "/",
|
|
|
|
Output: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Billing/billingAccounts/myAccount",
|
2022-04-01 08:22:07 +03:00
|
|
|
Output: "/",
|
2022-01-30 05:48:22 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Billing/billingAccounts/myAccount/billingProfiles/myProfile",
|
|
|
|
Output: "/providers/Microsoft.Billing/billingAccounts/myAccount",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012",
|
2022-04-01 08:22:07 +03:00
|
|
|
Output: "/",
|
2022-01-30 05:48:22 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Authorization/policydefinitions/myDef",
|
|
|
|
Output: "/subscriptions/12345678-1234-9876-4563-123456789012",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/featureProviders/myProvider/subscriptionFeatureRegistrations/myFeature",
|
|
|
|
Output: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/featureProviders/myProvider",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Management/managementGroups/myMgmtGroup",
|
2022-04-01 08:22:07 +03:00
|
|
|
Output: "/",
|
2022-01-30 05:48:22 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Management/managementGroups/myMgmtGroup/providers/Microsoft.CostManagement/externalSubscriptions/mySub",
|
|
|
|
Output: "/providers/Microsoft.Management/managementGroups/myMgmtGroup",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1",
|
|
|
|
Output: "/subscriptions/12345678-1234-9876-4563-123456789012",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network/networkSecurityGroups/sg1",
|
|
|
|
Output: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network/networkSecurityGroups/sg1/providers/Microsoft.Insights/metrics/m1",
|
|
|
|
Output: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network/networkSecurityGroups/sg1",
|
2022-01-21 06:48:59 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Logf("[DEBUG] Testing Value %s", tc.Input)
|
|
|
|
output := utils.GetParentId(tc.Input)
|
|
|
|
|
|
|
|
if tc.Output != output {
|
|
|
|
t.Fatalf("Expected %s but got %s", tc.Output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-24 12:12:16 +03:00
|
|
|
|
2022-09-07 04:39:17 +03:00
|
|
|
func Test_GetName(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
// invalid resource ID
|
|
|
|
Input: "",
|
|
|
|
Output: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// tenant ID doesn't have name
|
|
|
|
Input: "/",
|
|
|
|
Output: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Billing/billingAccounts/myAccount",
|
|
|
|
Output: "myAccount",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Billing/billingAccounts/myAccount/billingProfiles/myAccount",
|
|
|
|
Output: "myAccount",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012",
|
|
|
|
Output: "12345678-1234-9876-4563-123456789012",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Authorization/policydefinitions/myDef",
|
|
|
|
Output: "myDef",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/featureProviders/myProvider/subscriptionFeatureRegistrations/myFeature",
|
|
|
|
Output: "myFeature",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Management/managementGroups/myMgmtGroup",
|
|
|
|
Output: "myMgmtGroup",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Management/managementGroups/myMgmtGroup/providers/Microsoft.CostManagement/externalSubscriptions/mySub",
|
|
|
|
Output: "mySub",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1",
|
|
|
|
Output: "rg1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Invalid resource Id
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network",
|
|
|
|
Output: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network/networkSecurityGroups/sg1",
|
|
|
|
Output: "sg1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network/networkSecurityGroups/sg1/providers/Microsoft.Insights/metrics/m1",
|
|
|
|
Output: "m1",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Logf("[DEBUG] Testing Value %s", tc.Input)
|
|
|
|
output := utils.GetName(tc.Input)
|
|
|
|
|
|
|
|
if tc.Output != output {
|
|
|
|
t.Fatalf("Expected %s but got %s", tc.Output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:12:16 +03:00
|
|
|
func Test_GetResourceType(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
{
|
2022-01-30 05:48:22 +03:00
|
|
|
Input: "",
|
2022-09-07 04:39:17 +03:00
|
|
|
Output: "",
|
2022-01-30 05:48:22 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/",
|
2022-09-07 04:39:17 +03:00
|
|
|
Output: "Microsoft.Resources/tenants",
|
2022-01-30 05:48:22 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Billing/billingAccounts/myAccount",
|
|
|
|
Output: "Microsoft.Billing/billingAccounts",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Billing/billingAccounts/myAccount/billingProfiles/myProfile",
|
|
|
|
Output: "Microsoft.Billing/billingAccounts/billingProfiles",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012",
|
2022-09-07 04:39:17 +03:00
|
|
|
Output: "Microsoft.Resources/subscriptions",
|
2022-01-30 05:48:22 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Authorization/policydefinitions/myDef",
|
|
|
|
Output: "Microsoft.Authorization/policydefinitions",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/featureProviders/myProvider/subscriptionFeatureRegistrations/myFeature",
|
|
|
|
Output: "Microsoft.Features/featureProviders/subscriptionFeatureRegistrations",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Management/managementGroups/myMgmtGroup",
|
|
|
|
Output: "Microsoft.Management/managementGroups",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Management/managementGroups/myMgmtGroup/providers/Microsoft.CostManagement/externalSubscriptions/mySub",
|
|
|
|
Output: "Microsoft.CostManagement/externalSubscriptions",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1",
|
2022-01-24 12:12:16 +03:00
|
|
|
Output: "Microsoft.Resources/resourceGroups",
|
|
|
|
},
|
|
|
|
{
|
2022-01-30 05:48:22 +03:00
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network/networkSecurityGroups/sg1",
|
|
|
|
Output: "Microsoft.Network/networkSecurityGroups",
|
2022-01-24 12:12:16 +03:00
|
|
|
},
|
|
|
|
{
|
2022-01-30 05:48:22 +03:00
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network/networkSecurityGroups/sg1/providers/Microsoft.Insights/metrics/m1",
|
|
|
|
Output: "Microsoft.Insights/metrics",
|
2022-01-24 12:12:16 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Logf("[DEBUG] Testing Value %s", tc.Input)
|
|
|
|
output := utils.GetResourceType(tc.Input)
|
|
|
|
|
|
|
|
if tc.Output != output {
|
|
|
|
t.Fatalf("Expected %s but got %s", tc.Output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_GetParentType(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: "Microsoft.EventHub/clusters",
|
2022-01-30 05:48:22 +03:00
|
|
|
Output: "",
|
2022-01-24 12:12:16 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "Microsoft.EventHub/clusters/pools",
|
|
|
|
Output: "Microsoft.EventHub/clusters",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Logf("[DEBUG] Testing Value %s", tc.Input)
|
|
|
|
output := utils.GetParentType(tc.Input)
|
|
|
|
|
|
|
|
if tc.Output != output {
|
|
|
|
t.Fatalf("Expected %s but got %s", tc.Output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-30 05:48:22 +03:00
|
|
|
|
|
|
|
func Test_GetScopeType(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Output types.ScopeType
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Input: "",
|
|
|
|
Output: types.Tenant,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/",
|
|
|
|
Output: types.Tenant,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Billing/billingAccounts/myAccount",
|
|
|
|
Output: types.Tenant,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Billing/billingAccounts/myAccount/billingProfiles/myProfile",
|
|
|
|
Output: types.Tenant,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012",
|
|
|
|
Output: types.Subscription,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Authorization/policydefinitions/myDef",
|
|
|
|
Output: types.Subscription,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/providers/Microsoft.Features/featureProviders/myProvider/subscriptionFeatureRegistrations/myFeature",
|
|
|
|
Output: types.Subscription,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Management/managementGroups/myMgmtGroup",
|
|
|
|
Output: types.ManagementGroup,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/providers/Microsoft.Management/managementGroups/myMgmtGroup/providers/Microsoft.CostManagement/externalSubscriptions/mySub",
|
|
|
|
Output: types.ManagementGroup,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1",
|
|
|
|
Output: types.ResourceGroup,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/rg1/providers/Microsoft.Network/networkSecurityGroups/sg1",
|
|
|
|
Output: types.ResourceGroup,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Logf("[DEBUG] Testing Value %s", tc.Input)
|
|
|
|
output := utils.GetScopeType(tc.Input)
|
|
|
|
|
|
|
|
if tc.Output != output {
|
|
|
|
t.Fatalf("Expected %v but got %v", tc.Output, output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|