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/datasource azurerm_data_share_dataset_blob_storage #7107

Merged
merged 8 commits into from Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions azurerm/internal/services/datashare/client/client.go
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
@@ -0,0 +1,136 @@
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_name": {
yupwei68 marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Computed: true,
},

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

"storage_account_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)
d.Set("storage_account_name", props.StorageAccountName)
d.Set("storage_account_resource_group_name", props.ResourceGroup)
d.Set("storage_account_subscription_id", props.SubscriptionID)
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)
d.Set("storage_account_name", props.StorageAccountName)
d.Set("storage_account_resource_group_name", props.ResourceGroup)
d.Set("storage_account_subscription_id", props.SubscriptionID)
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)
d.Set("storage_account_name", props.StorageAccountName)
d.Set("storage_account_resource_group_name", props.ResourceGroup)
d.Set("storage_account_subscription_id", props.SubscriptionID)
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
@@ -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
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
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
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(),
}
}