⚠ This page is served via a proxy. Original site: https://github.com
This service does not collect credentials or authentication data.
Skip to content

Onboard logs service access token#1165

Open
GokceGK wants to merge 13 commits intomainfrom
feat/onboard-logs-service-access-token
Open

Onboard logs service access token#1165
GokceGK wants to merge 13 commits intomainfrom
feat/onboard-logs-service-access-token

Conversation

@GokceGK
Copy link
Contributor

@GokceGK GokceGK commented Jan 28, 2026

Description

relates to STACKITTPR-449

Checklist

  • Issue was linked above
  • Code format was applied: make fmt
  • Examples were added / adjusted (see examples/ directory)
  • Docs are up-to-date: make generate-docs (will be checked by CI)
  • Unit tests got implemented or updated
  • Acceptance tests got implemented or updated (see e.g. here)
  • Unit tests are passing: make test (will be checked by CI)
  • No linter issues: make lint (will be checked by CI)

@GokceGK GokceGK requested a review from a team as a code owner January 28, 2026 18:19
@GokceGK GokceGK marked this pull request as draft January 28, 2026 18:19
@GokceGK GokceGK marked this pull request as ready for review February 4, 2026 11:11
Comment on lines 169 to 176
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
if ok && oapiErr.StatusCode == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading Logs access token", fmt.Sprintf("Calling API: %v", err))
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can adjust the error handling in datasources like here:

if err != nil {
utils.LogError(
ctx,
&resp.Diagnostics,
err,
"Reading network",
fmt.Sprintf("Network with ID %q does not exist in project %q.", networkId, projectId),
map[int]string{
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId),
},
)
resp.State.RemoveResource(ctx)

This makes it easier to handle different kinds of error, like for example 403


instanceId := model.InstanceID.ValueString()
projectId := model.ProjectID.ValueString()
region := model.Region.ValueString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to read the region directly, you can just use the GetRegionWithOverride function

Suggested change
region := model.Region.ValueString()
region := r.providerData.GetRegionWithOverride(model.Region)

and remove the second call to this function, which is before the call to CreateAccessToken()

"stackit_logs_instance.logs", "instance_id",
),
resource.TestCheckResourceAttr("stackit_logs_access_token.accessToken", "display_name", testutil.ConvertConfigVariable(testConfigAccessTokenVarsMin["display_name"])),
resource.TestCheckResourceAttr("stackit_logs_access_token.accessToken", "permissions.0", testutil.ConvertConfigVariable(testConfigAccessTokenVarsMin["permissions"])),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some attribute checks are missing here like: "creator", "expires", "id", "status", "valid_until".
For most of them it's enough if you check if they are just set, because you may don't know the value they will have. Except expires, there you should know if it's false or true and same for status, there it will be probably always "active"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am only not sure about the valid_until field as it is not required field, it might be also nil.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't tested it yet, but I assume that valid_until is set when expires is true and is not set when expires is false.

@github-actions
Copy link

github-actions bot commented Feb 4, 2026

Merging this branch will increase overall coverage

Impacted Packages Coverage Δ 🤖
github.com/stackitcloud/terraform-provider-stackit/stackit 1.48% (ø)
github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs 0.00% (ø)
github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/accesstoken 24.73% (+24.73%) 🌟

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/accesstoken/datasource.go 37.14% (+37.14%) 70 (+70) 26 (+26) 44 (+44) 🌟
github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/accesstoken/resource.go 20.49% (+20.49%) 205 (+205) 42 (+42) 163 (+163) 🌟
github.com/stackitcloud/terraform-provider-stackit/stackit/provider.go 1.48% (ø) 135 2 133

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

Changed unit test files

  • github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/accesstoken/datasource_test.go
  • github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/accesstoken/resource_test.go
  • github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/logs_acc_test.go

&resp.Diagnostics,
err,
"Reading Logs access token",
fmt.Sprintf("Calling API: %v", err),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default error message and will mostly be a 404.
So the message should be something like "Access token with ID $q does not exist in project %q"

Comment on lines 309 to 324
var oapiErr *oapierror.GenericOpenAPIError
ok := errors.As(err, &oapiErr)
if ok && oapiErr.StatusCode == http.StatusNotFound {
resp.State.RemoveResource(ctx)
return
}
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading Logs access token", fmt.Sprintf("Calling API: %v", err))
tfutils.LogError(
ctx,
&resp.Diagnostics,
err,
"Reading Logs access token",
fmt.Sprintf("Calling API: %v", err),
map[int]string{
http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectID),
},
)
resp.State.RemoveResource(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert this change? The error logging with the LogError function should only be used in the datasource, because in the datasource we don't care about the exact status code and remove there always the resource from the state. There we can easily "restore" the state by running again $ terraform apply.
But in the resource we should remove the resource from the state only when we get a 404. So we need to explicit check here, which status code was sent.

Comment on lines 440 to 441
resource.TestCheckResourceAttrSet("data.stackit_logs_access_token.accessToken", "expires"),
resource.TestCheckResourceAttrSet("data.stackit_logs_access_token.accessToken", "status"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value of these two attributes can be checked as well. They should have the same value like in the resource

Suggested change
resource.TestCheckResourceAttrSet("data.stackit_logs_access_token.accessToken", "expires"),
resource.TestCheckResourceAttrSet("data.stackit_logs_access_token.accessToken", "status"),
resource.TestCheckResourceAttr("data.stackit_logs_access_token.accessToken", "expires", testutil.ConvertConfigVariable(testConfigAccessTokenVarsMin["expires"])),
resource.TestCheckResourceAttr("data.stackit_logs_access_token.accessToken", "status", testutil.ConvertConfigVariable(testConfigAccessTokenVarsMin["status"])),

"permissions": schema.ListAttribute{
Description: schemaDescriptions["permissions"],
ElementType: types.StringType,
Required: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: you can add here a validator to check if the size of the list is always at least 1. Because when it's an empty list, the api throws an error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants