Skip to content

Commit ecc1f64

Browse files
committed
allow to adjust token validity for api clients
1 parent 3725645 commit ecc1f64

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Changed
2+
body: '`resource_api_client` add support for setting and managing token validity via Terraform.'
3+
time: 2024-11-27T14:35:27.885461+01:00

commercetools/resource_api_client.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func resourceAPIClient() *schema.Resource {
1919
Description: "Create a new API client. Note that Commercetools might return slightly different scopes, " +
2020
"resulting in a new API client being created everytime Terraform is run. In this case, " +
2121
"fix your scopes accordingly to match what is returned by Commercetools.\n\n" +
22-
"Also see the [API client HTTP API documentation](https://docs.commercetools.com//http-api-projects-api-clients).",
22+
"Also see the [API client HTTP API documentation](https://docs.commercetools.com/api/projects/api-clients).",
2323
CreateContext: resourceAPIClientCreate,
2424
ReadContext: resourceAPIClientRead,
2525
DeleteContext: resourceAPIClientDelete,
@@ -34,12 +34,26 @@ func resourceAPIClient() *schema.Resource {
3434
ForceNew: true,
3535
},
3636
"scope": {
37-
Description: "A list of the [OAuth scopes](https://docs.commercetools.com/http-api-authorization.html#scopes)",
37+
Description: "A list of the [OAuth scopes](https://docs.commercetools.com/api/scopes)",
3838
Type: schema.TypeSet,
3939
Elem: &schema.Schema{Type: schema.TypeString},
4040
Required: true,
4141
ForceNew: true,
4242
},
43+
"accessTokenValiditySeconds": {
44+
Description: "Expiration time in seconds for each access token obtained by the APIClient. Only present when set with the APIClientDraft. If not present the default value applies.",
45+
Type: schema.TypeSet,
46+
Elem: &schema.Schema{Type: schema.TypeInt},
47+
Required: false,
48+
ForceNew: true,
49+
},
50+
"refreshTokenValiditySeconds": {
51+
Description: "Inactivity expiration time in seconds for each refresh token obtained by the APIClient. Only present when set with the APIClientDraft. If not present the default value applies.",
52+
Type: schema.TypeSet,
53+
Elem: &schema.Schema{Type: schema.TypeInt},
54+
Required: false,
55+
ForceNew: true,
56+
},
4357
"secret": {
4458
Type: schema.TypeString,
4559
Computed: true,
@@ -50,7 +64,6 @@ func resourceAPIClient() *schema.Resource {
5064
}
5165

5266
func resourceAPIClientCreate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics {
53-
name := d.Get("name").(string)
5467
scopes := d.Get("scope").(*schema.Set).List()
5568

5669
scopeParts := make([]string, 0)
@@ -59,9 +72,15 @@ func resourceAPIClientCreate(ctx context.Context, d *schema.ResourceData, m any)
5972
}
6073

6174
draft := platform.ApiClientDraft{
62-
Name: name,
75+
Name: d.Get("name").(string),
6376
Scope: strings.Join(scopeParts, " "),
6477
}
78+
if val := d.Get("accessTokenValiditySeconds").(*int); val != nil && *val > 0 {
79+
draft.AccessTokenValiditySeconds = val
80+
}
81+
if val := d.Get("refreshTokenValiditySeconds").(*int); val != nil && *val > 0 {
82+
draft.RefreshTokenValiditySeconds = val
83+
}
6584

6685
client := getClient(m)
6786

@@ -100,6 +119,8 @@ func resourceAPIClientRead(ctx context.Context, d *schema.ResourceData, m any) d
100119
scopes := strings.Split(apiClient.Scope, " ")
101120
sort.Strings(scopes)
102121
_ = d.Set("scope", scopes)
122+
_ = d.Set("accessTokenValiditySeconds", apiClient.AccessTokenValiditySeconds)
123+
_ = d.Set("refreshTokenValiditySeconds", apiClient.RefreshTokenValiditySeconds)
103124
return nil
104125
}
105126

docs/resources/api_client.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ page_title: "commercetools_api_client Resource - terraform-provider-commercetool
44
subcategory: ""
55
description: |-
66
Create a new API client. Note that Commercetools might return slightly different scopes, resulting in a new API client being created everytime Terraform is run. In this case, fix your scopes accordingly to match what is returned by Commercetools.
7-
Also see the API client HTTP API documentation https://docs.commercetools.com//http-api-projects-api-clients.
7+
Also see the API client HTTP API documentation https://docs.commercetools.com/api/projects/api-clients.
88
---
99

1010
# commercetools_api_client (Resource)
@@ -19,6 +19,8 @@ Also see the [API client HTTP API documentation](https://docs.commercetools.com/
1919
resource "commercetools_api_client" "my-api-client" {
2020
name = "My API Client"
2121
scope = ["manage_orders:my-ct-project-key", "manage_payments:my-ct-project-key"]
22+
accessTokenValiditySeconds = 3600
23+
refreshTokenValiditySeconds = 60
2224
}
2325
```
2426

@@ -27,8 +29,14 @@ resource "commercetools_api_client" "my-api-client" {
2729

2830
### Required
2931

30-
- `name` (String) Name of the API client
31-
- `scope` (Set of String) A list of the [OAuth scopes](https://docs.commercetools.com/http-api-authorization.html#scopes)
32+
- `name` (String) Name of the API client.
33+
- `scope` (Set of String) A list of the [OAuth scopes](https://docs.commercetools.com/api/scopes).
34+
35+
36+
### Optional
37+
38+
- `accessTokenValiditySeconds` (Int) Expiration time in seconds for each access token obtained by the APIClient. See the latest CommerceTools documentation for [API Clients](https://docs.commercetools.com/api/projects/api-clients) for valid number ranges.
39+
- `refreshTokenValiditySeconds` (Int) Inactivity expiration time in seconds for each refresh token obtained by the APIClient. See the latest CommerceTools documentation for [API Clients](https://docs.commercetools.com/api/projects/api-clients) for valid number ranges.
3240

3341
### Read-Only
3442

0 commit comments

Comments
 (0)