|
8 | 8 | )
|
9 | 9 |
|
10 | 10 | const (
|
11 |
| - delegateTokenListPath = "/delegate-token-ng" |
| 11 | + delegateTokenPath = "/delegate-token-ng" |
12 | 12 | )
|
13 | 13 |
|
14 | 14 | type DelegateTokenClient struct {
|
@@ -37,7 +37,7 @@ func setDefaultPaginationForDelegateToken(opts *dto.DelegateTokenOptions) {
|
37 | 37 | // - []DelegateToken: List of delegate tokens for the current page
|
38 | 38 | // - error: Any error that occurred during the request
|
39 | 39 | func (d *DelegateTokenClient) ListDelegateTokens(ctx context.Context, scope dto.Scope, opts *dto.DelegateTokenOptions) ([]dto.DelegateToken, error) {
|
40 |
| - path := delegateTokenListPath |
| 40 | + path := delegateTokenPath |
41 | 41 | params := make(map[string]string)
|
42 | 42 | addScope(scope, params)
|
43 | 43 |
|
@@ -73,3 +73,107 @@ func (d *DelegateTokenClient) ListDelegateTokens(ctx context.Context, scope dto.
|
73 | 73 |
|
74 | 74 | return response.Resource, nil
|
75 | 75 | }
|
| 76 | + |
| 77 | +// GetDelegateToken retrieves a specific delegate token by name. |
| 78 | +// Parameters: |
| 79 | +// - ctx: Context for the request |
| 80 | +// - scope: The scope (account/org/project) for the token |
| 81 | +// - name: Name of the delegate token to retrieve |
| 82 | +// - status: Optional status filter (ACTIVE/REVOKED) |
| 83 | +// Returns: |
| 84 | +// - *DelegateToken: The delegate token if found |
| 85 | +// - error: Any error that occurred during the request |
| 86 | +func (d *DelegateTokenClient) GetDelegateToken(ctx context.Context, scope dto.Scope, name string, status string) ([]dto.DelegateToken, error) { |
| 87 | + path := delegateTokenPath |
| 88 | + params := make(map[string]string) |
| 89 | + params["name"] = name |
| 90 | + |
| 91 | + addScope(scope, params) |
| 92 | + |
| 93 | + if status != "" { |
| 94 | + params["status"] = status |
| 95 | + } |
| 96 | + |
| 97 | + var response dto.DelegateTokenListResponse |
| 98 | + err := d.Client.Get(ctx, path, params, nil, &response) |
| 99 | + if err != nil { |
| 100 | + return nil, fmt.Errorf("failed to get delegate token: %w", err) |
| 101 | + } |
| 102 | + if len(response.Resource) == 0 { |
| 103 | + return nil, nil |
| 104 | + } |
| 105 | + |
| 106 | + return response.Resource, nil |
| 107 | +} |
| 108 | + |
| 109 | +// CreateDelegateToken creates a new delegate token with the specified parameters. |
| 110 | +// Parameters: |
| 111 | +// - ctx: Context for the request |
| 112 | +// - scope: The scope (account/org/project) for the token |
| 113 | +// - tokenName: Name of the delegate token |
| 114 | +// - revokeAfter: Optional epoch time in milliseconds after which the token will be revoked |
| 115 | +// Returns: |
| 116 | +// - *DelegateToken: The created delegate token |
| 117 | +// - error: Any error that occurred during the request |
| 118 | +func (d *DelegateTokenClient) CreateDelegateToken(ctx context.Context, scope dto.Scope, tokenName string, revokeAfter *int64) (dto.DelegateToken, error) { |
| 119 | + path := delegateTokenPath |
| 120 | + params := make(map[string]string) |
| 121 | + addScope(scope, params) |
| 122 | + params["tokenName"] = tokenName |
| 123 | + |
| 124 | + if revokeAfter != nil { |
| 125 | + params["revokeAfter"] = fmt.Sprintf("%d", *revokeAfter) |
| 126 | + } |
| 127 | + |
| 128 | + var response dto.DelegateTokenResponse |
| 129 | + err := d.Client.Post(ctx, path, params, nil, map[string]string{}, &response) |
| 130 | + if err != nil { |
| 131 | + return dto.DelegateToken{}, fmt.Errorf("failed to create delegate token: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + return response.Resource, nil |
| 135 | +} |
| 136 | + |
| 137 | +// RevokeDelegateToken revokes a delegate token with the specified name. |
| 138 | +// Parameters: |
| 139 | +// - ctx: Context for the request |
| 140 | +// - scope: The scope (account/org/project) for the token |
| 141 | +// - tokenName: Name of the delegate token to revoke |
| 142 | +// Returns: |
| 143 | +// - DelegateToken: The revoked delegate token |
| 144 | +// - error: Any error that occurred during the request |
| 145 | +func (d *DelegateTokenClient) RevokeDelegateToken(ctx context.Context, scope dto.Scope, tokenName string) (dto.DelegateToken, error) { |
| 146 | + path := delegateTokenPath |
| 147 | + params := make(map[string]string) |
| 148 | + addScope(scope, params) |
| 149 | + params["tokenName"] = tokenName |
| 150 | + |
| 151 | + var response dto.DelegateTokenResponse |
| 152 | + err := d.Client.Put(ctx, path, params, nil, &response) |
| 153 | + if err != nil { |
| 154 | + return dto.DelegateToken{}, fmt.Errorf("failed to revoke delegate token: %w", err) |
| 155 | + } |
| 156 | + |
| 157 | + return response.Resource, nil |
| 158 | +} |
| 159 | + |
| 160 | +// DeleteDelegateToken deletes a revoked delegate token with the specified name. |
| 161 | +// Parameters: |
| 162 | +// - ctx: Context for the request |
| 163 | +// - scope: The scope (account/org/project) for the token |
| 164 | +// - tokenName: Name of the delegate token to delete |
| 165 | +// Returns: |
| 166 | +// - error: Any error that occurred during the request |
| 167 | +func (d *DelegateTokenClient) DeleteDelegateToken(ctx context.Context, scope dto.Scope, tokenName string) error { |
| 168 | + path := delegateTokenPath |
| 169 | + params := make(map[string]string) |
| 170 | + addScope(scope, params) |
| 171 | + params["tokenName"] = tokenName |
| 172 | + |
| 173 | + err := d.Client.Delete(ctx, path, params, nil, nil) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("failed to delete delegate token. Please make sure the token exists and is REVOKED: %w", err) |
| 176 | + } |
| 177 | + |
| 178 | + return nil |
| 179 | +} |
0 commit comments