Conversation
…-token' into feat/onboard-logs-service-access-token
| 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 |
There was a problem hiding this comment.
You can adjust the error handling in datasources like here:
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() |
There was a problem hiding this comment.
you don't need to read the region directly, you can just use the GetRegionWithOverride function
| 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"])), |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
I am only not sure about the valid_until field as it is not required field, it might be also nil.
There was a problem hiding this comment.
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.
Merging this branch will increase overall coverage
Coverage by fileChanged files (no unit tests)
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
|
| &resp.Diagnostics, | ||
| err, | ||
| "Reading Logs access token", | ||
| fmt.Sprintf("Calling API: %v", err), |
There was a problem hiding this comment.
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"
| 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) |
There was a problem hiding this comment.
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.
| resource.TestCheckResourceAttrSet("data.stackit_logs_access_token.accessToken", "expires"), | ||
| resource.TestCheckResourceAttrSet("data.stackit_logs_access_token.accessToken", "status"), |
There was a problem hiding this comment.
The value of these two attributes can be checked as well. They should have the same value like in the resource
| 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, |
There was a problem hiding this comment.
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
Description
relates to STACKITTPR-449
Checklist
make fmtexamples/directory)make generate-docs(will be checked by CI)make test(will be checked by CI)make lint(will be checked by CI)