|
| 1 | +package castai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + _ datasource.DataSource = (*omniClusterDataSource)(nil) |
| 15 | + _ datasource.DataSourceWithConfigure = (*omniClusterDataSource)(nil) |
| 16 | +) |
| 17 | + |
| 18 | +type omniClusterDataSource struct { |
| 19 | + client *ProviderConfig |
| 20 | +} |
| 21 | + |
| 22 | +type omniClusterDataSourceModel struct { |
| 23 | + ID types.String `tfsdk:"id"` |
| 24 | + OrganizationID types.String `tfsdk:"organization_id"` |
| 25 | + ClusterID types.String `tfsdk:"cluster_id"` |
| 26 | + Name types.String `tfsdk:"name"` |
| 27 | + State types.String `tfsdk:"state"` |
| 28 | + ProviderType types.String `tfsdk:"provider_type"` |
| 29 | + ServiceAccountID types.String `tfsdk:"service_account_id"` |
| 30 | + CastaiOidcConfig *castaiOidcConfigModel `tfsdk:"castai_oidc_config"` |
| 31 | +} |
| 32 | + |
| 33 | +type castaiOidcConfigModel struct { |
| 34 | + GcpServiceAccountEmail types.String `tfsdk:"gcp_service_account_email"` |
| 35 | + GcpServiceAccountUniqueID types.String `tfsdk:"gcp_service_account_unique_id"` |
| 36 | +} |
| 37 | + |
| 38 | +func newOmniClusterDataSource() datasource.DataSource { |
| 39 | + return &omniClusterDataSource{} |
| 40 | +} |
| 41 | + |
| 42 | +func (d *omniClusterDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 43 | + resp.TypeName = req.ProviderTypeName + "_omni_cluster" |
| 44 | +} |
| 45 | + |
| 46 | +func (d *omniClusterDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 47 | + resp.Schema = schema.Schema{ |
| 48 | + Description: "Retrieve information about a CAST AI Omni cluster", |
| 49 | + Attributes: map[string]schema.Attribute{ |
| 50 | + "id": schema.StringAttribute{ |
| 51 | + Computed: true, |
| 52 | + Description: "Cluster ID (same as cluster_id)", |
| 53 | + }, |
| 54 | + "organization_id": schema.StringAttribute{ |
| 55 | + Required: true, |
| 56 | + Description: "CAST AI organization ID", |
| 57 | + }, |
| 58 | + "cluster_id": schema.StringAttribute{ |
| 59 | + Required: true, |
| 60 | + Description: "CAST AI cluster ID", |
| 61 | + }, |
| 62 | + "name": schema.StringAttribute{ |
| 63 | + Computed: true, |
| 64 | + Description: "Name of the cluster", |
| 65 | + }, |
| 66 | + "state": schema.StringAttribute{ |
| 67 | + Computed: true, |
| 68 | + Description: "State of the cluster on API level", |
| 69 | + }, |
| 70 | + "provider_type": schema.StringAttribute{ |
| 71 | + Computed: true, |
| 72 | + Description: "Provider type of the cluster (e.g. GKE, EKS)", |
| 73 | + }, |
| 74 | + "service_account_id": schema.StringAttribute{ |
| 75 | + Computed: true, |
| 76 | + Description: "CAST AI service account ID associated with OMNI operations", |
| 77 | + }, |
| 78 | + "castai_oidc_config": schema.SingleNestedAttribute{ |
| 79 | + Computed: true, |
| 80 | + Description: "CAST AI OIDC configuration for service account impersonation", |
| 81 | + Attributes: map[string]schema.Attribute{ |
| 82 | + "gcp_service_account_email": schema.StringAttribute{ |
| 83 | + Computed: true, |
| 84 | + Description: "CAST AI GCP service account email for impersonation", |
| 85 | + }, |
| 86 | + "gcp_service_account_unique_id": schema.StringAttribute{ |
| 87 | + Computed: true, |
| 88 | + Description: "CAST AI GCP service account unique ID for impersonation", |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func (d *omniClusterDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 97 | + if req.ProviderData == nil { |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + client, ok := req.ProviderData.(*ProviderConfig) |
| 102 | + if !ok { |
| 103 | + resp.Diagnostics.AddError( |
| 104 | + "Unexpected Data Source Configure Type", |
| 105 | + fmt.Sprintf("Expected *ProviderConfig, got: %T", req.ProviderData), |
| 106 | + ) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + d.client = client |
| 111 | +} |
| 112 | + |
| 113 | +func (d *omniClusterDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 114 | + var data omniClusterDataSourceModel |
| 115 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 116 | + if resp.Diagnostics.HasError() { |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + client := d.client.omniAPI |
| 121 | + organizationID := data.OrganizationID.ValueString() |
| 122 | + clusterID := data.ClusterID.ValueString() |
| 123 | + |
| 124 | + apiResp, err := client.ClustersAPIGetClusterWithResponse(ctx, organizationID, clusterID, nil) |
| 125 | + if err != nil { |
| 126 | + resp.Diagnostics.AddError("Failed to read omni cluster", err.Error()) |
| 127 | + return |
| 128 | + } |
| 129 | + |
| 130 | + if apiResp.StatusCode() != http.StatusOK { |
| 131 | + resp.Diagnostics.AddError( |
| 132 | + "Failed to read omni cluster", |
| 133 | + fmt.Sprintf("unexpected status code: %d, body: %s", apiResp.StatusCode(), string(apiResp.Body)), |
| 134 | + ) |
| 135 | + return |
| 136 | + } |
| 137 | + |
| 138 | + cluster := apiResp.JSON200 |
| 139 | + |
| 140 | + data.ID = types.StringValue(clusterID) |
| 141 | + data.Name = types.StringPointerValue(cluster.Name) |
| 142 | + data.ServiceAccountID = types.StringPointerValue(cluster.ServiceAccountId) |
| 143 | + |
| 144 | + if cluster.State != nil { |
| 145 | + data.State = types.StringValue(string(*cluster.State)) |
| 146 | + } |
| 147 | + if cluster.ProviderType != nil { |
| 148 | + data.ProviderType = types.StringValue(string(*cluster.ProviderType)) |
| 149 | + } |
| 150 | + |
| 151 | + if cluster.CastaiOidcConfig != nil { |
| 152 | + data.CastaiOidcConfig = &castaiOidcConfigModel{ |
| 153 | + GcpServiceAccountEmail: types.StringPointerValue(cluster.CastaiOidcConfig.GcpServiceAccountEmail), |
| 154 | + GcpServiceAccountUniqueID: types.StringPointerValue(cluster.CastaiOidcConfig.GcpServiceAccountUniqueId), |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 159 | +} |
0 commit comments