Skip to content

Commit b2286ca

Browse files
authored
add data sources databricks_external_locations and databricks_external_location (#3464)
* add data source `databricks_external_locations` * feedback * feedback
1 parent e10d36c commit b2286ca

File tree

8 files changed

+244
-0
lines changed

8 files changed

+244
-0
lines changed

catalog/data_external_location.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package catalog
2+
3+
import (
4+
"context"
5+
6+
"github.com/databricks/databricks-sdk-go"
7+
"github.com/databricks/databricks-sdk-go/service/catalog"
8+
"github.com/databricks/terraform-provider-databricks/common"
9+
)
10+
11+
func DataSourceExternalLocation() common.Resource {
12+
type ExternalLocationByID struct {
13+
Name string `json:"name"`
14+
ExternalLocation *catalog.ExternalLocationInfo `json:"external_location_info,omitempty" tf:"computed" `
15+
}
16+
return common.WorkspaceData(func(ctx context.Context, data *ExternalLocationByID, w *databricks.WorkspaceClient) error {
17+
location, err := w.ExternalLocations.GetByName(ctx, data.Name)
18+
if err != nil {
19+
return err
20+
}
21+
data.ExternalLocation = location
22+
return nil
23+
})
24+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package catalog
2+
3+
import (
4+
"testing"
5+
6+
"github.com/databricks/databricks-sdk-go/experimental/mocks"
7+
"github.com/databricks/databricks-sdk-go/service/catalog"
8+
"github.com/databricks/terraform-provider-databricks/qa"
9+
"github.com/stretchr/testify/mock"
10+
)
11+
12+
func TestExternalLocationDataVerify(t *testing.T) {
13+
qa.ResourceFixture{
14+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
15+
e := w.GetMockExternalLocationsAPI().EXPECT()
16+
e.GetByName(mock.Anything, "abc").Return(
17+
&catalog.ExternalLocationInfo{
18+
Name: "abc",
19+
Owner: "admin",
20+
CredentialName: "test",
21+
Url: "s3://test",
22+
ReadOnly: true,
23+
},
24+
nil)
25+
},
26+
Resource: DataSourceExternalLocation(),
27+
Read: true,
28+
NonWritable: true,
29+
ID: "_",
30+
HCL: `
31+
name = "abc"
32+
`,
33+
}.ApplyAndExpectData(t, map[string]any{
34+
"external_location_info.0.owner": "admin",
35+
"external_location_info.0.url": "s3://test",
36+
"external_location_info.0.credential_name": "test",
37+
"external_location_info.0.read_only": true,
38+
})
39+
}
40+
41+
func TestExternalLocationDataError(t *testing.T) {
42+
qa.ResourceFixture{
43+
Fixtures: qa.HTTPFailures,
44+
Resource: DataSourceExternalLocation(),
45+
Read: true,
46+
NonWritable: true,
47+
ID: "_",
48+
}.ExpectError(t, "i'm a teapot")
49+
}

catalog/data_external_locations.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package catalog
2+
3+
import (
4+
"context"
5+
6+
"github.com/databricks/databricks-sdk-go"
7+
"github.com/databricks/databricks-sdk-go/service/catalog"
8+
"github.com/databricks/terraform-provider-databricks/common"
9+
)
10+
11+
func DataSourceExternalLocations() common.Resource {
12+
type externalLocationsData struct {
13+
Names []string `json:"names,omitempty" tf:"computed"`
14+
}
15+
return common.WorkspaceData(func(ctx context.Context, data *externalLocationsData, w *databricks.WorkspaceClient) error {
16+
locations, err := w.ExternalLocations.ListAll(ctx, catalog.ListExternalLocationsRequest{})
17+
if err != nil {
18+
return err
19+
}
20+
data.Names = make([]string, 0, len(locations))
21+
for _, v := range locations {
22+
data.Names = append(data.Names, v.Name)
23+
}
24+
return nil
25+
})
26+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package catalog
2+
3+
import (
4+
"testing"
5+
6+
"github.com/databricks/databricks-sdk-go/experimental/mocks"
7+
"github.com/databricks/databricks-sdk-go/service/catalog"
8+
"github.com/databricks/terraform-provider-databricks/qa"
9+
"github.com/stretchr/testify/mock"
10+
)
11+
12+
func TestExternalLocationsData(t *testing.T) {
13+
qa.ResourceFixture{
14+
MockWorkspaceClientFunc: func(w *mocks.MockWorkspaceClient) {
15+
e := w.GetMockExternalLocationsAPI().EXPECT()
16+
e.ListAll(mock.Anything, catalog.ListExternalLocationsRequest{}).Return(
17+
[]catalog.ExternalLocationInfo{
18+
{Name: "a"}, {Name: "b"},
19+
},
20+
nil)
21+
},
22+
Resource: DataSourceExternalLocations(),
23+
Read: true,
24+
NonWritable: true,
25+
ID: "_",
26+
}.ApplyAndExpectData(t, map[string]any{
27+
"names": []interface{}{"a", "b"},
28+
})
29+
}
30+
31+
func TestExternalLocationsData_Error(t *testing.T) {
32+
qa.ResourceFixture{
33+
Fixtures: qa.HTTPFailures,
34+
Resource: DataSourceExternalLocations(),
35+
Read: true,
36+
NonWritable: true,
37+
ID: "_",
38+
}.ExpectError(t, "i'm a teapot")
39+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
subcategory: "Unity Catalog"
3+
---
4+
# databricks_external_location Data Source
5+
6+
-> **Note** This data source could be only used with workspace-level provider!
7+
8+
Retrieves details about a [databricks_external_location](../resources/external_location.md) that were created by Terraform or manually.
9+
10+
## Example Usage
11+
12+
Getting details of an existing external location in the metastore
13+
14+
```hcl
15+
data "databricks_external_location" "this" {
16+
name = "this"
17+
}
18+
19+
output "created_by" {
20+
value = data.databricks_external_location.this.created_by
21+
sensitive = false
22+
}
23+
```
24+
25+
## Argument Reference
26+
27+
* `name` - (Required) The name of the storage credential
28+
29+
## Attribute Reference
30+
31+
* `url` - Path URL in cloud storage, of the form: `s3://[bucket-host]/[bucket-dir]` (AWS), `abfss://[user]@[host]/[path]` (Azure), `gs://[bucket-host]/[bucket-dir]` (GCP).
32+
* `credential_name` - Name of the [databricks_storage_credential](storage_credential.md) to use with this external location.
33+
* `owner` - Username/groupname/sp application_id of the external location owner.
34+
* `comment` - User-supplied comment.
35+
* `read_only` - Indicates whether the external location is read-only.
36+
* `access_point` - The ARN of the s3 access point to use with the external location (AWS).
37+
* `encryption_details` - The options for Server-Side Encryption to be used by each Databricks s3 client when connecting to S3 cloud storage (AWS).
38+
39+
## Related Resources
40+
41+
The following resources are used in the same context:
42+
43+
* [databricks_external_locations](./external_locations.md) to get names of all external locations
44+
* [databricks_external_location](../resources/external_location.md) to manage external locations within Unity Catalog.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
subcategory: "Unity Catalog"
3+
---
4+
# databricks_external_locations Data Source
5+
6+
-> **Note** This data source could be only used with workspace-level provider!
7+
8+
Retrieves a list of [databricks_external_location](./external_location.md) objects, that were created by Terraform or manually, so that special handling could be applied.
9+
10+
## Example Usage
11+
12+
List all external locations in the metastore
13+
14+
```hcl
15+
data "databricks_external_locations" "all" {}
16+
17+
output "all_external_locations" {
18+
value = data.databricks_external_locations.all.names
19+
}
20+
```
21+
22+
## Attribute Reference
23+
24+
This data source exports the following attributes:
25+
26+
* `names` - List of names of [databricks_external_location](./external_location.md) in the metastore
27+
28+
## Related Resources
29+
30+
The following resources are used in the same context:
31+
32+
* [databricks_external_location](./external_location.md) to get information about a single external location
33+
* [databricks_external_location](../resources/external_location.md) to manage external locations within Unity Catalog.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package acceptance
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
8+
)
9+
10+
func TestUcAccDataSourceExternalLocations(t *testing.T) {
11+
unityWorkspaceLevel(t, step{
12+
Template: `
13+
data "databricks_external_locations" "this" {
14+
}`,
15+
Check: func(s *terraform.State) error {
16+
r, ok := s.RootModule().Resources["data.databricks_external_locations.this"]
17+
if !ok {
18+
return fmt.Errorf("data not found in state")
19+
}
20+
names := r.Primary.Attributes["names.#"]
21+
if names == "" {
22+
return fmt.Errorf("names are empty: %v", r.Primary.Attributes)
23+
}
24+
return nil
25+
},
26+
})
27+
}

provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ func DatabricksProvider() *schema.Provider {
6767
"databricks_dbfs_file": storage.DataSourceDbfsFile().ToResource(),
6868
"databricks_dbfs_file_paths": storage.DataSourceDbfsFilePaths().ToResource(),
6969
"databricks_directory": workspace.DataSourceDirectory().ToResource(),
70+
"databricks_external_location": catalog.DataSourceExternalLocation().ToResource(),
71+
"databricks_external_locations": catalog.DataSourceExternalLocations().ToResource(),
7072
"databricks_group": scim.DataSourceGroup().ToResource(),
7173
"databricks_instance_pool": pools.DataSourceInstancePool().ToResource(),
7274
"databricks_instance_profiles": aws.DataSourceInstanceProfiles().ToResource(),

0 commit comments

Comments
 (0)