Skip to content

Commit dc4f239

Browse files
committed
[Feature] Add databricks_secret write-only attributes
1 parent 8945a7b commit dc4f239

3 files changed

Lines changed: 115 additions & 3 deletions

File tree

common/resource.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

secrets/resource_secret.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/databricks/databricks-sdk-go/apierr"
99
"github.com/databricks/databricks-sdk-go/service/workspace"
1010
"github.com/databricks/terraform-provider-databricks/common"
11-
11+
"github.com/hashicorp/go-cty/cty"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1414
)
@@ -35,16 +35,41 @@ func readSecret(ctx context.Context, w *databricks.WorkspaceClient, scope string
3535
}
3636
}
3737

38+
func getStringValue(d *schema.ResourceData) (string, error) {
39+
woValue, diags := d.GetRawConfigAt(cty.GetAttrPath("string_value_wo"))
40+
if !diags.HasError() && woValue.Type().Equals(cty.String) && !woValue.IsNull() {
41+
return woValue.AsString(), nil
42+
}
43+
if v, ok := d.GetOk("string_value"); ok {
44+
return v.(string), nil
45+
}
46+
return "", fmt.Errorf("failed to get one of attributes `string_value_wo` or `string_value`")
47+
}
48+
3849
// ResourceSecret manages secrets
3950
func ResourceSecret() common.Resource {
4051
p := common.NewPairSeparatedID("scope", "key", "|||")
4152
s := map[string]*schema.Schema{
4253
"string_value": {
4354
Type: schema.TypeString,
4455
ValidateFunc: validation.StringIsNotEmpty,
45-
Required: true,
46-
ForceNew: true,
56+
Optional: true,
57+
Sensitive: true,
58+
ExactlyOneOf: []string{"string_value", "string_value_wo"},
59+
},
60+
"string_value_wo": {
61+
Type: schema.TypeString,
62+
ValidateFunc: validation.StringIsNotEmpty,
63+
Optional: true,
64+
WriteOnly: true,
4765
Sensitive: true,
66+
RequiredWith: []string{"string_value_wo_version"},
67+
ExactlyOneOf: []string{"string_value", "string_value_wo"},
68+
},
69+
"string_value_wo_version": {
70+
Type: schema.TypeInt,
71+
Optional: true,
72+
RequiredWith: []string{"string_value_wo"},
4873
},
4974
"scope": {
5075
Type: schema.TypeString,

secrets/resource_secret_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,90 @@ func TestResourceSecretCreate(t *testing.T) {
121121
assert.Equal(t, "foo|||bar", d.Id())
122122
}
123123

124+
func TestResourceSecretCreate_WriteOnlyValue(t *testing.T) {
125+
d, err := qa.ResourceFixture{
126+
Fixtures: []qa.HTTPFixture{
127+
{
128+
Method: "POST",
129+
Resource: "/api/2.0/secrets/put",
130+
ExpectedRequest: workspace.PutSecret{
131+
StringValue: "SparkIsTh3Be$t",
132+
Scope: "foo",
133+
Key: "bar",
134+
},
135+
},
136+
{
137+
Method: "GET",
138+
Resource: "/api/2.0/secrets/list?scope=foo",
139+
Response: workspace.ListSecretsResponse{
140+
Secrets: []workspace.SecretMetadata{
141+
{
142+
Key: "bar",
143+
LastUpdatedTimestamp: 12345678,
144+
},
145+
},
146+
},
147+
},
148+
},
149+
Resource: ResourceSecret(),
150+
State: map[string]any{
151+
"scope": "foo",
152+
"key": "bar",
153+
"string_value_wo": "SparkIsTh3Be$t",
154+
"string_value_wo_version": 1,
155+
},
156+
Create: true,
157+
}.Apply(t)
158+
assert.NoError(t, err)
159+
assert.Equal(t, "foo|||bar", d.Id())
160+
assert.Equal(t, "", d.Get("string_value"))
161+
}
162+
163+
func TestResourceSecretUpdate_WriteOnlyValueVersionChange(t *testing.T) {
164+
d, err := qa.ResourceFixture{
165+
Fixtures: []qa.HTTPFixture{
166+
{
167+
Method: "POST",
168+
Resource: "/api/2.0/secrets/put",
169+
ExpectedRequest: workspace.PutSecret{
170+
StringValue: "SparkIsTh3Be$t-v2",
171+
Scope: "foo",
172+
Key: "bar",
173+
},
174+
},
175+
{
176+
Method: "GET",
177+
Resource: "/api/2.0/secrets/list?scope=foo",
178+
Response: workspace.ListSecretsResponse{
179+
Secrets: []workspace.SecretMetadata{
180+
{
181+
Key: "bar",
182+
LastUpdatedTimestamp: 12345679,
183+
},
184+
},
185+
},
186+
},
187+
},
188+
Resource: ResourceSecret(),
189+
InstanceState: map[string]string{
190+
"scope": "foo",
191+
"key": "bar",
192+
"string_value_wo_version": "1",
193+
},
194+
State: map[string]any{
195+
"scope": "foo",
196+
"key": "bar",
197+
"string_value_wo": "SparkIsTh3Be$t-v2",
198+
"string_value_wo_version": 2,
199+
},
200+
Update: true,
201+
ID: "foo|||bar",
202+
}.Apply(t)
203+
assert.NoError(t, err)
204+
assert.Equal(t, "foo|||bar", d.Id())
205+
assert.Equal(t, 12345679, d.Get("last_updated_timestamp"))
206+
}
207+
124208
func TestResourceSecretCreate_Error(t *testing.T) {
125209
d, err := qa.ResourceFixture{
126210
Fixtures: []qa.HTTPFixture{

0 commit comments

Comments
 (0)