|
| 1 | +--- |
| 2 | +# generated by https://github.com/hashicorp/terraform-plugin-docs |
| 3 | +page_title: "gitlab_project_access_tokens Data Source - terraform-provider-gitlab" |
| 4 | +subcategory: "" |
| 5 | +description: |- |
| 6 | + The gitlab_project_access_tokens data source allows to retrieve all project access tokens for a given project. |
| 7 | + Upstream API: GitLab REST API docs https://docs.gitlab.com/api/project_access_tokens/ |
| 8 | +--- |
| 9 | + |
| 10 | +# gitlab_project_access_tokens (Data Source) |
| 11 | + |
| 12 | +The `gitlab_project_access_tokens` data source allows to retrieve all project access tokens for a given project. |
| 13 | + |
| 14 | +**Upstream API**: [GitLab REST API docs](https://docs.gitlab.com/api/project_access_tokens/) |
| 15 | + |
| 16 | +## Example Usage |
| 17 | + |
| 18 | +```terraform |
| 19 | +# Basic example: Retrieve all project access tokens |
| 20 | +data "gitlab_project_access_tokens" "all_tokens" { |
| 21 | + project = "my/example/project" |
| 22 | +} |
| 23 | +
|
| 24 | +# Example with state filtering: Only retrieve active tokens |
| 25 | +data "gitlab_project_access_tokens" "active_tokens" { |
| 26 | + project = "my/example/project" |
| 27 | + state = "active" |
| 28 | +} |
| 29 | +
|
| 30 | +# Example with state filtering: Only retrieve inactive tokens |
| 31 | +data "gitlab_project_access_tokens" "inactive_tokens" { |
| 32 | + project = "my/example/project" |
| 33 | + state = "inactive" |
| 34 | +} |
| 35 | +
|
| 36 | +# Output examples showing different ways to work with the data |
| 37 | +
|
| 38 | +# Get the total count of tokens |
| 39 | +output "token_count" { |
| 40 | + description = "Total number of project access tokens" |
| 41 | + value = length(data.gitlab_project_access_tokens.all_tokens.access_tokens) |
| 42 | +} |
| 43 | +
|
| 44 | +# Get only the names of active tokens |
| 45 | +output "active_token_names" { |
| 46 | + description = "Names of active project access tokens" |
| 47 | + value = [for token in data.gitlab_project_access_tokens.active_tokens.access_tokens : token.name] |
| 48 | +} |
| 49 | +
|
| 50 | +# Get detailed information about each token |
| 51 | +output "token_details" { |
| 52 | + description = "Detailed information about each access token" |
| 53 | + value = [ |
| 54 | + for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : { |
| 55 | + id = token.id |
| 56 | + name = token.name |
| 57 | + description = token.description |
| 58 | + scopes = token.scopes |
| 59 | + active = token.active |
| 60 | + revoked = token.revoked |
| 61 | + created_at = token.created_at |
| 62 | + expires_at = token.expires_at |
| 63 | + access_level = token.access_level |
| 64 | + user_id = token.user_id |
| 65 | + last_used_at = token.last_used_at |
| 66 | + } |
| 67 | + ] |
| 68 | +} |
| 69 | +
|
| 70 | +# Filter tokens programmatically to get only active and non-revoked tokens |
| 71 | +output "active_non_revoked_tokens" { |
| 72 | + description = "Only active and non-revoked tokens" |
| 73 | + value = [ |
| 74 | + for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token |
| 75 | + if token.active && !token.revoked |
| 76 | + ] |
| 77 | +} |
| 78 | +
|
| 79 | +# Get tokens that expire within the next 30 days |
| 80 | +output "expiring_soon_tokens" { |
| 81 | + description = "Tokens that expire within the next 30 days" |
| 82 | + value = [ |
| 83 | + for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token |
| 84 | + if token.expires_at != "" && can(parseint(split("T", token.expires_at)[0], 10)) && |
| 85 | + timeadd(timestamp(), "30d") > timeparse("2006-01-02", split("T", token.expires_at)[0]) |
| 86 | + ] |
| 87 | +} |
| 88 | +
|
| 89 | +# Get tokens with specific scopes |
| 90 | +output "api_tokens" { |
| 91 | + description = "Tokens that have API scope" |
| 92 | + value = [ |
| 93 | + for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token |
| 94 | + if contains(token.scopes, "api") |
| 95 | + ] |
| 96 | +} |
| 97 | +
|
| 98 | +# Get tokens by access level |
| 99 | +output "maintainer_tokens" { |
| 100 | + description = "Tokens with maintainer access level" |
| 101 | + value = [ |
| 102 | + for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token |
| 103 | + if token.access_level == "maintainer" |
| 104 | + ] |
| 105 | +} |
| 106 | +
|
| 107 | +# Get a summary of token statistics |
| 108 | +output "token_statistics" { |
| 109 | + description = "Summary statistics of project access tokens" |
| 110 | + value = { |
| 111 | + total_tokens = length(data.gitlab_project_access_tokens.all_tokens.access_tokens) |
| 112 | + active_tokens = length([for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token if token.active]) |
| 113 | + revoked_tokens = length([for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token if token.revoked]) |
| 114 | + expired_tokens = length([for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token if token.expires_at != "" && can(parseint(split("T", token.expires_at)[0], 10)) && timeadd(timestamp(), "0d") > timeparse("2006-01-02", split("T", token.expires_at)[0])]) |
| 115 | + tokens_by_level = { |
| 116 | + for level in distinct([for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token.access_level]) : level => length([for token in data.gitlab_project_access_tokens.all_tokens.access_tokens : token if token.access_level == level]) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +<!-- schema generated by tfplugindocs --> |
| 123 | +## Schema |
| 124 | + |
| 125 | +### Required |
| 126 | + |
| 127 | +- `project` (String) The name or id of the project. |
| 128 | + |
| 129 | +### Optional |
| 130 | + |
| 131 | +- `state` (String) List all project access token that match the specified state. Valid values are `active`, `inactive`. Returns all project access token if not set. |
| 132 | + |
| 133 | +### Read-Only |
| 134 | + |
| 135 | +- `access_tokens` (List of Object) The list of access tokens returned by the search (see [below for nested schema](#nestedatt--access_tokens)) |
| 136 | +- `id` (String) The ID of this Terraform resource. |
| 137 | + |
| 138 | +<a id="nestedatt--access_tokens"></a> |
| 139 | +### Nested Schema for `access_tokens` |
| 140 | + |
| 141 | +Read-Only: |
| 142 | + |
| 143 | +- `access_level` (String) |
| 144 | +- `active` (Boolean) |
| 145 | +- `created_at` (String) |
| 146 | +- `description` (String) |
| 147 | +- `expires_at` (String) |
| 148 | +- `id` (String) |
| 149 | +- `last_used_at` (String) |
| 150 | +- `name` (String) |
| 151 | +- `project` (String) |
| 152 | +- `revoked` (Boolean) |
| 153 | +- `scopes` (Set of String) |
| 154 | +- `user_id` (Number) |
0 commit comments