-
Notifications
You must be signed in to change notification settings - Fork 42
feat: support new omni API fields to support cluster per EL feature #673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)...) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.