Skip to content

Commit

Permalink
feat: added project datasource & resource
Browse files Browse the repository at this point in the history
  • Loading branch information
thulasirajkomminar committed Oct 8, 2024
1 parent f316da3 commit 80e9b53
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ provider "cratedb" {
* `cratedb_cluster`
* `cratedb_organization`
* `cratedb_organizations`
* `cratedb_project`

### Resources

Expand Down
35 changes: 35 additions & 0 deletions docs/data-sources/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "cratedb_project Data Source - terraform-provider-cratedb"
subcategory: ""
description: |-
To retrieve a project.
---

# cratedb_project (Data Source)

To retrieve a project.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `id` (String) The id of the project.

### Read-Only

- `dc` (Attributes) The DublinCore of the project. (see [below for nested schema](#nestedatt--dc))
- `name` (String) The name of the project.
- `organization_id` (String) The organization id of the project.
- `region` (String) The region of the project.

<a id="nestedatt--dc"></a>
### Nested Schema for `dc`

Read-Only:

- `created` (String) The created time.
- `modified` (String) The modified time.
15 changes: 15 additions & 0 deletions examples/data-sources/project/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_providers {
cratedb = {
source = "komminarlabs/cratedb"
}
}
}

data "cratedb_project" "default" {
id = "2f310566-f171-4bf6-bf2e-46e045ff3708"
}

output "default_project" {
value = data.cratedb_project.default
}
5 changes: 2 additions & 3 deletions internal/provider/cluster_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func getClusterModel(ctx context.Context, cluster cratedb.Cluster) (*ClusterMode
}
}

clusterModel := ClusterModel{
return &ClusterModel{
Dc: dcObjectValue,
HardwareSpecs: hardwareSpecsObjectValue,
Health: healthObjectValue,
Expand All @@ -155,6 +155,5 @@ func getClusterModel(ctx context.Context, cluster cratedb.Cluster) (*ClusterMode
Suspended: types.BoolPointerValue(cluster.Suspended),
Url: types.StringPointerValue(cluster.Url),
Username: types.StringValue(cluster.Username),
}
return &clusterModel, nil
}, nil
}
5 changes: 2 additions & 3 deletions internal/provider/organization_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func getOrganizationModel(ctx context.Context, organization cratedb.Organization
return nil, fmt.Errorf("error getting organization DC value")
}

organizationModel := OrganizationModel{
return &OrganizationModel{
Dc: dcObjectValue,
Email: types.StringValue(string(*organization.Email)),
Id: types.StringValue(*organization.Id),
Expand All @@ -40,6 +40,5 @@ func getOrganizationModel(ctx context.Context, organization cratedb.Organization
PlanType: types.Int32Value(int32(*organization.PlanType)),
ProjectCount: types.Int32Value(int32(*organization.ProjectCount)),
RoleFQN: types.StringValue(string(*organization.RoleFqn)),
}
return &organizationModel, nil
}, nil
}
135 changes: 135 additions & 0 deletions internal/provider/project_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package provider

import (
"context"
"fmt"

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

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &ProjectDataSource{}
_ datasource.DataSourceWithConfigure = &ProjectDataSource{}
)

// NewProjectDataSource is a helper function to simplify the provider implementation.
func NewProjectDataSource() datasource.DataSource {
return &ProjectDataSource{}
}

// ProjectDataSource is the data source implementation.
type ProjectDataSource struct {
client *cratedb.ClientWithResponses
}

// Metadata returns the data source type name.
func (d *ProjectDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_project"
}

// Schema defines the schema for the data source.
func (d *ProjectDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
Description: "To retrieve a project.",

Attributes: map[string]schema.Attribute{
"dc": schema.SingleNestedAttribute{
Computed: true,
Description: "The DublinCore of the project.",
Attributes: map[string]schema.Attribute{
"created": schema.StringAttribute{
Computed: true,
Description: "The created time.",
},
"modified": schema.StringAttribute{
Computed: true,
Description: "The modified time.",
},
},
},
"id": schema.StringAttribute{
Required: true,
Description: "The id of the project.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the project.",
},
"organization_id": schema.StringAttribute{
Computed: true,
Description: "The organization id of the project.",
},
"region": schema.StringAttribute{
Computed: true,
Description: "The region of the project.",
},
},
}
}

// Configure adds the provider configured client to the data source.
func (d *ProjectDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*cratedb.ClientWithResponses)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected cratedb.ClientWithResponses, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}

// Read refreshes the Terraform state with the latest data.
func (d *ProjectDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state ProjectModel

resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

readProjectResponse, err := d.client.GetApiV2ProjectsProjectIdWithResponse(ctx, state.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error getting project",
err.Error(),
)
return
}

if readProjectResponse.StatusCode() != 200 {
resp.Diagnostics.AddError(
"Error getting project",
fmt.Sprintf("HTTP Status Code: %d\nStatus: %v", readProjectResponse.StatusCode(), readProjectResponse.Status()),
)
return
}

// Map response body to model
projectState, err := getProjectModel(ctx, *readProjectResponse.JSON200)
if err != nil {
resp.Diagnostics.AddError(
"Error getting project model",
err.Error(),
)
return
}
state = *projectState

// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
38 changes: 38 additions & 0 deletions internal/provider/project_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/komminarlabs/cratedb"
)

// ProjectModel maps CrateDB project schema data.
type ProjectModel struct {
Dc types.Object `tfsdk:"dc"`
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
OrganizationId types.String `tfsdk:"organization_id"`
Region types.String `tfsdk:"region"`
}

func getProjectModel(ctx context.Context, project cratedb.Project) (*ProjectModel, error) {
dcValue := DCModel{
Created: types.StringValue(project.Dc.Created.String()),
Modified: types.StringValue(project.Dc.Modified.String()),
}

dcObjectValue, diags := types.ObjectValueFrom(ctx, dcValue.GetAttrType(), dcValue)
if diags.HasError() {
return nil, fmt.Errorf("error getting organization DC value")
}

return &ProjectModel{
Dc: dcObjectValue,
Id: types.StringPointerValue(project.Id),
Name: types.StringValue(project.Name),
OrganizationId: types.StringValue(project.OrganizationId),
Region: types.StringPointerValue(project.Region),
}, nil
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func (p *CrateDBProvider) DataSources(ctx context.Context) []func() datasource.D
NewClusterDataSource,
NewOrganizationDataSource,
NewOrganizationsDataSource,
NewProjectDataSource,
}
}

Expand Down

0 comments on commit 80e9b53

Please sign in to comment.