forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New resource:
azurerm_databricks_access_connector
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
Showing
65 changed files
with
3,026 additions
and
14 deletions.
There are no files selected for viewing
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
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
150
internal/services/databricks/databricks_access_connector_resource.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,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 | ||
} |
95 changes: 95 additions & 0 deletions
95
internal/services/databricks/databricks_access_connector_resource_test.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,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) | ||
} |
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
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
Oops, something went wrong.