Skip to content

Commit

Permalink
Merge pull request #9057 from terraform-providers/r/virtual-desktop
Browse files Browse the repository at this point in the history
r/virtual_desktop_workspace_application_group_association: fixing import support
  • Loading branch information
tombuildsstuff authored Nov 4, 2020
2 parents 235b82b + 0f9ad35 commit 16e77d1
Show file tree
Hide file tree
Showing 27 changed files with 636 additions and 462 deletions.
79 changes: 48 additions & 31 deletions azurerm/internal/services/datashare/data_source_data_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package datashare

import (
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/datashare/mgmt/2019-11-01/datashare"
Expand Down Expand Up @@ -78,62 +77,80 @@ func dataSourceDataShare() *schema.Resource {

func dataSourceArmDataShareRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataShare.SharesClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
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)
accountId, err := parse.DataShareAccountID(d.Get("account_id").(string))
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("DataShare %q (Account %q / Resource Group %q) was not found", name, accountId.Name, accountId.ResourceGroup)
}
return fmt.Errorf("retrieving DataShare %q (Resource Group %q / accountName %q): %+v", name, accountId.ResourceGroup, accountId.Name, err)
return fmt.Errorf("retrieving DataShare %q (Account %q / Resource Group %q): %+v", name, accountId.Name, accountId.ResourceGroup, 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)
}
dataShareId := parse.NewDataShareId(accountId.ResourceGroup, accountId.Name, name).ID(subscriptionId)
d.SetId(dataShareId)

d.SetId(*resp.ID)
d.Set("name", name)
d.Set("account_id", accountID)
d.Set("account_id", accountId.ID(subscriptionId))

if props := resp.ShareProperties; props != nil {
d.Set("kind", props.ShareKind)
d.Set("description", props.Description)
d.Set("kind", string(props.ShareKind))
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)
}
}
settings := make([]datashare.ScheduledSynchronizationSetting, 0)
syncIterator, err := syncClient.ListByShareComplete(ctx, accountId.ResourceGroup, accountId.Name, name, "")
if err != nil {
return fmt.Errorf("listing Snapshot Schedules for Data Share %q (Account %q / Resource Group %q): %+v", name, accountId.Name, accountId.ResourceGroup, err)
}
for syncIterator.NotDone() {
item, ok := syncIterator.Value().AsScheduledSynchronizationSetting()
if ok && item != nil {
settings = append(settings, *item)
}

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 fmt.Errorf("retrieving next Snapshot Schedule: %+v", err)
}
}

if err := d.Set("snapshot_schedule", flattenDataShareDataSourceSnapshotSchedule(settings)); err != nil {
return fmt.Errorf("setting `snapshot_schedule`: %+v", err)
}

return nil
}

func flattenDataShareDataSourceSnapshotSchedule(input []datashare.ScheduledSynchronizationSetting) []interface{} {
output := make([]interface{}, 0)

for _, sync := range input {
name := ""
if sync.Name != nil {
name = *sync.Name
}

startTime := ""
if sync.SynchronizationTime != nil && !sync.SynchronizationTime.IsZero() {
startTime = sync.SynchronizationTime.Format(time.RFC3339)
}

output = append(output, map[string]interface{}{
"name": name,
"recurrence": string(sync.RecurrenceInterval),
"start_time": startTime,
})
}

return output
}
43 changes: 43 additions & 0 deletions azurerm/internal/services/datashare/parse/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type DataShareAccountId struct {
ResourceGroup string
Name string
}

func (id DataShareAccountId) ID(subscriptionId string) string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DataShare/accounts/%s"
return fmt.Sprintf(fmtString, subscriptionId, id.ResourceGroup, id.Name)
}

func NewDataShareAccountId(resourceGroup, name string) DataShareAccountId {
return DataShareAccountId{
ResourceGroup: resourceGroup,
Name: name,
}
}

func DataShareAccountID(input string) (*DataShareAccountId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing DataShareAccount ID %q: %+v", input, err)
}

dataShareAccount := DataShareAccountId{
ResourceGroup: id.ResourceGroup,
}
if dataShareAccount.Name, err = id.PopSegment("accounts"); err != nil {
return nil, err
}
if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &dataShareAccount, nil
}
39 changes: 39 additions & 0 deletions azurerm/internal/services/datashare/parse/data_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type DataShareDataSetId struct {
ResourceGroup string
AccountName string
ShareName string
Name string
}

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
}
59 changes: 8 additions & 51 deletions azurerm/internal/services/datashare/parse/data_share.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,23 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type DataShareAccountId struct {
ResourceGroup string
Name string
}

type DataShareId struct {
ResourceGroup string
AccountName string
Name string
}

type DataShareDataSetId struct {
ResourceGroup string
AccountName string
ShareName string
Name string
func (id DataShareId) ID(subscriptionId string) string {
fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DataShare/accounts/%s/shares/%s"
return fmt.Sprintf(fmtString, subscriptionId, id.ResourceGroup, id.AccountName, id.Name)
}

func DataShareAccountID(input string) (*DataShareAccountId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing DataShareAccount ID %q: %+v", input, err)
func NewDataShareId(resourceGroup, accountName, name string) DataShareId {
return DataShareId{
ResourceGroup: resourceGroup,
AccountName: accountName,
Name: name,
}

dataShareAccount := DataShareAccountId{
ResourceGroup: id.ResourceGroup,
}
if dataShareAccount.Name, err = id.PopSegment("accounts"); err != nil {
return nil, err
}
if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &dataShareAccount, nil
}

func DataShareID(input string) (*DataShareId, error) {
Expand All @@ -64,28 +46,3 @@ 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
}
Loading

0 comments on commit 16e77d1

Please sign in to comment.