Skip to content

Commit

Permalink
OCM-12856 | feat: support arch attribute in dns domain
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbranco committed Nov 28, 2024
1 parent 7f69edb commit 923e44b
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 29 deletions.
4 changes: 4 additions & 0 deletions docs/resources/dns_domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ resource "rhcs_dns_domain" "dns_domain" {}
<!-- schema generated by tfplugindocs -->
## Schema

### Optional

- `cluster_arch` (String) Identifies the cluster architecture of the dns domain

### Read-Only

- `id` (String) Unique identifier of the DNS Domain
27 changes: 18 additions & 9 deletions provider/dnsdomain/dns_domain_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -127,19 +138,17 @@ 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 {
resp.Diagnostics.AddError("Failed to create DNS Domain.", err.Error())
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)
Expand Down
3 changes: 2 additions & 1 deletion provider/dnsdomain/dns_domain_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ import (
)

type DNSDomainState struct {
ID types.String `tfsdk:"id"`
ID types.String `tfsdk:"id"`
ClusterArch types.String `tfsdk:"cluster_arch"`
}
80 changes: 61 additions & 19 deletions subsystem/classic/dns_domain_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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() {
Expand Down
71 changes: 71 additions & 0 deletions subsystem/hcp/dns_domain_resource_test.go
Original file line number Diff line number Diff line change
@@ -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)))
})
})
})
})

0 comments on commit 923e44b

Please sign in to comment.