Skip to content

Commit

Permalink
azurerm_machine_learning_compute_cluster change subnet_resource_id co…
Browse files Browse the repository at this point in the history
…nstraint: Respect Managed Vnet when creating compute clusters fixes #25901  (#26073)

* Respect ManagedVnet constraints when creating compute clusters

* Apply suggestions from code review

Co-authored-by: stephybun <steph@hashicorp.com>

* extend nil checks

* fix error statement

* add accidently removed ImportAsExistsErrore

* fix linting and test issues

---------

Co-authored-by: Hamade, Marcel  SF/HZA-ZC3P <hamadmrc@schaeffler.com>
Co-authored-by: stephybun <steph@hashicorp.com>
  • Loading branch information
3 people committed May 23, 2024
1 parent 0369148 commit 4620361
Showing 1 changed file with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,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))
Expand Down Expand Up @@ -217,30 +259,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{
Expand Down

0 comments on commit 4620361

Please sign in to comment.