Skip to content

Commit d67b357

Browse files
yordisnickfloyd
andauthored
fix: destroy the resource on drift (#1351)
* fix: destroy the resource on drift closes 749 * add secret drifting example --------- Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
1 parent 90c871e commit d67b357

3 files changed

Lines changed: 31 additions & 1 deletion

File tree

examples/secret-drifting/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
provider "github" {
2+
}
3+
4+
terraform {
5+
required_providers {
6+
github = {
7+
source = "integrations/github"
8+
}
9+
}
10+
}
11+
12+
resource "github_actions_organization_secret" "plaintext_secret" {
13+
secret_name = "test_plaintext_secret"
14+
plaintext_value = "123"
15+
visibility = "private"
16+
}
17+
18+
resource "github_actions_organization_secret" "encrypted_secret" {
19+
secret_name = "test_encrypted_secret"
20+
plaintext_value = "123"
21+
visibility = "private"
22+
destroy_on_drift = false
23+
}

github/resource_github_actions_organization_secret.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ func resourceGithubActionsOrganizationSecret() *schema.Resource {
7878
Computed: true,
7979
Description: "Date of 'actions_secret' update.",
8080
},
81+
"destroy_on_drift": {
82+
Type: schema.TypeBool,
83+
Default: true,
84+
Optional: true,
85+
},
8186
},
8287
}
8388
}
@@ -214,7 +219,8 @@ func resourceGithubActionsOrganizationSecretRead(d *schema.ResourceData, meta in
214219
// The only solution to enforce consistency between is to mark the resource
215220
// as deleted (unset the ID) in order to fix potential drift by recreating
216221
// the resource.
217-
if updatedAt, ok := d.GetOk("updated_at"); ok && updatedAt != secret.UpdatedAt.String() {
222+
destroyOnDrift := d.Get("destroy_on_drift").(bool)
223+
if updatedAt, ok := d.GetOk("updated_at"); ok && destroyOnDrift && updatedAt != secret.UpdatedAt.String() {
218224
log.Printf("[INFO] The secret %s has been externally updated in GitHub", d.Id())
219225
d.SetId("")
220226
} else if !ok {

github/resource_github_actions_organization_secret_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestAccGithubActionsOrganizationSecret(t *testing.T) {
2525
secret_name = "test_encrypted_secret"
2626
encrypted_value = "%s"
2727
visibility = "private"
28+
destroy_on_drift = false
2829
}
2930
`, secretValue, secretValue)
3031

0 commit comments

Comments
 (0)