Skip to content

Commit

Permalink
OCM-3102: Fixing a bug in while updating the Proxy attribute
Browse files Browse the repository at this point in the history
While updating the Proxy need to initialize the state.Proxy object if this is the first time to be set.
  • Loading branch information
bardielle authored and openshift-merge-robot committed Aug 24, 2023
1 parent 97804f5 commit 7775b08
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 46 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/terraform-redhat/terraform-provider-rhcs
go 1.18

require (
github.com/aws/aws-sdk-go v1.39.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-framework v0.5.0
Expand All @@ -25,7 +26,6 @@ require (
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/aws/aws-sdk-go v1.39.3 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions provider/cluster_rosa_classic_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,9 @@ func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster,

proxy, ok := object.GetProxy()
if ok {
if state.Proxy == nil {
state.Proxy = &Proxy{}
}
httpProxy, ok := proxy.GetHTTPProxy()
if ok {
state.Proxy.HttpProxy = types.String{
Expand All @@ -1729,6 +1732,9 @@ func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster,

trustBundle, ok := object.GetAdditionalTrustBundle()
if ok {
if state.Proxy == nil {
state.Proxy = &Proxy{}
}
state.Proxy.AdditionalTrustBundle = types.String{
Value: trustBundle,
}
Expand Down
254 changes: 209 additions & 45 deletions subsystem/cluster_resource_rosa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1499,23 +1499,24 @@ var _ = Describe("rhcs_cluster_rosa_classic - create", func() {

})

It("Creates cluster with http proxy and update it", func() {
// Prepare the server:
server.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1/versions"),
RespondWithJSON(http.StatusOK, versionListPage1),
),
CombineHandlers(
VerifyRequest(http.MethodPost, "/api/clusters_mgmt/v1/clusters"),
VerifyJQ(`.name`, "my-cluster"),
VerifyJQ(`.cloud_provider.id`, "aws"),
VerifyJQ(`.region.id`, "us-west-1"),
VerifyJQ(`.product.id`, "rosa"),
VerifyJQ(`.proxy.http_proxy`, "http://proxy.com"),
VerifyJQ(`.proxy.https_proxy`, "https://proxy.com"),
VerifyJQ(`.additional_trust_bundle`, "123"),
RespondWithPatchedJSON(http.StatusOK, template, `[
Context("Test Proxy", func() {
It("Creates cluster with http proxy and update it", func() {
// Prepare the server:
server.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1/versions"),
RespondWithJSON(http.StatusOK, versionListPage1),
),
CombineHandlers(
VerifyRequest(http.MethodPost, "/api/clusters_mgmt/v1/clusters"),
VerifyJQ(`.name`, "my-cluster"),
VerifyJQ(`.cloud_provider.id`, "aws"),
VerifyJQ(`.region.id`, "us-west-1"),
VerifyJQ(`.product.id`, "rosa"),
VerifyJQ(`.proxy.http_proxy`, "http://proxy.com"),
VerifyJQ(`.proxy.https_proxy`, "https://proxy.com"),
VerifyJQ(`.additional_trust_bundle`, "123"),
RespondWithPatchedJSON(http.StatusOK, template, `[
{
"op": "add",
"path": "/aws",
Expand Down Expand Up @@ -1558,11 +1559,11 @@ var _ = Describe("rhcs_cluster_rosa_classic - create", func() {
}
}
}]`),
),
)
),
)

// Run the apply command:
terraform.Source(`
// Run the apply command:
terraform.Source(`
resource "rhcs_cluster_rosa_classic" "my_cluster" {
name = "my-cluster"
cloud_region = "us-west-1"
Expand All @@ -1584,14 +1585,14 @@ var _ = Describe("rhcs_cluster_rosa_classic - create", func() {
}
`)

Expect(terraform.Apply()).To(BeZero())
Expect(terraform.Apply()).To(BeZero())

// apply for update the proxy's attributes
// Prepare the server:
server.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1/clusters/123"),
RespondWithPatchedJSON(http.StatusOK, template, `[
// apply for update the proxy's attributes
// Prepare the server:
server.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1/clusters/123"),
RespondWithPatchedJSON(http.StatusOK, template, `[
{
"op": "add",
"path": "/aws",
Expand Down Expand Up @@ -1634,13 +1635,13 @@ var _ = Describe("rhcs_cluster_rosa_classic - create", func() {
}
}
}]`),
),
CombineHandlers(
VerifyRequest(http.MethodPatch, "/api/clusters_mgmt/v1/clusters/123"),
VerifyJQ(`.proxy.https_proxy`, "https://proxy2.com"),
VerifyJQ(`.proxy.no_proxy`, "test"),
VerifyJQ(`.additional_trust_bundle`, "123"),
RespondWithPatchedJSON(http.StatusOK, template, `[
),
CombineHandlers(
VerifyRequest(http.MethodPatch, "/api/clusters_mgmt/v1/clusters/123"),
VerifyJQ(`.proxy.https_proxy`, "https://proxy2.com"),
VerifyJQ(`.proxy.no_proxy`, "test"),
VerifyJQ(`.additional_trust_bundle`, "123"),
RespondWithPatchedJSON(http.StatusOK, template, `[
{
"op": "add",
"path": "/aws",
Expand Down Expand Up @@ -1683,11 +1684,11 @@ var _ = Describe("rhcs_cluster_rosa_classic - create", func() {
}
}
}]`),
),
)
),
)

// update the attribute "proxy"
terraform.Source(`
// update the attribute "proxy"
terraform.Source(`
resource "rhcs_cluster_rosa_classic" "my_cluster" {
name = "my-cluster"
cloud_region = "us-west-1"
Expand All @@ -1708,13 +1709,176 @@ var _ = Describe("rhcs_cluster_rosa_classic - create", func() {
}
}
`)
Expect(terraform.Apply()).To(BeZero())
resource := terraform.Resource("rhcs_cluster_rosa_classic", "my_cluster")
Expect(resource).To(MatchJQ(`.attributes.proxy.https_proxy`, "https://proxy2.com"))
Expect(resource).To(MatchJQ(`.attributes.proxy.no_proxy`, "test"))
Expect(resource).To(MatchJQ(`.attributes.proxy.additional_trust_bundle`, "123"))
})
Expect(terraform.Apply()).To(BeZero())
resource := terraform.Resource("rhcs_cluster_rosa_classic", "my_cluster")
Expect(resource).To(MatchJQ(`.attributes.proxy.https_proxy`, "https://proxy2.com"))
Expect(resource).To(MatchJQ(`.attributes.proxy.no_proxy`, "test"))
Expect(resource).To(MatchJQ(`.attributes.proxy.additional_trust_bundle`, "123"))
})
It("Creates cluster without http proxy and update trust bundle", func() {
// Prepare the server:
server.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1/versions"),
RespondWithJSON(http.StatusOK, versionListPage1),
),
CombineHandlers(
VerifyRequest(http.MethodPost, "/api/clusters_mgmt/v1/clusters"),
VerifyJQ(`.name`, "my-cluster"),
VerifyJQ(`.cloud_provider.id`, "aws"),
VerifyJQ(`.region.id`, "us-west-1"),
VerifyJQ(`.product.id`, "rosa"),
RespondWithPatchedJSON(http.StatusOK, template, `[
{
"op": "add",
"path": "/aws",
"value": {
"sts" : {
"oidc_endpoint_url": "https://127.0.0.2",
"thumbprint": "111111",
"role_arn": "",
"support_role_arn": "",
"instance_iam_roles" : {
"master_role_arn" : "",
"worker_role_arn" : ""
},
"operator_role_prefix" : "test"
}
}
},
{
"op": "add",
"path": "/nodes",
"value": {
"compute": 3,
"compute_machine_type": {
"id": "r5.xlarge"
}
}
}]`),
),
)

// Run the apply command:
terraform.Source(`
resource "rhcs_cluster_rosa_classic" "my_cluster" {
name = "my-cluster"
cloud_region = "us-west-1"
aws_account_id = "123"
sts = {
operator_role_prefix = "test"
role_arn = "",
support_role_arn = "",
instance_iam_roles = {
master_role_arn = "",
worker_role_arn = "",
}
}
}
`)

Expect(terraform.Apply()).To(BeZero())

// apply for update the proxy's attributes
// Prepare the server:
server.AppendHandlers(
CombineHandlers(
VerifyRequest(http.MethodGet, "/api/clusters_mgmt/v1/clusters/123"),
RespondWithPatchedJSON(http.StatusOK, template, `[
{
"op": "add",
"path": "/aws",
"value": {
"sts" : {
"oidc_endpoint_url": "https://127.0.0.2",
"thumbprint": "111111",
"role_arn": "",
"support_role_arn": "",
"instance_iam_roles" : {
"master_role_arn" : "",
"worker_role_arn" : ""
},
"operator_role_prefix" : "test"
}
}
},
{
"op": "add",
"path": "/nodes",
"value": {
"compute": 3,
"compute_machine_type": {
"id": "r5.xlarge"
}
}
}]`),
),
CombineHandlers(
VerifyRequest(http.MethodPatch, "/api/clusters_mgmt/v1/clusters/123"),
VerifyJQ(`.additional_trust_bundle`, "123"),
RespondWithPatchedJSON(http.StatusOK, template, `[
{
"op": "add",
"path": "/aws",
"value": {
"sts" : {
"oidc_endpoint_url": "https://127.0.0.2",
"thumbprint": "111111",
"role_arn": "",
"support_role_arn": "",
"instance_iam_roles" : {
"master_role_arn" : "",
"worker_role_arn" : ""
},
"operator_role_prefix" : "test"
}
}
},
{
"op": "add",
"path": "/",
"value": {
"additional_trust_bundle" : "123"
}
},
{
"op": "add",
"path": "/nodes",
"value": {
"compute": 3,
"compute_machine_type": {
"id": "r5.xlarge"
}
}
}]`),
),
)

// update the attribute "proxy"
terraform.Source(`
resource "rhcs_cluster_rosa_classic" "my_cluster" {
name = "my-cluster"
cloud_region = "us-west-1"
aws_account_id = "123"
proxy = {
additional_trust_bundle = "123",
}
sts = {
operator_role_prefix = "test"
role_arn = "",
support_role_arn = "",
instance_iam_roles = {
master_role_arn = "",
worker_role_arn = "",
}
}
}
`)
Expect(terraform.Apply()).To(BeZero())
resource := terraform.Resource("rhcs_cluster_rosa_classic", "my_cluster")
Expect(resource).To(MatchJQ(`.attributes.proxy.additional_trust_bundle`, "123"))
})
})
It("Creates cluster with default_mp_labels and update them", func() {
// Prepare the server:
server.AppendHandlers(
Expand Down

0 comments on commit 7775b08

Please sign in to comment.