Skip to content

Commit 30451ac

Browse files
committed
review changes
1 parent 2b113bc commit 30451ac

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

docs/resources/service_account_access_token.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ resource "stackit_service_account_access_token" "sa1" {
7979
- `access_token_id` (String) Identifier for the access token linked to the service account.
8080
- `active` (Boolean) Indicate whether the token is currently active or inactive
8181
- `created_at` (String) Timestamp indicating when the access token was created.
82-
- `id` (String) Unique internal resource ID for Terraform, formatted as "project_id,access_token_id".
82+
- `id` (String) Unique internal resource ID for Terraform, formatted as "`project_id`,`access_token_id`".
8383
- `token` (String, Sensitive) JWT access token for API authentication. Prefixed by 'Bearer' and should be stored securely as it is irretrievable once lost.
8484
- `valid_until` (String) Estimated expiration timestamp of the access token. For precise validity, check the JWT details.

stackit/internal/services/serviceaccount/token/resource.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (r *serviceAccountTokenResource) Metadata(_ context.Context, req resource.M
119119
// Schema defines the resource schema for the service account access token.
120120
func (r *serviceAccountTokenResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
121121
descriptions := map[string]string{
122-
"id": "Unique internal resource ID for Terraform, formatted as \"project_id,access_token_id\".",
122+
"id": "Unique internal resource ID for Terraform, formatted as \"`project_id`,`access_token_id`\".",
123123
"project_id": "STACKIT project ID associated with the service account token.",
124124
"service_account_email": "Email address linked to the service account.",
125125
"ttl_days": "Specifies the token's validity duration in days. If unspecified, defaults to 90 days.",
@@ -133,7 +133,7 @@ func (r *serviceAccountTokenResource) Schema(_ context.Context, _ resource.Schem
133133

134134
resp.Schema = schema.Schema{
135135
MarkdownDescription: markdownDescription,
136-
Description: "Schema for managing a STACKIT service account access token.",
136+
Description: "STACKIT service account access token schema.",
137137

138138
Attributes: map[string]schema.Attribute{
139139
"id": schema.StringAttribute{
@@ -269,6 +269,7 @@ func (r *serviceAccountTokenResource) Read(ctx context.Context, req resource.Rea
269269
if err != nil {
270270
var oapiErr *oapierror.GenericOpenAPIError
271271
ok := errors.As(err, &oapiErr) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
272+
// due to security purposes, attempting to list access tokens for a non-existent Service Account will return 403.
272273
if ok && oapiErr.StatusCode == http.StatusNotFound || oapiErr.StatusCode == http.StatusForbidden {
273274
resp.State.RemoveResource(ctx)
274275
return

stackit/internal/services/serviceaccount/token/resource_test.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
"github.com/google/go-cmp/cmp"
8+
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/types"
910
"github.com/stackitcloud/stackit-sdk-go/core/utils"
1011
"github.com/stackitcloud/stackit-sdk-go/services/serviceaccount"
@@ -70,10 +71,11 @@ func TestMapCreateResponse(t *testing.T) {
7071
Token: utils.Ptr("token"),
7172
},
7273
Model{
73-
Id: types.StringValue("pid,aid"),
74-
ProjectId: types.StringValue("pid"),
75-
Token: types.StringValue("token"),
76-
AccessTokenId: types.StringValue("aid"),
74+
Id: types.StringValue("pid,aid"),
75+
ProjectId: types.StringValue("pid"),
76+
Token: types.StringValue("token"),
77+
AccessTokenId: types.StringValue("aid"),
78+
RotateWhenChanged: types.MapValueMust(types.StringType, map[string]attr.Value{}),
7779
},
7880
true,
7981
},
@@ -84,14 +86,17 @@ func TestMapCreateResponse(t *testing.T) {
8486
Token: utils.Ptr("token"),
8587
CreatedAt: utils.Ptr(time.Now()),
8688
ValidUntil: utils.Ptr(time.Now().Add(24 * time.Hour)),
89+
Active: utils.Ptr(true),
8790
},
8891
Model{
89-
Id: types.StringValue("pid,aid"),
90-
ProjectId: types.StringValue("pid"),
91-
Token: types.StringValue("token"),
92-
AccessTokenId: types.StringValue("aid"),
93-
CreatedAt: types.StringValue(time.Now().Format(time.RFC3339)), // Adjust to the format used
94-
ValidUntil: types.StringValue(time.Now().Add(24 * time.Hour).Format(time.RFC3339)), // Adjust format
92+
Id: types.StringValue("pid,aid"),
93+
ProjectId: types.StringValue("pid"),
94+
Token: types.StringValue("token"),
95+
AccessTokenId: types.StringValue("aid"),
96+
Active: types.BoolValue(true),
97+
CreatedAt: types.StringValue(time.Now().Format(time.RFC3339)),
98+
ValidUntil: types.StringValue(time.Now().Add(24 * time.Hour).Format(time.RFC3339)),
99+
RotateWhenChanged: types.MapValueMust(types.StringType, map[string]attr.Value{}),
95100
},
96101
true,
97102
},
@@ -126,16 +131,23 @@ func TestMapCreateResponse(t *testing.T) {
126131
}
127132
for _, tt := range tests {
128133
t.Run(tt.description, func(t *testing.T) {
129-
state := &Model{
130-
ProjectId: tt.expected.ProjectId,
134+
model := &Model{
135+
ProjectId: tt.expected.ProjectId,
136+
RotateWhenChanged: types.MapValueMust(types.StringType, map[string]attr.Value{}),
131137
}
132-
err := mapCreateResponse(tt.input, state)
138+
err := mapCreateResponse(tt.input, model)
133139
if !tt.isValid && err == nil {
134140
t.Fatalf("Should have failed")
135141
}
136142
if tt.isValid && err != nil {
137143
t.Fatalf("Should not have failed: %v", err)
138144
}
145+
if tt.isValid {
146+
diff := cmp.Diff(*model, tt.expected)
147+
if diff != "" {
148+
t.Fatalf("Data does not match: %s", diff)
149+
}
150+
}
139151
})
140152
}
141153
}
@@ -155,11 +167,12 @@ func TestMapListResponse(t *testing.T) {
155167
ValidUntil: utils.Ptr(time.Now().Add(24 * time.Hour)),
156168
},
157169
Model{
158-
Id: types.StringValue("pid,aid"),
159-
ProjectId: types.StringValue("pid"),
160-
AccessTokenId: types.StringValue("aid"),
161-
CreatedAt: types.StringValue(time.Now().Format(time.RFC3339)), // Adjusted for test setup time
162-
ValidUntil: types.StringValue(time.Now().Add(24 * time.Hour).Format(time.RFC3339)), // Adjust for format
170+
Id: types.StringValue("pid,aid"),
171+
ProjectId: types.StringValue("pid"),
172+
AccessTokenId: types.StringValue("aid"),
173+
CreatedAt: types.StringValue(time.Now().Format(time.RFC3339)), // Adjusted for test setup time
174+
ValidUntil: types.StringValue(time.Now().Add(24 * time.Hour).Format(time.RFC3339)), // Adjust for format
175+
RotateWhenChanged: types.MapValueMust(types.StringType, map[string]attr.Value{}),
163176
},
164177
true,
165178
},
@@ -190,16 +203,23 @@ func TestMapListResponse(t *testing.T) {
190203

191204
for _, tt := range tests {
192205
t.Run(tt.description, func(t *testing.T) {
193-
state := &Model{
194-
ProjectId: tt.expected.ProjectId,
206+
model := &Model{
207+
ProjectId: tt.expected.ProjectId,
208+
RotateWhenChanged: types.MapValueMust(types.StringType, map[string]attr.Value{}),
195209
}
196-
err := mapListResponse(tt.input, state)
210+
err := mapListResponse(tt.input, model)
197211
if !tt.isValid && err == nil {
198212
t.Fatalf("Expected an error but did not get one")
199213
}
200214
if tt.isValid && err != nil {
201215
t.Fatalf("Did not expect an error but got one: %v", err)
202216
}
217+
if tt.isValid {
218+
diff := cmp.Diff(*model, tt.expected)
219+
if diff != "" {
220+
t.Fatalf("Data does not match: %s", diff)
221+
}
222+
}
203223
})
204224
}
205225
}

0 commit comments

Comments
 (0)