-
Notifications
You must be signed in to change notification settings - Fork 67
feat(opensearch) instance + credential, save IDs immediately after cr… #1246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cgoetz-inovex
merged 13 commits into
main
from
feat/STACKITTPR-388_store_ids_opensearch
Feb 27, 2026
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
243f831
fix(rabbitmq): Store IDs immediately after provisioning
cgoetz-inovex 3e9999f
chore(rabbitmq) write tests for saving IDs on create error
cgoetz-inovex f5fc4e5
fix(lint) ignore write error in mockserver
cgoetz-inovex bde77f8
fix(lint) add explanation to ignore comment
cgoetz-inovex f745838
feat(scf) save IDs before calling the waiter in Create
cgoetz-inovex 9764542
Merge branch 'main' into feat/STACKITTPR-393_store_ids_scf
cgoetz-inovex f361273
feat(opensearch) instance + credential, save IDs immediately after cr…
cgoetz-inovex 9f61fb6
Merge branch 'main' into feat/STACKITTPR-388_store_ids_opensearch
cgoetz-inovex 7267364
chore(opensearch) move SavesIDsOnError tests into new file, document
cgoetz-inovex eca638f
Merge branch 'main' into feat/STACKITTPR-388_store_ids_opensearch
cgoetz-inovex bcbdd65
Merge branch 'main' into feat/STACKITTPR-388_store_ids_opensearch
cgoetz-inovex b60fb6c
Merge branch 'main' into feat/STACKITTPR-388_store_ids_opensearch
cgoetz-inovex 102fc4a
Update .github/docs/contribution-guide/package_test.go
cgoetz-inovex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package foo | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "regexp" | ||
| "testing" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil" | ||
| ) | ||
|
|
||
| func TestFooSavesIDsOnError(t *testing.T) { | ||
| /* Setup code: | ||
| - define known values for attributes used in id | ||
| - create mock server | ||
| - define minimal tf config with custom endpoint pointing to mock server | ||
| */ | ||
| var ( | ||
| projectId = uuid.NewString() | ||
| barId = uuid.NewString() | ||
| ) | ||
| const region = "eu01" | ||
| s := testutil.NewMockServer(t) | ||
| defer s.Server.Close() | ||
| tfConfig := fmt.Sprintf(` | ||
| provider "stackit" { | ||
| foo_custom_endpoint = "%s" | ||
| service_account_token = "mock-server-needs-no-auth" | ||
| } | ||
|
|
||
| resource "stackit_foo" "foo" { | ||
| project_id = "%s" | ||
| } | ||
| `, s.Server.URL, projectId) | ||
|
|
||
| /* Test steps: | ||
| 1. Create resource with mocked backend | ||
| 2. Verify with a refresh, that IDs are saved to state | ||
| */ | ||
| resource.UnitTest(t, resource.TestCase{ | ||
| ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| PreConfig: func() { | ||
| /* Setup mock responses for create and waiter. | ||
| The create response succeeds and returns the barId, but the waiter fails with an error. | ||
| We can't check the state in this step, because the create returns early due to the waiter error. | ||
| TF won't execute any Checks of the TestStep if there is an error. | ||
| */ | ||
| s.Reset( | ||
| testutil.MockResponse{ | ||
| Description: "create foo", | ||
| ToJsonBody: &BarResponse{ | ||
| BarId: barId, | ||
| }, | ||
| }, | ||
| testutil.MockResponse{Description: "failing waiter", StatusCode: http.StatusInternalServerError}, | ||
| ) | ||
| }, | ||
| Config: tfConfig, | ||
| ExpectError: regexp.MustCompile("Error creating foo.*"), | ||
| }, | ||
| { | ||
| PreConfig: func() { | ||
| /* Setup mock responses for refresh and delete. | ||
| The refresh response fails with an error, but we want to verify that the URL contains the correct IDs. | ||
| After the test TF will automatically destroy the resource. So we set up mocks to simulate a successful dlete. | ||
| */ | ||
| s.Reset( | ||
| testutil.MockResponse{ | ||
| Description: "refresh", | ||
| Handler: func(w http.ResponseWriter, req *http.Request) { | ||
| expected := fmt.Sprintf("/v1/projects/%s/regions/%s/foo/%s", projectId, region, barId) | ||
| if req.URL.Path != expected { | ||
| t.Errorf("unexpected URL path: got %s, want %s", req.URL.Path, expected) | ||
| } | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| }, | ||
| }, | ||
| testutil.MockResponse{Description: "delete"}, | ||
| testutil.MockResponse{Description: "delete waiter", StatusCode: http.StatusGone}, | ||
| ) | ||
| }, | ||
| RefreshState: true, | ||
| ExpectError: regexp.MustCompile("Error reading foo.*"), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
stackit/internal/services/opensearch/opensearch_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package opensearch | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "regexp" | ||
| "testing" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/stackitcloud/stackit-sdk-go/core/utils" | ||
| "github.com/stackitcloud/stackit-sdk-go/services/opensearch" | ||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil" | ||
| ) | ||
|
|
||
| func TestOpensearchInstanceSavesIDsOnError(t *testing.T) { | ||
| var ( | ||
| projectId = uuid.NewString() | ||
| instanceId = uuid.NewString() | ||
| ) | ||
| const ( | ||
| name = "opensearch-instance-test" | ||
| version = "version" | ||
| planName = "plan-name" | ||
| ) | ||
| s := testutil.NewMockServer(t) | ||
| defer s.Server.Close() | ||
| tfConfig := fmt.Sprintf(` | ||
| provider "stackit" { | ||
| opensearch_custom_endpoint = "%s" | ||
| service_account_token = "mock-server-needs-no-auth" | ||
| } | ||
|
|
||
| resource "stackit_opensearch_instance" "instance" { | ||
| project_id = "%s" | ||
| name = "%s" | ||
| version = "%s" | ||
| plan_name = "%s" | ||
| } | ||
| `, s.Server.URL, projectId, name, version, planName) | ||
|
|
||
| resource.UnitTest(t, resource.TestCase{ | ||
| ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| PreConfig: func() { | ||
| s.Reset( | ||
| testutil.MockResponse{ | ||
| Description: "offerings", | ||
| ToJsonBody: &opensearch.ListOfferingsResponse{ | ||
| Offerings: &[]opensearch.Offering{ | ||
| { | ||
| Name: utils.Ptr("offering-name"), | ||
| Version: utils.Ptr(version), | ||
| Plans: &[]opensearch.Plan{ | ||
| { | ||
| Id: utils.Ptr("plan-id"), | ||
| Name: utils.Ptr(planName), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| testutil.MockResponse{ | ||
| Description: "create instance", | ||
| ToJsonBody: &opensearch.CreateInstanceResponse{ | ||
| InstanceId: utils.Ptr(instanceId), | ||
| }, | ||
| }, | ||
| testutil.MockResponse{Description: "failing waiter", StatusCode: http.StatusInternalServerError}, | ||
| ) | ||
| }, | ||
| Config: tfConfig, | ||
| ExpectError: regexp.MustCompile("Error creating instance.*"), | ||
| }, | ||
| { | ||
| PreConfig: func() { | ||
| s.Reset( | ||
| testutil.MockResponse{ | ||
| Description: "refresh", | ||
| Handler: func(w http.ResponseWriter, req *http.Request) { | ||
| expected := fmt.Sprintf("/v1/projects/%s/instances/%s", projectId, instanceId) | ||
| if req.URL.Path != expected { | ||
| t.Errorf(fmt.Sprintf("unexpected URL path: got %s, want %s", req.URL.Path, expected), http.StatusBadRequest) | ||
| } | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| }, | ||
| }, | ||
| testutil.MockResponse{Description: "delete"}, | ||
| testutil.MockResponse{Description: "delete waiter", StatusCode: http.StatusGone}, | ||
| ) | ||
| }, | ||
| RefreshState: true, | ||
| ExpectError: regexp.MustCompile("Error reading instance.*"), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func TestOpensearchCredentialSavesIDsOnError(t *testing.T) { | ||
| var ( | ||
| projectId = uuid.NewString() | ||
| instanceId = uuid.NewString() | ||
| credentialId = uuid.NewString() | ||
| ) | ||
| s := testutil.NewMockServer(t) | ||
| defer s.Server.Close() | ||
| tfConfig := fmt.Sprintf(` | ||
| provider "stackit" { | ||
| opensearch_custom_endpoint = "%s" | ||
| service_account_token = "mock-server-needs-no-auth" | ||
| } | ||
|
|
||
| resource "stackit_opensearch_credential" "credential" { | ||
| project_id = "%s" | ||
| instance_id = "%s" | ||
| } | ||
| `, s.Server.URL, projectId, instanceId) | ||
|
|
||
| resource.UnitTest(t, resource.TestCase{ | ||
| ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| PreConfig: func() { | ||
| s.Reset( | ||
| testutil.MockResponse{ | ||
| Description: "create credential", | ||
| ToJsonBody: &opensearch.CredentialsResponse{ | ||
| Id: utils.Ptr(credentialId), | ||
| }, | ||
| }, | ||
| testutil.MockResponse{Description: "create waiter", StatusCode: http.StatusInternalServerError}, | ||
| ) | ||
| }, | ||
| Config: tfConfig, | ||
| ExpectError: regexp.MustCompile("Error creating credential.*"), | ||
| }, | ||
| { | ||
| PreConfig: func() { | ||
| s.Reset( | ||
| testutil.MockResponse{ | ||
| Description: "refresh", | ||
| Handler: func(w http.ResponseWriter, req *http.Request) { | ||
| expected := fmt.Sprintf("/v1/projects/%s/instances/%s/credentials/%s", projectId, instanceId, credentialId) | ||
| if req.URL.Path != expected { | ||
| t.Errorf(fmt.Sprintf("unexpected URL path: got %s, want %s", req.URL.Path, expected), http.StatusBadRequest) | ||
| } | ||
| w.WriteHeader(http.StatusInternalServerError) | ||
| }, | ||
| }, | ||
| testutil.MockResponse{Description: "delete"}, | ||
| testutil.MockResponse{Description: "delete waiter", StatusCode: http.StatusGone}, | ||
| ) | ||
| }, | ||
| RefreshState: true, | ||
| ExpectError: regexp.MustCompile("Error reading credential.*"), | ||
| }, | ||
| }, | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.