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

add ocm_cluster_rosa_classic data source to get cluster id/name, stat… #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions docs/data-sources/ocm_cluster_rosa_classic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
page_title: "ocm_cluster_rosa_classic Data Source"
subcategory: ""
description: |-
Get ROSA cluster details.
---

# ocm_cluster_rosa_classic (Data Source)

Get ROSA cluster details.

## Schema

### Optional

- **name** (String) Name of the cluster.

- **id** (String) Unique identifier of the cluster.

Either name or id must be provided.


### Read-Only

- **version** (String) Version of OpenShift used to create the cluster

- **state** (String) State of the cluster.
6 changes: 6 additions & 0 deletions examples/get_cluster_info/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Cluster Data source example

This example shows how to use the cluster rosa classic data source to get
name of the cluster from ID or ID of the cluster frm the name.
To run it adjust the configuration of the provider in the `main.tf` file
and then run the `terraform apply` command.
36 changes: 36 additions & 0 deletions examples/get_cluster_info/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
terraform {
required_providers {
ocm = {
version = ">= 0.1"
source = "openshift-online/ocm"
}
}
}

provider "ocm" {
}

data "ocm_cluster_rosa_classic" "mycluster-name" {
name = "my-cluster"
}

output "cluster_id"{
value = data.ocm_cluster_rosa_classic.mycluster-name.id //outputs: cluster_id = "1n2j3k4l5m6n7o8p9q0r"
}

data "ocm_cluster_rosa_classic" "mycluster-id" {
id = "1n2j3k4l5m6n7o8p9q0r"
}

output "cluster_name"{
value = data.ocm_cluster_rosa_classic.mycluster-id.name //outputs: cluster_name = "my-cluster"
}

output "cluster_status"{
value = data.ocm_cluster_rosa_classic.mycluster-id.state //outputs: cluster_status = "<cluster state>"
}

output "cluster_version"{
value = data.ocm_cluster_rosa_classic.mycluster-id.version // outputs: cluster_version = "<x.x.x>"

}
123 changes: 123 additions & 0 deletions provider/cluster_rosa_classic_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift-online/ocm-sdk-go/logging"
)

type ClusterRosaClassicDataSourceType struct {
logger logging.Logger
}

type ClusterRosaClassicDataSource struct {
logger logging.Logger
collection *cmv1.ClustersClient
}

func (t *ClusterRosaClassicDataSourceType) GetSchema(ctx context.Context) (result tfsdk.Schema,
diags diag.Diagnostics) {
result = tfsdk.Schema{
Description: "OpenShift managed cluster using rosa sts.",
Attributes: map[string]tfsdk.Attribute{
"name": {
Description: "Name of the cluster.",
Type: types.StringType,
Computed: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be also an optional attribute

},
//future : search with id or name instead of just name
"id": {
Description: "Unique identifier of the cluster.",
Type: types.StringType,
Computed: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be also an optional attribute

},
"version": {
Description: "Identifier of the version of OpenShift, for example 'openshift-v4.1.0'.",
Type: types.StringType,
Computed: true,
},
"state": {
Description: "State of the cluster.",
Type: types.StringType,
Computed: true,
},
// to include -> STS Role ARN, Support Role ARN, Instance IAM Roles - Control plane, Instance IAM Roles - Worker, Operator IAM Roles (list), Created,Details Page,OIDC Endpoint URL
},
}
return
}

func (t *ClusterRosaClassicDataSourceType) NewDataSource(ctx context.Context,
p tfsdk.Provider) (result tfsdk.DataSource, diags diag.Diagnostics) {
// Cast the provider interface to the specific implementation:
parent := p.(*Provider)

// Get the collection of clusters:
collection := parent.connection.ClustersMgmt().V1().Clusters()

// Create the resource:
result = &ClusterRosaClassicDataSource{
logger: parent.logger,
collection: collection,
}
return
}

func (s *ClusterRosaClassicDataSource) Read(ctx context.Context, request tfsdk.ReadDataSourceRequest,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All clusters have those attributes right? Any reason to call it RosaClassic?

Also, this is confusing I think something like ClusterStateDataSource might be more appropriate.
What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vamsinm, please open the PR in the new repository - https://github.com/terraform-redhat/terraform-provider-ocm
This repository soon would be archived

response *tfsdk.ReadDataSourceResponse) {
// Get the state:
state := &DataClusterRosaClassicState{}
diags := request.Config.Get(ctx, state)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
// Fetch the complete details of the cluster:
//var listItem *cmv1.Cluster
listRequest := s.collection.List()
if !state.Name.Unknown && !state.Name.Null {
listRequest.Search("name is '" + state.Name.Value + "'")
} else if !state.ID.Unknown && !state.ID.Null {
listRequest.Search("id is '" + state.ID.Value + "'")
} else {
response.Diagnostics.AddError(
"Data source requires either cluster Name or ID",
"",
)
return
}
listResponse, err := listRequest.SendContext(ctx)
if err != nil {
response.Diagnostics.AddError(
"Can't list Clusters",
err.Error(),
)
return
}
if listResponse.Size() > 0 {
// Populate the state:
listResponse.Items().Each(func(cluster *cmv1.Cluster) bool {
state.ID = types.String{
Value: cluster.ID(),
}
state.Name = types.String{
Value: cluster.Name(),
}
state.State = types.String{
Value: fmt.Sprintf("%s", cluster.State()),
}
state.Version = types.String{
Value: cluster.OpenshiftVersion(),
}
return true
})
}
// Save the state:
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
}
7 changes: 7 additions & 0 deletions provider/cluster_rosa_classic_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ type InstanceIAMRole struct {
MasterRoleARN types.String `tfsdk:"master_role_arn"`
WorkerRoleARN types.String `tfsdk:"worker_role_arn"`
}

type DataClusterRosaClassicState struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
State types.String `tfsdk:"state"`
Version types.String `tfsdk:"version"`
}
11 changes: 6 additions & 5 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,12 @@ func (p *Provider) GetResources(ctx context.Context) (result map[string]tfsdk.Re
func (p *Provider) GetDataSources(ctx context.Context) (result map[string]tfsdk.DataSourceType,
diags diag.Diagnostics) {
result = map[string]tfsdk.DataSourceType{
"ocm_cloud_providers": &CloudProvidersDataSourceType{},
"ocm_rosa_operator_roles": &RosaOperatorRolesDataSourceType{},
"ocm_groups": &GroupsDataSourceType{},
"ocm_machine_types": &MachineTypesDataSourceType{},
"ocm_versions": &VersionsDataSourceType{},
"ocm_cloud_providers": &CloudProvidersDataSourceType{},
"ocm_rosa_operator_roles": &RosaOperatorRolesDataSourceType{},
"ocm_groups": &GroupsDataSourceType{},
"ocm_machine_types": &MachineTypesDataSourceType{},
"ocm_versions": &VersionsDataSourceType{},
"ocm_cluster_rosa_classic": &ClusterRosaClassicDataSourceType{},
}
return
}