-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New resource/data source
azurerm_data_share_dataset_kusto_database
(#…
- Loading branch information
Showing
11 changed files
with
640 additions
and
4 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
azurerm/internal/services/datashare/data_source_data_share_dataset_kusto_database.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
186 changes: 186 additions & 0 deletions
186
azurerm/internal/services/datashare/resource_arm_data_share_dataset_kusto_database.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.