-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Experimental - Possibility to import state into CSB (#1074)
* spike: alternative to subsume [#187405764](https://www.pivotaltracker.com/story/show/187405764) * test: use new uuid package * chore: improve uuid validation * test: the format function is not needed --------- Co-authored-by: George Blue <gblue@pivotal.io> Co-authored-by: Andrea Zucchini <zandrea@vmware.com>
- Loading branch information
1 parent
922d220
commit 9818188
Showing
12 changed files
with
245 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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
36 changes: 36 additions & 0 deletions
36
integrationtest/fixtures/import-state-data/terraform.tfstate
This file contains 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,36 @@ | ||
{ | ||
"version": 4, | ||
"terraform_version": "1.6.2", | ||
"serial": 2, | ||
"lineage": "f4f27494-7de8-a572-4e5b-d2530202be8f", | ||
"outputs": { | ||
"provision_output": { | ||
"value": "831e0be4-7ff3-26ac-751c-80951adb3fe7", | ||
"type": "string" | ||
}, | ||
"status": { | ||
"value": "created random GUID: 831e0be4-7ff3-26ac-751c-80951adb3fe7", | ||
"type": "string" | ||
} | ||
}, | ||
"resources": [ | ||
{ | ||
"mode": "managed", | ||
"type": "random_uuid", | ||
"name": "random", | ||
"provider": "provider[\"registry.terraform.io/hashicorp/random\"]", | ||
"instances": [ | ||
{ | ||
"schema_version": 0, | ||
"attributes": { | ||
"id": "831e0be4-7ff3-26ac-751c-80951adb3fe7", | ||
"keepers": null, | ||
"result": "831e0be4-7ff3-26ac-751c-80951adb3fe7" | ||
}, | ||
"sensitive_attributes": [] | ||
} | ||
] | ||
} | ||
], | ||
"check_results": null | ||
} |
This file contains 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,8 @@ | ||
resource "random_uuid" "random" { | ||
} | ||
|
||
output provision_output { value = random_uuid.random.result } | ||
|
||
output "status" { | ||
value = format("created random GUID: %s", random_uuid.random.result) | ||
} |
25 changes: 25 additions & 0 deletions
25
integrationtest/fixtures/import-state/fake-uuid-service.yml
This file contains 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,25 @@ | ||
version: 1 | ||
name: fake-uuid-service | ||
id: 5b4f6244-f7ee-11ee-b5b3-3389c8712346 | ||
description: description | ||
display_name: Fake | ||
image_url: https://example.com/icon.jpg | ||
documentation_url: https://example.com | ||
support_url: https://example.com/support.html | ||
plans: | ||
- name: default | ||
id: 5b50951a-f7ee-11ee-b564-6b989de50807 | ||
description: default plan | ||
display_name: default | ||
provision: | ||
template_refs: | ||
main: fake-uuid-provision.tf | ||
versions: versions.tf | ||
outputs: | ||
- field_name: provision_output | ||
type: string | ||
details: provision output | ||
user_inputs: | ||
- field_name: value | ||
type: string | ||
details: input |
This file contains 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,18 @@ | ||
packversion: 1 | ||
name: fake-brokerpak | ||
version: 0.1.0 | ||
metadata: | ||
author: noone@nowhere.com | ||
platforms: | ||
- os: linux | ||
arch: amd64 | ||
- os: darwin | ||
arch: amd64 | ||
terraform_binaries: | ||
- name: tofu | ||
version: 1.6.0 | ||
source: https://github.com/opentofu/opentofu/archive/refs/tags/v1.6.0.zip | ||
- name: terraform-provider-random | ||
version: 3.1.0 | ||
service_definitions: | ||
- fake-uuid-service.yml |
This file contains 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,7 @@ | ||
terraform { | ||
required_providers { | ||
random = { | ||
source = "registry.terraform.io/hashicorp/random" | ||
} | ||
} | ||
} |
This file contains 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,85 @@ | ||
package integrationtest_test | ||
|
||
import ( | ||
"bytes" | ||
_ "embed" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/cloudfoundry/cloud-service-broker/v2/dbservice/models" | ||
"github.com/cloudfoundry/cloud-service-broker/v2/integrationtest/packer" | ||
"github.com/cloudfoundry/cloud-service-broker/v2/internal/testdrive" | ||
"github.com/cloudfoundry/cloud-service-broker/v2/pkg/providers/tf/workspace" | ||
"github.com/google/uuid" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/pivotal-cf/brokerapi/v11/domain" | ||
) | ||
|
||
//go:embed "fixtures/import-state-data/terraform.tfstate" | ||
var stateToImport []byte | ||
|
||
var _ = Describe("Import State", func() { | ||
var ( | ||
brokerpak string | ||
broker *testdrive.Broker | ||
) | ||
|
||
BeforeEach(func() { | ||
brokerpak = must(packer.BuildBrokerpak(csb, fixtures("import-state"))) | ||
broker = must(testdrive.StartBroker(csb, brokerpak, database)) | ||
|
||
DeferCleanup(func() { | ||
Expect(broker.Stop()).To(Succeed()) | ||
cleanup(brokerpak) | ||
}) | ||
}) | ||
|
||
It("can create a vacant service instance and import a terraform state", func() { | ||
const ( | ||
serviceOfferingGUID = "5b4f6244-f7ee-11ee-b5b3-3389c8712346" | ||
servicePlanGUID = "5b50951a-f7ee-11ee-b564-6b989de50807" | ||
importedValue = "831e0be4-7ff3-26ac-751c-80951adb3fe7" // matches what's in the test fixture | ||
) | ||
|
||
By("creating a 'vacant' service instance") | ||
instance, err := broker.Provision(serviceOfferingGUID, servicePlanGUID, testdrive.WithProvisionParams(`{"vacant":true}`)) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("checking that the state is empty") | ||
var d models.TerraformDeployment | ||
terraformDeploymentID := fmt.Sprintf("tf:%s:", instance.GUID) | ||
Expect(dbConn.Where("id = ?", terraformDeploymentID).First(&d).Error).To(Succeed()) | ||
var w workspace.TerraformWorkspace | ||
Expect(json.Unmarshal(d.Workspace, &w)).To(Succeed()) | ||
Expect(w.State).To(MatchJSON(`{"version":4}`)) | ||
|
||
By("checking that the `vacant` parameter was not stored") | ||
var i models.ProvisionRequestDetails | ||
Expect(dbConn.Where("service_instance_id = ?", instance.GUID).First(&i).Error).To(Succeed()) | ||
Expect(i.RequestDetails).To(MatchJSON(`{}`)) | ||
|
||
By("importing state into the vacant service instance") | ||
req := must(http.NewRequest(http.MethodPatch, fmt.Sprintf("http://localhost:%d/import_state/%s", broker.Port, instance.GUID), bytes.NewReader(stateToImport))) | ||
req.SetBasicAuth(broker.Username, broker.Password) | ||
importResponse := must(http.DefaultClient.Do(req)) | ||
Expect(importResponse).To(HaveHTTPStatus(http.StatusOK)) | ||
|
||
By("checking that the state was imported into the database") | ||
Expect(dbConn.Where("id = ?", terraformDeploymentID).First(&d).Error).To(Succeed()) | ||
Expect(json.Unmarshal(d.Workspace, &w)).To(Succeed()) | ||
Expect(w.State).To(MatchJSON(stateToImport)) | ||
|
||
By("performing a no-op update to trigger an Apply") | ||
updateResponse := broker.Client.Update(instance.GUID, instance.ServiceOfferingGUID, servicePlanGUID, uuid.NewString(), nil, domain.PreviousValues{}, nil) | ||
Expect(updateResponse.Error).NotTo(HaveOccurred()) | ||
Expect(updateResponse.StatusCode).To(Equal(http.StatusAccepted)) | ||
state, err := broker.LastOperationFinalValue(instance.GUID) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(state.State).To(BeEquivalentTo("succeeded")) | ||
|
||
By("checking that data from the imported state made it into the Last Operation output") | ||
Expect(state.Description).To(Equal(fmt.Sprintf("update succeeded: created random GUID: %s", importedValue))) | ||
}) | ||
}) |
This file contains 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 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 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