diff --git a/internal/services/machinelearning/machine_learning_compute_cluster_resource.go b/internal/services/machinelearning/machine_learning_compute_cluster_resource.go index 5193ca81381e..eeb55e0e5458 100644 --- a/internal/services/machinelearning/machine_learning_compute_cluster_resource.go +++ b/internal/services/machinelearning/machine_learning_compute_cluster_resource.go @@ -171,21 +171,63 @@ func resourceComputeClusterCreate(d *pluginsdk.ResourceData, meta interface{}) e if err != nil { return err } - + // Get the Machine Learning Workspace... id := machinelearningcomputes.NewComputeID(workspaceID.SubscriptionId, workspaceID.ResourceGroupName, workspaceID.WorkspaceName, d.Get("name").(string)) + workspace, err := mlWorkspacesClient.Get(ctx, *workspaceID) + if err != nil { + return fmt.Errorf("retrieving %s: %+v", workspaceID, err) + } + + workspaceModel := workspace.Model + if workspaceModel == nil { + return fmt.Errorf("retrieving %s: `model` was nil", workspaceID) + } + + if workspaceModel.Sku == nil || workspaceModel.Sku.Tier == nil || workspaceModel.Sku.Name == "" { + return fmt.Errorf("retrieving %s: `sku` was nil or empty", workspaceID) + } + + if workspaceModel.Location == nil { + return fmt.Errorf("retrieving %s: `location` was nil", workspaceID) + } + + identity, err := expandIdentity(d.Get("identity").([]interface{})) + if err != nil { + return fmt.Errorf("expanding `identity`: %+v", err) + } + existing, err := client.ComputeGet(ctx, id) if err != nil { if !response.WasNotFound(existing.HttpResponse) { return fmt.Errorf("checking for presence of existing %s: %+v", id, err) } } + if !response.WasNotFound(existing.HttpResponse) { return tf.ImportAsExistsError("azurerm_machine_learning_compute_cluster", id.ID()) } + nodePublicIPEnabled, ok := d.Get("node_public_ip_enabled").(bool) + if !ok { + return fmt.Errorf("unable to assert type for `node_public_ip_enabled`") + } - if !d.Get("node_public_ip_enabled").(bool) && d.Get("subnet_resource_id").(string) == "" { - return fmt.Errorf("`subnet_resource_id` must be set if `node_public_ip_enabled` is set to `false`") + subnetResourceID, ok := d.Get("subnet_resource_id").(string) + if !ok { + return fmt.Errorf("unable to assert type for `subnet_resource_id`") + } + + workspaceInManagedVnet := false + + if workspaceModel.Properties != nil && + workspaceModel.Properties.ManagedNetwork != nil && + workspaceModel.Properties.ManagedNetwork.Status != nil && + workspaceModel.Properties.ManagedNetwork.Status.Status != nil { + workspaceInManagedVnet = *workspaceModel.Properties.ManagedNetwork.Status.Status == workspaces.ManagedNetworkStatusActive + } + + if !nodePublicIPEnabled && subnetResourceID == "" && !workspaceInManagedVnet { + return fmt.Errorf("`subnet_resource_id` must be set if `node_public_ip_enabled` is set to `false` or the workspace is not in a managed network") } vmPriority := machinelearningcomputes.VMPriority(d.Get("vm_priority").(string)) @@ -215,30 +257,6 @@ func resourceComputeClusterCreate(d *pluginsdk.ResourceData, meta interface{}) e DisableLocalAuth: utils.Bool(!d.Get("local_auth_enabled").(bool)), } - // Get the Machine Learning Workspace... - workspace, err := mlWorkspacesClient.Get(ctx, *workspaceID) - if err != nil { - return err - } - - workspaceModel := workspace.Model - if workspaceModel == nil { - return fmt.Errorf("machine learning %s Workspace: model was nil", id) - } - - if workspaceModel.Sku == nil || workspaceModel.Sku.Tier == nil || workspaceModel.Sku.Name == "" { - return fmt.Errorf("machine learning %s Workspace: `SKU` was nil or empty", id) - } - - if workspaceModel.Location == nil { - return fmt.Errorf("machine learning %s Workspace: `Location` was nil", id) - } - - identity, err := expandIdentity(d.Get("identity").([]interface{})) - if err != nil { - return fmt.Errorf("expanding `identity`: %+v", err) - } - // NOTE: The 'ComputeResource' 'Location' field should always point // to the workspace's 'location'... computeClusterParameters := machinelearningcomputes.ComputeResource{