Skip to content

Commit 20f9a37

Browse files
authored
Add local file source for installed collector (#477)
* Add local file source for installed collector * Add docs for local file source * fix docs * fix docs indent
1 parent fff31b1 commit 20f9a37

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func Provider() terraform.ResourceProvider {
107107
"sumologic_policies": resourceSumologicPolicies(),
108108
"sumologic_hierarchy": resourceSumologicHierarchy(),
109109
"sumologic_content_permission": resourceSumologicPermissions(),
110+
"sumologic_local_file_source": resourceSumologicLocalFileSource(),
110111
},
111112
DataSourcesMap: map[string]*schema.Resource{
112113
"sumologic_cse_log_mapping_vendor_product": dataSourceCSELogMappingVendorAndProduct(),
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package sumologic
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strconv"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func resourceSumologicLocalFileSource() *schema.Resource {
12+
localFileSource := resourceSumologicSource()
13+
localFileSource.Create = resourceSumologicLocalFileSourceCreate
14+
localFileSource.Read = resourceSumologicLocalFileSourceRead
15+
localFileSource.Update = resourceSumologicLocalFileSourceUpdate
16+
localFileSource.Importer = &schema.ResourceImporter{
17+
State: resourceSumologicSourceImport,
18+
}
19+
20+
localFileSource.Schema["path_expression"] = &schema.Schema{
21+
Type: schema.TypeString,
22+
Required: true,
23+
}
24+
25+
localFileSource.Schema["encoding"] = &schema.Schema{
26+
Type: schema.TypeString,
27+
Optional: true,
28+
Default: "UTF-8",
29+
}
30+
31+
localFileSource.Schema["deny_list"] = &schema.Schema{
32+
Type: schema.TypeSet,
33+
Optional: true,
34+
Elem: &schema.Schema{Type: schema.TypeString},
35+
}
36+
37+
return localFileSource
38+
}
39+
40+
func resourceSumologicLocalFileSourceCreate(d *schema.ResourceData, meta interface{}) error {
41+
c := meta.(*Client)
42+
43+
if d.Id() == "" {
44+
source := resourceToLocalFileSource(d)
45+
46+
id, err := c.CreateLocalFileSource(source, d.Get("collector_id").(int))
47+
48+
if err != nil {
49+
return err
50+
}
51+
52+
d.SetId(strconv.Itoa(id))
53+
}
54+
55+
return resourceSumologicLocalFileSourceRead(d, meta)
56+
}
57+
58+
func resourceSumologicLocalFileSourceUpdate(d *schema.ResourceData, meta interface{}) error {
59+
c := meta.(*Client)
60+
61+
source := resourceToLocalFileSource(d)
62+
63+
err := c.UpdateLocalFileSource(source, d.Get("collector_id").(int))
64+
65+
if err != nil {
66+
return err
67+
}
68+
69+
return resourceSumologicLocalFileSourceRead(d, meta)
70+
}
71+
72+
func resourceToLocalFileSource(d *schema.ResourceData) LocalFileSource {
73+
rawDenyList := d.Get("deny_list").(*schema.Set).List()
74+
var denylist []string
75+
for _, j := range rawDenyList {
76+
denylist = append(denylist, j.(string))
77+
}
78+
source := resourceToSource(d)
79+
source.Type = "LocalFile"
80+
81+
localFileSource := LocalFileSource{
82+
Source: source,
83+
PathExpression: d.Get("path_expression").(string),
84+
Encoding: d.Get("encoding").(string),
85+
DenyList: denylist,
86+
}
87+
88+
return localFileSource
89+
}
90+
91+
func resourceSumologicLocalFileSourceRead(d *schema.ResourceData, meta interface{}) error {
92+
c := meta.(*Client)
93+
94+
id, _ := strconv.Atoi(d.Id())
95+
source, err := c.GetLocalFileSource(d.Get("collector_id").(int), id)
96+
97+
if err != nil {
98+
return err
99+
}
100+
101+
if source == nil {
102+
log.Printf("[WARN] LocalFile source not found, removing from state: %v - %v", id, err)
103+
d.SetId("")
104+
105+
return nil
106+
}
107+
108+
if err := resourceSumologicSourceRead(d, source.Source); err != nil {
109+
return fmt.Errorf("%s", err)
110+
}
111+
d.Set("path_expression", source.PathExpression)
112+
d.Set("encoding", source.Encoding)
113+
d.Set("deny_list", source.DenyList)
114+
115+
return nil
116+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package sumologic
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
type LocalFileSource struct {
9+
Source
10+
PathExpression string `json:"pathExpression,omitempty"`
11+
Encoding string `json:"encoding,omitempty"`
12+
DenyList []string `json:"denylist,omitempty"`
13+
}
14+
15+
func (s *Client) CreateLocalFileSource(source LocalFileSource, collectorID int) (int, error) {
16+
17+
type LocalFileSourceMessage struct {
18+
Source LocalFileSource `json:"source"`
19+
}
20+
21+
request := LocalFileSourceMessage{
22+
Source: source,
23+
}
24+
25+
urlPath := fmt.Sprintf("v1/collectors/%d/sources", collectorID)
26+
body, err := s.Post(urlPath, request)
27+
28+
if err != nil {
29+
return -1, err
30+
}
31+
32+
var response LocalFileSourceMessage
33+
34+
err = json.Unmarshal(body, &response)
35+
if err != nil {
36+
return -1, err
37+
}
38+
39+
return response.Source.ID, nil
40+
}
41+
42+
func (s *Client) GetLocalFileSource(collectorID, sourceID int) (*LocalFileSource, error) {
43+
44+
body, _, err := s.Get(fmt.Sprintf("v1/collectors/%d/sources/%d", collectorID, sourceID))
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
if body == nil {
50+
return nil, nil
51+
}
52+
53+
type LocalFileSourceResponse struct {
54+
Source LocalFileSource `json:"source"`
55+
}
56+
57+
var response LocalFileSourceResponse
58+
59+
err = json.Unmarshal(body, &response)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
return &response.Source, nil
65+
}
66+
67+
func (s *Client) UpdateLocalFileSource(source LocalFileSource, collectorID int) error {
68+
69+
type LocalFileSourceMessage struct {
70+
Source LocalFileSource `json:"source"`
71+
}
72+
73+
request := LocalFileSourceMessage{
74+
Source: source,
75+
}
76+
77+
urlPath := fmt.Sprintf("v1/collectors/%d/sources/%d", collectorID, source.ID)
78+
_, err := s.Put(urlPath, request)
79+
80+
return err
81+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
layout: "sumologic"
3+
page_title: "SumoLogic: sumologic_local_file_source"
4+
description: |-
5+
Provides a Sumologic Local File Source.
6+
---
7+
8+
# sumologic_local_file_source
9+
Provides a [Sumologic Local File Source][1].
10+
11+
## Example Usage
12+
```hcl
13+
resource "sumologic_installed_collector" "installed_collector" {
14+
name = "test-collector"
15+
category = "macos/test"
16+
ephemeral = true
17+
}
18+
19+
resource "sumologic_local_file_source" "local" {
20+
name = "localfile-mac"
21+
description = "test"
22+
category = "test"
23+
collector_id = "${sumologic_installed_collector.installed_collector.id}"
24+
path_expression = "/Applications/Sumo Logic Collector/logs/*.log.*"
25+
}
26+
```
27+
28+
## Argument Reference
29+
30+
The following arguments are supported:
31+
32+
* `name` - (Required) The name of the local file source. This is required, and has to be unique. Changing this will force recreation the source.
33+
* `path_expression` - (Required) A valid path expression (full path) of the file to collect. For files on Windows systems (not including Windows Events), enter the absolute path including the drive letter. Escape special characters and spaces with a backslash (). If you are collecting from Windows using CIFS/SMB, see Prerequisites for Windows Log Collection. Use a single asterisk wildcard [*] for file or folder names. Example:[var/foo/*.log]. Use two asterisks [**]to recurse within directories and subdirectories. Example: [var/*/.log].
34+
* `description` - (Optional) The description of the source.
35+
* `category` - (Optional) The default source category for the source.
36+
* `fields` - (Optional) Map containing [key/value pairs][2].
37+
* `denylist` - (Optional) Comma-separated list of valid path expressions from which logs will not be collected.
38+
Example: "denylist":["/var/log/**/*.bak","/var/oldlog/*.log"]
39+
* `encoding` - (Optional) Defines the encoding form. Default is "UTF-8". Other supported encodings are listed [here][3].
40+
41+
### See also
42+
* [Common Source Properties](https://github.com/terraform-providers/terraform-provider-sumologic/tree/master/website#common-source-properties)
43+
44+
## Attributes Reference
45+
The following attributes are exported:
46+
47+
* `id` - The internal ID of the local file source.
48+
49+
## Import
50+
Local file sources can be imported using the collector and source IDs, e.g.:
51+
52+
```hcl
53+
terraform import sumologic_local_file_source.test 123/456
54+
```
55+
56+
Local file sources can also be imported using the collector name and source name, e.g.:
57+
58+
```hcl
59+
terraform import sumologic_local_file_source.test my-test-collector/my-test-source
60+
```
61+
62+
[1]: https://help.sumologic.com/docs/send-data/installed-collectors/sources/local-file-source/
63+
[2]: https://help.sumologic.com/Manage/Fields
64+
[3]: https://help.sumologic.com/docs/send-data/installed-collectors/sources/local-file-source/#supported-encoding-for-local-file-sources

0 commit comments

Comments
 (0)