Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add devtestlab global schedule parse and validate funcs
- Loading branch information
Sean Nixon
committed
Mar 27, 2020
1 parent
4d12fe9
commit ca9c6fc
Showing
10 changed files
with
333 additions
and
17 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
azurerm/internal/services/devtestlabs/parse/global_schedule.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type GlobalScheduleId struct { | ||
ResourceGroup string | ||
Name string | ||
} | ||
|
||
func GlobalScheduleID(input string) (*GlobalScheduleId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("[ERROR] Unable to parse Global Schedule ID %q: %+v", input, err) | ||
} | ||
|
||
service := GlobalScheduleId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if service.Name, err = id.PopSegment("schedules"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &service, nil | ||
} |
61 changes: 61 additions & 0 deletions
61
azurerm/internal/services/devtestlabs/parse/global_schedule_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package parse | ||
|
||
import "testing" | ||
|
||
func TestGlobalSchedule(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Expected *GlobalScheduleId | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No DevTest Lab segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No schedule name", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.DevTestLab/schedules/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Case incorrect in path element", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.DevTestLab/Schedules/schedule1", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Valid", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.DevTestLab/schedules/schedule1", | ||
Expected: &GlobalScheduleId{ | ||
ResourceGroup: "myGroup1", | ||
Name: "schedule1", | ||
}, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Name) | ||
|
||
actual, err := GlobalScheduleID(v.Input) | ||
if err != nil { | ||
if v.Expected == nil { | ||
continue | ||
} | ||
|
||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.Name != v.Expected.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expected.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
azurerm/internal/services/devtestlabs/parse/global_schedule_virtual_machine.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package parse | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
) | ||
|
||
type GlobalScheduleVirtualMachineId struct { | ||
ResourceGroup string | ||
Name string | ||
} | ||
|
||
func GlobalScheduleVirtualMachineID(input string) (*GlobalScheduleVirtualMachineId, error) { | ||
id, err := azure.ParseAzureResourceID(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("[ERROR] Unable to parse Virtual Machine ID %q: %+v", input, err) | ||
} | ||
|
||
service := GlobalScheduleVirtualMachineId{ | ||
ResourceGroup: id.ResourceGroup, | ||
} | ||
|
||
if service.Name, err = id.PopSegment("virtualMachines"); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := id.ValidateNoEmptySegments(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &service, nil | ||
} |
61 changes: 61 additions & 0 deletions
61
azurerm/internal/services/devtestlabs/parse/global_schedule_virtual_machine_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package parse | ||
|
||
import "testing" | ||
|
||
func TestGlobalScheduleVirtualMachine(t *testing.T) { | ||
testData := []struct { | ||
Name string | ||
Input string | ||
Expected *GlobalScheduleVirtualMachineId | ||
}{ | ||
{ | ||
Name: "Empty", | ||
Input: "", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No virtual machine segment", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "No virtual machine name", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/microsoft.compute/virtualMachines/", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Case incorrect in path element", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/microsoft.compute/VirtualMachines/machine1", | ||
Expected: nil, | ||
}, | ||
{ | ||
Name: "Valid", | ||
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/microsoft.compute/virtualMachines/machine1", | ||
Expected: &GlobalScheduleVirtualMachineId{ | ||
ResourceGroup: "myGroup1", | ||
Name: "machine1", | ||
}, | ||
}, | ||
} | ||
|
||
for _, v := range testData { | ||
t.Logf("[DEBUG] Testing %q", v.Name) | ||
|
||
actual, err := GlobalScheduleVirtualMachineID(v.Input) | ||
if err != nil { | ||
if v.Expected == nil { | ||
continue | ||
} | ||
|
||
t.Fatalf("Expected a value but got an error: %s", err) | ||
} | ||
|
||
if actual.Name != v.Expected.Name { | ||
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) | ||
} | ||
|
||
if actual.ResourceGroup != v.Expected.ResourceGroup { | ||
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
azurerm/internal/services/devtestlabs/validate/global_schedule.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/devtestlabs/parse" | ||
) | ||
|
||
func GlobalScheduleID(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.GlobalScheduleID(v); err != nil { | ||
errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err)) | ||
return | ||
} | ||
|
||
return warnings, errors | ||
} |
40 changes: 40 additions & 0 deletions
40
azurerm/internal/services/devtestlabs/validate/global_schedule_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package validate | ||
|
||
import "testing" | ||
|
||
func TestGlobalScheduleID(t *testing.T) { | ||
cases := []struct { | ||
ID string | ||
Valid bool | ||
}{ | ||
{ | ||
ID: "", | ||
Valid: false, | ||
}, | ||
{ | ||
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/", | ||
Valid: false, | ||
}, | ||
{ | ||
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/Microsoft.DevTestLab/schedules/", | ||
Valid: false, | ||
}, | ||
{ | ||
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.DevTestLab/Schedules/schedule1", | ||
Valid: false, | ||
}, | ||
{ | ||
ID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myGroup1/providers/Microsoft.DevTestLab/schedules/schedule1", | ||
Valid: true, | ||
}, | ||
} | ||
for _, tc := range cases { | ||
t.Logf("[DEBUG] Testing Value %s", tc.ID) | ||
_, errors := GlobalScheduleID(tc.ID, "test") | ||
valid := len(errors) == 0 | ||
|
||
if tc.Valid != valid { | ||
t.Fatalf("Expected %t but got %t", tc.Valid, valid) | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
azurerm/internal/services/devtestlabs/validate/global_schedule_virtual_machine.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/devtestlabs/parse" | ||
) | ||
|
||
func GlobalScheduleVirtualMachineID(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.GlobalScheduleVirtualMachineID(v); err != nil { | ||
errors = append(errors, fmt.Errorf("Can not parse %q as a resource id: %v", k, err)) | ||
return | ||
} | ||
|
||
return warnings, errors | ||
} |
Oops, something went wrong.