diff --git a/provider/dnsdomain/dns_domain_resource.go b/provider/dnsdomain/dns_domain_resource.go index 701831be..ed2ec6d8 100644 --- a/provider/dnsdomain/dns_domain_resource.go +++ b/provider/dnsdomain/dns_domain_resource.go @@ -54,6 +54,10 @@ func (r *DNSDomainResource) Schema(ctx context.Context, req resource.SchemaReque Description: "Unique identifier of the DNS Domain", Computed: true, }, + "cluster_arch": schema.StringAttribute{ + Description: "Identifies the cluster architecture of the dns domain", + Optional: true, + }, }, } } @@ -107,14 +111,21 @@ func (r *DNSDomainResource) Read(ctx context.Context, req resource.ReadRequest, } object := get.Body() + populateState(object, state) + // Save the state + diags = resp.State.Set(ctx, state) + resp.Diagnostics.Append(diags...) +} + +func populateState(object *cmv1.DNSDomain, state *DNSDomainState) { if id, ok := object.GetID(); ok { state.ID = types.StringValue(id) } - // Save the state - diags = resp.State.Set(ctx, state) - resp.Diagnostics.Append(diags...) + if arch, ok := object.GetClusterArch(); !state.ClusterArch.IsNull() && ok { + state.ClusterArch = types.StringValue(string(arch)) + } } func (r *DNSDomainResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { @@ -127,6 +138,9 @@ func (r *DNSDomainResource) Create(ctx context.Context, req resource.CreateReque } dnsDomain := ocmr.NewDNSDomain(r.collection) + if !plan.ClusterArch.IsNull() { + dnsDomain.GetDNSDomainBuilder().ClusterArch(cmv1.ClusterArchitecture(plan.ClusterArch.ValueString())) + } createResp, err := dnsDomain.Create() if err != nil { @@ -134,12 +148,7 @@ func (r *DNSDomainResource) Create(ctx context.Context, req resource.CreateReque return } - if id, ok := createResp.Body().GetID(); ok { - plan.ID = types.StringValue(id) - } else { - resp.Diagnostics.AddError("Failed to create DNS Domain.", "Failed to get an ID") - return - } + populateState(createResp.Body(), plan) // Save the state diags = resp.State.Set(ctx, plan) diff --git a/provider/dnsdomain/dns_domain_state.go b/provider/dnsdomain/dns_domain_state.go index a90bb82d..26e8848b 100644 --- a/provider/dnsdomain/dns_domain_state.go +++ b/provider/dnsdomain/dns_domain_state.go @@ -21,5 +21,6 @@ import ( ) type DNSDomainState struct { - ID types.String `tfsdk:"id"` + ID types.String `tfsdk:"id"` + ClusterArch types.String `tfsdk:"cluster_arch"` } diff --git a/subsystem/classic/dns_domain_resource_test.go b/subsystem/classic/dns_domain_resource_test.go index a3743346..8675dcb7 100644 --- a/subsystem/classic/dns_domain_resource_test.go +++ b/subsystem/classic/dns_domain_resource_test.go @@ -22,6 +22,7 @@ import ( . "github.com/onsi/ginkgo/v2/dsl/core" . "github.com/onsi/gomega" . "github.com/onsi/gomega/ghttp" + v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" . "github.com/openshift-online/ocm-sdk-go/testing" . "github.com/terraform-redhat/terraform-provider-rhcs/subsystem/framework" ) @@ -30,36 +31,77 @@ var _ = Describe("DNS Domain creation", func() { domain := "my.domain.openshift.dev" Context("Verify success", func() { - It("Should create a DNS domain", func() { - // Prepare the server: - TestServer.AppendHandlers( - // first post (create) - CombineHandlers( - VerifyRequest( - http.MethodPost, - "/api/clusters_mgmt/v1/dns_domains", - ), - VerifyJSON(`{ + When("cluster arch is not specified does not set state", func() { + It("Should create a DNS domain", func() { + // Prepare the server: + TestServer.AppendHandlers( + // first post (create) + CombineHandlers( + VerifyRequest( + http.MethodPost, + "/api/clusters_mgmt/v1/dns_domains", + ), + VerifyJSON(`{ "kind": "DNSDomain" }`), - RespondWithJSON(http.StatusOK, `{ + RespondWithJSON(http.StatusOK, `{ "kind": "DNSDomain", "href": "/api/clusters_mgmt/v1/dns_domains/`+domain+`", - "id": "`+domain+`" + "id": "`+domain+`", + "cluster_arch": "`+string(v1.ClusterArchitectureClassic)+`" }`), - ), - ) + ), + ) - Terraform.Source(` + Terraform.Source(` resource "rhcs_dns_domain" "dns" { # (resource arguments) } `) - runOutput := Terraform.Apply() - Expect(runOutput.ExitCode).To(BeZero()) - resource := Terraform.Resource("rhcs_dns_domain", "dns") - Expect(resource).To(MatchJQ(".attributes.id", domain)) + runOutput := Terraform.Apply() + Expect(runOutput.ExitCode).To(BeZero()) + resource := Terraform.Resource("rhcs_dns_domain", "dns") + Expect(resource).To(MatchJQ(".attributes.id", domain)) + Expect(resource).To(MatchJQ(".attributes.cluster_arch", nil)) + }) + }) + + When("cluster arch is specified sets state", func() { + It("Should create a DNS domain", func() { + // Prepare the server: + TestServer.AppendHandlers( + // first post (create) + CombineHandlers( + VerifyRequest( + http.MethodPost, + "/api/clusters_mgmt/v1/dns_domains", + ), + VerifyJSON(`{ + "kind": "DNSDomain", + "cluster_arch": "classic" + }`), + RespondWithJSON(http.StatusOK, `{ + "kind": "DNSDomain", + "href": "/api/clusters_mgmt/v1/dns_domains/`+domain+`", + "id": "`+domain+`", + "cluster_arch": "`+string(v1.ClusterArchitectureClassic)+`" + }`), + ), + ) + + Terraform.Source(` + resource "rhcs_dns_domain" "dns" { + cluster_arch = "classic" + } + `) + + runOutput := Terraform.Apply() + Expect(runOutput.ExitCode).To(BeZero()) + resource := Terraform.Resource("rhcs_dns_domain", "dns") + Expect(resource).To(MatchJQ(".attributes.id", domain)) + Expect(resource).To(MatchJQ(".attributes.cluster_arch", string(v1.ClusterArchitectureClassic))) + }) }) It("Should recreate a DNS domain on 404 (reconcile)", func() { diff --git a/subsystem/hcp/dns_domain_resource_test.go b/subsystem/hcp/dns_domain_resource_test.go new file mode 100644 index 00000000..3e33ba16 --- /dev/null +++ b/subsystem/hcp/dns_domain_resource_test.go @@ -0,0 +1,71 @@ +/* +Copyright (c) 2021 Red Hat, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package hcp + +import ( + "net/http" + + . "github.com/onsi/ginkgo/v2/dsl/core" + . "github.com/onsi/gomega" + . "github.com/onsi/gomega/ghttp" + v1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" + . "github.com/openshift-online/ocm-sdk-go/testing" + . "github.com/terraform-redhat/terraform-provider-rhcs/subsystem/framework" +) + +var _ = Describe("DNS Domain creation", func() { + domain := "my.domain.openshift.dev" + + Context("Verify success", func() { + When("cluster arch is specified sets state", func() { + It("Should create a DNS domain", func() { + // Prepare the server: + TestServer.AppendHandlers( + // first post (create) + CombineHandlers( + VerifyRequest( + http.MethodPost, + "/api/clusters_mgmt/v1/dns_domains", + ), + VerifyJSON(`{ + "kind": "DNSDomain", + "cluster_arch": "hcp" + }`), + RespondWithJSON(http.StatusOK, `{ + "kind": "DNSDomain", + "href": "/api/clusters_mgmt/v1/dns_domains/`+domain+`", + "id": "`+domain+`", + "cluster_arch": "`+string(v1.ClusterArchitectureHcp)+`" + }`), + ), + ) + + Terraform.Source(` + resource "rhcs_dns_domain" "dns" { + cluster_arch = "hcp" + } + `) + + runOutput := Terraform.Apply() + Expect(runOutput.ExitCode).To(BeZero()) + resource := Terraform.Resource("rhcs_dns_domain", "dns") + Expect(resource).To(MatchJQ(".attributes.id", domain)) + Expect(resource).To(MatchJQ(".attributes.cluster_arch", string(v1.ClusterArchitectureHcp))) + }) + }) + }) +})