-
Notifications
You must be signed in to change notification settings - Fork 19
add ocm_cluster_rosa_classic data source to get cluster id/name, stat… #78
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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. |
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>" | ||
|
||
} |
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, | ||
}, | ||
//future : search with id or name instead of just name | ||
"id": { | ||
Description: "Unique identifier of the cluster.", | ||
Type: types.StringType, | ||
Computed: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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...) | ||
} |
There was a problem hiding this comment.
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