Skip to content

Commit

Permalink
Merge pull request #7107 from yupwei68/wyp-datashare-dataset-blob
Browse files Browse the repository at this point in the history
  • Loading branch information
jackofallops authored Jun 18, 2020
2 parents b0fea38 + ea24f91 commit de16440
Show file tree
Hide file tree
Showing 13 changed files with 1,163 additions and 4 deletions.
5 changes: 5 additions & 0 deletions azurerm/internal/services/datashare/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

type Client struct {
AccountClient *datashare.AccountsClient
DataSetClient *datashare.DataSetsClient
SharesClient *datashare.SharesClient
SynchronizationClient *datashare.SynchronizationSettingsClient
}
Expand All @@ -15,6 +16,9 @@ func NewClient(o *common.ClientOptions) *Client {
accountClient := datashare.NewAccountsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&accountClient.Client, o.ResourceManagerAuthorizer)

dataSetClient := datashare.NewDataSetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&dataSetClient.Client, o.ResourceManagerAuthorizer)

sharesClient := datashare.NewSharesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&sharesClient.Client, o.ResourceManagerAuthorizer)

Expand All @@ -23,6 +27,7 @@ func NewClient(o *common.ClientOptions) *Client {

return &Client{
AccountClient: &accountClient,
DataSetClient: &dataSetClient,
SharesClient: &sharesClient,
SynchronizationClient: &synchronizationSettingsClient,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package datashare

import (
"fmt"
"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/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"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"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
)

func dataSourceDataShareDatasetBlobStorage() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDataShareDatasetBlobStorageRead,

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

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

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

"container_name": {
Type: schema.TypeString,
Computed: true,
},

"storage_account": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},

"resource_group_name": {
Type: schema.TypeString,
Computed: true,
},

"subscription_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"file_path": {
Type: schema.TypeString,
Computed: true,
},

"folder_path": {
Type: schema.TypeString,
Computed: true,
},

"display_name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

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

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

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

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

d.SetId(*respId)
d.Set("name", name)
d.Set("data_share_id", shareID)

switch resp := respModel.Value.(type) {
case datashare.BlobDataSet:
if props := resp.BlobProperties; props != nil {
d.Set("container_name", props.ContainerName)
if err := d.Set("storage_account", flattenAzureRmDataShareDataSetBlobStorageAccount(props.StorageAccountName, props.ResourceGroup, props.SubscriptionID)); err != nil {
return fmt.Errorf("setting `storage_account`: %+v", err)
}
d.Set("file_path", props.FilePath)
d.Set("display_name", props.DataSetID)
}

case datashare.BlobFolderDataSet:
if props := resp.BlobFolderProperties; props != nil {
d.Set("container_name", props.ContainerName)
if err := d.Set("storage_account", flattenAzureRmDataShareDataSetBlobStorageAccount(props.StorageAccountName, props.ResourceGroup, props.SubscriptionID)); err != nil {
return fmt.Errorf("setting `storage_account`: %+v", err)
}
d.Set("folder_path", props.Prefix)
d.Set("display_name", props.DataSetID)
}

case datashare.BlobContainerDataSet:
if props := resp.BlobContainerProperties; props != nil {
d.Set("container_name", props.ContainerName)
if err := d.Set("storage_account", flattenAzureRmDataShareDataSetBlobStorageAccount(props.StorageAccountName, props.ResourceGroup, props.SubscriptionID)); err != nil {
return fmt.Errorf("setting `storage_account`: %+v", err)
}
d.Set("display_name", props.DataSetID)
}

default:
return fmt.Errorf("data share dataset %q (Resource Group %q / accountName %q / shareName %q) is not a blob storage dataset", name, shareId.ResourceGroup, shareId.AccountName, shareId.Name)
}

return nil
}
39 changes: 39 additions & 0 deletions azurerm/internal/services/datashare/helper/data_share.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package helper

import (
"github.com/Azure/azure-sdk-for-go/services/datashare/mgmt/2019-11-01/datashare"
)

func GetAzurermDataShareDataSetId(dataset datashare.BasicDataSet) *string {
if dataset == nil {
return nil
}
switch t := dataset.(type) {
case datashare.BlobDataSet:
return t.ID
case datashare.BlobFolderDataSet:
return t.ID
case datashare.BlobContainerDataSet:
return t.ID
case datashare.ADLSGen2FileDataSet:
return t.ID
case datashare.ADLSGen2FolderDataSet:
return t.ID
case datashare.ADLSGen2FileSystemDataSet:
return t.ID
case datashare.ADLSGen1FolderDataSet:
return t.ID
case datashare.ADLSGen1FileDataSet:
return t.ID
case datashare.KustoClusterDataSet:
return t.ID
case datashare.KustoDatabaseDataSet:
return t.ID
case datashare.SQLDWTableDataSet:
return t.ID
case datashare.SQLDBTableDataSet:
return t.ID
default:
return nil
}
}
32 changes: 32 additions & 0 deletions azurerm/internal/services/datashare/parse/data_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ type DataShareId struct {
Name string
}

type DataShareDataSetId struct {
ResourceGroup string
AccountName string
ShareName string
Name string
}

func DataShareAccountID(input string) (*DataShareAccountId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
Expand Down Expand Up @@ -57,3 +64,28 @@ func DataShareID(input string) (*DataShareId, error) {

return &DataShare, nil
}

func DataShareDataSetID(input string) (*DataShareDataSetId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("[ERROR] Unable to parse DataShareDataSet ID %q: %+v", input, err)
}

dataShareDataSet := DataShareDataSetId{
ResourceGroup: id.ResourceGroup,
}
if dataShareDataSet.AccountName, err = id.PopSegment("accounts"); err != nil {
return nil, err
}
if dataShareDataSet.ShareName, err = id.PopSegment("shares"); err != nil {
return nil, err
}
if dataShareDataSet.Name, err = id.PopSegment("dataSets"); err != nil {
return nil, err
}
if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &dataShareDataSet, nil
}
97 changes: 97 additions & 0 deletions azurerm/internal/services/datashare/parse/data_share_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,100 @@ func TestDataShareID(t *testing.T) {
}
}
}

func TestDataShareDataSetID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *DataShareDataSetId
}{
{
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.DataShare/accounts/",
Expected: nil,
},
{
Name: "Missing Share",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/account1/",
Expected: nil,
},
{
Name: "Missing Share Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/account1/shares/",
Expected: nil,
},
{
Name: "Missing DataSet",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/account1/shares/share1",
Expected: nil,
},
{
Name: "Missing DataSet Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/account1/shares/share1/dataSets",
Expected: nil,
},
{
Name: "DataSet ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/account1/shares/share1/dataSets/dataSet1",
Expected: &DataShareDataSetId{
Name: "dataSet1",
AccountName: "account1",
ResourceGroup: "resGroup1",
ShareName: "share1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/account1/shares/share1/DataSets/dataSet1",
Expected: nil,
},
}

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

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

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

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

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)
}
}
}
10 changes: 6 additions & 4 deletions azurerm/internal/services/datashare/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ func (r Registration) WebsiteCategories() []string {
// SupportedDataSources returns the supported Data Sources supported by this Service
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_data_share_account": dataSourceDataShareAccount(),
"azurerm_data_share": dataSourceDataShare(),
"azurerm_data_share_account": dataSourceDataShareAccount(),
"azurerm_data_share": dataSourceDataShare(),
"azurerm_data_share_dataset_blob_storage": dataSourceDataShareDatasetBlobStorage(),
}
}

// 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_account": resourceArmDataShareAccount(),
"azurerm_data_share": resourceArmDataShare(),
"azurerm_data_share_dataset_blob_storage": resourceArmDataShareDataSetBlobStorage(),
}
}
Loading

0 comments on commit de16440

Please sign in to comment.