Skip to content

Commit a3da714

Browse files
authored
Add log ingest config terraform resource (#123)
1 parent 799b80b commit a3da714

File tree

9 files changed

+278
-0
lines changed

9 files changed

+278
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Added:
99
* Add time window to the unstable `chronosphere_slo` resource.
1010
* Moved `chronosphere_slo` resource from stable to v1 API.
1111
* Add `log_allocation_config` from model conversion
12+
* Add `chronosphere_log_ingest_config` resource.
1213

1314
Removed:
1415
* Remove query less SLO fields from unstable `chronosphere_slo` resource.

chronosphere/generated_resources.gen.go

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chronosphere/intschema/log_ingest_config.go

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chronosphere/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func Provider() *schema.Provider {
6868
"chronosphere_logscale_action": resourceLogscaleAction(),
6969
"chronosphere_log_allocation_config": resourceLogAllocationConfig(),
7070
"chronosphere_slo": resourceSLO(),
71+
"chronosphere_log_ingest_config": resourceLogIngestConfig(),
7172
}
7273

7374
// Apply common CRUD wrappers to all resources.

chronosphere/registry/registry.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,11 @@ var Resources = mustValidate([]Resource{
353353
Schema: tfschema.LogAllocationConfig,
354354
SingletonID: "log_allocation_config_singleton",
355355
},
356+
{
357+
Name: "log_ingest_config",
358+
Entity: "LogIngestConfig",
359+
API: V1,
360+
Schema: tfschema.LogIngestConfig,
361+
SingletonID: "log_ingest_config_singleton",
362+
},
356363
})
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2024 Chronosphere Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package chronosphere
16+
17+
import (
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
19+
"go.uber.org/atomic"
20+
21+
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/intschema"
22+
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/pkg/configv1/models"
23+
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/sliceutil"
24+
"github.com/chronosphereio/terraform-provider-chronosphere/chronosphere/tfschema"
25+
)
26+
27+
// LogIngestConfigFromModel maps an API model into an intschema model.
28+
func LogIngestConfigFromModel(m *models.Configv1LogIngestConfig) (*intschema.LogIngestConfig, error) {
29+
return (logIngestConfigConverter{}).fromModel(m)
30+
}
31+
32+
func resourceLogIngestConfig() *schema.Resource {
33+
r := newGenericResource[
34+
*models.Configv1LogIngestConfig,
35+
intschema.LogIngestConfig,
36+
*intschema.LogIngestConfig,
37+
](
38+
"log_ingest_config",
39+
logIngestConfigConverter{},
40+
generatedLogIngestConfig{},
41+
)
42+
43+
return &schema.Resource{
44+
Schema: tfschema.LogIngestConfig,
45+
CreateContext: r.CreateContext,
46+
ReadContext: r.ReadContext,
47+
UpdateContext: r.UpdateContext,
48+
DeleteContext: r.DeleteContext,
49+
CustomizeDiff: r.ValidateDryRun(&LogIngestConfigDryRunCount),
50+
SchemaVersion: 1,
51+
Description: "Config configuring log ingestion in Chronosphere.",
52+
Importer: &schema.ResourceImporter{
53+
StateContext: schema.ImportStatePassthroughContext,
54+
},
55+
}
56+
}
57+
58+
// LogIngestConfigDryRunCount tracks how many times dry run is run during validation for testing.
59+
var LogIngestConfigDryRunCount atomic.Int64
60+
61+
type logIngestConfigConverter struct{}
62+
63+
func (logIngestConfigConverter) toModel(
64+
m *intschema.LogIngestConfig,
65+
) (*models.Configv1LogIngestConfig, error) {
66+
return &models.Configv1LogIngestConfig{
67+
Parsers: sliceutil.Map(m.Parser, func(p intschema.LogIngestConfigParser) *models.Configv1LogParser {
68+
return &models.Configv1LogParser{
69+
Name: p.Name,
70+
Regex: p.Regex,
71+
}
72+
}),
73+
}, nil
74+
}
75+
76+
func (logIngestConfigConverter) fromModel(
77+
m *models.Configv1LogIngestConfig,
78+
) (*intschema.LogIngestConfig, error) {
79+
return &intschema.LogIngestConfig{
80+
Parser: sliceutil.Map(m.Parsers, func(p *models.Configv1LogParser) intschema.LogIngestConfigParser {
81+
return intschema.LogIngestConfigParser{
82+
Name: p.Name,
83+
Regex: p.Regex,
84+
}
85+
}),
86+
}, nil
87+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package tfschema
2+
3+
import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
4+
5+
const maxLogParsers = 10
6+
7+
var LogIngestConfig = map[string]*schema.Schema{
8+
"parser": {
9+
Type: schema.TypeList,
10+
Elem: LogParserSchema,
11+
Optional: true,
12+
MaxItems: maxLogParsers,
13+
},
14+
}
15+
16+
var LogParserSchema = &schema.Resource{
17+
Schema: map[string]*schema.Schema{
18+
"name": {
19+
Type: schema.TypeString,
20+
Required: true,
21+
},
22+
"regex": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
},
27+
}

examples/log-ingest-config/main.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "chronosphere_log_ingest_config" "my-log-ingest-config" {
2+
parser {
3+
name = "syslog"
4+
regex = <<-EOT
5+
^\<(?<pri>[0-9]+)\>(?<time>[^ ]* {1,2}[^ ]* [^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?(?:[^\:]*\:)? *(?<message>.*)$
6+
EOT
7+
}
8+
9+
parser {
10+
name = "apache_error"
11+
regex = <<-EOT
12+
^\[[^ ]* (?<time>[^\]]*)\] \[(?<level>[^\]]*)\](?: \[pid (?<pid>[^\]]*)\])?( \[client (?<client>[^\]]*)\])? (?<message>.*)$
13+
EOT
14+
}
15+
}

examples/log-ingest-config/version.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../version.tf

0 commit comments

Comments
 (0)