From 8451d333f2e9e92c8b149c59c5fcf0b26ede8a0d Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Wed, 27 May 2020 16:47:42 +0800 Subject: [PATCH 1/7] split blob --- .../services/datashare/client/client.go | 5 + ..._source_data_share_dataset_blob_storage.go | 136 +++++++++ .../services/datashare/helper/data_share.go | 39 +++ .../services/datashare/parse/data_share.go | 32 +++ .../datashare/parse/data_share_test.go | 97 +++++++ .../services/datashare/registration.go | 10 +- ...rce_arm_data_share_dataset_blob_storage.go | 260 +++++++++++++++++ ...ce_data_share_dataset_blob_storage_test.go | 45 +++ ...rm_data_share_dataset_blob_storage_test.go | 269 ++++++++++++++++++ .../services/datashare/validate/data_share.go | 20 ++ website/azurerm.erb | 7 + ...a_share_dataset_blob_storage.html.markdown | 62 ++++ ...a_share_dataset_blob_storage.html.markdown | 120 ++++++++ 13 files changed, 1098 insertions(+), 4 deletions(-) create mode 100644 azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go create mode 100644 azurerm/internal/services/datashare/helper/data_share.go create mode 100644 azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go create mode 100644 azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go create mode 100644 azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go create mode 100644 website/docs/d/data_share_dataset_blob_storage.html.markdown create mode 100644 website/docs/r/data_share_dataset_blob_storage.html.markdown diff --git a/azurerm/internal/services/datashare/client/client.go b/azurerm/internal/services/datashare/client/client.go index 09bbc336a08c..d255b829b68a 100644 --- a/azurerm/internal/services/datashare/client/client.go +++ b/azurerm/internal/services/datashare/client/client.go @@ -7,6 +7,7 @@ import ( type Client struct { AccountClient *datashare.AccountsClient + DataSetClient *datashare.DataSetsClient SharesClient *datashare.SharesClient SynchronizationClient *datashare.SynchronizationSettingsClient } @@ -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) @@ -23,6 +27,7 @@ func NewClient(o *common.ClientOptions) *Client { return &Client{ AccountClient: &accountClient, + DataSetClient: &dataSetClient, SharesClient: &sharesClient, SynchronizationClient: &synchronizationSettingsClient, } diff --git a/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go b/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go new file mode 100644 index 000000000000..f375ea94ab24 --- /dev/null +++ b/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go @@ -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(), + }, + + "share_id": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.DataShareID, + }, + + "container_name": { + Type: schema.TypeString, + Computed: true, + }, + + "storage_account_name": { + 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("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("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 +} diff --git a/azurerm/internal/services/datashare/helper/data_share.go b/azurerm/internal/services/datashare/helper/data_share.go new file mode 100644 index 000000000000..6d56a0748a8f --- /dev/null +++ b/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 + } +} diff --git a/azurerm/internal/services/datashare/parse/data_share.go b/azurerm/internal/services/datashare/parse/data_share.go index a9247a7b0da5..88331cbd53dd 100644 --- a/azurerm/internal/services/datashare/parse/data_share.go +++ b/azurerm/internal/services/datashare/parse/data_share.go @@ -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 { @@ -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 +} diff --git a/azurerm/internal/services/datashare/parse/data_share_test.go b/azurerm/internal/services/datashare/parse/data_share_test.go index fe1d4f3deca5..6c8c6e96d3ee 100644 --- a/azurerm/internal/services/datashare/parse/data_share_test.go +++ b/azurerm/internal/services/datashare/parse/data_share_test.go @@ -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) + } + } +} diff --git a/azurerm/internal/services/datashare/registration.go b/azurerm/internal/services/datashare/registration.go index 87cff3e1cbaa..9b54d829791e 100644 --- a/azurerm/internal/services/datashare/registration.go +++ b/azurerm/internal/services/datashare/registration.go @@ -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(), } } diff --git a/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go b/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go new file mode 100644 index 000000000000..9a8bd1dde2d4 --- /dev/null +++ b/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go @@ -0,0 +1,260 @@ +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/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" + azValidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "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/services/storage" + 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 resourceArmDataShareDataSetBlobStorage() *schema.Resource { + return &schema.Resource{ + Create: resourceArmDataShareDataSetBlobStorageCreate, + Read: resourceArmDataShareDataSetBlobStorageRead, + Delete: resourceArmDataShareDataSetBlobStorageDelete, + + 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(), + }, + + "share_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validate.DataShareID, + }, + + "container_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: azValidate.StorageContainerName, + }, + + "storage_account_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: storage.ValidateArmStorageAccountName, + }, + + "storage_account_resource_group_name": azure.SchemaResourceGroupName(), + + "storage_account_subscription_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.IsUUID, + }, + + "file_path": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + ConflictsWith: []string{"folder_path"}, + }, + + "folder_path": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + ValidateFunc: validation.StringIsNotEmpty, + ConflictsWith: []string{"file_path"}, + }, + + "display_name": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} +func resourceArmDataShareDataSetBlobStorageCreate(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("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 Blob Storage 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_blob_storage", *existingId) + } + + var dataSet datashare.BasicDataSet + if filePath, ok := d.GetOk("file_path"); ok { + dataSet = datashare.BlobDataSet{ + Kind: datashare.KindBlob, + BlobProperties: &datashare.BlobProperties{ + ContainerName: utils.String(d.Get("container_name").(string)), + StorageAccountName: utils.String(d.Get("storage_account_name").(string)), + ResourceGroup: utils.String(d.Get("storage_account_resource_group_name").(string)), + SubscriptionID: utils.String(d.Get("storage_account_subscription_id").(string)), + FilePath: utils.String(filePath.(string)), + }, + } + } else if folderPath, ok := d.GetOk("folder_path"); ok { + dataSet = datashare.BlobFolderDataSet{ + Kind: datashare.KindBlobFolder, + BlobFolderProperties: &datashare.BlobFolderProperties{ + ContainerName: utils.String(d.Get("container_name").(string)), + StorageAccountName: utils.String(d.Get("storage_account_name").(string)), + ResourceGroup: utils.String(d.Get("storage_account_resource_group_name").(string)), + SubscriptionID: utils.String(d.Get("storage_account_subscription_id").(string)), + Prefix: utils.String(folderPath.(string)), + }, + } + } else { + dataSet = datashare.BlobContainerDataSet{ + Kind: datashare.KindContainer, + BlobContainerProperties: &datashare.BlobContainerProperties{ + ContainerName: utils.String(d.Get("container_name").(string)), + StorageAccountName: utils.String(d.Get("storage_account_name").(string)), + ResourceGroup: utils.String(d.Get("storage_account_resource_group_name").(string)), + SubscriptionID: utils.String(d.Get("storage_account_subscription_id").(string)), + }, + } + } + + if _, err := client.Create(ctx, shareId.ResourceGroup, shareId.AccountName, shareId.Name, name, dataSet); err != nil { + return fmt.Errorf("creating DataShare Blob Storage 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 Blob Storage 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 Blob Storage DataSet %q (Resource Group %q / accountName %q / shareName %q)", name, shareId.ResourceGroup, shareId.AccountName, shareId.Name) + } + + d.SetId(*respId) + return resourceArmDataShareDataSetBlobStorageRead(d, meta) +} + +func resourceArmDataShareDataSetBlobStorageRead(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 Blob Storage 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("empty or nil ID returned for DataShare %q (Resource Group %q / accountName %q)", id.ShareName, id.ResourceGroup, id.AccountName) + } + + d.Set("share_id", shareResp.ID) + + switch resp := resp.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", id.Name, id.ResourceGroup, id.AccountName, id.ShareName) + } + + return nil +} + +func resourceArmDataShareDataSetBlobStorageDelete(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 Blob Storage DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", id.Name, id.ResourceGroup, id.AccountName, id.ShareName, err) + } + return nil +} diff --git a/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go new file mode 100644 index 000000000000..fadc61fa8246 --- /dev/null +++ b/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go @@ -0,0 +1,45 @@ +package tests + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" +) + +func TestAccDataSourceAzureRMDataShareDatasetBlobStorage_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "data.azurerm_data_share_dataset_blob_storage", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDataShareDataSetDestroy("azurerm_data_share_dataset_blob_storage"), + Steps: []resource.TestStep{ + { + Config: testAccDataSourceDataShareDatasetBlobStorage_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDataShareDataSetExists(data.ResourceName), + resource.TestCheckResourceAttrSet(data.ResourceName, "container_name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account_name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account_resource_group_name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account_subscription_id"), + resource.TestCheckResourceAttrSet(data.ResourceName, "file_path"), + resource.TestCheckResourceAttrSet(data.ResourceName, "display_name"), + ), + }, + }, + }) +} + +func testAccDataSourceDataShareDatasetBlobStorage_basic(data acceptance.TestData) string { + config := testAccAzureRMDataShareDataSetBlobStorageFile_basic(data) + return fmt.Sprintf(` +%s + +data "azurerm_data_share_dataset_blob_storage" "test" { + name = azurerm_data_share_dataset_blob_storage.test.name + share_id = azurerm_data_share_dataset_blob_storage.test.share_id +} +`, config) +} diff --git a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go new file mode 100644 index 000000000000..ae91276975f5 --- /dev/null +++ b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go @@ -0,0 +1,269 @@ +package tests + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datashare/parse" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func TestAccAzureRMDataShareDataSetBlobStorageFile_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_data_share_dataset_blob_storage", "test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDataShareDataSetDestroy("azurerm_data_share_dataset_blob_storage"), + Steps: []resource.TestStep{ + { + Config: testAccAzureRMDataShareDataSetBlobStorageFile_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDataShareDataSetExists(data.ResourceName), + resource.TestCheckResourceAttrSet(data.ResourceName, "display_name"), + ), + }, + data.ImportStep(), + }, + }) +} + +func TestAccAzureRMDataShareDataSetBlobStorageFolder_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_data_share_dataset_blob_storage", "test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDataShareDataSetDestroy("azurerm_data_share_dataset_blob_storage"), + Steps: []resource.TestStep{ + { + Config: testAccAzureRMDataShareDataSetBlobStorageFolder_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDataShareDataSetExists(data.ResourceName), + resource.TestCheckResourceAttrSet(data.ResourceName, "display_name"), + ), + }, + data.ImportStep(), + }, + }) +} + +func TestAccAzureRMDataShareDataSetBlobStorageContainer_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_data_share_dataset_blob_storage", "test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDataShareDataSetDestroy("azurerm_data_share_dataset_blob_storage"), + Steps: []resource.TestStep{ + { + Config: testAccAzureRMDataShareDataSetBlobStorageContainer_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDataShareDataSetExists(data.ResourceName), + resource.TestCheckResourceAttrSet(data.ResourceName, "display_name"), + ), + }, + data.ImportStep(), + }, + }) +} + +func TestAccAzureRMDataShareDataSetBlobStorage_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_data_share_dataset_blob_storage", "test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMDataShareDataSetDestroy("azurerm_data_share_dataset_blob_storage"), + Steps: []resource.TestStep{ + { + Config: testAccAzureRMDataShareDataSetBlobStorageFile_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDataShareDataSetExists(data.ResourceName), + ), + }, + data.RequiresImportErrorStep(testAccAzureRMDataShareDataSetBlobStorage_requiresImport), + }, + }) +} + +func testCheckAzureRMDataShareDataSetExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := acceptance.AzureProvider.Meta().(*clients.Client).DataShare.DataSetClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("DataShare DataSet not found: %s", resourceName) + } + id, err := parse.DataShareDataSetID(rs.Primary.ID) + if err != nil { + return err + } + if resp, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.ShareName, id.Name); err != nil { + if !utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("bad: data share data set %q does not exist", id.Name) + } + return fmt.Errorf("bad: Get on DataShare DataSet Client: %+v", err) + } + return nil + } +} + +func testCheckAzureRMDataShareDataSetDestroy(resourceTypeName string) func(s *terraform.State) error { + return func(s *terraform.State) error { + client := acceptance.AzureProvider.Meta().(*clients.Client).DataShare.DataSetClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != resourceTypeName { + continue + } + id, err := parse.DataShareDataSetID(rs.Primary.ID) + if err != nil { + return err + } + if resp, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.ShareName, id.Name); err != nil { + if !utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("bad: get on data share data set client: %+v", err) + } + } + return nil + } + return nil + } +} + +func testAccAzureRMDataShareDataSetBlobStorage_template(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +provider "azuread" { +} + +resource "azurerm_resource_group" "test" { + name = "acctest-datashare-%[1]d" + location = "%[2]s" +} + +resource "azurerm_data_share_account" "test" { + name = "acctest-dsa-%[1]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + identity { + type = "SystemAssigned" + } +} + +resource "azurerm_data_share" "test" { + name = "acctest_ds_%[1]d" + account_id = azurerm_data_share_account.test.id + kind = "CopyBased" +} + +resource "azurerm_storage_account" "test" { + name = "acctest%[3]d" + resource_group_name = azurerm_resource_group.test.name + location = azurerm_resource_group.test.location + account_tier = "Standard" + account_replication_type = "RAGRS" +} + +resource "azurerm_storage_container" "test" { + name = "acctest-sc-%[1]d" + storage_account_name = azurerm_storage_account.test.name + container_access_type = "container" +} + +data "azuread_service_principal" "test" { + display_name = azurerm_data_share_account.test.name +} + +resource "azurerm_role_assignment" "test" { + scope = azurerm_storage_account.test.id + role_definition_name = "Storage Blob Data Reader" + principal_id = data.azuread_service_principal.test.object_id +} +`, data.RandomInteger, data.Locations.Primary, data.RandomIntOfLength(12)) +} + +func testAccAzureRMDataShareDataSetBlobStorageFile_basic(data acceptance.TestData) string { + config := testAccAzureRMDataShareDataSetBlobStorage_template(data) + return fmt.Sprintf(` +%[1]s + +resource "azurerm_data_share_dataset_blob_storage" "test" { + name = "acctest-dsbds-file-%[2]d" + share_id = azurerm_data_share.test.id + container_name = azurerm_storage_container.test.name + storage_account_name = azurerm_storage_account.test.name + storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name + storage_account_subscription_id = "%[3]s" + file_path = "myfile.txt" + depends_on = [ + azurerm_role_assignment.test, + ] +} +`, config, data.RandomInteger, os.Getenv("ARM_SUBSCRIPTION_ID")) +} + +func testAccAzureRMDataShareDataSetBlobStorageFolder_basic(data acceptance.TestData) string { + config := testAccAzureRMDataShareDataSetBlobStorage_template(data) + return fmt.Sprintf(` +%[1]s + +resource "azurerm_data_share_dataset_blob_storage" "test" { + name = "acctest-dsbds-folder-%[2]d" + share_id = azurerm_data_share.test.id + container_name = azurerm_storage_container.test.name + storage_account_name = azurerm_storage_account.test.name + storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name + storage_account_subscription_id = "%[3]s" + folder_path = "/test/" + depends_on = [ + azurerm_role_assignment.test, + ] +} +`, config, data.RandomInteger, os.Getenv("ARM_SUBSCRIPTION_ID")) +} + +func testAccAzureRMDataShareDataSetBlobStorageContainer_basic(data acceptance.TestData) string { + config := testAccAzureRMDataShareDataSetBlobStorage_template(data) + return fmt.Sprintf(` +%[1]s + +resource "azurerm_data_share_dataset_blob_storage" "test" { + name = "acctest-dsbds-folder-%[2]d" + share_id = azurerm_data_share.test.id + container_name = azurerm_storage_container.test.name + storage_account_name = azurerm_storage_account.test.name + storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name + storage_account_subscription_id = "%[3]s" + depends_on = [ + azurerm_role_assignment.test, + ] +} +`, config, data.RandomInteger, os.Getenv("ARM_SUBSCRIPTION_ID")) +} + +func testAccAzureRMDataShareDataSetBlobStorage_requiresImport(data acceptance.TestData) string { + config := testAccAzureRMDataShareDataSetBlobStorageFile_basic(data) + return fmt.Sprintf(` +%s + +resource "azurerm_data_share_dataset_blob_storage" "import" { + name = azurerm_data_share_dataset_blob_storage.test.name + share_id = azurerm_data_share.test.id + container_name = azurerm_data_share_dataset_blob_storage.test.container_name + storage_account_name = azurerm_data_share_dataset_blob_storage.test.storage_account_name + storage_account_resource_group_name = azurerm_data_share_dataset_blob_storage.test.storage_account_resource_group_name + storage_account_subscription_id = azurerm_data_share_dataset_blob_storage.test.storage_account_subscription_id +} +`, config) +} diff --git a/azurerm/internal/services/datashare/validate/data_share.go b/azurerm/internal/services/datashare/validate/data_share.go index c1a393af2b1b..0176f55ce38b 100644 --- a/azurerm/internal/services/datashare/validate/data_share.go +++ b/azurerm/internal/services/datashare/validate/data_share.go @@ -40,3 +40,23 @@ func DataShareSyncName() schema.SchemaValidateFunc { regexp.MustCompile(`^[^&%#/]{1,90}$`), `Data share snapshot schedule name should have length of 1 - 90, and cannot contain &%#/`, ) } + +func DataShareID(i interface{}, k string) (warnings []string, errors []error) { + v, ok := i.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected type of %q to be string", k)) + return warnings, errors + } + + if _, err := parse.DataShareID(v); err != nil { + errors = append(errors, fmt.Errorf("can not parse %q as a data share id: %v", k, err)) + } + + return warnings, errors +} + +func DatashareDataSetName() schema.SchemaValidateFunc { + return validation.StringMatch( + regexp.MustCompile(`^[\w-]{2,90}$`), `Dataset name can only contain number, letters, - and _, and must be between 2 and 90 characters long.`, + ) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index 972c812fe1e3..ff4db5bdfc6f 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -197,6 +197,10 @@ azurerm_data_share_account +
  • + azurerm_data_share_dataset_blob_storage +
  • +
  • azurerm_dedicated_host
  • @@ -1443,6 +1447,9 @@
  • azurerm_data_share_account
  • +
  • + azurerm_data_share_dataset_blob_storage +
  • diff --git a/website/docs/d/data_share_dataset_blob_storage.html.markdown b/website/docs/d/data_share_dataset_blob_storage.html.markdown new file mode 100644 index 000000000000..180ea9ad193c --- /dev/null +++ b/website/docs/d/data_share_dataset_blob_storage.html.markdown @@ -0,0 +1,62 @@ +--- +subcategory: "Data Share" +layout: "azurerm" +page_title: "Azure Resource Manager: Data Source: azurerm_data_share_dataset_blob_storage" +description: |- + Gets information about an existing Data Share Blob Storage Dataset. +--- + +# Data Source: azurerm_data_share_dataset_blob_storage + +Use this data source to access information about an existing Data Share Blob Storage Dataset. + +## Example Usage + +```hcl +provider "azurerm" { + features {} +} + +data "azurerm_data_share_dataset_blob_storage" "example" { + name = "example-dsbsds" + share_id = "example-share-id" +} + +output "id" { + value = data.azurerm_data_share_dataset_blob_storage.example.id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name of this Data Share Blob Storage Dataset. + +* `share_id` - (Required) The ID of the Data Share in which this Data Share Blob Storage Dataset should be created. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Data Share Blob Storage Dataset. + +* `container_name` - The name of the storage account container to be shared with the receiver. + +* `storage_account_name` - The name of the storage account to be shared with the receiver. + +* `storage_account_resource_group_name` - The resource group name of the storage account to be shared with the receiver. + +* `storage_account_subscription_id` - The subscription id of the storage account to be shared with the receiver. + +* `file_path` - The path of the file in the storage container to be shared with the receiver. + +* `folder_path` - The folder path of the file in the storage container to be shared with the receiver. + +* `display_name` - The name of the Data Share Dataset. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `read` - (Defaults to 5 minutes) Used when retrieving the Data Share Blob Storage Dataset. diff --git a/website/docs/r/data_share_dataset_blob_storage.html.markdown b/website/docs/r/data_share_dataset_blob_storage.html.markdown new file mode 100644 index 000000000000..bbdd7b518f30 --- /dev/null +++ b/website/docs/r/data_share_dataset_blob_storage.html.markdown @@ -0,0 +1,120 @@ +--- +subcategory: "Data Share" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_data_share_dataset_blob_storage" +description: |- + Manages a Data Share Blob Storage Dataset. +--- + +# azurerm_data_share_dataset_blob_storage + +Manages a Data Share Blob Storage Dataset. + +## Example Usage + +```hcl +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "West Europe" +} + +resource "azurerm_data_share_account" "example" { + name = "example-dsa" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name + identity { + type = "SystemAssigned" + } +} + +resource "azurerm_data_share" "example" { + name = "example_ds" + account_id = azurerm_data_share_account.example.id + kind = "CopyBased" +} + +resource "azurerm_storage_account" "example" { + name = "examplestr" + resource_group_name = azurerm_resource_group.example.name + location = azurerm_resource_group.example.location + account_tier = "Standard" + account_replication_type = "RAGRS" +} + +resource "azurerm_storage_container" "example" { + name = "example-sc" + storage_account_name = azurerm_storage_account.example.name + container_access_type = "container" +} + +data "azuread_service_principal" "example" { + display_name = azurerm_data_share_account.example.name +} + +resource "azurerm_role_assignment" "example" { + scope = azurerm_storage_account.example.id + role_definition_name = "Storage Blob Data Reader" + principal_id = data.azuread_service_principal.example.object_id +} + +resource "azurerm_data_share_dataset_blob_storage" "example" { + name = "example-dsbsds-file" + share_id = azurerm_data_share.example.id + container_name = azurerm_storage_container.example.name + storage_account_name = azurerm_storage_account.example.name + storage_account_resource_group_name = azurerm_storage_account.example.resource_group_name + storage_account_subscription_id = "00000000-0000-0000-0000-000000000000" + file_path = "myfile.txt" + depends_on = [ + azurerm_role_assignment.example, + ] +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `name` - (Required) The name which should be used for this Data Share Blob Storage Dataset. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `share_id` - (Required) The ID of the Data Share in which this Data Share Blob Storage Dataset should be created. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `container_name` - (Required) The name of the storage account container to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `storage_account_name` - (Required) The name of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `storage_account_resource_group_name` - (Required) The resource group name of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `storage_account_subscription_id` - (Required) The subscription id of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `file_path` - (Optional) The path of the file in the storage container to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `folder_path` - (Optional) The path of the folder in the storage container to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the Data Share Blob Storage Dataset. + +* `display_name` - The name of the Data Share Dataset. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating the Data Share Blob Storage Dataset. +* `read` - (Defaults to 5 minutes) Used when retrieving the Data Share Blob Storage Dataset. +* `delete` - (Defaults to 30 minutes) Used when deleting the Data Share Blob Storage Dataset. + +## Import + +Data Share Blob Storage Datasets can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_data_share_dataset_blob_storage.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.DataShare/accounts/account1/shares/share1/dataSets/dataSet1 +``` From d0143abfe73c107ff216d53d277234d458b3c43c Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Wed, 27 May 2020 16:50:53 +0800 Subject: [PATCH 2/7] r1 --- .../data_source_data_share_dataset_blob_storage.go | 6 +++--- azurerm/internal/services/datashare/registration.go | 12 ++++++------ .../resource_arm_data_share_dataset_blob_storage.go | 6 +++--- ...ta_source_data_share_dataset_blob_storage_test.go | 2 +- ...ource_arm_data_share_dataset_blob_storage_test.go | 8 ++++---- .../d/data_share_dataset_blob_storage.html.markdown | 4 ++-- .../r/data_share_dataset_blob_storage.html.markdown | 4 ++-- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go b/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go index f375ea94ab24..d726612a1b3f 100644 --- a/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go +++ b/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go @@ -28,7 +28,7 @@ func dataSourceDataShareDatasetBlobStorage() *schema.Resource { ValidateFunc: validate.DatashareDataSetName(), }, - "share_id": { + "data_share_id": { Type: schema.TypeString, Required: true, ValidateFunc: validate.DataShareID, @@ -78,7 +78,7 @@ func dataSourceArmDataShareDatasetBlobStorageRead(d *schema.ResourceData, meta i defer cancel() name := d.Get("name").(string) - shareID := d.Get("share_id").(string) + shareID := d.Get("data_share_id").(string) shareId, err := parse.DataShareID(shareID) if err != nil { return err @@ -96,7 +96,7 @@ func dataSourceArmDataShareDatasetBlobStorageRead(d *schema.ResourceData, meta i d.SetId(*respId) d.Set("name", name) - d.Set("share_id", shareID) + d.Set("data_share_id", shareID) switch resp := respModel.Value.(type) { case datashare.BlobDataSet: diff --git a/azurerm/internal/services/datashare/registration.go b/azurerm/internal/services/datashare/registration.go index 9b54d829791e..758d5664a94a 100644 --- a/azurerm/internal/services/datashare/registration.go +++ b/azurerm/internal/services/datashare/registration.go @@ -19,17 +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_dataset_blob_storage": dataSourceDataShareDatasetBlobStorage(), + "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_dataset_blob_storage": resourceArmDataShareDataSetBlobStorage(), + "azurerm_data_share_account": resourceArmDataShareAccount(), + "azurerm_data_share": resourceArmDataShare(), + "azurerm_data_share_dataset_blob_storage": resourceArmDataShareDataSetBlobStorage(), } } diff --git a/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go b/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go index 9a8bd1dde2d4..305f8e4ca505 100644 --- a/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go +++ b/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go @@ -46,7 +46,7 @@ func resourceArmDataShareDataSetBlobStorage() *schema.Resource { ValidateFunc: validate.DatashareDataSetName(), }, - "share_id": { + "data_share_id": { Type: schema.TypeString, Required: true, ForceNew: true, @@ -105,7 +105,7 @@ func resourceArmDataShareDataSetBlobStorageCreate(d *schema.ResourceData, meta i defer cancel() name := d.Get("name").(string) - shareId, err := parse.DataShareID(d.Get("share_id").(string)) + shareId, err := parse.DataShareID(d.Get("data_share_id").(string)) if err != nil { return err } @@ -204,7 +204,7 @@ func resourceArmDataShareDataSetBlobStorageRead(d *schema.ResourceData, meta int return fmt.Errorf("empty or nil ID returned for DataShare %q (Resource Group %q / accountName %q)", id.ShareName, id.ResourceGroup, id.AccountName) } - d.Set("share_id", shareResp.ID) + d.Set("data_share_id", shareResp.ID) switch resp := resp.Value.(type) { case datashare.BlobDataSet: diff --git a/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go index fadc61fa8246..7c4edb1fe635 100644 --- a/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go +++ b/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go @@ -39,7 +39,7 @@ func testAccDataSourceDataShareDatasetBlobStorage_basic(data acceptance.TestData data "azurerm_data_share_dataset_blob_storage" "test" { name = azurerm_data_share_dataset_blob_storage.test.name - share_id = azurerm_data_share_dataset_blob_storage.test.share_id + data_share_id = azurerm_data_share_dataset_blob_storage.test.data_share_id } `, config) } diff --git a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go index ae91276975f5..59d7592a9073 100644 --- a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go +++ b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go @@ -200,7 +200,7 @@ func testAccAzureRMDataShareDataSetBlobStorageFile_basic(data acceptance.TestDat resource "azurerm_data_share_dataset_blob_storage" "test" { name = "acctest-dsbds-file-%[2]d" - share_id = azurerm_data_share.test.id + data_share_id = azurerm_data_share.test.id container_name = azurerm_storage_container.test.name storage_account_name = azurerm_storage_account.test.name storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name @@ -220,7 +220,7 @@ func testAccAzureRMDataShareDataSetBlobStorageFolder_basic(data acceptance.TestD resource "azurerm_data_share_dataset_blob_storage" "test" { name = "acctest-dsbds-folder-%[2]d" - share_id = azurerm_data_share.test.id + data_share_id = azurerm_data_share.test.id container_name = azurerm_storage_container.test.name storage_account_name = azurerm_storage_account.test.name storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name @@ -240,7 +240,7 @@ func testAccAzureRMDataShareDataSetBlobStorageContainer_basic(data acceptance.Te resource "azurerm_data_share_dataset_blob_storage" "test" { name = "acctest-dsbds-folder-%[2]d" - share_id = azurerm_data_share.test.id + data_share_id = azurerm_data_share.test.id container_name = azurerm_storage_container.test.name storage_account_name = azurerm_storage_account.test.name storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name @@ -259,7 +259,7 @@ func testAccAzureRMDataShareDataSetBlobStorage_requiresImport(data acceptance.Te resource "azurerm_data_share_dataset_blob_storage" "import" { name = azurerm_data_share_dataset_blob_storage.test.name - share_id = azurerm_data_share.test.id + data_share_id = azurerm_data_share.test.id container_name = azurerm_data_share_dataset_blob_storage.test.container_name storage_account_name = azurerm_data_share_dataset_blob_storage.test.storage_account_name storage_account_resource_group_name = azurerm_data_share_dataset_blob_storage.test.storage_account_resource_group_name diff --git a/website/docs/d/data_share_dataset_blob_storage.html.markdown b/website/docs/d/data_share_dataset_blob_storage.html.markdown index 180ea9ad193c..5a6c09b238cd 100644 --- a/website/docs/d/data_share_dataset_blob_storage.html.markdown +++ b/website/docs/d/data_share_dataset_blob_storage.html.markdown @@ -19,7 +19,7 @@ provider "azurerm" { data "azurerm_data_share_dataset_blob_storage" "example" { name = "example-dsbsds" - share_id = "example-share-id" + data_share_id = "example-share-id" } output "id" { @@ -33,7 +33,7 @@ The following arguments are supported: * `name` - (Required) The name of this Data Share Blob Storage Dataset. -* `share_id` - (Required) The ID of the Data Share in which this Data Share Blob Storage Dataset should be created. +* `data_share_id` - (Required) The ID of the Data Share in which this Data Share Blob Storage Dataset should be created. ## Attributes Reference diff --git a/website/docs/r/data_share_dataset_blob_storage.html.markdown b/website/docs/r/data_share_dataset_blob_storage.html.markdown index bbdd7b518f30..64fe3e2715ce 100644 --- a/website/docs/r/data_share_dataset_blob_storage.html.markdown +++ b/website/docs/r/data_share_dataset_blob_storage.html.markdown @@ -63,7 +63,7 @@ resource "azurerm_role_assignment" "example" { resource "azurerm_data_share_dataset_blob_storage" "example" { name = "example-dsbsds-file" - share_id = azurerm_data_share.example.id + data_share_id = azurerm_data_share.example.id container_name = azurerm_storage_container.example.name storage_account_name = azurerm_storage_account.example.name storage_account_resource_group_name = azurerm_storage_account.example.resource_group_name @@ -81,7 +81,7 @@ The following arguments are supported: * `name` - (Required) The name which should be used for this Data Share Blob Storage Dataset. Changing this forces a new Data Share Blob Storage Dataset to be created. -* `share_id` - (Required) The ID of the Data Share in which this Data Share Blob Storage Dataset should be created. Changing this forces a new Data Share Blob Storage Dataset to be created. +* `data_share_id` - (Required) The ID of the Data Share in which this Data Share Blob Storage Dataset should be created. Changing this forces a new Data Share Blob Storage Dataset to be created. * `container_name` - (Required) The name of the storage account container to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. From 443d152035bc795e10895602793a26a11fe1fef5 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Thu, 28 May 2020 10:37:41 +0800 Subject: [PATCH 3/7] fmt --- .../data_source_data_share_dataset_blob_storage_test.go | 2 +- .../resource_arm_data_share_dataset_blob_storage_test.go | 8 ++++---- .../docs/d/data_share_dataset_blob_storage.html.markdown | 2 +- .../docs/r/data_share_dataset_blob_storage.html.markdown | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go index 7c4edb1fe635..c8863e1bb1b1 100644 --- a/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go +++ b/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go @@ -38,7 +38,7 @@ func testAccDataSourceDataShareDatasetBlobStorage_basic(data acceptance.TestData %s data "azurerm_data_share_dataset_blob_storage" "test" { - name = azurerm_data_share_dataset_blob_storage.test.name + name = azurerm_data_share_dataset_blob_storage.test.name data_share_id = azurerm_data_share_dataset_blob_storage.test.data_share_id } `, config) diff --git a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go index 59d7592a9073..75711b038bd4 100644 --- a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go +++ b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go @@ -200,7 +200,7 @@ func testAccAzureRMDataShareDataSetBlobStorageFile_basic(data acceptance.TestDat resource "azurerm_data_share_dataset_blob_storage" "test" { name = "acctest-dsbds-file-%[2]d" - data_share_id = azurerm_data_share.test.id + data_share_id = azurerm_data_share.test.id container_name = azurerm_storage_container.test.name storage_account_name = azurerm_storage_account.test.name storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name @@ -220,7 +220,7 @@ func testAccAzureRMDataShareDataSetBlobStorageFolder_basic(data acceptance.TestD resource "azurerm_data_share_dataset_blob_storage" "test" { name = "acctest-dsbds-folder-%[2]d" - data_share_id = azurerm_data_share.test.id + data_share_id = azurerm_data_share.test.id container_name = azurerm_storage_container.test.name storage_account_name = azurerm_storage_account.test.name storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name @@ -240,7 +240,7 @@ func testAccAzureRMDataShareDataSetBlobStorageContainer_basic(data acceptance.Te resource "azurerm_data_share_dataset_blob_storage" "test" { name = "acctest-dsbds-folder-%[2]d" - data_share_id = azurerm_data_share.test.id + data_share_id = azurerm_data_share.test.id container_name = azurerm_storage_container.test.name storage_account_name = azurerm_storage_account.test.name storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name @@ -259,7 +259,7 @@ func testAccAzureRMDataShareDataSetBlobStorage_requiresImport(data acceptance.Te resource "azurerm_data_share_dataset_blob_storage" "import" { name = azurerm_data_share_dataset_blob_storage.test.name - data_share_id = azurerm_data_share.test.id + data_share_id = azurerm_data_share.test.id container_name = azurerm_data_share_dataset_blob_storage.test.container_name storage_account_name = azurerm_data_share_dataset_blob_storage.test.storage_account_name storage_account_resource_group_name = azurerm_data_share_dataset_blob_storage.test.storage_account_resource_group_name diff --git a/website/docs/d/data_share_dataset_blob_storage.html.markdown b/website/docs/d/data_share_dataset_blob_storage.html.markdown index 5a6c09b238cd..3c50ea87da8b 100644 --- a/website/docs/d/data_share_dataset_blob_storage.html.markdown +++ b/website/docs/d/data_share_dataset_blob_storage.html.markdown @@ -18,7 +18,7 @@ provider "azurerm" { } data "azurerm_data_share_dataset_blob_storage" "example" { - name = "example-dsbsds" + name = "example-dsbsds" data_share_id = "example-share-id" } diff --git a/website/docs/r/data_share_dataset_blob_storage.html.markdown b/website/docs/r/data_share_dataset_blob_storage.html.markdown index 64fe3e2715ce..1c9590155166 100644 --- a/website/docs/r/data_share_dataset_blob_storage.html.markdown +++ b/website/docs/r/data_share_dataset_blob_storage.html.markdown @@ -63,7 +63,7 @@ resource "azurerm_role_assignment" "example" { resource "azurerm_data_share_dataset_blob_storage" "example" { name = "example-dsbsds-file" - data_share_id = azurerm_data_share.example.id + data_share_id = azurerm_data_share.example.id container_name = azurerm_storage_container.example.name storage_account_name = azurerm_storage_account.example.name storage_account_resource_group_name = azurerm_storage_account.example.resource_group_name From 084a71b32aa8a9d4480d010f7ccb33357555b77b Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Thu, 28 May 2020 15:22:47 +0800 Subject: [PATCH 4/7] ci --- .../tests/resource_arm_data_share_dataset_blob_storage_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go index 75711b038bd4..0a8ad9de9369 100644 --- a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go +++ b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go @@ -114,6 +114,7 @@ func testCheckAzureRMDataShareDataSetExists(resourceName string) resource.TestCh } } +// nolint func testCheckAzureRMDataShareDataSetDestroy(resourceTypeName string) func(s *terraform.State) error { return func(s *terraform.State) error { client := acceptance.AzureProvider.Meta().(*clients.Client).DataShare.DataSetClient From 77406c634330d50c9fa5ba74618b8dc8a9d96188 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Thu, 4 Jun 2020 13:22:03 +0800 Subject: [PATCH 5/7] kt's review --- ..._source_data_share_dataset_blob_storage.go | 50 ++++++---- ...rce_arm_data_share_dataset_blob_storage.go | 98 +++++++++++++------ ...ce_data_share_dataset_blob_storage_test.go | 6 +- ...rm_data_share_dataset_blob_storage_test.go | 71 ++++++++------ .../services/mysql/mysql_server_resource.go | 3 +- .../mysql/tests/mysql_server_resource_test.go | 47 ++++++++- ...a_share_dataset_blob_storage.html.markdown | 16 ++- ...a_share_dataset_blob_storage.html.markdown | 32 +++--- 8 files changed, 220 insertions(+), 103 deletions(-) diff --git a/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go b/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go index d726612a1b3f..6ea6a98d36f7 100644 --- a/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go +++ b/azurerm/internal/services/datashare/data_source_data_share_dataset_blob_storage.go @@ -39,19 +39,27 @@ func dataSourceDataShareDatasetBlobStorage() *schema.Resource { Computed: true, }, - "storage_account_name": { - Type: schema.TypeString, - Computed: true, - }, - - "storage_account_resource_group_name": { - Type: schema.TypeString, - Computed: true, - }, - - "storage_account_subscription_id": { - Type: schema.TypeString, + "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": { @@ -102,9 +110,9 @@ func dataSourceArmDataShareDatasetBlobStorageRead(d *schema.ResourceData, meta i 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) + 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) } @@ -112,9 +120,9 @@ func dataSourceArmDataShareDatasetBlobStorageRead(d *schema.ResourceData, meta i 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) + 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) } @@ -122,9 +130,9 @@ func dataSourceArmDataShareDatasetBlobStorageRead(d *schema.ResourceData, meta i 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) + 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) } diff --git a/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go b/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go index 305f8e4ca505..6e030c3ec699 100644 --- a/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go +++ b/azurerm/internal/services/datashare/resource_arm_data_share_dataset_blob_storage.go @@ -60,20 +60,31 @@ func resourceArmDataShareDataSetBlobStorage() *schema.Resource { ValidateFunc: azValidate.StorageContainerName, }, - "storage_account_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: storage.ValidateArmStorageAccountName, - }, - - "storage_account_resource_group_name": azure.SchemaResourceGroupName(), - - "storage_account_subscription_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validation.IsUUID, + "storage_account": { + Type: schema.TypeList, + Required: true, + ForceNew: true, + MaxItems: 1, + MinItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: storage.ValidateArmStorageAccountName, + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + + "subscription_id": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.IsUUID, + }, + }, + }, }, "file_path": { @@ -127,9 +138,9 @@ func resourceArmDataShareDataSetBlobStorageCreate(d *schema.ResourceData, meta i Kind: datashare.KindBlob, BlobProperties: &datashare.BlobProperties{ ContainerName: utils.String(d.Get("container_name").(string)), - StorageAccountName: utils.String(d.Get("storage_account_name").(string)), - ResourceGroup: utils.String(d.Get("storage_account_resource_group_name").(string)), - SubscriptionID: utils.String(d.Get("storage_account_subscription_id").(string)), + StorageAccountName: utils.String(d.Get("storage_account.0.name").(string)), + ResourceGroup: utils.String(d.Get("storage_account.0.resource_group_name").(string)), + SubscriptionID: utils.String(d.Get("storage_account.0.subscription_id").(string)), FilePath: utils.String(filePath.(string)), }, } @@ -138,9 +149,9 @@ func resourceArmDataShareDataSetBlobStorageCreate(d *schema.ResourceData, meta i Kind: datashare.KindBlobFolder, BlobFolderProperties: &datashare.BlobFolderProperties{ ContainerName: utils.String(d.Get("container_name").(string)), - StorageAccountName: utils.String(d.Get("storage_account_name").(string)), - ResourceGroup: utils.String(d.Get("storage_account_resource_group_name").(string)), - SubscriptionID: utils.String(d.Get("storage_account_subscription_id").(string)), + StorageAccountName: utils.String(d.Get("storage_account.0.name").(string)), + ResourceGroup: utils.String(d.Get("storage_account.0.resource_group_name").(string)), + SubscriptionID: utils.String(d.Get("storage_account.0.subscription_id").(string)), Prefix: utils.String(folderPath.(string)), }, } @@ -149,9 +160,9 @@ func resourceArmDataShareDataSetBlobStorageCreate(d *schema.ResourceData, meta i Kind: datashare.KindContainer, BlobContainerProperties: &datashare.BlobContainerProperties{ ContainerName: utils.String(d.Get("container_name").(string)), - StorageAccountName: utils.String(d.Get("storage_account_name").(string)), - ResourceGroup: utils.String(d.Get("storage_account_resource_group_name").(string)), - SubscriptionID: utils.String(d.Get("storage_account_subscription_id").(string)), + StorageAccountName: utils.String(d.Get("storage_account.0.name").(string)), + ResourceGroup: utils.String(d.Get("storage_account.0.resource_group_name").(string)), + SubscriptionID: utils.String(d.Get("storage_account.0.subscription_id").(string)), }, } } @@ -210,9 +221,9 @@ func resourceArmDataShareDataSetBlobStorageRead(d *schema.ResourceData, meta int 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) + 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) } @@ -220,9 +231,9 @@ func resourceArmDataShareDataSetBlobStorageRead(d *schema.ResourceData, meta int 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) + 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) } @@ -230,9 +241,9 @@ func resourceArmDataShareDataSetBlobStorageRead(d *schema.ResourceData, meta int 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) + 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) } @@ -258,3 +269,26 @@ func resourceArmDataShareDataSetBlobStorageDelete(d *schema.ResourceData, meta i } return nil } + +func flattenAzureRmDataShareDataSetBlobStorageAccount(strName, strRG, strSubs *string) []interface{} { + var name, rg, subs string + if strName != nil { + name = *strName + } + + if strRG != nil { + rg = *strRG + } + + if strSubs != nil { + subs = *strSubs + } + + return []interface{}{ + map[string]interface{}{ + "name": name, + "resource_group_name": rg, + "subscription_id": subs, + }, + } +} diff --git a/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go index c8863e1bb1b1..f986effc9ea2 100644 --- a/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go +++ b/azurerm/internal/services/datashare/tests/data_source_data_share_dataset_blob_storage_test.go @@ -21,9 +21,9 @@ func TestAccDataSourceAzureRMDataShareDatasetBlobStorage_basic(t *testing.T) { Check: resource.ComposeTestCheckFunc( testCheckAzureRMDataShareDataSetExists(data.ResourceName), resource.TestCheckResourceAttrSet(data.ResourceName, "container_name"), - resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account_name"), - resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account_resource_group_name"), - resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account_subscription_id"), + resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account.0.name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account.0.resource_group_name"), + resource.TestCheckResourceAttrSet(data.ResourceName, "storage_account.0.subscription_id"), resource.TestCheckResourceAttrSet(data.ResourceName, "file_path"), resource.TestCheckResourceAttrSet(data.ResourceName, "display_name"), ), diff --git a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go index 0a8ad9de9369..0ce1ffb105b0 100644 --- a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go +++ b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go @@ -151,10 +151,17 @@ provider "azuread" { resource "azurerm_resource_group" "test" { name = "acctest-datashare-%[1]d" location = "%[2]s" + lifecycle { + ignore_changes = [ + # Ignore changes to tags, e.g. because a management agent + # updates these based on some ruleset managed elsewhere. + tags, + ] + } } resource "azurerm_data_share_account" "test" { - name = "acctest-dsa-%[1]d" + name = "acctest-DSA-%[1]d" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name identity { @@ -163,7 +170,7 @@ resource "azurerm_data_share_account" "test" { } resource "azurerm_data_share" "test" { - name = "acctest_ds_%[1]d" + name = "acctest_DS_%[1]d" account_id = azurerm_data_share_account.test.id kind = "CopyBased" } @@ -200,13 +207,15 @@ func testAccAzureRMDataShareDataSetBlobStorageFile_basic(data acceptance.TestDat %[1]s resource "azurerm_data_share_dataset_blob_storage" "test" { - name = "acctest-dsbds-file-%[2]d" - data_share_id = azurerm_data_share.test.id - container_name = azurerm_storage_container.test.name - storage_account_name = azurerm_storage_account.test.name - storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name - storage_account_subscription_id = "%[3]s" - file_path = "myfile.txt" + name = "acctest-DSDSBS-file-%[2]d" + data_share_id = azurerm_data_share.test.id + container_name = azurerm_storage_container.test.name + storage_account { + name = azurerm_storage_account.test.name + resource_group_name = azurerm_storage_account.test.resource_group_name + subscription_id = "%[3]s" + } + file_path = "myfile.txt" depends_on = [ azurerm_role_assignment.test, ] @@ -220,13 +229,15 @@ func testAccAzureRMDataShareDataSetBlobStorageFolder_basic(data acceptance.TestD %[1]s resource "azurerm_data_share_dataset_blob_storage" "test" { - name = "acctest-dsbds-folder-%[2]d" - data_share_id = azurerm_data_share.test.id - container_name = azurerm_storage_container.test.name - storage_account_name = azurerm_storage_account.test.name - storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name - storage_account_subscription_id = "%[3]s" - folder_path = "/test/" + name = "acctest-DSDSBS-folder-%[2]d" + data_share_id = azurerm_data_share.test.id + container_name = azurerm_storage_container.test.name + storage_account { + name = azurerm_storage_account.test.name + resource_group_name = azurerm_storage_account.test.resource_group_name + subscription_id = "%[3]s" + } + folder_path = "test" depends_on = [ azurerm_role_assignment.test, ] @@ -240,12 +251,14 @@ func testAccAzureRMDataShareDataSetBlobStorageContainer_basic(data acceptance.Te %[1]s resource "azurerm_data_share_dataset_blob_storage" "test" { - name = "acctest-dsbds-folder-%[2]d" - data_share_id = azurerm_data_share.test.id - container_name = azurerm_storage_container.test.name - storage_account_name = azurerm_storage_account.test.name - storage_account_resource_group_name = azurerm_storage_account.test.resource_group_name - storage_account_subscription_id = "%[3]s" + name = "acctest-DSDSBS-folder-%[2]d" + data_share_id = azurerm_data_share.test.id + container_name = azurerm_storage_container.test.name + storage_account { + name = azurerm_storage_account.test.name + resource_group_name = azurerm_storage_account.test.resource_group_name + subscription_id = "%[3]s" + } depends_on = [ azurerm_role_assignment.test, ] @@ -259,12 +272,14 @@ func testAccAzureRMDataShareDataSetBlobStorage_requiresImport(data acceptance.Te %s resource "azurerm_data_share_dataset_blob_storage" "import" { - name = azurerm_data_share_dataset_blob_storage.test.name - data_share_id = azurerm_data_share.test.id - container_name = azurerm_data_share_dataset_blob_storage.test.container_name - storage_account_name = azurerm_data_share_dataset_blob_storage.test.storage_account_name - storage_account_resource_group_name = azurerm_data_share_dataset_blob_storage.test.storage_account_resource_group_name - storage_account_subscription_id = azurerm_data_share_dataset_blob_storage.test.storage_account_subscription_id + name = azurerm_data_share_dataset_blob_storage.test.name + data_share_id = azurerm_data_share.test.id + container_name = azurerm_data_share_dataset_blob_storage.test.container_name + storage_account { + name = azurerm_data_share_dataset_blob_storage.test.storage_account.0.name + resource_group_name = azurerm_data_share_dataset_blob_storage.test.storage_account.0.resource_group_name + subscription_id = azurerm_data_share_dataset_blob_storage.test.storage_account.0.subscription_id + } } `, config) } diff --git a/azurerm/internal/services/mysql/mysql_server_resource.go b/azurerm/internal/services/mysql/mysql_server_resource.go index 7cc3d8364401..b531cb483cf1 100644 --- a/azurerm/internal/services/mysql/mysql_server_resource.go +++ b/azurerm/internal/services/mysql/mysql_server_resource.go @@ -269,7 +269,8 @@ func resourceArmMySqlServer() *schema.Resource { "version": { Type: schema.TypeString, - Required: true, + Optional: true, + Computed: true, ValidateFunc: validation.StringInSlice([]string{ string(mysql.FiveFullStopSix), string(mysql.FiveFullStopSeven), diff --git a/azurerm/internal/services/mysql/tests/mysql_server_resource_test.go b/azurerm/internal/services/mysql/tests/mysql_server_resource_test.go index 645cd296f6a7..1b6aec036aa2 100644 --- a/azurerm/internal/services/mysql/tests/mysql_server_resource_test.go +++ b/azurerm/internal/services/mysql/tests/mysql_server_resource_test.go @@ -302,6 +302,34 @@ func TestAccAzureRMMySQLServer_createReplica(t *testing.T) { }) } +func TestAccAzureRMMySQLServer_createReplicaAltLocation(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_mysql_server", "test") + mysqlVersion := "8.0" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMMySQLServerDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMMySQLServer_basic(data, mysqlVersion), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMySQLServerExists(data.ResourceName), + ), + }, + data.ImportStep("administrator_login_password"), + { + Config: testAccAzureRMMySQLServer_createReplicaAltLocation(data, mysqlVersion), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMMySQLServerExists(data.ResourceName), + testCheckAzureRMMySQLServerExists("azurerm_mysql_server.replica"), + ), + }, + data.ImportStep("administrator_login_password"), + }, + }) +} + func TestAccAzureRMMySQLServer_createPointInTimeRestore(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_mysql_server", "test") restoreTime := time.Now().Add(11 * time.Minute) @@ -590,7 +618,7 @@ resource "azurerm_mysql_server" "replica" { resource_group_name = azurerm_resource_group.test.name sku_name = "GP_Gen5_2" version = "%s" - +storage_mb = 51200 create_mode = "Replica" creation_source_server_id = azurerm_mysql_server.test.id ssl_enforcement_enabled = true @@ -598,6 +626,23 @@ resource "azurerm_mysql_server" "replica" { `, testAccAzureRMMySQLServer_basic(data, version), data.RandomInteger, version) } +func testAccAzureRMMySQLServer_createReplicaAltLocation(data acceptance.TestData, version string) string { + return fmt.Sprintf(` +%s + +resource "azurerm_mysql_server" "replica" { + name = "acctestmysqlsvr-%d-replica" + location = "%s" + resource_group_name = azurerm_resource_group.test.name + sku_name = "GP_Gen5_2" +storage_mb = 51200 + create_mode = "Replica" + creation_source_server_id = azurerm_mysql_server.test.id + ssl_enforcement_enabled = true +} +`, testAccAzureRMMySQLServer_basic(data, version), data.RandomInteger, data.Locations.Secondary) +} + func testAccAzureRMMySQLServer_createPointInTimeRestore(data acceptance.TestData, version, restoreTime string) string { return fmt.Sprintf(` %s diff --git a/website/docs/d/data_share_dataset_blob_storage.html.markdown b/website/docs/d/data_share_dataset_blob_storage.html.markdown index 3c50ea87da8b..e42b1f87a972 100644 --- a/website/docs/d/data_share_dataset_blob_storage.html.markdown +++ b/website/docs/d/data_share_dataset_blob_storage.html.markdown @@ -43,11 +43,7 @@ In addition to the Arguments listed above - the following Attributes are exporte * `container_name` - The name of the storage account container to be shared with the receiver. -* `storage_account_name` - The name of the storage account to be shared with the receiver. - -* `storage_account_resource_group_name` - The resource group name of the storage account to be shared with the receiver. - -* `storage_account_subscription_id` - The subscription id of the storage account to be shared with the receiver. +* `storage_account` - A `storage_account` block as defined below. * `file_path` - The path of the file in the storage container to be shared with the receiver. @@ -55,6 +51,16 @@ In addition to the Arguments listed above - the following Attributes are exporte * `display_name` - The name of the Data Share Dataset. +--- + +A `storage_account` block supports the following: + +* `name` - The name of the storage account to be shared with the receiver. + +* `resource_group_name` - The resource group name of the storage account to be shared with the receiver. + +* `subscription_id` - The subscription id of the storage account to be shared with the receiver. + ## Timeouts The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: diff --git a/website/docs/r/data_share_dataset_blob_storage.html.markdown b/website/docs/r/data_share_dataset_blob_storage.html.markdown index 1c9590155166..c9e94267aaf1 100644 --- a/website/docs/r/data_share_dataset_blob_storage.html.markdown +++ b/website/docs/r/data_share_dataset_blob_storage.html.markdown @@ -62,13 +62,15 @@ resource "azurerm_role_assignment" "example" { } resource "azurerm_data_share_dataset_blob_storage" "example" { - name = "example-dsbsds-file" - data_share_id = azurerm_data_share.example.id - container_name = azurerm_storage_container.example.name - storage_account_name = azurerm_storage_account.example.name - storage_account_resource_group_name = azurerm_storage_account.example.resource_group_name - storage_account_subscription_id = "00000000-0000-0000-0000-000000000000" - file_path = "myfile.txt" + name = "example-dsbsds-file" + data_share_id = azurerm_data_share.example.id + container_name = azurerm_storage_container.example.name + storage_account { + name = azurerm_storage_account.example.name + resource_group_name = azurerm_storage_account.example.resource_group_name + subscription_id = "00000000-0000-0000-0000-000000000000" + } + file_path = "myfile.txt" depends_on = [ azurerm_role_assignment.example, ] @@ -85,16 +87,22 @@ The following arguments are supported: * `container_name` - (Required) The name of the storage account container to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. -* `storage_account_name` - (Required) The name of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. - -* `storage_account_resource_group_name` - (Required) The resource group name of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. - -* `storage_account_subscription_id` - (Required) The subscription id of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. +* `storage_account` - (Required) A `storage_account` block as defined below. * `file_path` - (Optional) The path of the file in the storage container to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. * `folder_path` - (Optional) The path of the folder in the storage container to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. +--- + +A `storage_account` block supports the following: + +* `name` - (Required) The name of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `resource_group_name` - (Required) The resource group name of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + +* `subscription_id` - (Required) The subscription id of the storage account to be shared with the receiver. Changing this forces a new Data Share Blob Storage Dataset to be created. + ## Attributes Reference In addition to the Arguments listed above - the following Attributes are exported: From 212a63cb9a4be55113c4725bf911f5681e793c1f Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Thu, 4 Jun 2020 14:22:48 +0800 Subject: [PATCH 6/7] remove useless --- .../services/mysql/mysql_server_resource.go | 3 +- .../mysql/tests/mysql_server_resource_test.go | 47 +------------------ 2 files changed, 2 insertions(+), 48 deletions(-) diff --git a/azurerm/internal/services/mysql/mysql_server_resource.go b/azurerm/internal/services/mysql/mysql_server_resource.go index b531cb483cf1..7cc3d8364401 100644 --- a/azurerm/internal/services/mysql/mysql_server_resource.go +++ b/azurerm/internal/services/mysql/mysql_server_resource.go @@ -269,8 +269,7 @@ func resourceArmMySqlServer() *schema.Resource { "version": { Type: schema.TypeString, - Optional: true, - Computed: true, + Required: true, ValidateFunc: validation.StringInSlice([]string{ string(mysql.FiveFullStopSix), string(mysql.FiveFullStopSeven), diff --git a/azurerm/internal/services/mysql/tests/mysql_server_resource_test.go b/azurerm/internal/services/mysql/tests/mysql_server_resource_test.go index 1b6aec036aa2..645cd296f6a7 100644 --- a/azurerm/internal/services/mysql/tests/mysql_server_resource_test.go +++ b/azurerm/internal/services/mysql/tests/mysql_server_resource_test.go @@ -302,34 +302,6 @@ func TestAccAzureRMMySQLServer_createReplica(t *testing.T) { }) } -func TestAccAzureRMMySQLServer_createReplicaAltLocation(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_mysql_server", "test") - mysqlVersion := "8.0" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { acceptance.PreCheck(t) }, - Providers: acceptance.SupportedProviders, - CheckDestroy: testCheckAzureRMMySQLServerDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAzureRMMySQLServer_basic(data, mysqlVersion), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMMySQLServerExists(data.ResourceName), - ), - }, - data.ImportStep("administrator_login_password"), - { - Config: testAccAzureRMMySQLServer_createReplicaAltLocation(data, mysqlVersion), - Check: resource.ComposeTestCheckFunc( - testCheckAzureRMMySQLServerExists(data.ResourceName), - testCheckAzureRMMySQLServerExists("azurerm_mysql_server.replica"), - ), - }, - data.ImportStep("administrator_login_password"), - }, - }) -} - func TestAccAzureRMMySQLServer_createPointInTimeRestore(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_mysql_server", "test") restoreTime := time.Now().Add(11 * time.Minute) @@ -618,29 +590,12 @@ resource "azurerm_mysql_server" "replica" { resource_group_name = azurerm_resource_group.test.name sku_name = "GP_Gen5_2" version = "%s" -storage_mb = 51200 - create_mode = "Replica" - creation_source_server_id = azurerm_mysql_server.test.id - ssl_enforcement_enabled = true -} -`, testAccAzureRMMySQLServer_basic(data, version), data.RandomInteger, version) -} - -func testAccAzureRMMySQLServer_createReplicaAltLocation(data acceptance.TestData, version string) string { - return fmt.Sprintf(` -%s -resource "azurerm_mysql_server" "replica" { - name = "acctestmysqlsvr-%d-replica" - location = "%s" - resource_group_name = azurerm_resource_group.test.name - sku_name = "GP_Gen5_2" -storage_mb = 51200 create_mode = "Replica" creation_source_server_id = azurerm_mysql_server.test.id ssl_enforcement_enabled = true } -`, testAccAzureRMMySQLServer_basic(data, version), data.RandomInteger, data.Locations.Secondary) +`, testAccAzureRMMySQLServer_basic(data, version), data.RandomInteger, version) } func testAccAzureRMMySQLServer_createPointInTimeRestore(data acceptance.TestData, version, restoreTime string) string { From ea24f91c1cb2732bf4e0084363c329d7c33ef573 Mon Sep 17 00:00:00 2001 From: yupwei68 Date: Fri, 5 Jun 2020 09:58:56 +0800 Subject: [PATCH 7/7] remove useless --- .../resource_arm_data_share_dataset_blob_storage_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go index 0ce1ffb105b0..5a6a31c50de4 100644 --- a/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go +++ b/azurerm/internal/services/datashare/tests/resource_arm_data_share_dataset_blob_storage_test.go @@ -151,13 +151,6 @@ provider "azuread" { resource "azurerm_resource_group" "test" { name = "acctest-datashare-%[1]d" location = "%[2]s" - lifecycle { - ignore_changes = [ - # Ignore changes to tags, e.g. because a management agent - # updates these based on some ruleset managed elsewhere. - tags, - ] - } } resource "azurerm_data_share_account" "test" {