Skip to content

Commit e56310b

Browse files
hashimoonctrombley
andauthored
[IPL-7245] Fix OAuth Token ID updates not registering (#1631)
* [IPL-7245] Ensure `oauth_token_id` updates register when performing a `terraform apply` This resolves the issue where when updating the `oauth_token_id`, `terraform apply` would have to be performed twice before to confirm the change in the state file. The first apply would perform the change on the server but would not register the change in the state file as expected. * tests: use muxed provider factory * chore: update changelog --------- Co-authored-by: Chris Trombley <[email protected]>
1 parent c59cc51 commit e56310b

File tree

5 files changed

+95
-12
lines changed

5 files changed

+95
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ FEATURES:
1313
* **New Ephemeral Resource:** `agent_token` is a new ephemeral
1414
resource for creating and managing agent tokens, by @uturunku1 ([#1627](https://github.com/hashicorp/terraform-provider-tfe/pull/1627))
1515

16+
BUG FIXES:
17+
* `r/tfe_oauth_client`: Ensure `oauth_token_id` updates register when performing a `terraform apply`, by @hashimoon [#1631](https://github.com/hashicorp/terraform-provider-tfe/pull/1631)
18+
1619
ENHANCEMENTS:
1720

1821
* resource/tfe_variable: Add `value_wo` write-only attribute, by @uturunku ([#1639](https://github.com/hashicorp/terraform-provider-tfe/pull/1639))

docs/testing.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ these values with the environment variables specified below:
3636

3737
##### Optional:
3838
1. `TFE_USER1` and `TFE_USER2`: The usernames of two pre-existing users on the HCP Terraform or Terraform Enterprise instance being used for testing. Required for running team membership tests.
39-
2. `GITHUB_TOKEN` - [GitHub personal access token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). Used to establish a VCS provider connection.
40-
3. `GITHUB_POLICY_SET_IDENTIFIER` - GitHub policy set repository identifier in the format `username/repository`. Required for running policy set tests.
41-
4. `GITHUB_POLICY_SET_BRANCH`: A GitHub branch for the repository specified by `GITHUB_POLICY_SET_IDENTIFIER`. Required for running policy set tests.
42-
5. `GITHUB_POLICY_SET_PATH`: A GitHub subdirectory for the repository specified by `GITHUB_POLICY_SET_IDENTIFIER`. Required for running policy set tests.
43-
6. `GITHUB_REGISTRY_MODULE_IDENTIFIER` - GitHub registry module repository identifier in the format `username/repository`. Required for running registry module tests.
44-
7. `GITHUB_WORKSPACE_IDENTIFIER` - GitHub workspace repository identifier in the format `username/repository`. Required for running workspace tests.
45-
8. `GITHUB_WORKSPACE_BRANCH`: A GitHub branch for the repository specified by `GITHUB_WORKSPACE_IDENTIFIER`. Required for running workspace tests.
46-
9. `ENABLE_TFE` - Some tests cover features available only in HCP Terraform. To skip these tests when running against a Terraform Enterprise instance, set `ENABLE_TFE=1`.
47-
10. `RUN_TASKS_URL` - External URL to use for testing Run Tasks operations, for example `RUN_TASKS_URL=http://somewhere.local:8080/pass`. Required for running run tasks tests.
48-
11. `RUN_TASKS_HMAC` - The optional HMAC Key that should be used for Run Task operations. The default is no key.
49-
12. `GITHUB_APP_INSTALLATION_ID` - GitHub App installation internal id in the format `ghain-xxxxxxx`. Required for running any tests that use GitHub App VCS (workspace, policy sets, registry module).
50-
13. `GITHUB_APP_INSTALLATION_NAME` - GitHub App installation name. Required for running tfe_github_app_installation data source test.
39+
1. `GITHUB_TOKEN` - [GitHub personal access token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). Used to establish a VCS provider connection.
40+
1. `GITHUB_TOKEN2` - [GitHub personal access token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line). Used to establish a secondary VCS provider connection.
41+
1. `GITHUB_POLICY_SET_IDENTIFIER` - GitHub policy set repository identifier in the format `username/repository`. Required for running policy set tests.
42+
1. `GITHUB_POLICY_SET_BRANCH`: A GitHub branch for the repository specified by `GITHUB_POLICY_SET_IDENTIFIER`. Required for running policy set tests.
43+
1. `GITHUB_POLICY_SET_PATH`: A GitHub subdirectory for the repository specified by `GITHUB_POLICY_SET_IDENTIFIER`. Required for running policy set tests.
44+
1. `GITHUB_REGISTRY_MODULE_IDENTIFIER` - GitHub registry module repository identifier in the format `username/repository`. Required for running registry module tests.
45+
1. `GITHUB_WORKSPACE_IDENTIFIER` - GitHub workspace repository identifier in the format `username/repository`. Required for running workspace tests.
46+
1. `GITHUB_WORKSPACE_BRANCH`: A GitHub branch for the repository specified by `GITHUB_WORKSPACE_IDENTIFIER`. Required for running workspace tests.
47+
1. `ENABLE_TFE` - Some tests cover features available only in HCP Terraform. To skip these tests when running against a Terraform Enterprise instance, set `ENABLE_TFE=1`.
48+
1. `RUN_TASKS_URL` - External URL to use for testing Run Tasks operations, for example `RUN_TASKS_URL=http://somewhere.local:8080/pass`. Required for running run tasks tests.
49+
1. `RUN_TASKS_HMAC` - The optional HMAC Key that should be used for Run Task operations. The default is no key.
50+
1. `GITHUB_APP_INSTALLATION_ID` - GitHub App installation internal id in the format `ghain-xxxxxxx`. Required for running any tests that use GitHub App VCS (workspace, policy sets, registry module).
51+
1. `GITHUB_APP_INSTALLATION_NAME` - GitHub App installation name. Required for running tfe_github_app_installation data source test.
5152

5253
**Note:** In order to run integration tests for **Paid** features you will need a token `TFE_TOKEN` with HCP Terraform or Terraform Enterprise administrator privileges, otherwise the attempt to upgrade an organization's feature set will fail.
5354

internal/provider/provider_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ func init() {
264264
envGithubPolicySetPath = os.Getenv("GITHUB_POLICY_SET_PATH")
265265
envGithubRegistryModuleIdentifer = os.Getenv("GITHUB_REGISTRY_MODULE_IDENTIFIER")
266266
envGithubToken = os.Getenv("GITHUB_TOKEN")
267+
envGithubToken2 = os.Getenv("GITHUB_TOKEN2")
267268
envGithubAppInstallationID = os.Getenv("GITHUB_APP_INSTALLATION_ID")
268269
envGithubAppInstallationName = os.Getenv("GITHUB_APP_INSTALLATION_NAME")
269270
envGithubWorkspaceIdentifier = os.Getenv("GITHUB_WORKSPACE_IDENTIFIER")
@@ -277,6 +278,7 @@ var envGithubPolicySetBranch string
277278
var envGithubPolicySetPath string
278279
var envGithubRegistryModuleIdentifer string
279280
var envGithubToken string
281+
var envGithubToken2 string
280282
var envGithubAppInstallationID string
281283
var envGithubAppInstallationName string
282284
var envGithubWorkspaceIdentifier string

internal/provider/resource_tfe_oauth_client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ func resourceTFEOAuthClientCreate(d *schema.ResourceData, meta interface{}) erro
181181

182182
d.SetId(oc.ID)
183183

184+
if len(oc.OAuthTokens) > 0 {
185+
d.Set("oauth_token_id", oc.OAuthTokens[0].ID)
186+
} else {
187+
d.Set("oauth_token_id", "")
188+
}
189+
184190
return resourceTFEOAuthClientRead(d, meta)
185191
}
186192

@@ -238,6 +244,7 @@ func resourceTFEOAuthClientUpdate(d *schema.ResourceData, meta interface{}) erro
238244
// Create a new options struct.
239245
options := tfe.OAuthClientUpdateOptions{
240246
OrganizationScoped: tfe.Bool(d.Get("organization_scoped").(bool)),
247+
OAuthToken: tfe.String(d.Get("oauth_token").(string)),
241248
}
242249

243250
log.Printf("[DEBUG] Update OAuth client %s", d.Id())

internal/provider/resource_tfe_oauth_client_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,60 @@ func TestAccTFEOAuthClient_agentPool(t *testing.T) {
134134
})
135135
}
136136

137+
func TestAccTFEOAuthClient_updateOAuthTokenID(t *testing.T) {
138+
oc := &tfe.OAuthClient{}
139+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
140+
var initialOAuthTokenID string
141+
142+
resource.Test(t, resource.TestCase{
143+
PreCheck: func() {
144+
testAccPreCheck(t)
145+
if envGithubToken == "" {
146+
t.Skip("Please set GITHUB_TOKEN to run this test")
147+
}
148+
149+
if envGithubToken2 == "" {
150+
t.Skip("Please set GITHUB_TOKEN2 to run this test")
151+
}
152+
},
153+
ProtoV5ProviderFactories: testAccMuxedProviders,
154+
CheckDestroy: testAccCheckTFEOAuthClientDestroy,
155+
Steps: []resource.TestStep{
156+
// Step 1: Create with the initial oauth_token_id.
157+
{
158+
Config: testAccTFEOAuthClient_updateOAuthTokenID(rInt, envGithubToken),
159+
Check: resource.ComposeTestCheckFunc(
160+
testAccCheckTFEOAuthClientExists("tfe_oauth_client.foobar", oc),
161+
resource.TestCheckResourceAttrSet("tfe_oauth_client.foobar", "oauth_token_id"),
162+
func(s *terraform.State) error {
163+
initialOAuthTokenID = oc.OAuthTokens[0].ID
164+
return nil
165+
},
166+
),
167+
},
168+
// Step 2: Update the oauth_token_id value.
169+
{
170+
Config: testAccTFEOAuthClient_updateOAuthTokenID(rInt, envGithubToken2),
171+
Check: resource.ComposeTestCheckFunc(
172+
testAccCheckTFEOAuthClientExists("tfe_oauth_client.foobar", oc),
173+
resource.TestCheckResourceAttrSet("tfe_oauth_client.foobar", "oauth_token_id"),
174+
func(s *terraform.State) error {
175+
if initialOAuthTokenID == oc.OAuthTokens[0].ID {
176+
return fmt.Errorf("oauth_token_id did not change")
177+
}
178+
return nil
179+
},
180+
),
181+
},
182+
// Step 3: Run a plan-only step to ensure no changes.
183+
{
184+
Config: testAccTFEOAuthClient_updateOAuthTokenID(rInt, envGithubToken2),
185+
PlanOnly: true,
186+
},
187+
},
188+
})
189+
}
190+
137191
func testAccCheckTFEOAuthClientExists(
138192
n string, oc *tfe.OAuthClient) resource.TestCheckFunc {
139193
return func(s *terraform.State) error {
@@ -260,3 +314,19 @@ resource "tfe_oauth_client" "foobar" {
260314
agent_pool_id = data.tfe_agent_pool.foobar.id
261315
}`, envGithubToken)
262316
}
317+
318+
func testAccTFEOAuthClient_updateOAuthTokenID(rInt int, oAuthToken string) string {
319+
return fmt.Sprintf(`
320+
resource "tfe_organization" "foobar" {
321+
name = "tst-terraform-%d"
322+
323+
}
324+
resource "tfe_oauth_client" "foobar" {
325+
organization = tfe_organization.foobar.id
326+
api_url = "https://api.github.com"
327+
http_url = "https://github.com"
328+
oauth_token = "%s"
329+
service_provider = "github"
330+
organization_scoped = true
331+
}`, rInt, oAuthToken)
332+
}

0 commit comments

Comments
 (0)