Skip to content
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
159 changes: 159 additions & 0 deletions castai/data_source_omni_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package castai

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ datasource.DataSource = (*omniClusterDataSource)(nil)
_ datasource.DataSourceWithConfigure = (*omniClusterDataSource)(nil)
)

type omniClusterDataSource struct {
client *ProviderConfig
}

type omniClusterDataSourceModel struct {
ID types.String `tfsdk:"id"`
OrganizationID types.String `tfsdk:"organization_id"`
ClusterID types.String `tfsdk:"cluster_id"`
Name types.String `tfsdk:"name"`
State types.String `tfsdk:"state"`
ProviderType types.String `tfsdk:"provider_type"`
ServiceAccountID types.String `tfsdk:"service_account_id"`
CastaiOidcConfig *castaiOidcConfigModel `tfsdk:"castai_oidc_config"`
}

type castaiOidcConfigModel struct {
GcpServiceAccountEmail types.String `tfsdk:"gcp_service_account_email"`
GcpServiceAccountUniqueID types.String `tfsdk:"gcp_service_account_unique_id"`
}

func newOmniClusterDataSource() datasource.DataSource {
return &omniClusterDataSource{}
}

func (d *omniClusterDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_omni_cluster"
}

func (d *omniClusterDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Retrieve information about a CAST AI Omni cluster",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "Cluster ID (same as cluster_id)",
},
"organization_id": schema.StringAttribute{
Required: true,
Description: "CAST AI organization ID",
},
"cluster_id": schema.StringAttribute{
Required: true,
Description: "CAST AI cluster ID",
},
"name": schema.StringAttribute{
Computed: true,
Description: "Name of the cluster",
},
"state": schema.StringAttribute{
Computed: true,
Description: "State of the cluster on API level",
},
"provider_type": schema.StringAttribute{
Computed: true,
Description: "Provider type of the cluster (e.g. GKE, EKS)",
},
"service_account_id": schema.StringAttribute{
Computed: true,
Description: "CAST AI service account ID associated with OMNI operations",
},
"castai_oidc_config": schema.SingleNestedAttribute{
Computed: true,
Description: "CAST AI OIDC configuration for service account impersonation",
Attributes: map[string]schema.Attribute{
"gcp_service_account_email": schema.StringAttribute{
Computed: true,
Description: "CAST AI GCP service account email for impersonation",
},
"gcp_service_account_unique_id": schema.StringAttribute{
Computed: true,
Description: "CAST AI GCP service account unique ID for impersonation",
},
},
},
},
}
}

func (d *omniClusterDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*ProviderConfig)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *ProviderConfig, got: %T", req.ProviderData),
)
return
}

d.client = client
}

func (d *omniClusterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data omniClusterDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

client := d.client.omniAPI
organizationID := data.OrganizationID.ValueString()
clusterID := data.ClusterID.ValueString()

apiResp, err := client.ClustersAPIGetClusterWithResponse(ctx, organizationID, clusterID, nil)
if err != nil {
resp.Diagnostics.AddError("Failed to read omni cluster", err.Error())
return
}

if apiResp.StatusCode() != http.StatusOK {
resp.Diagnostics.AddError(
"Failed to read omni cluster",
fmt.Sprintf("unexpected status code: %d, body: %s", apiResp.StatusCode(), string(apiResp.Body)),
)
return
}

cluster := apiResp.JSON200

data.ID = types.StringValue(clusterID)
data.Name = types.StringPointerValue(cluster.Name)
data.ServiceAccountID = types.StringPointerValue(cluster.ServiceAccountId)

if cluster.State != nil {
data.State = types.StringValue(string(*cluster.State))
}
if cluster.ProviderType != nil {
data.ProviderType = types.StringValue(string(*cluster.ProviderType))
}

if cluster.CastaiOidcConfig != nil {
data.CastaiOidcConfig = &castaiOidcConfigModel{
GcpServiceAccountEmail: types.StringPointerValue(cluster.CastaiOidcConfig.GcpServiceAccountEmail),
GcpServiceAccountUniqueID: types.StringPointerValue(cluster.CastaiOidcConfig.GcpServiceAccountUniqueId),
}
}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
50 changes: 50 additions & 0 deletions castai/data_source_omni_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package castai

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccCloudAgnostic_DataSourceOmniCluster(t *testing.T) {
clusterName := "omni-tf-acc-gcp"
dataSourceName := "data.castai_omni_cluster.test"
resourceName := "castai_omni_cluster.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccOmniClusterDataSourceConfig(clusterName),
Check: resource.ComposeTestCheckFunc(
// Verify data source ID matches the resource ID
resource.TestCheckResourceAttrPair(dataSourceName, "id", resourceName, "id"),
// Verify organization_id is correctly passed through
resource.TestCheckResourceAttr(dataSourceName, "organization_id", testAccGetOrganizationID()),
// Verify cluster metadata fields are populated
resource.TestCheckResourceAttrSet(dataSourceName, "name"),
resource.TestCheckResourceAttrSet(dataSourceName, "state"),
// Verify OIDC config fields have expected formats
resource.TestMatchResourceAttr(dataSourceName, "castai_oidc_config.gcp_service_account_email",
regexp.MustCompile(`^.+@.+\.iam\.gserviceaccount\.com$`)),
resource.TestMatchResourceAttr(dataSourceName, "castai_oidc_config.gcp_service_account_unique_id",
regexp.MustCompile(`^\d+$`)),
),
},
},
})
}

func testAccOmniClusterDataSourceConfig(clusterName string) string {
organizationID := testAccGetOrganizationID()

return ConfigCompose(testOmniClusterConfig(clusterName), fmt.Sprintf(`
data "castai_omni_cluster" "test" {
organization_id = %[1]q
cluster_id = castai_omni_cluster.test.id
}
`, organizationID))
}
4 changes: 3 additions & 1 deletion castai/provider_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,7 @@ func (p *frameworkProvider) Resources(_ context.Context) []func() resource.Resou
}

func (p *frameworkProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{}
return []func() datasource.DataSource{
newOmniClusterDataSource,
}
}
Loading
Loading