-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added project datasource & resource
- Loading branch information
1 parent
f316da3
commit 80e9b53
Showing
8 changed files
with
229 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,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. |
This file contains 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,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 | ||
} |
This file contains 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
This file contains 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
This file contains 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,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 | ||
} | ||
} |
This file contains 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,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 | ||
} |
This file contains 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