Skip to content

Commit

Permalink
Add deprecation message to datadog_application_key data source and er…
Browse files Browse the repository at this point in the history
…ror if the application key is not provided from the datadog API.
  • Loading branch information
jackakeller committed Jul 10, 2024
1 parent 746f450 commit cd1adaf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
24 changes: 20 additions & 4 deletions datadog/fwprovider/data_source_datadog_application_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (d *applicationKeyDataSource) Metadata(_ context.Context, req datasource.Me
// Schema implements datasource.DataSource.
func (d *applicationKeyDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Use this data source to retrieve information about an existing application key.",
Description: "Use this data source to retrieve information about an existing application key. Deprecated. This will be removed in a future release with prior notice. Securely store your application keys using a secret management system or use the datadog_application_key resource to manage application keys in your Datadog account.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Id for Application Key.",
Expand All @@ -57,6 +57,7 @@ func (d *applicationKeyDataSource) Schema(_ context.Context, req datasource.Sche
Sensitive: true,
},
},
DeprecationMessage: "The datadog_application_key data source is deprecated and will be removed in a future release with prior notice. Securely store your application key using a secret management system or use the datadog_application_key resource to manage application keys in your Datadog account.",
}
}

Expand All @@ -80,7 +81,9 @@ func (d *applicationKeyDataSource) Read(ctx context.Context, req datasource.Read
return
}
apiKeyData := ddResp.GetData()
d.updateState(&state, &apiKeyData)
if !d.checkAPIDeprecated(&apiKeyData, resp) {
d.updateState(&state, &apiKeyData)
}
} else if !state.Name.IsNull() {
optionalParams := datadogV2.NewListCurrentUserApplicationKeysOptionalParameters()
optionalParams.WithFilter(state.Name.ValueString())
Expand Down Expand Up @@ -122,7 +125,9 @@ func (d *applicationKeyDataSource) Read(ctx context.Context, req datasource.Read
resp.Diagnostics.AddError("your query returned no exact matches, please try a less specific search criteria", "")
return
}
d.updateState(&state, &applicationKeyData)
if !d.checkAPIDeprecated(&applicationKeyData, resp) {
d.updateState(&state, &applicationKeyData)
}
} else {
id := applicationKeysData[0].GetId()
applicationKeyResponse, _, err := d.Api.GetCurrentUserApplicationKey(d.Auth, id)
Expand All @@ -131,7 +136,9 @@ func (d *applicationKeyDataSource) Read(ctx context.Context, req datasource.Read
return
}
applicationKeyFullData := applicationKeyResponse.GetData()
d.updateState(&state, &applicationKeyFullData)
if !d.checkAPIDeprecated(&applicationKeyFullData, resp) {
d.updateState(&state, &applicationKeyFullData)
}
}
} else {
resp.Diagnostics.AddError("missing id or name parameter", "")
Expand All @@ -148,3 +155,12 @@ func (r *applicationKeyDataSource) updateState(state *applicationKeyDataSourceMo
state.Name = types.StringValue(applicationKeyAttributes.GetName())
state.Key = types.StringValue(applicationKeyAttributes.GetKey())
}

func (r *applicationKeyDataSource) checkAPIDeprecated(applicationKeyData *datadogV2.FullApplicationKey, resp *datasource.ReadResponse) bool {
applicationKeyAttributes := applicationKeyData.GetAttributes()
if !applicationKeyAttributes.HasKey() {
resp.Diagnostics.AddError("Deprecated", "The datadog_application_key data source is deprecated and will be removed in a future release. Securely store your application key using a secret management system or use the datadog_application_key resource to manage application keys in your Datadog account")
return true
}
return false
}
7 changes: 0 additions & 7 deletions datadog/tests/data_source_datadog_application_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ func TestAccDatadogApplicationKeyDatasource_matchId(t *testing.T) {
ctx, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)

applicationKeyName := uniqueEntityName(ctx, t)
nonEmptyStringRegex := `[\S\s]+[\S]+`
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: accProviders,
Expand All @@ -30,7 +29,6 @@ func TestAccDatadogApplicationKeyDatasource_matchId(t *testing.T) {
resource.TestCheckResourceAttr("datadog_application_key.app_key_1", "name", fmt.Sprintf("%s 1", applicationKeyName)),
resource.TestCheckResourceAttr("datadog_application_key.app_key_2", "name", fmt.Sprintf("%s 2", applicationKeyName)),
resource.TestCheckResourceAttr("data.datadog_application_key.app_key", "name", fmt.Sprintf("%s 1", applicationKeyName)),
resource.TestMatchResourceAttr("data.datadog_application_key.app_key", "key", regexp.MustCompile(nonEmptyStringRegex)),
resource.TestCheckResourceAttrSet("data.datadog_application_key.app_key", "id"),
),
},
Expand All @@ -46,7 +44,6 @@ func TestAccDatadogApplicationKeyDatasource_matchName(t *testing.T) {
ctx, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
applicationKeyName := uniqueEntityName(ctx, t)

nonEmptyStringRegex := `[\S\s]+[\S]+`
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: accProviders,
Expand All @@ -59,7 +56,6 @@ func TestAccDatadogApplicationKeyDatasource_matchName(t *testing.T) {
resource.TestCheckResourceAttr("datadog_application_key.app_key_1", "name", fmt.Sprintf("%s 1", applicationKeyName)),
resource.TestCheckResourceAttr("datadog_application_key.app_key_2", "name", fmt.Sprintf("%s 2", applicationKeyName)),
resource.TestCheckResourceAttr("data.datadog_application_key.app_key", "name", fmt.Sprintf("%s 1", applicationKeyName)),
resource.TestMatchResourceAttr("data.datadog_application_key.app_key", "key", regexp.MustCompile(nonEmptyStringRegex)),
resource.TestCheckResourceAttrSet("data.datadog_application_key.app_key", "id"),
),
},
Expand All @@ -70,7 +66,6 @@ func TestAccDatadogApplicationKeyDatasource_matchName(t *testing.T) {
resource.TestCheckResourceAttr("datadog_application_key.app_key_1", "name", applicationKeyName),
resource.TestCheckResourceAttr("datadog_application_key.app_key_2", "name", fmt.Sprintf("%s 2", applicationKeyName)),
resource.TestCheckResourceAttr("data.datadog_application_key.app_key", "name", applicationKeyName),
resource.TestMatchResourceAttr("data.datadog_application_key.app_key", "key", regexp.MustCompile(nonEmptyStringRegex)),
resource.TestCheckResourceAttrSet("data.datadog_application_key.app_key", "id"),
),
},
Expand All @@ -86,7 +81,6 @@ func TestAccDatadogApplicationKeyDatasource_exactMatchName(t *testing.T) {
ctx, providers, accProviders := testAccFrameworkMuxProviders(context.Background(), t)
applicationKeyName := uniqueEntityName(ctx, t)

nonEmptyStringRegex := `[\S\s]+[\S]+`
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV5ProviderFactories: accProviders,
Expand All @@ -99,7 +93,6 @@ func TestAccDatadogApplicationKeyDatasource_exactMatchName(t *testing.T) {
resource.TestCheckResourceAttr("datadog_application_key.app_key_1", "name", applicationKeyName),
resource.TestCheckResourceAttr("datadog_application_key.app_key_2", "name", fmt.Sprintf("%s 2", applicationKeyName)),
resource.TestCheckResourceAttr("data.datadog_application_key.app_key", "name", applicationKeyName),
resource.TestMatchResourceAttr("data.datadog_application_key.app_key", "key", regexp.MustCompile(nonEmptyStringRegex)),
resource.TestCheckResourceAttrSet("data.datadog_application_key.app_key", "id"),
),
},
Expand Down
4 changes: 2 additions & 2 deletions docs/data-sources/application_key.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "datadog_application_key Data Source - terraform-provider-datadog"
subcategory: ""
description: |-
Use this data source to retrieve information about an existing application key.
Use this data source to retrieve information about an existing application key. Deprecated. This will be removed in a future release with prior notice. Securely store your application keys using a secret management system or use the datadogapplicationkey resource to manage application keys in your Datadog account.
---

# datadog_application_key (Data Source)

Use this data source to retrieve information about an existing application key.
Use this data source to retrieve information about an existing application key. Deprecated. This will be removed in a future release with prior notice. Securely store your application keys using a secret management system or use the datadog_application_key resource to manage application keys in your Datadog account.

## Example Usage

Expand Down

0 comments on commit cd1adaf

Please sign in to comment.