|
| 1 | +terraform { |
| 2 | + required_providers { |
| 3 | + github = { |
| 4 | + source = "integrations/github" |
| 5 | + version = "~> 6.11" |
| 6 | + } |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +provider "github" { |
| 11 | + token = var.github_token |
| 12 | + owner = var.enterprise_slug |
| 13 | +} |
| 14 | + |
| 15 | +variable "github_token" { |
| 16 | + description = "GitHub classic personal access token (PAT) for an enterprise admin" |
| 17 | + type = string |
| 18 | + sensitive = true |
| 19 | +} |
| 20 | + |
| 21 | +variable "enterprise_slug" { |
| 22 | + description = "The GitHub Enterprise slug" |
| 23 | + type = string |
| 24 | +} |
| 25 | + |
| 26 | +variable "cost_center_name" { |
| 27 | + description = "Name for the cost center" |
| 28 | + type = string |
| 29 | +} |
| 30 | + |
| 31 | +variable "users" { |
| 32 | + description = "Usernames to assign to the cost center" |
| 33 | + type = list(string) |
| 34 | + default = [] |
| 35 | +} |
| 36 | + |
| 37 | +variable "organizations" { |
| 38 | + description = "Organization logins to assign to the cost center" |
| 39 | + type = list(string) |
| 40 | + default = [] |
| 41 | +} |
| 42 | + |
| 43 | +variable "repositories" { |
| 44 | + description = "Repositories (full name, e.g. org/repo) to assign to the cost center" |
| 45 | + type = list(string) |
| 46 | + default = [] |
| 47 | +} |
| 48 | + |
| 49 | +# The cost center resource manages only the cost center entity itself. |
| 50 | +resource "github_enterprise_cost_center" "example" { |
| 51 | + enterprise_slug = var.enterprise_slug |
| 52 | + name = var.cost_center_name |
| 53 | +} |
| 54 | + |
| 55 | +# Use separate authoritative resources for assignments. |
| 56 | +# These are optional - only create them if you have items to assign. |
| 57 | + |
| 58 | +resource "github_enterprise_cost_center_users" "example" { |
| 59 | + count = length(var.users) > 0 ? 1 : 0 |
| 60 | + |
| 61 | + enterprise_slug = var.enterprise_slug |
| 62 | + cost_center_id = github_enterprise_cost_center.example.id |
| 63 | + usernames = var.users |
| 64 | +} |
| 65 | + |
| 66 | +resource "github_enterprise_cost_center_organizations" "example" { |
| 67 | + count = length(var.organizations) > 0 ? 1 : 0 |
| 68 | + |
| 69 | + enterprise_slug = var.enterprise_slug |
| 70 | + cost_center_id = github_enterprise_cost_center.example.id |
| 71 | + organization_logins = var.organizations |
| 72 | +} |
| 73 | + |
| 74 | +resource "github_enterprise_cost_center_repositories" "example" { |
| 75 | + count = length(var.repositories) > 0 ? 1 : 0 |
| 76 | + |
| 77 | + enterprise_slug = var.enterprise_slug |
| 78 | + cost_center_id = github_enterprise_cost_center.example.id |
| 79 | + repository_names = var.repositories |
| 80 | +} |
| 81 | + |
| 82 | +# Data sources for reading cost center information |
| 83 | +data "github_enterprise_cost_center" "by_id" { |
| 84 | + enterprise_slug = var.enterprise_slug |
| 85 | + cost_center_id = github_enterprise_cost_center.example.id |
| 86 | +} |
| 87 | + |
| 88 | +data "github_enterprise_cost_centers" "active" { |
| 89 | + enterprise_slug = var.enterprise_slug |
| 90 | + state = "active" |
| 91 | + |
| 92 | + depends_on = [github_enterprise_cost_center.example] |
| 93 | +} |
| 94 | + |
| 95 | +output "cost_center" { |
| 96 | + description = "Created cost center" |
| 97 | + value = { |
| 98 | + id = github_enterprise_cost_center.example.id |
| 99 | + name = github_enterprise_cost_center.example.name |
| 100 | + state = github_enterprise_cost_center.example.state |
| 101 | + azure_subscription = github_enterprise_cost_center.example.azure_subscription |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +output "cost_center_from_data_source" { |
| 106 | + description = "Cost center fetched by data source (includes all assignments)" |
| 107 | + value = { |
| 108 | + id = data.github_enterprise_cost_center.by_id.cost_center_id |
| 109 | + name = data.github_enterprise_cost_center.by_id.name |
| 110 | + state = data.github_enterprise_cost_center.by_id.state |
| 111 | + users = sort(tolist(data.github_enterprise_cost_center.by_id.users)) |
| 112 | + organizations = sort(tolist(data.github_enterprise_cost_center.by_id.organizations)) |
| 113 | + repositories = sort(tolist(data.github_enterprise_cost_center.by_id.repositories)) |
| 114 | + } |
| 115 | +} |
0 commit comments