Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Resource ] - azurerm_machine_learning_datastore_blobstorage #19909

Merged
merged 19 commits into from
Jan 20, 2023
6 changes: 6 additions & 0 deletions internal/services/machinelearning/client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"github.com/hashicorp/go-azure-sdk/resource-manager/machinelearningservices/2022-05-01/datastore"
"github.com/hashicorp/go-azure-sdk/resource-manager/machinelearningservices/2022-05-01/machinelearningcomputes"
"github.com/hashicorp/go-azure-sdk/resource-manager/machinelearningservices/2022-05-01/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
Expand All @@ -9,6 +10,7 @@ import (
type Client struct {
ComputeClient *machinelearningcomputes.MachineLearningComputesClient
WorkspacesClient *workspaces.WorkspacesClient
DatastoreClient *datastore.DatastoreClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -18,8 +20,12 @@ func NewClient(o *common.ClientOptions) *Client {
WorkspacesClient := workspaces.NewWorkspacesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&WorkspacesClient.Client, o.ResourceManagerAuthorizer)

DatastoreClient := datastore.NewDatastoreClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&DatastoreClient.Client, o.ResourceManagerAuthorizer)

return &Client{
ComputeClient: &ComputeClient,
WorkspacesClient: &WorkspacesClient,
DatastoreClient: &DatastoreClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
package machinelearning

import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/machinelearningservices/2022-05-01/datastore"
"github.com/hashicorp/go-azure-sdk/resource-manager/machinelearningservices/2022-05-01/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/machinelearning/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/suppress"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceMachineLearningDataStore() *pluginsdk.Resource {
resource := &pluginsdk.Resource{
Create: resourceMachineLearningDataStoreCreateOrUpdate,
Read: resourceMachineLearningDataStoreRead,
Update: resourceMachineLearningDataStoreCreateOrUpdate,
Delete: resourceMachineLearningDataStoreDelete,

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := datastore.ParseDataStoreID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataStoreName,
},

"workspace_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.WorkspaceID,
},

"storage_account_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringIsNotEmpty,
},

"container_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved

"description": {
Type: pluginsdk.TypeString,
Optional: true,
ForceNew: true,
},

"is_default": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"service_data_auth_identity": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(datastore.ServiceDataAccessAuthIdentityNone),
string(datastore.ServiceDataAccessAuthIdentityWorkspaceSystemAssignedIdentity),
string(datastore.ServiceDataAccessAuthIdentityWorkspaceUserAssignedIdentity),
},
false),
Default: string(datastore.ServiceDataAccessAuthIdentityNone),
},

"account_key": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
ExactlyOneOf: []string{"account_key", "shared_access_signature"},
},

"shared_access_signature": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
ValidateFunc: validation.StringIsNotEmpty,
AtLeastOneOf: []string{"account_key", "shared_access_signature"},
},

"tags": commonschema.TagsForceNew(),
},
}
return resource
}

func resourceMachineLearningDataStoreCreateOrUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved
client := meta.(*clients.Client).MachineLearning.DatastoreClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

workspaceId, err := workspaces.ParseWorkspaceID(d.Get("workspace_id").(string))
if err != nil {
return err
}

id := datastore.NewDataStoreID(subscriptionId, workspaceId.ResourceGroupName, workspaceId.WorkspaceName, d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(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_datastore_blobstorage", id.ID())
}
}

datastoreRaw := datastore.DatastoreResource{
Name: utils.String(d.Get("name").(string)),
Type: utils.ToPtr(string(datastore.DatastoreTypeAzureBlob)),
}

prop := expandBlobStorage(d)
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved
datastoreRaw.Properties = prop

_, err = client.CreateOrUpdate(ctx, id, datastoreRaw, datastore.DefaultCreateOrUpdateOperationOptions())
if err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

d.SetId(id.ID())
return resourceMachineLearningDataStoreRead(d, meta)
}

func resourceMachineLearningDataStoreRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MachineLearning.DatastoreClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := datastore.ParseDataStoreID(d.Id())
if err != nil {
return fmt.Errorf("parsing Machine Learning Data Store ID `%q`: %+v", d.Id(), err)
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on Machine Learning Data Store %q (Resource Group %q): %+v", id.Name, id.ResourceGroupName, err)
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved
}

workspaceId := workspaces.NewWorkspaceID(subscriptionId, id.ResourceGroupName, id.WorkspaceName)
d.Set("name", resp.Model.Name)
d.Set("workspace_id", workspaceId.ID())
return flattenBlobStorage(d, resp.Model.Properties.(datastore.AzureBlobDatastore))
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved
}

func resourceMachineLearningDataStoreDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).MachineLearning.DatastoreClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := datastore.ParseDataStoreID(d.Id())
if err != nil {
return fmt.Errorf("parsing Machine Learning Workspace Date Store ID `%q`: %+v", d.Id(), err)
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved
}

if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting Machine Learning Workspace Date Strore %q (Resource Group %q): %+v", id.Name, id.ResourceGroupName, err)
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved
}

return nil
}

func expandBlobStorage(d *pluginsdk.ResourceData) *datastore.AzureBlobDatastore {
storeProps := &datastore.AzureBlobDatastore{
AccountName: utils.String(d.Get("storage_account_name").(string)),
ContainerName: utils.String(d.Get("container_name").(string)),
Description: utils.String(d.Get("description").(string)),
ServiceDataAccessAuthIdentity: utils.ToPtr(datastore.ServiceDataAccessAuthIdentity(d.Get("service_data_auth_identity").(string))),
IsDefault: utils.Bool(d.Get("is_default").(bool)),
Tags: utils.ToPtr(expandTags(d.Get("tags").(map[string]interface{}))),
}

accountKey := d.Get("account_key").(string)
if accountKey != "" {
storeProps.Credentials = map[string]interface{}{
"credentialsType": string(datastore.CredentialsTypeAccountKey),
"secrets": map[string]interface{}{
"secretsType": "AccountKey",
"key": accountKey,
},
}
}

sasToken := d.Get("shared_access_signature").(string)
if sasToken != "" {
storeProps.Credentials = map[string]interface{}{
"credentialsType": string(datastore.CredentialsTypeSas),
"secrets": map[string]interface{}{
"secretsType": "Sas",
"sasToken": sasToken,
},
}
}

return storeProps
}

func flattenBlobStorage(d *pluginsdk.ResourceData, data datastore.AzureBlobDatastore) error {
d.Set("description", data.Description)
d.Set("is_default", data.IsDefault)
d.Set("service_data_auth_identity", string(*data.ServiceDataAccessAuthIdentity))
d.Set("storage_account_name", *data.AccountName)
d.Set("container_name", *data.ContainerName)
xuzhang3 marked this conversation as resolved.
Show resolved Hide resolved
return flattenAndSetTags(d, *data.Tags)
}

func expandTags(tagsMap map[string]interface{}) map[string]string {
output := make(map[string]string, len(tagsMap))

for i, v := range tagsMap {
// Validate should have ignored this error already
value, _ := tags.TagValueToString(v)
output[i] = value
}

return output
}

func flattenAndSetTags(d *pluginsdk.ResourceData, tagMap map[string]string) error {
output := make(map[string]interface{}, len(tagMap))
for i, v := range tagMap {
output[i] = v
}

if err := d.Set("tags", output); err != nil {
return fmt.Errorf("setting `tags`: %s", err)
}

return nil
}
Loading