Skip to content
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

OCM-12856 | feat: support arch attribute in dns domain #854

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/onsi/ginkgo/v2 v2.17.1
github.com/onsi/gomega v1.30.0
github.com/openshift-online/ocm-common v0.0.12
github.com/openshift-online/ocm-sdk-go v0.1.443
github.com/openshift-online/ocm-sdk-go v0.1.448
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
github.com/thoas/go-funk v0.9.3
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
Expand All @@ -262,7 +261,6 @@ github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20240525223248-4bfdf5a9a2af/go.mod h1:K1liHPHnj73Fdn/EKuT8nrFqBihUSKXoLYU0BuatOYo=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -500,12 +498,10 @@ github.com/onsi/ginkgo/v2 v2.17.1 h1:V++EzdbhI4ZV4ev0UTIj0PzhzOcReJFyJaLjtSF55M8
github.com/onsi/ginkgo/v2 v2.17.1/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.30.0 h1:hvMK7xYz4D3HapigLTeGdId/NcfQx1VHMJc60ew99+8=
github.com/onsi/gomega v1.30.0/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
github.com/openshift-online/ocm-common v0.0.11 h1:DOj7fB59q0vAUFxSEQpLPp2AkReCCFq3r3NMaoZU20I=
github.com/openshift-online/ocm-common v0.0.11/go.mod h1:6MWje2NFNJ3IWpGs7BYj6DWagWXHyp8EnmYY7XFTtI4=
github.com/openshift-online/ocm-common v0.0.12 h1:2oGZCXd8O/nZVlM0pvTtm3hDGbW/ncTtvSLLB+nuQf0=
github.com/openshift-online/ocm-common v0.0.12/go.mod h1:6MWje2NFNJ3IWpGs7BYj6DWagWXHyp8EnmYY7XFTtI4=
github.com/openshift-online/ocm-sdk-go v0.1.443 h1:wb79sOFAzA2f4hvJMOz2YJ6Q0HTIXY3kbDJoy4/xnBg=
github.com/openshift-online/ocm-sdk-go v0.1.443/go.mod h1:CiAu2jwl3ITKOxkeV0Qnhzv4gs35AmpIzVABQLtcI2Y=
github.com/openshift-online/ocm-sdk-go v0.1.448 h1:U7zXZER/2nBR25AnAxENgB3wS0O/NB6mZnQjT6qQ85Y=
github.com/openshift-online/ocm-sdk-go v0.1.448/go.mod h1:CiAu2jwl3ITKOxkeV0Qnhzv4gs35AmpIzVABQLtcI2Y=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4=
github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI=
Expand Down
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)))
})
})
})
})