-
Notifications
You must be signed in to change notification settings - Fork 384
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
Add databricks_instance_pool
data source
#1907
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
subcategory: "Compute" | ||
--- | ||
|
||
# databricks_instance_pool Data Source | ||
|
||
-> **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. | ||
|
||
Retrieves information about [databricks_instance_pool](../resources/instance_pool.md). | ||
|
||
## Example Usage | ||
|
||
Referring to an instance pool by name: | ||
|
||
```hcl | ||
data "databricks_instance_pool" "Pool" { | ||
name = "All spot" | ||
} | ||
|
||
resource "databricks_cluster" "my_cluster" { | ||
instance_pool_id = data.databricks_instance_pool.pool.id | ||
... | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
Data source allows you to pick instance pool by the following attribute | ||
|
||
- `name` - Name of the instance pool. The instance pool must exist before this resource can be planned. | ||
|
||
## Attribute Reference | ||
|
||
Data source exposes the following attributes: | ||
|
||
- `id` - The id of the instance pool. | ||
- `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. | ||
|
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,47 @@ | ||
package pools | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/databricks/terraform-provider-databricks/common" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func getPool(poolsAPI InstancePoolsAPI, name string) (*InstancePoolAndStats, error) { | ||
poolList, err := poolsAPI.List() | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, pool := range poolList.InstancePools { | ||
if pool.InstancePoolName == name { | ||
return &pool, nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("instance pool '%s' doesn't exist", name) | ||
} | ||
|
||
// DataSourceInstancePool returns information about instance pool specified by name | ||
func DataSourceInstancePool() *schema.Resource { | ||
type poolDetails struct { | ||
Name string `json:"name"` | ||
Attributes *InstancePoolAndStats `json:"pool_info,omitempty" tf:"computed"` | ||
} | ||
s := common.StructToSchema(poolDetails{}, nil) | ||
return &schema.Resource{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why common.DataSource is not a fit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just had this code before DataSource was introduced, simply picked up it from stash. Also, there will be not so much difference in the code size at the end |
||
Schema: s, | ||
ReadContext: func(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||
name := d.Get("name").(string) | ||
poolsAPI := NewInstancePoolsAPI(ctx, m) | ||
pool, err := getPool(poolsAPI, name) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
d.SetId(pool.InstancePoolID) | ||
err = common.StructToData(poolDetails{Name: name, Attributes: pool}, s, d) | ||
return diag.FromErr(err) | ||
}, | ||
} | ||
} |
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,89 @@ | ||
package pools | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/databricks/terraform-provider-databricks/common" | ||
"github.com/databricks/terraform-provider-databricks/qa" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDataSourceInstnacePool(t *testing.T) { | ||
d, err := qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/instance-pools/list", | ||
Response: InstancePoolList{ | ||
InstancePools: []InstancePoolAndStats{ | ||
{ | ||
InstancePoolID: "abc", | ||
InstancePoolName: "pool", | ||
NodeTypeID: "node-type", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
Read: true, | ||
NonWritable: true, | ||
Resource: DataSourceInstancePool(), | ||
ID: ".", | ||
State: map[string]any{ | ||
"name": "pool", | ||
}, | ||
}.Apply(t) | ||
require.NoError(t, err) | ||
assert.Equal(t, "abc", d.Id()) | ||
assert.NotNil(t, d.Get("pool_info")) | ||
assert.Equal(t, "node-type", d.Get("pool_info.0.node_type_id").(string)) | ||
} | ||
|
||
func TestDataSourceInstancePoolsGetPool(t *testing.T) { | ||
qa.HTTPFixturesApply(t, []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/instance-pools/list", | ||
Status: 404, | ||
Response: common.APIError{ | ||
Message: "searching_error", | ||
}, | ||
}, | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/instance-pools/list", | ||
Response: InstancePoolList{}, | ||
}, | ||
}, func(ctx context.Context, client *common.DatabricksClient) { | ||
poolsAPI := NewInstancePoolsAPI(ctx, client) | ||
|
||
_, err := getPool(poolsAPI, "searching_error") | ||
assert.EqualError(t, err, "searching_error") | ||
|
||
_, err = getPool(poolsAPI, "unknown") | ||
assert.EqualError(t, err, "instance pool 'unknown' doesn't exist") | ||
}) | ||
} | ||
|
||
func TestDataSourceInstnacePool_NotFound(t *testing.T) { | ||
qa.ResourceFixture{ | ||
Fixtures: []qa.HTTPFixture{ | ||
{ | ||
Method: "GET", | ||
Resource: "/api/2.0/instance-pools/list", | ||
Response: InstancePoolList{ | ||
InstancePools: []InstancePoolAndStats{}, | ||
}, | ||
}, | ||
}, | ||
Read: true, | ||
NonWritable: true, | ||
Resource: DataSourceInstancePool(), | ||
ID: ".", | ||
State: map[string]any{ | ||
"name": "Unknown", | ||
}, | ||
}.ExpectError(t, "instance pool 'Unknown' doesn't exist") | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this may fail make fmt docs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree, this need to be fixed in few other places as well