From cf90e1941217e7530240b3e0582283eac7c241c2 Mon Sep 17 00:00:00 2001 From: Juan Hernandez Date: Wed, 24 Nov 2021 13:51:15 +0100 Subject: [PATCH] Add `version` attribute This patch adds the `version` attribute to the `ocm_cluster` resource. For example, to create a cluster with version `4.9.7`: ```hcl resource "ocm_cluster" "my_cluster" { name = "my-cluster" cloud_provider = "aws" cloud_region = "us-east-1" version = "openshift-v4.9.7" } ``` Signed-off-by: Juan Hernandez --- docs/resources/ocm_cluster.md | 4 +++ examples/create_cluster/main.tf | 3 +- provider/cluster_resource.go | 19 +++++++++++ provider/cluster_state.go | 1 + tests/cluster_resource_test.go | 58 +++++++++++++++++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) diff --git a/docs/resources/ocm_cluster.md b/docs/resources/ocm_cluster.md index 0daef05..83fa9bd 100644 --- a/docs/resources/ocm_cluster.md +++ b/docs/resources/ocm_cluster.md @@ -66,6 +66,10 @@ OpenShift managed cluster. - **service_cidr** (String) Block of IP addresses for services. Default value is `172.30.0.0/16`. +- **version** (String) Version of _OpenShift_ used to create the cluster, for + example `openshift-v4.9.7`. The default is to use the latest version. To get the + available versions use the `ocm_versions` data source. + - **wait** (Boolean) Wait till the cluster is ready. ### Read-Only diff --git a/examples/create_cluster/main.tf b/examples/create_cluster/main.tf index bf3658f..6f65553 100644 --- a/examples/create_cluster/main.tf +++ b/examples/create_cluster/main.tf @@ -31,6 +31,7 @@ resource "ocm_cluster" "my_cluster" { cloud_provider = "aws" cloud_region = "us-east-1" compute_nodes = 10 + version = "openshift-v4.9.7" properties = { department = "accounting" application = "billing" @@ -47,7 +48,7 @@ resource "ocm_identity_provider" "my_idp" { } resource "ocm_group_membership" "my_admin" { - cluster = ocm.cluster.my_cluster.id + cluster = ocm_cluster.my_cluster.id group = "dedicated-admins" user = "admin" } diff --git a/provider/cluster_resource.go b/provider/cluster_resource.go index 0690ad5..0c16ddd 100644 --- a/provider/cluster_resource.go +++ b/provider/cluster_resource.go @@ -157,6 +157,12 @@ func (t *ClusterResourceType) GetSchema(ctx context.Context) (result tfsdk.Schem Optional: true, Computed: true, }, + "version": { + Description: "Identifier of the version of OpenShift, for example 'openshift-v4.1.0'.", + Type: types.StringType, + Optional: true, + Computed: true, + }, "state": { Description: "State of the cluster.", Type: types.StringType, @@ -262,6 +268,9 @@ func (r *ClusterResource) Create(ctx context.Context, if !network.Empty() { builder.Network(network) } + if !state.Version.Unknown && !state.Version.Null { + builder.Version(cmv1.NewVersion().ID(state.Version.Value)) + } object, err := builder.Build() if err != nil { response.Diagnostics.AddError( @@ -595,6 +604,16 @@ func (r *ClusterResource) populateState(object *cmv1.Cluster, state *ClusterStat Null: true, } } + version, ok := object.Version().GetID() + if ok { + state.Version = types.String{ + Value: version, + } + } else { + state.Version = types.String{ + Null: true, + } + } state.State = types.String{ Value: string(object.State()), } diff --git a/provider/cluster_state.go b/provider/cluster_state.go index 717bf06..c23519e 100644 --- a/provider/cluster_state.go +++ b/provider/cluster_state.go @@ -40,5 +40,6 @@ type ClusterState struct { Properties types.Map `tfsdk:"properties"` ServiceCIDR types.String `tfsdk:"service_cidr"` State types.String `tfsdk:"state"` + Version types.String `tfsdk:"version"` Wait types.Bool `tfsdk:"wait"` } diff --git a/tests/cluster_resource_test.go b/tests/cluster_resource_test.go index 1964760..ab5baf4 100644 --- a/tests/cluster_resource_test.go +++ b/tests/cluster_resource_test.go @@ -69,6 +69,9 @@ var _ = Describe("Cluster creation", func() { "pod_cidr": "10.128.0.0/14", "host_prefix": 23 }, + "version": { + "id": "openshift-4.8.0" + }, "state": "ready" }` @@ -372,6 +375,61 @@ var _ = Describe("Cluster creation", func() { Expect(resource).To(MatchJQ(".attributes.host_prefix", 22.0)) }) + It("Sets version", func() { + // Prepare the server: + server.AppendHandlers( + CombineHandlers( + VerifyRequest(http.MethodPost, "/api/clusters_mgmt/v1/clusters"), + VerifyJQ(".version.id", "openshift-v4.8.1"), + RespondWithPatchedJSON(http.StatusOK, template, `[ + { + "op": "replace", + "path": "/version", + "value": { + "id": "openshift-v4.8.1" + } + } + ]`), + ), + ) + + // Run the apply command: + result := NewTerraformRunner(). + File( + "main.tf", ` + terraform { + required_providers { + ocm = { + source = "localhost/openshift-online/ocm" + } + } + } + + provider "ocm" { + url = "{{ .URL }}" + token = "{{ .Token }}" + trusted_cas = file("{{ .CA }}") + } + + resource "ocm_cluster" "my_cluster" { + name = "my-cluster" + cloud_provider = "aws" + cloud_region = "us-west-1" + version = "openshift-v4.8.1" + } + `, + "URL", server.URL(), + "Token", token, + "CA", strings.ReplaceAll(ca, "\\", "/"), + ). + Apply(ctx) + Expect(result.ExitCode()).To(BeZero()) + + // Check the state: + resource := result.Resource("ocm_cluster", "my_cluster") + Expect(resource).To(MatchJQ(".attributes.version", "openshift-v4.8.1")) + }) + It("Fails if the cluster already exists", func() { // Prepare the server: server.AppendHandlers(