Skip to content

Commit

Permalink
New resource/data source azurerm_data_share_dataset_kusto_database (#…
Browse files Browse the repository at this point in the history
…8544)

Partially fix #6480

Sub PR of : #7064
  • Loading branch information
yupwei68 authored Nov 9, 2020
1 parent 65a93c0 commit b71a28b
Show file tree
Hide file tree
Showing 11 changed files with 640 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package datashare

import (
"fmt"
"time"

"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 dataSourceDataShareDatasetKustoDatabase() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDataShareDatasetKustoDatabaseRead,

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,
},

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

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

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

func dataSourceArmDataShareDatasetKustoDatabaseRead(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 Kusto Database 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 DataShare Kusto Database 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)

resp, ok := respModel.Value.AsKustoDatabaseDataSet()
if !ok {
return fmt.Errorf("dataShare %q (Resource Group %q / accountName %q / shareName %q) is not kusto database dataset", name, shareId.ResourceGroup, shareId.AccountName, shareId.Name)
}
if props := resp.KustoDatabaseDataSetProperties; props != nil {
d.Set("kusto_database_id", props.KustoDatabaseResourceID)
d.Set("display_name", props.DataSetID)
d.Set("kusto_cluster_location", props.Location)
}

return nil
}
2 changes: 2 additions & 0 deletions azurerm/internal/services/datashare/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
"azurerm_data_share_dataset_data_lake_gen1": dataSourceDataShareDatasetDataLakeGen1(),
"azurerm_data_share_dataset_data_lake_gen2": dataSourceDataShareDatasetDataLakeGen2(),
"azurerm_data_share_dataset_kusto_cluster": dataSourceDataShareDatasetKustoCluster(),
"azurerm_data_share_dataset_kusto_database": dataSourceDataShareDatasetKustoDatabase(),
}
}

Expand All @@ -37,5 +38,6 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_data_share_dataset_data_lake_gen1": resourceArmDataShareDataSetDataLakeGen1(),
"azurerm_data_share_dataset_data_lake_gen2": resourceArmDataShareDataSetDataLakeGen2(),
"azurerm_data_share_dataset_kusto_cluster": resourceArmDataShareDataSetKustoCluster(),
"azurerm_data_share_dataset_kusto_database": resourceArmDataShareDataSetKustoDatabase(),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,14 @@ func resourceArmDataShareDataSetBlobStorageDelete(d *schema.ResourceData, meta i
return err
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.AccountName, id.ShareName, id.Name); err != nil {
future, err := client.Delete(ctx, id.ResourceGroup, id.AccountName, id.ShareName, id.Name)
if 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)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of DataShare Blob Storage DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", id.Name, id.ResourceGroup, id.AccountName, id.ShareName, err)
}
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,14 @@ func resourceArmDataShareDataSetDataLakeGen1Delete(d *schema.ResourceData, meta
return err
}

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

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of DataShare Data Lake Gen1 DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", id.Name, id.ResourceGroup, id.AccountName, id.ShareName, err)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,14 @@ func resourceArmDataShareDataSetDataLakeGen2Delete(d *schema.ResourceData, meta
return err
}

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

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of DataShare Data Lake Gen2 DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", id.Name, id.ResourceGroup, id.AccountName, id.ShareName, err)
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,14 @@ func resourceArmDataShareDataSetKustoClusterDelete(d *schema.ResourceData, meta
return err
}

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

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of DataShare Kusto Cluster DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", id.Name, id.ResourceGroup, id.AccountName, id.ShareName, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
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/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"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"
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 resourceArmDataShareDataSetKustoDatabase() *schema.Resource {
return &schema.Resource{
Create: resourceArmDataShareDataSetKustoDatabaseCreate,
Read: resourceArmDataShareDataSetKustoDatabaseRead,
Delete: resourceArmDataShareDataSetKustoDatabaseDelete,

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,
},

"kusto_database_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},

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

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

func resourceArmDataShareDataSetKustoDatabaseCreate(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 Kusto Database 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_kusto_database", *existingId)
}

dataSet := datashare.KustoDatabaseDataSet{
Kind: datashare.KindKustoDatabase,
KustoDatabaseDataSetProperties: &datashare.KustoDatabaseDataSetProperties{
KustoDatabaseResourceID: utils.String(d.Get("kusto_database_id").(string)),
},
}

if _, err := client.Create(ctx, shareId.ResourceGroup, shareId.AccountName, shareId.Name, name, dataSet); err != nil {
return fmt.Errorf("creating DataShare Kusto Database 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 Kusto Database 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 Kusto Database DataSet %q (Resource Group %q / accountName %q / shareName %q)", name, shareId.ResourceGroup, shareId.AccountName, shareId.Name)
}

d.SetId(*respId)

return resourceArmDataShareDataSetKustoDatabaseRead(d, meta)
}

func resourceArmDataShareDataSetKustoDatabaseRead(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
}

respModel, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.ShareName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(respModel.Response) {
log.Printf("[INFO] DataShare %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("retrieving DataShare Kusto Database 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)

resp, ok := respModel.Value.AsKustoDatabaseDataSet()
if !ok {
return fmt.Errorf("dataShare %q (Resource Group %q / accountName %q) is not Kusto Database DataSet", id.ShareName, id.ResourceGroup, id.AccountName)
}
if props := resp.KustoDatabaseDataSetProperties; props != nil {
d.Set("kusto_database_id", props.KustoDatabaseResourceID)
d.Set("display_name", props.DataSetID)
d.Set("kusto_cluster_location", props.Location)
}

return nil
}

func resourceArmDataShareDataSetKustoDatabaseDelete(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
}

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

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of DataShare Kusto Database DataSet %q (Resource Group %q / accountName %q / shareName %q): %+v", id.Name, id.ResourceGroup, id.AccountName, id.ShareName, err)
}

return nil
}
Loading

0 comments on commit b71a28b

Please sign in to comment.