Skip to content

Commit

Permalink
Allow to override resource ID for specific data source
Browse files Browse the repository at this point in the history
The `DataResource` function now checks if the provided schema has the field with name `id`
and set its value as resource ID.  This will allow to have uniform references between
normal resources and data sources.

If this change is ok, then I'll proceed with other data sources.

See hashicorp/terraform-plugin-sdk#607 for reference.
  • Loading branch information
alexott committed Dec 29, 2022
1 parent 3754dd0 commit b086db4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
12 changes: 12 additions & 0 deletions common/reflect_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ func TestDataResource(t *testing.T) {
r := func() *schema.Resource {
type entry struct {
In string `json:"in"`
ID string `json:"id,omitempty" tf:"computed"`
Out string `json:"out,omitempty" tf:"computed"`
}
return DataResource(entry{}, func(ctx context.Context, e any, c *DatabricksClient) error {
Expand All @@ -594,6 +595,9 @@ func TestDataResource(t *testing.T) {
if dto.In == "fail" {
return fmt.Errorf("happens")
}
if dto.In == "id" {
dto.ID = "abc"
}
return nil
})
}()
Expand All @@ -607,6 +611,14 @@ func TestDataResource(t *testing.T) {
assert.Len(t, diags, 0)
assert.Equal(t, "out: test", d.Get("out"))

d = r.TestResourceData()
d.Set("in", "id")
diags = r.ReadContext(context.Background(), d, &DatabricksClient{})
assert.Len(t, diags, 0)
assert.Equal(t, "out: id", d.Get("out"))
assert.Equal(t, "abc", d.Id())

d = r.TestResourceData()
d.Set("in", "fail")
diags = r.ReadContext(context.Background(), d, &DatabricksClient{})
assert.Len(t, diags, 1)
Expand Down
6 changes: 5 additions & 1 deletion common/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ func DataResource(sc any, read func(context.Context, any, *DatabricksClient) err
diags = diag.FromErr(err)
}
StructToData(ptr.Elem().Interface(), s, d)
d.SetId("_")
if _, ok := s["id"]; ok {
d.SetId(d.Get("id").(string))
} else {
d.SetId("_")
}
return
},
}
Expand Down
5 changes: 3 additions & 2 deletions docs/data-sources/service_principal.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ data "databricks_service_principal" "spn" {
resource "databricks_group_member" "my_member_a" {
group_id = data.databricks_group.admins.id
member_id = data.databricks_service_principal.spn.sp_id
member_id = data.databricks_service_principal.spn.id
}
```

Expand All @@ -37,7 +37,8 @@ Data source allows you to pick service principals by the following attributes

Data source exposes the following attributes:

- `sp_id` - The id of the service principal.
- `id` - The id of the service principal.
- `sp_id` - The id of the service principal (deprecated, kept for compatibility with earlier versions).
- `external_id` - ID of the service principal in an external identity provider.
- `display_name` - Display name of the [service principal](../resources/service_principal.md), e.g. `Foo SPN`.
- `home` - Home folder of the [service principal](../resources/service_principal.md), e.g. `/Users/11111111-2222-3333-4444-555666777888`.
Expand Down
2 changes: 2 additions & 0 deletions scim/data_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func DataSourceServicePrincipal() *schema.Resource {
ApplicationID string `json:"application_id,omitempty" tf:"computed"`
DisplayName string `json:"display_name,omitempty" tf:"computed"`
SpID string `json:"sp_id,omitempty" tf:"computed"`
ID string `json:"id,omitempty" tf:"computed"`
Home string `json:"home,omitempty" tf:"computed"`
Repos string `json:"repos,omitempty" tf:"computed"`
Active bool `json:"active,omitempty" tf:"computed"`
Expand All @@ -36,6 +37,7 @@ func DataSourceServicePrincipal() *schema.Resource {
response.ExternalID = sp.ExternalID
response.Active = sp.Active
response.SpID = sp.ID
response.ID = sp.ID
return nil
})
}
3 changes: 2 additions & 1 deletion scim/data_service_principal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ func TestDataServicePrincipalReadByAppId(t *testing.T) {
HCL: `application_id = "abc"`,
Read: true,
NonWritable: true,
ID: "_",
ID: "abc",
}.ApplyAndExpectData(t, map[string]any{
"sp_id": "abc",
"id": "abc",
"application_id": "abc",
"display_name": "Example Service Principal",
"active": true,
Expand Down

0 comments on commit b086db4

Please sign in to comment.