Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #26 from jhernand/add_version_attribute
Browse files Browse the repository at this point in the history
Add `version` attribute
  • Loading branch information
jhernand authored Nov 24, 2021
2 parents 0af4312 + cf90e19 commit 9532d5d
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/resources/ocm_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/create_cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
Expand Down
19 changes: 19 additions & 0 deletions provider/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()),
}
Expand Down
1 change: 1 addition & 0 deletions provider/cluster_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
58 changes: 58 additions & 0 deletions tests/cluster_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}`

Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 9532d5d

Please sign in to comment.