Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New resource/Data source azurerm_data_share_account #6575

Merged
merged 4 commits into from May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions azurerm/internal/clients/client.go
Expand Up @@ -26,6 +26,7 @@ import (
databricks "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/databricks/client"
datafactory "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/client"
datalake "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datalake/client"
datashare "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datashare/client"
devspace "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/devspace/client"
devtestlabs "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/devtestlabs/client"
dns "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns/client"
Expand Down Expand Up @@ -104,6 +105,7 @@ type Client struct {
DataBricks *databricks.Client
DataFactory *datafactory.Client
Datalake *datalake.Client
DataShare *datashare.Client
DevSpace *devspace.Client
DevTestLabs *devtestlabs.Client
Dns *dns.Client
Expand Down Expand Up @@ -183,6 +185,7 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
client.DataBricks = databricks.NewClient(o)
client.DataFactory = datafactory.NewClient(o)
client.Datalake = datalake.NewClient(o)
client.DataShare = datashare.NewClient(o)
client.DevSpace = devspace.NewClient(o)
client.DevTestLabs = devtestlabs.NewClient(o)
client.Dns = dns.NewClient(o)
Expand Down
2 changes: 2 additions & 0 deletions azurerm/internal/provider/services.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/databricks"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datalake"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datashare"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/devspace"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/devtestlabs"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/dns"
Expand Down Expand Up @@ -97,6 +98,7 @@ func SupportedServices() []common.ServiceRegistration {
datafactory.Registration{},
datalake.Registration{},
databasemigration.Registration{},
datashare.Registration{},
devspace.Registration{},
devtestlabs.Registration{},
dns.Registration{},
Expand Down
19 changes: 19 additions & 0 deletions azurerm/internal/services/datashare/client/client.go
@@ -0,0 +1,19 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/datashare/mgmt/2019-11-01/datashare"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/common"
)

type Client struct {
AccountClient *datashare.AccountsClient
}

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

return &Client{
AccountClient: &accountClient,
}
}
@@ -0,0 +1,89 @@
package datashare

import (
"fmt"
"log"
"time"

"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/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datashare/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceDataShareAccount() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDataShareAccountRead,

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

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

"resource_group_name": azure.SchemaResourceGroupNameForDataSource(),

"identity": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Computed: true,
},
"principal_id": {
Type: schema.TypeString,
Computed: true,
},
"tenant_id": {
Type: schema.TypeString,
Computed: true,
},
},
},
},

"tags": tags.SchemaDataSource(),
},
}
}

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

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

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

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("reading DataShare Account %q (Resource Group %q): ID is empty or nil", name, resourceGroup)
}

d.SetId(*resp.ID)
d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
if err := d.Set("identity", flattenAzureRmDataShareAccountIdentity(resp.Identity)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}
return tags.FlattenAndSet(d, resp.Tags)
}
31 changes: 31 additions & 0 deletions azurerm/internal/services/datashare/parse/data_share.go
@@ -0,0 +1,31 @@
package parse

import (
"fmt"

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

type DataShareAccountId struct {
ResourceGroup string
Name string
}

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
}
72 changes: 72 additions & 0 deletions azurerm/internal/services/datashare/parse/data_share_test.go
@@ -0,0 +1,72 @@
package parse

import (
"testing"
)

func TestDataShareAccountID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *DataShareAccountId
}{
{
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: "Datashare account ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/accounts/account1",
Expected: &DataShareAccountId{
Name: "account1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.DataShare/Accounts/account1",
Expected: nil,
},
}

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

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

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)
}
}
}
31 changes: 31 additions & 0 deletions azurerm/internal/services/datashare/registration.go
@@ -0,0 +1,31 @@
package datashare

import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"

type Registration struct{}

// Name is the name of this Service
func (r Registration) Name() string {
return "Data Share"
}

// WebsiteCategories returns a list of categories which can be used for the sidebar
func (r Registration) WebsiteCategories() []string {
return []string{
"Data Share",
}
}

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

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