Skip to content

Commit

Permalink
New resource: azurerm_databricks_access_connector
Browse files Browse the repository at this point in the history
Also upgrade Databricks SDK to `2022-04-01-preview`
I added acceptance tests, but feature is in private preview.

Fixes hashicorp#18518
  • Loading branch information
favoretti committed Oct 13, 2022
1 parent 5036264 commit 503bc25
Show file tree
Hide file tree
Showing 63 changed files with 3,022 additions and 10 deletions.
11 changes: 8 additions & 3 deletions internal/services/databricks/client/client.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package client

import (
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2021-04-01-preview/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/accessconnector"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
WorkspacesClient *workspaces.WorkspacesClient
AccessConnectorClient *accessconnector.AccessConnectorClient
WorkspacesClient *workspaces.WorkspacesClient
}

func NewClient(o *common.ClientOptions) *Client {
AccessConnectorClient := accessconnector.NewAccessConnectorClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&AccessConnectorClient.Client, o.ResourceManagerAuthorizer)
WorkspacesClient := workspaces.NewWorkspacesClientWithBaseURI(o.ResourceManagerEndpoint)
o.ConfigureClient(&WorkspacesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
WorkspacesClient: &WorkspacesClient,
AccessConnectorClient: &AccessConnectorClient,
WorkspacesClient: &WorkspacesClient,
}
}
150 changes: 150 additions & 0 deletions internal/services/databricks/databricks_access_connector_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package databricks

import (
"fmt"
"log"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/accessconnector"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/databricks/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
)

func resourceDatabricksAccessConnector() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceDatabricksAccessConnectorCreateUpdate,
Read: resourceDatabricksAccessConnectorRead,
Update: resourceDatabricksAccessConnectorCreateUpdate,
Delete: resourceDatabricksAccessConnectorDelete,

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(5 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(5 * time.Minute),
Delete: pluginsdk.DefaultTimeout(5 * time.Minute),
},

Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := accessconnector.ParseAccessConnectorID(id)
return err
}),

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.AccessConnectorName,
},

"location": commonschema.Location(),
"resource_group_name": commonschema.ResourceGroupName(),
"identity": commonschema.SystemAssignedIdentityRequired(),
"tags": commonschema.Tags(),
},
}
}

func resourceDatabricksAccessConnectorCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataBricks.AccessConnectorClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

id := accessconnector.NewAccessConnectorID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id)
if err != nil {
if !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}
}

if !response.WasNotFound(existing.HttpResponse) {
return tf.ImportAsExistsError("azurerm_databricks_access_connector", id.ID())
}
}

location := azure.NormalizeLocation(d.Get("location").(string))

identity, err := identity.ExpandSystemAssigned(d.Get("identity").([]interface{}))
if err != nil {
return fmt.Errorf("expanding `identity`: %+v", err)
}

accessConnector := accessconnector.AccessConnector{
Location: location,
Identity: identity,
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if err := client.CreateOrUpdateThenPoll(ctx, id, accessConnector); err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceDatabricksAccessConnectorRead(d, meta)
}

func resourceDatabricksAccessConnectorRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataBricks.AccessConnectorClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := accessconnector.ParseAccessConnectorID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, *id)
if err != nil {
if response.WasNotFound(resp.HttpResponse) {
log.Printf("[DEBUG] %s was not found - removing from state", *id)
d.SetId("")
return nil
}

return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.ConnectorName)
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
d.Set("location", azure.NormalizeLocation(model.Location))

if err := d.Set("identity", identity.FlattenSystemAssigned(model.Identity)); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

return tags.FlattenAndSet(d, model.Tags)
}

return nil
}

func resourceDatabricksAccessConnectorDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataBricks.AccessConnectorClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := accessconnector.ParseAccessConnectorID(d.Id())
if err != nil {
return err
}

if err = client.DeleteThenPoll(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package databricks_test

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/accessconnector"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type DatabricksAccessConnectorResource struct{}

func TestAccDatabricksAccessConnector_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_databricks_access_connector", "test")
r := DatabricksAccessConnectorResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccDatabricksAccessConnector_requiresImport(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_databricks_access_connector", "test")
r := DatabricksAccessConnectorResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

func (DatabricksAccessConnectorResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := accessconnector.ParseAccessConnectorID(state.ID)
if err != nil {
return nil, err
}

resp, err := clients.DataBricks.AccessConnectorClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving Databricks Access Connector %q (resource group: %q): %+v", id.ConnectorName, id.ResourceGroupName, err)
}

return utils.Bool(resp.Model != nil), nil
}

func (DatabricksAccessConnectorResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-databricks-%d"
location = "%s"
}
resource "azurerm_databricks_access_connector" "test" {
name = "acctestDBAC%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
identity {
type = "SystemAssigned"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (DatabricksAccessConnectorResource) requiresImport(data acceptance.TestData) string {
template := DatabricksAccessConnectorResource{}.basic(data)
return fmt.Sprintf(`
%s
resource "azurerm_databricks_access_connector" "import" {
name = azurerm_databricks_access_connector.test.name
resource_group_name = azurerm_databricks_access_connector.test.resource_group_name
location = azurerm_databricks_access_connector.test.location
}
`, template)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2021-04-01-preview/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2021-04-01-preview/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2021-04-01-preview/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2021-04-01-preview/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2021-04-01-preview/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"testing"

"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2021-04-01-preview/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/hashicorp/go-azure-helpers/resourcemanager/resourceids"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2021-04-01-preview/workspaces"
"github.com/hashicorp/go-azure-sdk/resource-manager/databricks/2022-04-01-preview/workspaces"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

Expand Down
1 change: 1 addition & 0 deletions internal/services/databricks/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (r Registration) SupportedDataSources() map[string]*pluginsdk.Resource {
// SupportedResources returns the supported Resources supported by this Service
func (r Registration) SupportedResources() map[string]*pluginsdk.Resource {
return map[string]*pluginsdk.Resource{
"azurerm_databricks_access_connector": resourceDatabricksAccessConnector(),
"azurerm_databricks_workspace": resourceDatabricksWorkspace(),
"azurerm_databricks_workspace_customer_managed_key": resourceDatabricksWorkspaceCustomerManagedKey(),
}
Expand Down
Loading

0 comments on commit 503bc25

Please sign in to comment.