Skip to content

Commit 6246208

Browse files
authored
Added databricks_instance_pool data source (#1907)
1 parent d2eafbc commit 6246208

File tree

4 files changed

+175
-0
lines changed

4 files changed

+175
-0
lines changed

docs/data-sources/instance_pool.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
subcategory: "Compute"
3+
---
4+
5+
# databricks_instance_pool Data Source
6+
7+
-> **Note** If you have a fully automated setup with workspaces created by [databricks_mws_workspaces](../resources/mws_workspaces.md) or [azurerm_databricks_workspace](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/databricks_workspace), please make sure to add [depends_on attribute](../index.md#data-resources-and-authentication-is-not-configured-errors) in order to prevent _authentication is not configured for provider_ errors.
8+
9+
Retrieves information about [databricks_instance_pool](../resources/instance_pool.md).
10+
11+
## Example Usage
12+
13+
Referring to an instance pool by name:
14+
15+
```hcl
16+
data "databricks_instance_pool" "Pool" {
17+
name = "All spot"
18+
}
19+
20+
resource "databricks_cluster" "my_cluster" {
21+
instance_pool_id = data.databricks_instance_pool.pool.id
22+
...
23+
}
24+
```
25+
26+
## Argument Reference
27+
28+
Data source allows you to pick instance pool by the following attribute
29+
30+
- `name` - Name of the instance pool. The instance pool must exist before this resource can be planned.
31+
32+
## Attribute Reference
33+
34+
Data source exposes the following attributes:
35+
36+
- `id` - The id of the instance pool.
37+
- `pool_info` - block describing instance pool and its state. Check documentation for [databricks_instance_pool](../resources/instance_pool.md) for a list of exposed attributes.
38+

pools/data_instance_pool.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package pools
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/databricks/terraform-provider-databricks/common"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func getPool(poolsAPI InstancePoolsAPI, name string) (*InstancePoolAndStats, error) {
13+
poolList, err := poolsAPI.List()
14+
if err != nil {
15+
return nil, err
16+
}
17+
for _, pool := range poolList.InstancePools {
18+
if pool.InstancePoolName == name {
19+
return &pool, nil
20+
}
21+
}
22+
23+
return nil, fmt.Errorf("instance pool '%s' doesn't exist", name)
24+
}
25+
26+
// DataSourceInstancePool returns information about instance pool specified by name
27+
func DataSourceInstancePool() *schema.Resource {
28+
type poolDetails struct {
29+
Name string `json:"name"`
30+
Attributes *InstancePoolAndStats `json:"pool_info,omitempty" tf:"computed"`
31+
}
32+
s := common.StructToSchema(poolDetails{}, nil)
33+
return &schema.Resource{
34+
Schema: s,
35+
ReadContext: func(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
36+
name := d.Get("name").(string)
37+
poolsAPI := NewInstancePoolsAPI(ctx, m)
38+
pool, err := getPool(poolsAPI, name)
39+
if err != nil {
40+
return diag.FromErr(err)
41+
}
42+
d.SetId(pool.InstancePoolID)
43+
err = common.StructToData(poolDetails{Name: name, Attributes: pool}, s, d)
44+
return diag.FromErr(err)
45+
},
46+
}
47+
}

pools/data_instance_pool_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package pools
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/databricks/terraform-provider-databricks/common"
8+
"github.com/databricks/terraform-provider-databricks/qa"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
func TestDataSourceInstnacePool(t *testing.T) {
14+
d, err := qa.ResourceFixture{
15+
Fixtures: []qa.HTTPFixture{
16+
{
17+
Method: "GET",
18+
Resource: "/api/2.0/instance-pools/list",
19+
Response: InstancePoolList{
20+
InstancePools: []InstancePoolAndStats{
21+
{
22+
InstancePoolID: "abc",
23+
InstancePoolName: "pool",
24+
NodeTypeID: "node-type",
25+
},
26+
},
27+
},
28+
},
29+
},
30+
Read: true,
31+
NonWritable: true,
32+
Resource: DataSourceInstancePool(),
33+
ID: ".",
34+
State: map[string]any{
35+
"name": "pool",
36+
},
37+
}.Apply(t)
38+
require.NoError(t, err)
39+
assert.Equal(t, "abc", d.Id())
40+
assert.NotNil(t, d.Get("pool_info"))
41+
assert.Equal(t, "node-type", d.Get("pool_info.0.node_type_id").(string))
42+
}
43+
44+
func TestDataSourceInstancePoolsGetPool(t *testing.T) {
45+
qa.HTTPFixturesApply(t, []qa.HTTPFixture{
46+
{
47+
Method: "GET",
48+
Resource: "/api/2.0/instance-pools/list",
49+
Status: 404,
50+
Response: common.APIError{
51+
Message: "searching_error",
52+
},
53+
},
54+
{
55+
Method: "GET",
56+
Resource: "/api/2.0/instance-pools/list",
57+
Response: InstancePoolList{},
58+
},
59+
}, func(ctx context.Context, client *common.DatabricksClient) {
60+
poolsAPI := NewInstancePoolsAPI(ctx, client)
61+
62+
_, err := getPool(poolsAPI, "searching_error")
63+
assert.EqualError(t, err, "searching_error")
64+
65+
_, err = getPool(poolsAPI, "unknown")
66+
assert.EqualError(t, err, "instance pool 'unknown' doesn't exist")
67+
})
68+
}
69+
70+
func TestDataSourceInstnacePool_NotFound(t *testing.T) {
71+
qa.ResourceFixture{
72+
Fixtures: []qa.HTTPFixture{
73+
{
74+
Method: "GET",
75+
Resource: "/api/2.0/instance-pools/list",
76+
Response: InstancePoolList{
77+
InstancePools: []InstancePoolAndStats{},
78+
},
79+
},
80+
},
81+
Read: true,
82+
NonWritable: true,
83+
Resource: DataSourceInstancePool(),
84+
ID: ".",
85+
State: map[string]any{
86+
"name": "Unknown",
87+
},
88+
}.ExpectError(t, "instance pool 'Unknown' doesn't exist")
89+
}

provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func DatabricksProvider() *schema.Provider {
4848
"databricks_dbfs_file_paths": storage.DataSourceDbfsFilePaths(),
4949
"databricks_directory": workspace.DataSourceDirectory(),
5050
"databricks_group": scim.DataSourceGroup(),
51+
"databricks_instance_pool": pools.DataSourceInstancePool(),
5152
"databricks_jobs": jobs.DataSourceJobs(),
5253
"databricks_job": jobs.DataSourceJob(),
5354
"databricks_mws_workspaces": mws.DataSourceMwsWorkspaces(),

0 commit comments

Comments
 (0)