Skip to content

feat(cost-centers): Enterprise Cost Centers resource (CRUD)#45

Merged
vmvarela merged 34 commits intomasterfrom
cost-centers
Mar 20, 2026
Merged

feat(cost-centers): Enterprise Cost Centers resource (CRUD)#45
vmvarela merged 34 commits intomasterfrom
cost-centers

Conversation

@vmvarela
Copy link
Owner

Summary

  • Implements github_enterprise_cost_center resource (full CRUD) and github_enterprise_cost_centers / github_enterprise_cost_center data sources
  • Adds authoritative sub-resources: github_enterprise_cost_center_users, github_enterprise_cost_center_organizations, github_enterprise_cost_center_repositories
  • All reviewer feedback from upstream PR #3000 addressed: ConfigStateChecks, testAccConf, tflog, HasChange, authoritative Create/Delete, API-fetch-on-Delete, etc.
  • Branch rebased onto upstream/main

Closes

Closes #9

github-actions bot and others added 30 commits March 20, 2026 13:46
Add utility functions needed for enterprise cost center resources:
- errIs404(): check if error is a GitHub 404 Not Found response
- errIsRetryable(): check if error is retryable (409, 5xx)
- expandStringSet(): convert schema.Set to []string
- chunkStringSlice(): split slice into chunks for batching
Add util_enterprise_cost_center.go with helper functions for managing
cost center assignments with proper retry logic and batching support.
Manages GitHub Enterprise cost center entities (create, read, update, archive).

Includes:
- Resource implementation with CRUD operations
- Acceptance tests
- Documentation
Authoritative management of user assignments to cost centers.

Includes:
- Resource implementation with batched add/remove operations
- Acceptance tests
- Documentation
…esource

Authoritative management of organization assignments to cost centers.

Includes:
- Resource implementation with batched add/remove operations
- Acceptance tests
- Documentation
…source

Authoritative management of repository assignments to cost centers.

Includes:
- Resource implementation with batched add/remove operations
- Acceptance tests
- Documentation
Add two data sources:
- github_enterprise_cost_center: retrieve a cost center by ID
- github_enterprise_cost_centers: list cost centers with optional state filter

Includes:
- Data source implementations
- Acceptance tests
- Documentation
- Register 4 resources in provider.go:
  - github_enterprise_cost_center
  - github_enterprise_cost_center_users
  - github_enterprise_cost_center_organizations
  - github_enterprise_cost_center_repositories
- Register 2 data sources in provider.go:
  - github_enterprise_cost_center
  - github_enterprise_cost_centers
- Add navigation links in website/github.erb

T_EDITOR=true git rebase --continue
t status
Add example Terraform configuration demonstrating how to:
- Create a cost center
- Assign users, organizations, and repositories
- Use data sources to query cost centers
Update import paths from go-github/v81 to go-github/v82 to match
the current version in upstream/main.
Co-authored-by: Timo Sand <timo.sand@iki.fi>
Remove expandStringSet from util.go and replace usages with direct
expandStringList(set.List()) calls. The function was unnecessary since
schema.Set from d.Get() is never nil.

Resolves PR comments #1-2.
Move the archived/deleted state check from Update to Read function.
If the cost center is archived (deleted), it will be removed from
Terraform state during Read rather than blocking updates.

Resolves PR comment #3.
Split resourceGithubEnterpriseCostCenterUsersCreateOrUpdate into
separate Create and Update functions. Create only adds users,
Update handles the full diff. Both return nil instead of calling Read.

Resolves PR comments #4-5.
Split resourceGithubEnterpriseCostCenterOrganizationsCreateOrUpdate into
separate Create and Update functions. Create only adds organizations,
Update handles the full diff. Both return nil instead of calling Read.

Resolves PR comments #6-7.
Split resourceGithubEnterpriseCostCenterRepositoriesCreateOrUpdate into
separate Create and Update functions. Create only adds repositories,
Update handles the full diff. Both return nil instead of calling Read.

Resolves PR comments #8-9.
…tions

The API returns type strings as 'User', 'Org', and 'Repo' but the tests
were checking for lowercase 'user', 'organization', and 'repository'.
This fix ensures CheckDestroy properly detects remaining assignments.
Add CostCenterResourceType constants (User, Org, Repo) to avoid
magic strings throughout the codebase. This prevents typos and
makes the code more maintainable.

Addresses review feedback from @deiga.
Replace terraform-plugin-sdk/v2 test imports with terraform-plugin-testing
to fix flag redefinition conflict ('sweep' flag registered twice).
This aligns cost center tests with the rest of the codebase.
Keep chunkStringSlice(items, maxSize) generic in util.go to avoid coupling with cost-center-specific constants.

Add nolint:unparam with explicit rationale because current call sites pass the same value, while preserving future reuse for other resources.
The import function now calls GetCostCenter to populate the 'name'
field from the API response. Without this, the Required 'name' field
would be empty after import, causing recreate churn on the next plan.

Addresses review comment from @stevehipwell.
Sub-resources (_users, _organizations, _repositories) now use a simple
cost_center_id as their Terraform ID, consistent with the main
github_enterprise_cost_center resource.

Also moves d.SetId() to after the API call succeeds, preventing
corrupt state if the add-resources call fails.

Tests updated to use ImportStateIdPrefix and read attributes from
state instead of parsing the now-simple ID.

Addresses review comments from @stevehipwell.
Before adding resources to a cost center, check via API whether
the cost center already has resources of the managed type assigned.
If so, return an error asking the user to import or remove them
manually. This prevents silently clobbering pre-existing assignments.
Replace the two-map diff (currentX + desiredX) with a single map
where false=remove and true=keep. Desired items not in the map are
added. This is shorter and avoids constructing a second map.
Instead of reading resource names from Terraform state (which may be
stale or incomplete), Delete now calls GetCostCenter to fetch the
current list of resources of the managed type from the API and removes
them all. Also handles 404 gracefully (cost center already gone).
The cost center sub-resources (organizations, repositories, users)
were registered in provider.go and had docs, but were accidentally
omitted from the website sidebar navigation.
@vmvarela vmvarela added the type:feature New functionality label Mar 20, 2026
@github-actions github-actions bot added the type:docs Documentation only label Mar 20, 2026
@vmvarela vmvarela merged commit f51ff0b into master Mar 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:docs Documentation only type:feature New functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Enterprise Cost Centers resource (CRUD)

1 participant