Skip to content

Commit 0b8d770

Browse files
committed
Add org access token data source
1 parent b31ca14 commit 0b8d770

5 files changed

Lines changed: 708 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "docker_org_access_token Data Source - docker"
4+
subcategory: ""
5+
description: |-
6+
Reads metadata for an existing organization access token.
7+
Provide either id for a direct lookup or a filter block to resolve exactly one token by metadata. The filter block follows the common Terraform name/value pattern and currently supports label only.
8+
-> Note: This data source is only available when authenticated with a username and password as an owner of the org.
9+
-> Note: This data source returns metadata only. Docker Hub only returns the secret token value during creation, so token is intentionally not exposed here.
10+
-> Note: Filter-based lookups scan all organization access token pages to guarantee unique matching, even if the provider max_page_results setting is lower.
11+
Example Usage
12+
13+
data "docker_org_access_token" "by_id" {
14+
org_name = "my-organization"
15+
id = "a7a5ef25-8889-43a0-8cc7-f2a94268e861"
16+
}
17+
18+
data "docker_org_access_token" "by_label" {
19+
org_name = "my-organization"
20+
21+
filter {
22+
name = "label"
23+
values = ["ci-token"]
24+
}
25+
}
26+
---
27+
28+
# docker_org_access_token (Data Source)
29+
30+
Reads metadata for an existing organization access token.
31+
32+
Provide either `id` for a direct lookup or a `filter` block to resolve exactly one token by metadata. The `filter` block follows the common Terraform name/value pattern and currently supports `label` only.
33+
34+
-> **Note**: This data source is only available when authenticated with a username and password as an owner of the org.
35+
36+
-> **Note**: This data source returns metadata only. Docker Hub only returns the secret token value during creation, so `token` is intentionally not exposed here.
37+
38+
-> **Note**: Filter-based lookups scan all organization access token pages to guarantee unique matching, even if the provider `max_page_results` setting is lower.
39+
40+
## Example Usage
41+
42+
```hcl
43+
data "docker_org_access_token" "by_id" {
44+
org_name = "my-organization"
45+
id = "a7a5ef25-8889-43a0-8cc7-f2a94268e861"
46+
}
47+
48+
data "docker_org_access_token" "by_label" {
49+
org_name = "my-organization"
50+
51+
filter {
52+
name = "label"
53+
values = ["ci-token"]
54+
}
55+
}
56+
```
57+
58+
59+
60+
<!-- schema generated by tfplugindocs -->
61+
## Schema
62+
63+
### Required
64+
65+
- `org_name` (String) The organization namespace.
66+
67+
### Optional
68+
69+
- `filter` (Attributes List) One or more name/value filter blocks. Exactly one of `id` or `filter` must be set. Filters are applied with OR semantics within `values` and AND semantics across blocks. Only `label` is supported in v1. (see [below for nested schema](#nestedatt--filter))
70+
- `id` (String) The ID of the organization access token. Set this for a direct lookup, or omit it when using `filter`.
71+
72+
### Read-Only
73+
74+
- `created_at` (String) The creation timestamp of the access token.
75+
- `created_by` (String) The user that created the access token.
76+
- `description` (String) The description of the access token.
77+
- `expires_at` (String) The expiration timestamp of the access token, if set.
78+
- `is_active` (Boolean) Whether the access token is active.
79+
- `label` (String) The label of the access token.
80+
- `last_used_at` (String) The last time the access token was used, if available.
81+
- `resources` (Attributes List) Resources this token has access to. (see [below for nested schema](#nestedatt--resources))
82+
83+
<a id="nestedatt--filter"></a>
84+
### Nested Schema for `filter`
85+
86+
Required:
87+
88+
- `name` (String) Name of the field to filter by. Only `label` is supported.
89+
- `values` (List of String) Accepted values for the given filter name.
90+
91+
92+
<a id="nestedatt--resources"></a>
93+
### Nested Schema for `resources`
94+
95+
Read-Only:
96+
97+
- `path` (String) The path of the resource.
98+
- `scopes` (List of String) The scopes this token has access to.
99+
- `type` (String) The type of resource.

internal/hubclient/client_org_access_token.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ type OrgAccessToken struct {
4141
Resources []OrgAccessTokenResource `json:"resources,omitempty"`
4242
}
4343

44+
type OrgAccessTokenPage struct {
45+
Total int `json:"total"`
46+
Next string `json:"next,omitempty"`
47+
Previous string `json:"previous,omitempty"`
48+
Results []OrgAccessToken `json:"results"`
49+
}
50+
4451
type OrgAccessTokenResource struct {
4552
Type string `json:"type"`
4653
Path string `json:"path"`
@@ -75,6 +82,29 @@ func (c *Client) CreateOrgAccessToken(ctx context.Context, orgName string, param
7582
return accessToken, err
7683
}
7784

85+
func (c *Client) ListOrgAccessTokens(ctx context.Context, orgName string) ([]OrgAccessToken, error) {
86+
if orgName == "" {
87+
return nil, fmt.Errorf("orgName is required")
88+
}
89+
90+
var accessTokens []OrgAccessToken
91+
nextURL := fmt.Sprintf("/orgs/%s/access-tokens", orgName)
92+
93+
for nextURL != "" {
94+
relativeURL := c.convertToRelativeURL(nextURL)
95+
96+
var page OrgAccessTokenPage
97+
if err := c.sendRequest(ctx, "GET", relativeURL, nil, &page); err != nil {
98+
return nil, err
99+
}
100+
101+
accessTokens = append(accessTokens, page.Results...)
102+
nextURL = page.Next
103+
}
104+
105+
return accessTokens, nil
106+
}
107+
78108
func (c *Client) GetOrgAccessToken(ctx context.Context, orgName, accessTokenID string) (OrgAccessToken, error) {
79109
if orgName == "" {
80110
return OrgAccessToken{}, fmt.Errorf("orgName is required")

0 commit comments

Comments
 (0)