-
Notifications
You must be signed in to change notification settings - Fork 45
feat(logs) onboard logs service #1096
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
base: main
Are you sure you want to change the base?
Changes from all commits
6859226
89e5e87
d25578e
71751ad
b75ec60
849bd4a
b8cc6bc
433019e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_logs_instance Data Source - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| Logs instance data source schema. | ||
| ~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our guide https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources for how to opt-in to use beta resources. | ||
| --- | ||
|
|
||
| # stackit_logs_instance (Data Source) | ||
|
|
||
| Logs instance data source schema. | ||
|
|
||
| ~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our [guide](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources) for how to opt-in to use beta resources. | ||
|
|
||
|
|
||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `instance_id` (String) The Logs instance ID | ||
| - `project_id` (String) STACKIT project ID associated with the Logs instance | ||
|
|
||
| ### Optional | ||
|
|
||
| - `region` (String) STACKIT region name the resource is located in. If not defined, the provider region is used. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `acl` (List of String) The access control list entries for the Logs instance | ||
| - `created` (String) The date and time the creation of the Logs instance was initiated | ||
| - `datasource_url` (String) Logs instance datasource URL, can be used in Grafana as datasource URL | ||
| - `description` (String) The description of the Logs instance | ||
| - `display_name` (String) The displayed name of the Logs instance | ||
| - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`region`,`instance_id`". | ||
| - `ingest_otlp_url` (String) The Logs instance's ingest logs via OTLP URL | ||
| - `ingest_url` (String) The logs instance's ingest logs URL | ||
| - `query_range_url` (String) The Logs instance's query range URL | ||
| - `query_url` (String) The Logs instance's query URL | ||
| - `retention_days` (Number) The log retention time in days | ||
| - `status` (String) The status of the Logs instance, possible values: Possible values are: `active`, `deleting`, `reconciling`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| --- | ||
| # generated by https://github.com/hashicorp/terraform-plugin-docs | ||
| page_title: "stackit_logs_instance Resource - stackit" | ||
| subcategory: "" | ||
| description: |- | ||
| Logs instance resource schema. | ||
| ~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our guide https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources for how to opt-in to use beta resources. | ||
| --- | ||
|
|
||
| # stackit_logs_instance (Resource) | ||
|
|
||
| Logs instance resource schema. | ||
|
|
||
| ~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our [guide](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources) for how to opt-in to use beta resources. | ||
|
|
||
|
|
||
|
|
||
| <!-- schema generated by tfplugindocs --> | ||
| ## Schema | ||
|
|
||
| ### Required | ||
|
|
||
| - `display_name` (String) The displayed name of the Logs instance | ||
| - `project_id` (String) STACKIT project ID associated with the Logs instance | ||
| - `retention_days` (Number) The log retention time in days | ||
|
|
||
| ### Optional | ||
|
|
||
| - `acl` (List of String) The access control list entries for the Logs instance | ||
| - `description` (String) The description of the Logs instance | ||
| - `region` (String) STACKIT region name the resource is located in. If not defined, the provider region is used. | ||
|
|
||
| ### Read-Only | ||
|
|
||
| - `created` (String) The date and time the creation of the Logs instance was initiated | ||
| - `datasource_url` (String) Logs instance datasource URL, can be used in Grafana as datasource URL | ||
| - `id` (String) Terraform's internal resource identifier. It is structured as "`project_id`,`region`,`instance_id`". | ||
| - `ingest_otlp_url` (String) The Logs instance's ingest logs via OTLP URL | ||
| - `ingest_url` (String) The logs instance's ingest logs URL | ||
| - `instance_id` (String) The Logs instance ID | ||
| - `query_range_url` (String) The Logs instance's query range URL | ||
| - `query_url` (String) The Logs instance's query URL | ||
| - `status` (String) The status of the Logs instance, possible values: Possible values are: `active`, `deleting`, `reconciling`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| data "stackit_logs_instance" "logs" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| region = "eu01" | ||
| instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| resource "stackit_logs_instance" "logs" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| region = "eu01" | ||
| display_name = "logs-instance-example" | ||
| retention_days = 30 | ||
| } | ||
|
|
||
| resource "stackit_logs_instance" "logs2" { | ||
| project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | ||
| region = "eu01" | ||
| display_name = "logs-instance-example" | ||
| retention_days = 30 | ||
| acl = [ | ||
| "0.0.0.0/0" | ||
| ] | ||
| description = "Example description" | ||
| } | ||
|
|
||
| # Only use the import statement, if you want to import an existing logs instance | ||
| import { | ||
| to = stackit_logs_instance.import-example | ||
| id = "${var.project_id},${var.region},${var.logs_instance_id}" | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,179 @@ | ||||||
| package instance | ||||||
|
|
||||||
| import ( | ||||||
| "context" | ||||||
| "errors" | ||||||
| "fmt" | ||||||
| "net/http" | ||||||
|
|
||||||
| "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/datasource" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||||||
| "github.com/hashicorp/terraform-plugin-framework/types" | ||||||
| "github.com/hashicorp/terraform-plugin-log/tflog" | ||||||
| "github.com/stackitcloud/stackit-sdk-go/core/oapierror" | ||||||
| "github.com/stackitcloud/stackit-sdk-go/services/logs" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/logs/utils" | ||||||
| "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" | ||||||
| ) | ||||||
|
|
||||||
| var ( | ||||||
| _ datasource.DataSource = &logsInstanceDataSource{} | ||||||
| ) | ||||||
|
|
||||||
| func NewLogsInstanceDataSource() datasource.DataSource { | ||||||
| return &logsInstanceDataSource{} | ||||||
| } | ||||||
|
|
||||||
| type logsInstanceDataSource struct { | ||||||
| client *logs.APIClient | ||||||
| } | ||||||
|
|
||||||
| func (d *logsInstanceDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { | ||||||
| resp.TypeName = req.ProviderTypeName + "_logs_instance" | ||||||
| } | ||||||
|
|
||||||
| func (d *logsInstanceDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { | ||||||
| providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) | ||||||
| if !ok { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| apiClient := utils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
| d.client = apiClient | ||||||
| tflog.Info(ctx, "Logs client configured") | ||||||
| } | ||||||
|
|
||||||
| func (d *logsInstanceDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||||||
| resp.Schema = schema.Schema{ | ||||||
| MarkdownDescription: features.AddBetaDescription("Logs instance data source schema.", core.Resource), | ||||||
| Description: fmt.Sprintf("Logs instance resource schema. %s", core.DatasourceRegionFallbackDocstring), | ||||||
| Attributes: map[string]schema.Attribute{ | ||||||
| "id": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["id"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "instance_id": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["instance_id"], | ||||||
| Required: true, | ||||||
| Validators: []validator.String{ | ||||||
| validate.UUID(), | ||||||
| validate.NoSeparator(), | ||||||
| }, | ||||||
| }, | ||||||
| "region": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["region"], | ||||||
| Optional: true, | ||||||
| // must be computed to allow for storing the override value from the provider | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "project_id": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["project_id"], | ||||||
| Required: true, | ||||||
| Validators: []validator.String{ | ||||||
| validate.UUID(), | ||||||
| validate.NoSeparator(), | ||||||
| }, | ||||||
| }, | ||||||
| "acl": schema.ListAttribute{ | ||||||
| Description: schemaDescriptions["acl"], | ||||||
| ElementType: types.StringType, | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "created": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["created"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "datasource_url": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["datasource_url"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "description": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["description"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "display_name": schema.StringAttribute{ | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we also need here a validator like |
||||||
| Description: schemaDescriptions["display_name"], | ||||||
| Computed: true, | ||||||
| Validators: []validator.String{stringvalidator.LengthAtLeast(1)}, | ||||||
| }, | ||||||
| "ingest_otlp_url": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["ingest_otlp_url"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "ingest_url": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["ingest_url"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "query_range_url": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["query_range_url"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "query_url": schema.StringAttribute{ | ||||||
| Description: schemaDescriptions["query_url"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "retention_days": schema.Int64Attribute{ | ||||||
| Description: schemaDescriptions["retention_days"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| "status": schema.StringAttribute{ | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Status has enum values. It will be good if we can mention the values in the description. |
||||||
| Description: schemaDescriptions["status"], | ||||||
| Computed: true, | ||||||
| }, | ||||||
| }, | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| func (d *logsInstanceDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform | ||||||
| var model Model | ||||||
| diags := req.Config.Get(ctx, &model) | ||||||
| resp.Diagnostics.Append(diags...) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
|
|
||||||
| ctx = core.InitProviderContext(ctx) | ||||||
|
|
||||||
| projectID := model.ProjectID.ValueString() | ||||||
| region := model.Region.ValueString() | ||||||
| instanceID := model.InstanceID.ValueString() | ||||||
|
|
||||||
| ctx = tflog.SetField(ctx, "project_id", projectID) | ||||||
| ctx = tflog.SetField(ctx, "region", region) | ||||||
| ctx = tflog.SetField(ctx, "instance_id", instanceID) | ||||||
|
|
||||||
| instanceResponse, err := d.client.GetLogsInstance(ctx, projectID, region, instanceID).Execute() | ||||||
| if err != nil { | ||||||
| var oapiErr *oapierror.GenericOpenAPIError | ||||||
| ok := errors.As(err, &oapiErr) | ||||||
| if ok && oapiErr.StatusCode == http.StatusNotFound { | ||||||
| resp.State.RemoveResource(ctx) | ||||||
| return | ||||||
| } | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading logs instance", fmt.Sprintf("Calling API: %v", err)) | ||||||
| return | ||||||
| } | ||||||
| ctx = core.LogResponse(ctx) | ||||||
|
|
||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| err = mapFields(ctx, instanceResponse, &model) | ||||||
| if err != nil { | ||||||
| core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading logs instance", fmt.Sprintf("Processing response: %v", err)) | ||||||
| return | ||||||
| } | ||||||
| diags = resp.State.Set(ctx, model) | ||||||
| resp.Diagnostics.Append(diags...) | ||||||
| if resp.Diagnostics.HasError() { | ||||||
| return | ||||||
| } | ||||||
| tflog.Info(ctx, "Logs Instance read", map[string]interface{}{ | ||||||
| "instance_id": instanceID, | ||||||
| }) | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.