From 7a262d3709d91d25197925a01f62c75273d645ee Mon Sep 17 00:00:00 2001 From: Michael Gross Date: Sun, 16 May 2021 17:52:57 +0200 Subject: [PATCH 1/2] use auto-generated parsers and validators --- .../machine_learning_workspace_resource.go | 135 +++------------- .../machinelearning/parse/workspace.go | 68 ++++---- .../machinelearning/parse/workspace_test.go | 145 +++++++----------- .../services/machinelearning/resourceids.go | 3 + .../machinelearning/validate/workspace_id.go | 23 +++ .../validate/workspace_id_test.go | 76 +++++++++ 6 files changed, 226 insertions(+), 224 deletions(-) create mode 100644 azurerm/internal/services/machinelearning/resourceids.go create mode 100644 azurerm/internal/services/machinelearning/validate/workspace_id.go create mode 100644 azurerm/internal/services/machinelearning/validate/workspace_id_test.go diff --git a/azurerm/internal/services/machinelearning/machine_learning_workspace_resource.go b/azurerm/internal/services/machinelearning/machine_learning_workspace_resource.go index ae09d3008260..a3b25ee34ebc 100644 --- a/azurerm/internal/services/machinelearning/machine_learning_workspace_resource.go +++ b/azurerm/internal/services/machinelearning/machine_learning_workspace_resource.go @@ -1,13 +1,10 @@ package machinelearning import ( - "context" "fmt" "time" "github.com/Azure/azure-sdk-for-go/services/machinelearningservices/mgmt/2020-04-01/machinelearningservices" - "github.com/Azure/azure-sdk-for-go/services/preview/containerregistry/mgmt/2020-11-01-preview/containerregistry" - "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-01-01/storage" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" @@ -169,34 +166,26 @@ func resourceMachineLearningWorkspaceCreate(d *schema.ResourceData, meta interfa existing, err := client.Get(ctx, resGroup, name) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("Error checking for existing AML Workspace %q (Resource Group %q): %s", name, resGroup, err) + return fmt.Errorf("checking for existing AML Workspace %q (Resource Group %q): %s", name, resGroup, err) } } if existing.ID != nil && *existing.ID != "" { return tf.ImportAsExistsError("azurerm_machine_learning_workspace", *existing.ID) } - location := azure.NormalizeLocation(d.Get("location").(string)) - storageAccountId := d.Get("storage_account_id").(string) - keyVaultId := d.Get("key_vault_id").(string) - applicationInsightsId := d.Get("application_insights_id").(string) - skuName := d.Get("sku_name").(string) - - t := d.Get("tags").(map[string]interface{}) - workspace := machinelearningservices.Workspace{ - Name: &name, - Location: &location, - Tags: tags.Expand(t), + Name: utils.String(name), + Location: utils.String(azure.NormalizeLocation(d.Get("location").(string))), + Tags: tags.Expand(d.Get("tags").(map[string]interface{})), Sku: &machinelearningservices.Sku{ - Name: utils.String(skuName), - Tier: utils.String(skuName), + Name: utils.String(d.Get("sku_name").(string)), + Tier: utils.String(d.Get("sku_name").(string)), }, Identity: expandMachineLearningWorkspaceIdentity(d.Get("identity").([]interface{})), WorkspaceProperties: &machinelearningservices.WorkspaceProperties{ - StorageAccount: &storageAccountId, - ApplicationInsights: &applicationInsightsId, - KeyVault: &keyVaultId, + StorageAccount: utils.String(d.Get("storage_account_id").(string)), + ApplicationInsights: utils.String(d.Get("application_insights_id").(string)), + KeyVault: utils.String(d.Get("key_vault_id").(string)), }, } @@ -216,35 +205,18 @@ func resourceMachineLearningWorkspaceCreate(d *schema.ResourceData, meta interfa workspace.HbiWorkspace = utils.Bool(v.(bool)) } - accountsClient := meta.(*clients.Client).Storage.AccountsClient - if err := validateStorageAccount(ctx, *accountsClient, storageAccountId); err != nil { - return fmt.Errorf("Error creating Machine Learning Workspace %q (Resource Group %q): %+v", name, resGroup, err) - } - - registriesClient := meta.(*clients.Client).Containers.RegistriesClient - if err := validateContainerRegistry(ctx, *registriesClient, workspace.ContainerRegistry); err != nil { - return fmt.Errorf("Error creating Machine Learning Workspace %q (Resource Group %q): %+v", name, resGroup, err) - } - future, err := client.CreateOrUpdate(ctx, resGroup, name, workspace) if err != nil { - return fmt.Errorf("Error creating Machine Learning Workspace %q (Resource Group %q): %+v", name, resGroup, err) + return fmt.Errorf("creating Machine Learning Workspace %q (Resource Group %q): %+v", name, resGroup, err) } if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("Error waiting for creation of Machine Learning Workspace %q (Resource Group %q): %+v", name, resGroup, err) - } - - resp, err := client.Get(ctx, resGroup, name) - if err != nil { - return fmt.Errorf("Error retrieving Machine Learning Workspace %q (Resource Group %q): %+v", name, resGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Machine Learning Workspace %q (Resource Group %q) ID", name, resGroup) + return fmt.Errorf("waiting for creation of Machine Learning Workspace %q (Resource Group %q): %+v", name, resGroup, err) } - d.SetId(*resp.ID) + subscriptionId := meta.(*clients.Client).Account.SubscriptionId + id := parse.NewWorkspaceID(subscriptionId, resGroup, name) + d.SetId(id.ID()) return resourceMachineLearningWorkspaceRead(d, meta) } @@ -256,7 +228,7 @@ func resourceMachineLearningWorkspaceRead(d *schema.ResourceData, meta interface id, err := parse.WorkspaceID(d.Id()) if err != nil { - return fmt.Errorf("Error parsing Machine Learning Workspace ID `%q`: %+v", d.Id(), err) + return fmt.Errorf("parsing Machine Learning Workspace ID `%q`: %+v", d.Id(), err) } resp, err := client.Get(ctx, id.ResourceGroup, id.Name) @@ -265,7 +237,7 @@ func resourceMachineLearningWorkspaceRead(d *schema.ResourceData, meta interface d.SetId("") return nil } - return fmt.Errorf("Error making Read request on Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + return fmt.Errorf("making Read request on Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) } d.Set("name", id.Name) @@ -290,7 +262,7 @@ func resourceMachineLearningWorkspaceRead(d *schema.ResourceData, meta interface } if err := d.Set("identity", flattenMachineLearningWorkspaceIdentity(resp.Identity)); err != nil { - return fmt.Errorf("Error flattening identity on Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + return fmt.Errorf("flattening identity on Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) } return tags.FlattenAndSet(d, resp.Tags) @@ -331,7 +303,7 @@ func resourceMachineLearningWorkspaceUpdate(d *schema.ResourceData, meta interfa } if _, err := client.Update(ctx, id.ResourceGroup, id.Name, update); err != nil { - return fmt.Errorf("Error updating Machine Learning Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + return fmt.Errorf("updating Machine Learning Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) } return resourceMachineLearningWorkspaceRead(d, meta) @@ -344,67 +316,16 @@ func resourceMachineLearningWorkspaceDelete(d *schema.ResourceData, meta interfa id, err := parse.WorkspaceID(d.Id()) if err != nil { - return fmt.Errorf("Error parsing Machine Learning Workspace ID `%q`: %+v", d.Id(), err) + return fmt.Errorf("parsing Machine Learning Workspace ID `%q`: %+v", d.Id(), err) } future, err := client.Delete(ctx, id.ResourceGroup, id.Name) if err != nil { - return fmt.Errorf("Error deleting Machine Learning Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) + return fmt.Errorf("deleting Machine Learning Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) } if err := future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("Error waiting for deletion of Machine Learning Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) - } - - return nil -} - -func validateStorageAccount(ctx context.Context, client storage.AccountsClient, accountID string) error { - if accountID == "" { - return fmt.Errorf("Error validating Storage Account: Empty ID") - } - - // TODO -- use parse function "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/parsers".ParseAccountID - // when issue https://github.com/Azure/azure-rest-api-specs/issues/8323 is addressed - id, err := parse.AccountIDCaseDiffSuppress(accountID) - if err != nil { - return fmt.Errorf("Error validating Storage Account: %+v", err) - } - - account, err := client.GetProperties(ctx, id.ResourceGroup, id.Name, "") - if err != nil { - return fmt.Errorf("Error validating Storage Account %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) - } - if sku := account.Sku; sku != nil { - if sku.Tier == storage.Premium { - return fmt.Errorf("Error validating Storage Account %q (Resource Group %q): The associated Storage Account must not be Premium", id.Name, id.ResourceGroup) - } - } - - return nil -} - -func validateContainerRegistry(ctx context.Context, client containerregistry.RegistriesClient, acrID *string) error { - if acrID == nil { - return nil - } - - // TODO: use container registry's custom ID parse function when implemented - id, err := azure.ParseAzureResourceID(*acrID) - if err != nil { - return fmt.Errorf("Error validating Container Registry: %+v", err) - } - - acrName := id.Path["registries"] - resourceGroup := id.ResourceGroup - client.SubscriptionID = id.SubscriptionID - - acr, err := client.Get(ctx, resourceGroup, acrName) - if err != nil { - return fmt.Errorf("Error validating Container Registry %q (Resource Group %q): %+v", acrName, resourceGroup, err) - } - if acr.AdminUserEnabled == nil || !*acr.AdminUserEnabled { - return fmt.Errorf("Error validating Container Registry%q (Resource Group %q): The associated Container Registry must set `admin_enabled` to true", acrName, resourceGroup) + return fmt.Errorf("waiting for deletion of Machine Learning Workspace %q (Resource Group %q): %+v", id.Name, id.ResourceGroup, err) } return nil @@ -417,13 +338,9 @@ func expandMachineLearningWorkspaceIdentity(input []interface{}) *machinelearnin v := input[0].(map[string]interface{}) - identityType := machinelearningservices.ResourceIdentityType(v["type"].(string)) - - identity := machinelearningservices.Identity{ - Type: identityType, + return &machinelearningservices.Identity{ + Type: machinelearningservices.ResourceIdentityType(v["type"].(string)), } - - return &identity } func flattenMachineLearningWorkspaceIdentity(identity *machinelearningservices.Identity) []interface{} { @@ -431,8 +348,6 @@ func flattenMachineLearningWorkspaceIdentity(identity *machinelearningservices.I return []interface{}{} } - t := string(identity.Type) - principalID := "" if identity.PrincipalID != nil { principalID = *identity.PrincipalID @@ -445,9 +360,9 @@ func flattenMachineLearningWorkspaceIdentity(identity *machinelearningservices.I return []interface{}{ map[string]interface{}{ - "type": t, + "type": string(identity.Type), "principal_id": principalID, "tenant_id": tenantID, }, } -} +} \ No newline at end of file diff --git a/azurerm/internal/services/machinelearning/parse/workspace.go b/azurerm/internal/services/machinelearning/parse/workspace.go index b5f72ac7a57f..da44338629d3 100644 --- a/azurerm/internal/services/machinelearning/parse/workspace.go +++ b/azurerm/internal/services/machinelearning/parse/workspace.go @@ -1,57 +1,69 @@ package parse +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + import ( + "fmt" + "strings" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" - accountParser "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/parse" ) type WorkspaceId struct { - Name string - ResourceGroup string + SubscriptionId string + ResourceGroup string + Name string } -func WorkspaceID(input string) (*WorkspaceId, error) { - id, err := azure.ParseAzureResourceID(input) - if err != nil { - return nil, err - } - - workspace := WorkspaceId{ - ResourceGroup: id.ResourceGroup, - } - - if workspace.Name, err = id.PopSegment("workspaces"); err != nil { - return nil, err +func NewWorkspaceID(subscriptionId, resourceGroup, name string) WorkspaceId { + return WorkspaceId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + Name: name, } +} - if err := id.ValidateNoEmptySegments(input); err != nil { - return nil, err +func (id WorkspaceId) String() string { + segments := []string{ + fmt.Sprintf("Name %q", id.Name), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Workspace", segmentsStr) +} - return &workspace, nil +func (id WorkspaceId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.MachineLearningServices/workspaces/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.Name) } -// TODO -- use parse function "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/parsers".ParseAccountID -// when issue https://github.com/Azure/azure-rest-api-specs/issues/8323 is addressed -func AccountIDCaseDiffSuppress(input string) (*accountParser.StorageAccountId, error) { +// WorkspaceID parses a Workspace ID into an WorkspaceId struct +func WorkspaceID(input string) (*WorkspaceId, error) { id, err := azure.ParseAzureResourceID(input) if err != nil { return nil, err } - account := accountParser.StorageAccountId{ - ResourceGroup: id.ResourceGroup, + resourceId := WorkspaceId{ + SubscriptionId: id.SubscriptionID, + ResourceGroup: id.ResourceGroup, } - if account.Name, err = id.PopSegment("storageAccounts"); err != nil { - if account.Name, err = id.PopSegment("storageaccounts"); err != nil { - return nil, err - } + if resourceId.SubscriptionId == "" { + return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + } + + if resourceId.ResourceGroup == "" { + return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + } + + if resourceId.Name, err = id.PopSegment("workspaces"); err != nil { + return nil, err } if err := id.ValidateNoEmptySegments(input); err != nil { return nil, err } - return &account, nil + return &resourceId, nil } diff --git a/azurerm/internal/services/machinelearning/parse/workspace_test.go b/azurerm/internal/services/machinelearning/parse/workspace_test.go index 48c6fbffafc0..00a6b100fd69 100644 --- a/azurerm/internal/services/machinelearning/parse/workspace_test.go +++ b/azurerm/internal/services/machinelearning/parse/workspace_test.go @@ -1,139 +1,112 @@ package parse +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + import ( "testing" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/storage/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/resourceid" ) +var _ resourceid.Formatter = WorkspaceId{} + +func TestWorkspaceIDFormatter(t *testing.T) { + actual := NewWorkspaceID("00000000-0000-0000-0000-000000000000", "resGroup1", "workspace1").ID() + expected := "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/workspaces/workspace1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + func TestWorkspaceID(t *testing.T) { testData := []struct { - Name string - Input string - Error bool - Expect *WorkspaceId + Input string + Error bool + Expected *WorkspaceId }{ + { - Name: "Empty", + // 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/resGroup1", + // missing SubscriptionId + Input: "/", Error: true, }, + { - Name: "Missing Workspace Value", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/workspaces/", + // missing value for SubscriptionId + Input: "/subscriptions/", Error: true, }, - { - Name: "Machine Learning Workspace ID", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/workspaces/workspace1", - Error: false, - Expect: &WorkspaceId{ - ResourceGroup: "resGroup1", - Name: "workspace1", - }, - }, - } - - for _, v := range testData { - t.Logf("[DEBUG] Testing %q", v.Name) - - actual, err := WorkspaceID(v.Input) - if err != nil { - if v.Error { - continue - } - - t.Fatalf("Expected a value but got an error: %+v", 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) - } - } -} -func TestAccountIDCaseDiffSuppress(t *testing.T) { - testData := []struct { - Name string - Input string - Error bool - Expect *parse.StorageAccountId - }{ { - Name: "Empty", - Input: "", + // missing ResourceGroup + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/", Error: true, }, + { - Name: "No Resource Group Segment", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000", + // missing value for ResourceGroup + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", Error: true, }, + { - Name: "No Resource Groups Value", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups", + // missing Name + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/", Error: true, }, + { - Name: "Resource Group ID", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1", + // missing value for Name + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/workspaces/", Error: true, }, + { - Name: "Account ID with right casing", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/storageAccounts/account1", - Expect: &parse.StorageAccountId{ - Name: "account1", - ResourceGroup: "resGroup1", + // valid + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/workspaces/workspace1", + Expected: &WorkspaceId{ + SubscriptionId: "00000000-0000-0000-0000-000000000000", + ResourceGroup: "resGroup1", + Name: "workspace1", }, }, + { - Name: "Wrong Casing", - Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resgroup1/storageaccounts/account1", - Expect: &parse.StorageAccountId{ - Name: "account1", - ResourceGroup: "resgroup1", - }, + // upper-cased + Input: "/SUBSCRIPTIONS/00000000-0000-0000-0000-000000000000/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.MACHINELEARNINGSERVICES/WORKSPACES/WORKSPACE1", + Error: true, }, } for _, v := range testData { - t.Logf("[DEBUG] Testing %q", v.Name) + t.Logf("[DEBUG] Testing %q", v.Input) - actual, err := AccountIDCaseDiffSuppress(v.Input) + actual, err := WorkspaceID(v.Input) if err != nil { if v.Error { continue } - t.Fatalf("Expected a value but got an error: %+v", err) + t.Fatalf("Expect 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 v.Error { + t.Fatal("Expect an error but didn't get one") } - if actual.ResourceGroup != v.Expect.ResourceGroup { - t.Fatalf("Expected %q but got %q for Resource Group", v.Expect.ResourceGroup, actual.ResourceGroup) + if actual.SubscriptionId != v.Expected.SubscriptionId { + t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) + } + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) } } } diff --git a/azurerm/internal/services/machinelearning/resourceids.go b/azurerm/internal/services/machinelearning/resourceids.go new file mode 100644 index 000000000000..9a401d115c9b --- /dev/null +++ b/azurerm/internal/services/machinelearning/resourceids.go @@ -0,0 +1,3 @@ +package machinelearning + +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Workspace -id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/workspaces/workspace1 diff --git a/azurerm/internal/services/machinelearning/validate/workspace_id.go b/azurerm/internal/services/machinelearning/validate/workspace_id.go new file mode 100644 index 000000000000..8a03456b8040 --- /dev/null +++ b/azurerm/internal/services/machinelearning/validate/workspace_id.go @@ -0,0 +1,23 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/machinelearning/parse" +) + +func WorkspaceID(input interface{}, key string) (warnings []string, errors []error) { + v, ok := input.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected %q to be a string", key)) + return + } + + if _, err := parse.WorkspaceID(v); err != nil { + errors = append(errors, err) + } + + return +} \ No newline at end of file diff --git a/azurerm/internal/services/machinelearning/validate/workspace_id_test.go b/azurerm/internal/services/machinelearning/validate/workspace_id_test.go new file mode 100644 index 000000000000..bbbf5a754d8f --- /dev/null +++ b/azurerm/internal/services/machinelearning/validate/workspace_id_test.go @@ -0,0 +1,76 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import "testing" + +func TestWorkspaceID(t *testing.T) { + cases := []struct { + Input string + Valid bool + }{ + + { + // empty + Input: "", + Valid: false, + }, + + { + // missing SubscriptionId + Input: "/", + Valid: false, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Valid: false, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/", + Valid: false, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/", + Valid: false, + }, + + { + // missing Name + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/", + Valid: false, + }, + + { + // missing value for Name + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/workspaces/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.MachineLearningServices/workspaces/workspace1", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/00000000-0000-0000-0000-000000000000/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.MACHINELEARNINGSERVICES/WORKSPACES/WORKSPACE1", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := WorkspaceID(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +} \ No newline at end of file From 0ede35c45903d14a17f160b3625ab288db174cac Mon Sep 17 00:00:00 2001 From: Michael Gross Date: Sun, 16 May 2021 19:53:10 +0200 Subject: [PATCH 2/2] make fmt -> fix unit tests --- .../machinelearning/machine_learning_workspace_resource.go | 2 +- .../internal/services/machinelearning/validate/workspace_id.go | 2 +- .../services/machinelearning/validate/workspace_id_test.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/azurerm/internal/services/machinelearning/machine_learning_workspace_resource.go b/azurerm/internal/services/machinelearning/machine_learning_workspace_resource.go index a3b25ee34ebc..2c4390df1661 100644 --- a/azurerm/internal/services/machinelearning/machine_learning_workspace_resource.go +++ b/azurerm/internal/services/machinelearning/machine_learning_workspace_resource.go @@ -365,4 +365,4 @@ func flattenMachineLearningWorkspaceIdentity(identity *machinelearningservices.I "tenant_id": tenantID, }, } -} \ No newline at end of file +} diff --git a/azurerm/internal/services/machinelearning/validate/workspace_id.go b/azurerm/internal/services/machinelearning/validate/workspace_id.go index 8a03456b8040..ffe3eacffd64 100644 --- a/azurerm/internal/services/machinelearning/validate/workspace_id.go +++ b/azurerm/internal/services/machinelearning/validate/workspace_id.go @@ -20,4 +20,4 @@ func WorkspaceID(input interface{}, key string) (warnings []string, errors []err } return -} \ No newline at end of file +} diff --git a/azurerm/internal/services/machinelearning/validate/workspace_id_test.go b/azurerm/internal/services/machinelearning/validate/workspace_id_test.go index bbbf5a754d8f..2afab4d72451 100644 --- a/azurerm/internal/services/machinelearning/validate/workspace_id_test.go +++ b/azurerm/internal/services/machinelearning/validate/workspace_id_test.go @@ -73,4 +73,4 @@ func TestWorkspaceID(t *testing.T) { t.Fatalf("Expected %t but got %t", tc.Valid, valid) } } -} \ No newline at end of file +}