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_data_share_dataset_data_lake_gen1 #7511

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions azurerm/internal/services/datalake/parse/data_lake.go
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 DataLakeStoreId struct {
Subscription string
ResourceGroup string
Name string
}

func DataLakeStoreID(input string) (*DataLakeStoreId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Data Lake Store ID %q: %+v", input, err)
}

dataLakeStore := DataLakeStoreId{
ResourceGroup: id.ResourceGroup,
Subscription: id.SubscriptionID,
}
if dataLakeStore.Name, err = id.PopSegment("accounts"); err != nil {
return nil, err
}
if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &dataLakeStore, nil
}
75 changes: 75 additions & 0 deletions azurerm/internal/services/datalake/parse/data_lake_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package parse

import "testing"

func TestDataLakeStoreID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *DataLakeStoreId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Account Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/",
Expected: nil,
},
{
Name: "Data lake account ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1",
Expected: &DataLakeStoreId{
Name: "account1",
ResourceGroup: "resGroup1",
Subscription: "00000000-0000-0000-0000-000000000000",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/Accounts/account1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q..", v.Name)

actual, err := DataLakeStoreID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}
t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Subscription != v.Expected.Subscription {
t.Fatalf("Expected %q but got %q for Subscription", v.Expected.Subscription, actual.Subscription)
}

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)
}
}
}
7 changes: 4 additions & 3 deletions azurerm/internal/services/datashare/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_data_share_account": resourceArmDataShareAccount(),
"azurerm_data_share": resourceArmDataShare(),
"azurerm_data_share_dataset_blob_storage": resourceArmDataShareDataSetBlobStorage(),
"azurerm_data_share_account": resourceArmDataShareAccount(),
"azurerm_data_share": resourceArmDataShare(),
"azurerm_data_share_dataset_blob_storage": resourceArmDataShareDataSetBlobStorage(),
"azurerm_data_share_dataset_data_lake_gen1": resourceArmDataShareDataSetDataLakeGen1(),
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package datashare

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/datashare/mgmt/2019-11-01/datashare"
"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/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
dataLakeParse "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datalake/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datashare/helper"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datashare/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datashare/validate"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmDataShareDataSetDataLakeGen1() *schema.Resource {
return &schema.Resource{
Create: resourceArmDataShareDataSetDataLakeGen1Create,
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
Read: resourceArmDataShareDataSetDataLakeGen1Read,
Delete: resourceArmDataShareDataSetDataLakeGen1Delete,

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

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.DataShareDataSetID(id)
return err
}),

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DatashareDataSetName(),
},

"data_share_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataShareID,
},

"data_lake_store_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DatalakeStoreID,
},

"folder_path": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"file_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"display_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceArmDataShareDataSetDataLakeGen1Create(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataShare.DataSetClient
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
shareId, err := parse.DataShareID(d.Get("data_share_id").(string))
if err != nil {
return err
}

existing, err := client.Get(ctx, shareId.ResourceGroup, shareId.AccountName, shareId.Name, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for present of existing DataShare DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", name, shareId.ResourceGroup, shareId.AccountName, shareId.Name, err)
}
}
existingId := helper.GetAzurermDataShareDataSetId(existing.Value)
if existingId != nil && *existingId != "" {
return tf.ImportAsExistsError("azurerm_data_share_dataset_data_lake_gen1", *existingId)
}

dataLakeStoreId, err := dataLakeParse.DataLakeStoreID(d.Get("data_lake_store_id").(string))
if err != nil {
return err
}

var dataSet datashare.BasicDataSet

if fileName, ok := d.GetOk("file_name"); ok {
dataSet = datashare.ADLSGen1FileDataSet{
Kind: datashare.KindAdlsGen1File,
ADLSGen1FileProperties: &datashare.ADLSGen1FileProperties{
AccountName: utils.String(dataLakeStoreId.Name),
ResourceGroup: utils.String(dataLakeStoreId.ResourceGroup),
SubscriptionID: utils.String(dataLakeStoreId.Subscription),
FolderPath: utils.String(d.Get("folder_path").(string)),
FileName: utils.String(fileName.(string)),
},
}
} else {
dataSet = datashare.ADLSGen1FolderDataSet{
Kind: datashare.KindAdlsGen1Folder,
ADLSGen1FolderProperties: &datashare.ADLSGen1FolderProperties{
AccountName: utils.String(dataLakeStoreId.Name),
ResourceGroup: utils.String(dataLakeStoreId.ResourceGroup),
SubscriptionID: utils.String(dataLakeStoreId.Subscription),
FolderPath: utils.String(d.Get("folder_path").(string)),
},
}
}

if _, err := client.Create(ctx, shareId.ResourceGroup, shareId.AccountName, shareId.Name, name, dataSet); err != nil {
return fmt.Errorf("creating/updating DataShare DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", name, shareId.ResourceGroup, shareId.AccountName, shareId.Name, err)
}

resp, err := client.Get(ctx, shareId.ResourceGroup, shareId.AccountName, shareId.Name, name)
if err != nil {
return fmt.Errorf("retrieving DataShare DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", name, shareId.ResourceGroup, shareId.AccountName, shareId.Name, err)
}

respId := helper.GetAzurermDataShareDataSetId(resp.Value)
if respId == nil || *respId == "" {
return fmt.Errorf("empty or nil ID returned for DataShare DataSet %q (Resource Group %q / accountName %q / shareName %q)", name, shareId.ResourceGroup, shareId.AccountName, shareId.Name)
}

d.SetId(*respId)
return resourceArmDataShareDataSetDataLakeGen1Read(d, meta)
}

func resourceArmDataShareDataSetDataLakeGen1Read(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataShare.DataSetClient
shareClient := meta.(*clients.Client).DataShare.SharesClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DataShareDataSetID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.ShareName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] DataShare %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("retrieving DataShare DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", id.Name, id.ResourceGroup, id.AccountName, id.ShareName, err)
}
d.Set("name", id.Name)
shareResp, err := shareClient.Get(ctx, id.ResourceGroup, id.AccountName, id.ShareName)
if err != nil {
return fmt.Errorf("retrieving DataShare %q (Resource Group %q / accountName %q): %+v", id.ShareName, id.ResourceGroup, id.AccountName, err)
}
if shareResp.ID == nil || *shareResp.ID == "" {
return fmt.Errorf("reading ID of DataShare %q (Resource Group %q / accountName %q): ID is empt", id.ShareName, id.ResourceGroup, id.AccountName)
}
d.Set("data_share_id", shareResp.ID)

switch resp := resp.Value.(type) {
case datashare.ADLSGen1FileDataSet:
if props := resp.ADLSGen1FileProperties; props != nil {
if props.SubscriptionID != nil && props.ResourceGroup != nil && props.AccountName != nil {
d.Set("data_lake_store_id", fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DataLakeStore/accounts/%s", *props.SubscriptionID, *props.ResourceGroup, *props.AccountName))
}
d.Set("folder_path", props.FolderPath)
d.Set("file_name", props.FileName)
d.Set("display_name", props.DataSetID)
}

case datashare.ADLSGen1FolderDataSet:
if props := resp.ADLSGen1FolderProperties; props != nil {
if props.SubscriptionID != nil && props.ResourceGroup != nil && props.AccountName != nil {
d.Set("data_lake_store_id", fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DataLakeStore/accounts/%s", *props.SubscriptionID, *props.ResourceGroup, *props.AccountName))
}
d.Set("folder_path", props.FolderPath)
d.Set("display_name", props.DataSetID)
}

default:
return fmt.Errorf("data share dataset %q (Resource Group %q / accountName %q / shareName %q) is not a datalake store gen1 dataset", id.Name, id.ResourceGroup, id.AccountName, id.ShareName)
}

return nil
}

func resourceArmDataShareDataSetDataLakeGen1Delete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataShare.DataSetClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.DataShareDataSetID(d.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.AccountName, id.ShareName, id.Name); err != nil {
return fmt.Errorf("deleting DataShare DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", id.Name, id.ResourceGroup, id.AccountName, id.ShareName, err)
}
return nil
}
Loading