Skip to content

Commit

Permalink
For application key resources, only update the key attribute if prese…
Browse files Browse the repository at this point in the history
…nt in API response
  • Loading branch information
jackakeller committed Jul 10, 2024
1 parent 746f450 commit 1eb5e9e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
4 changes: 3 additions & 1 deletion datadog/fwprovider/resource_datadog_application_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,5 +160,7 @@ func (r *applicationKeyResource) buildDatadogApplicationKeyUpdateV2Struct(state
func (r *applicationKeyResource) updateState(state *applicationKeyResourceModel, applicationKeyData *datadogV2.FullApplicationKey) {
applicationKeyAttributes := applicationKeyData.GetAttributes()
state.Name = types.StringValue(applicationKeyAttributes.GetName())
state.Key = types.StringValue(applicationKeyAttributes.GetKey())
if applicationKeyAttributes.HasKey() {
state.Key = types.StringValue(applicationKeyAttributes.GetKey())
}
}
41 changes: 31 additions & 10 deletions datadog/tests/resource_datadog_application_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestAccDatadogApplicationKey_Update(t *testing.T) {
applicationKeyName := uniqueEntityName(ctx, t)
applicationKeyNameUpdate := applicationKeyName + "-2"
resourceName := "datadog_application_key.foo"
var keyValue string

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand All @@ -32,15 +33,36 @@ func TestAccDatadogApplicationKey_Update(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckDatadogApplicationKeyExists(providers.frameworkProvider, resourceName),
resource.TestCheckResourceAttr(resourceName, "name", applicationKeyName),
testAccCheckDatadogApplicationKeyValueMatches(providers.frameworkProvider, resourceName),
resource.TestCheckResourceAttrSet(resourceName, "key"),
func(s *terraform.State) error {
resource, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Resource not found: %s", resourceName)
}
// Store the key value for later comparison after update
keyValue = resource.Primary.Attributes["key"]
return nil
},
),
},
{
Config: testAccCheckDatadogApplicationKeyConfigRequired(applicationKeyNameUpdate),
Check: resource.ComposeTestCheckFunc(
testAccCheckDatadogApplicationKeyExists(providers.frameworkProvider, resourceName),
resource.TestCheckResourceAttr(resourceName, "name", applicationKeyNameUpdate),
testAccCheckDatadogApplicationKeyValueMatches(providers.frameworkProvider, resourceName),
testAccCheckDatadogApplicationKeyNameMatches(providers.frameworkProvider, resourceName),
resource.TestCheckResourceAttrSet(resourceName, "key"),
func(s *terraform.State) error {
resource, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Resource not found: %s", resourceName)
}
stateKeyValue := resource.Primary.Attributes["key"]
if stateKeyValue != keyValue {
return fmt.Errorf("application key value %s does not match expected value %s", stateKeyValue, keyValue)
}
return nil
},
),
},
},
Expand Down Expand Up @@ -100,29 +122,28 @@ func datadogApplicationKeyExistsHelper(ctx context.Context, s *terraform.State,
return nil
}

func testAccCheckDatadogApplicationKeyValueMatches(accProvider *fwprovider.FrameworkProvider, n string) resource.TestCheckFunc {
func testAccCheckDatadogApplicationKeyNameMatches(accProvider *fwprovider.FrameworkProvider, n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
apiInstances := accProvider.DatadogApiInstances
auth := accProvider.Auth

if err := datadogApplicationKeyValueMatches(auth, s, apiInstances, n); err != nil {
if err := datadogApplicationKeyNameMatches(auth, s, apiInstances, n); err != nil {
return err
}
return nil
}
}

func datadogApplicationKeyValueMatches(ctx context.Context, s *terraform.State, apiInstances *utils.ApiInstances, name string) error {
func datadogApplicationKeyNameMatches(ctx context.Context, s *terraform.State, apiInstances *utils.ApiInstances, name string) error {
primaryResource := s.RootModule().Resources[name].Primary
id := primaryResource.ID
expectedKey := primaryResource.Attributes["key"]
resp, _, err := apiInstances.GetKeyManagementApiV2().GetCurrentUserApplicationKey(ctx, id)
if err != nil {
return fmt.Errorf("received an error retrieving application key %s", err)
}
actualKey := resp.Data.Attributes.GetKey()
if expectedKey != actualKey {
return fmt.Errorf("application key value does not match")
keyName := resp.Data.Attributes.GetName()
stateKeyName := primaryResource.Attributes["name"]
if keyName != stateKeyName {
return fmt.Errorf("application key name %s in state does not match expected name %s from API", stateKeyName, keyName)
}
return nil
}
Expand Down

0 comments on commit 1eb5e9e

Please sign in to comment.