Skip to content

Commit

Permalink
New resource/Data Source azurerm_data_share (#6789)
Browse files Browse the repository at this point in the history
fix #6480
  • Loading branch information
yupwei68 committed May 21, 2020
1 parent 76fbea8 commit 6595a4c
Show file tree
Hide file tree
Showing 13 changed files with 1,242 additions and 2 deletions.
14 changes: 12 additions & 2 deletions azurerm/internal/services/datashare/client/client.go
Expand Up @@ -6,14 +6,24 @@ import (
)

type Client struct {
AccountClient *datashare.AccountsClient
AccountClient *datashare.AccountsClient
SharesClient *datashare.SharesClient
SynchronizationClient *datashare.SynchronizationSettingsClient
}

func NewClient(o *common.ClientOptions) *Client {
accountClient := datashare.NewAccountsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&accountClient.Client, o.ResourceManagerAuthorizer)

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

synchronizationSettingsClient := datashare.NewSynchronizationSettingsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&synchronizationSettingsClient.Client, o.ResourceManagerAuthorizer)

return &Client{
AccountClient: &accountClient,
AccountClient: &accountClient,
SharesClient: &sharesClient,
SynchronizationClient: &synchronizationSettingsClient,
}
}
139 changes: 139 additions & 0 deletions azurerm/internal/services/datashare/data_source_data_share.go
@@ -0,0 +1,139 @@
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/internal/clients"
"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"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceDataShare() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDataShareRead,

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

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

"account_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.DatashareAccountID,
},

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

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

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

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

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

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

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

name := d.Get("name").(string)
accountID := d.Get("account_id").(string)
accountId, err := parse.DataShareAccountID(accountID)
if err != nil {
return err
}

resp, err := client.Get(ctx, accountId.ResourceGroup, accountId.Name, 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 %q (Resource Group %q / accountName %q): %+v", name, accountId.ResourceGroup, accountId.Name, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("reading DataShare %q (Resource Group %q / accountName %q): ID is empty", name, accountId.ResourceGroup, accountId.Name)
}

d.SetId(*resp.ID)
d.Set("name", name)
d.Set("account_id", accountID)
if props := resp.ShareProperties; props != nil {
d.Set("kind", props.ShareKind)
d.Set("description", props.Description)
d.Set("terms", props.Terms)
}

if syncIterator, err := syncClient.ListByShareComplete(ctx, accountId.ResourceGroup, accountId.Name, name, ""); syncIterator.NotDone() {
if err != nil {
return fmt.Errorf("listing DataShare %q snapshot schedule (Resource Group %q / accountName %q): %+v", name, accountId.ResourceGroup, accountId.Name, err)
}
if syncName := syncIterator.Value().(datashare.ScheduledSynchronizationSetting).Name; syncName != nil && *syncName != "" {
syncResp, err := syncClient.Get(ctx, accountId.ResourceGroup, accountId.Name, name, *syncName)
if err != nil {
return fmt.Errorf("reading DataShare %q snapshot schedule (Resource Group %q / accountName %q): %+v", name, accountId.ResourceGroup, accountId.Name, err)
}
if schedule := syncResp.Value.(datashare.ScheduledSynchronizationSetting); schedule.ID != nil && *schedule.ID != "" {
if err := d.Set("snapshot_schedule", flattenAzureRmDataShareSnapshotSchedule(&schedule)); err != nil {
return fmt.Errorf("setting `snapshot_schedule`: %+v", err)
}
}
}
if err := syncIterator.NextWithContext(ctx); err != nil {
return fmt.Errorf("listing DataShare %q snapshot schedule (Resource Group %q / accountName %q): %+v", name, accountId.ResourceGroup, accountId.Name, err)
}
if syncIterator.NotDone() {
return fmt.Errorf("more than one DataShare %q snapshot schedule (Resource Group %q / accountName %q) is returned", name, accountId.ResourceGroup, accountId.Name)
}
}

return nil
}
28 changes: 28 additions & 0 deletions azurerm/internal/services/datashare/parse/data_share.go
Expand Up @@ -11,6 +11,12 @@ type DataShareAccountId struct {
Name string
}

type DataShareId struct {
ResourceGroup string
AccountName string
Name string
}

func DataShareAccountID(input string) (*DataShareAccountId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
Expand All @@ -29,3 +35,25 @@ func DataShareAccountID(input string) (*DataShareAccountId, error) {

return &dataShareAccount, nil
}

func DataShareID(input string) (*DataShareId, error) {
var id, err = azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("unable to parse DataShare ID %q: %+v", input, err)
}

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

return &DataShare, nil
}
82 changes: 82 additions & 0 deletions azurerm/internal/services/datashare/parse/data_share_test.go
Expand Up @@ -70,3 +70,85 @@ func TestDataShareAccountID(t *testing.T) {
}
}
}

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

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

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

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)
}
}
}
2 changes: 2 additions & 0 deletions azurerm/internal/services/datashare/registration.go
Expand Up @@ -20,12 +20,14 @@ func (r Registration) WebsiteCategories() []string {
func (r Registration) SupportedDataSources() map[string]*schema.Resource {
return map[string]*schema.Resource{
"azurerm_data_share_account": dataSourceDataShareAccount(),
"azurerm_data_share": dataSourceDataShare(),
}
}

// 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(),
}
}

0 comments on commit 6595a4c

Please sign in to comment.