Skip to content
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

Add support for Sumo Logic #1858

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
47 changes: 47 additions & 0 deletions cmd/provider_cmd_sumologic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd

import (
sumologic_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/sumologic"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/spf13/cobra"
)

func newCmdSumoLogicImporter(options ImportOptions) *cobra.Command {
var accessId, accessKey, environment string
cmd := &cobra.Command{
Use: "sumologic",
Short: "Import current state to Terraform configuration from Sumo Logic",
Long: "Import current state to Terraform configuration from Sumo Logic",
RunE: func(cmd *cobra.Command, args []string) error {
provider := newSumoLogicProvider()
err := Import(provider, options, []string{accessId, accessKey, environment})
if err != nil {
return err
}
return nil
},
}
cmd.AddCommand(listCmd(newSumoLogicProvider()))
baseProviderFlags(cmd.PersistentFlags(), &options, "users", "users=id1:id2:id4")
cmd.PersistentFlags().StringVarP(&accessId, "access-id", "", "", "Sumo Logic Access ID or env param SUMOLOGIC_ACCESS_ID")
cmd.PersistentFlags().StringVarP(&accessKey, "access-key", "", "", "Sumo Logic Access Key or env param SUMOLOGIC_ACCESS_KEY")
cmd.PersistentFlags().StringVarP(&environment, "environment", "", "", "Sumo Logic environment or env param SUMOLOGIC_ENVIRONMENT")
return cmd
}

func newSumoLogicProvider() terraformutils.ProviderGenerator {
return &sumologic_terraforming.SumoLogicProvider{}
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func providerImporterSubcommands() []func(options ImportOptions) *cobra.Command
newCmdOpsgenieImporter,
newCmdHoneycombioImporter,
newCmdOpalImporter,
newCmdSumoLogicImporter,
// Community
newCmdKeycloakImporter,
newCmdLogzioImporter,
Expand Down Expand Up @@ -130,6 +131,7 @@ func providerGenerators() map[string]func() terraformutils.ProviderGenerator {
newPagerDutyProvider,
newHoneycombioProvider,
newOpalProvider,
newSumoLogicProvider,
// Community
newKeycloakProvider,
newLogzioProvider,
Expand Down
43 changes: 43 additions & 0 deletions docs/sumologic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Use with Sumologic

Example:

```
./terraformer import sumologic --resources=user --access-id=SUMOLOGIC_ACCESS_ID --access-key=SUMOLOGIC_ACCESS_KEY --environment=SUMOLOGIC_ENVIRONMENT
```

You can also export env vars instead of providing them via args.
```
export SUMOLOGIC_ENVIRONMENT="us2"
export SUMOLOGIC_ACCESS_ID="US2_ACCESS_ID"
export SUMOLOGIC_ACCESS_KEY="US2_ACCESS_KEY"

./terraformer import sumologic --resources=user
```

List of supported Sumologic services:

* `connection`
* `sumologic_connection`
* `dashboard`
* `sumologic_dashboard`
* `field_extraction_rule`
* `sumologic_field_extraction_rule`
* `monitor`
* `sumologic_monitor`
* `partition`
* `sumologic_partition`
* `role`
* `sumologic_role`

_Roles can be imported by name, which can be passed via `--filter` option._
```
terraformer import sumologic --resources=role --filter 'Name=name;Value=App Admin'
```
* `user`
* `sumologic_user`

_Users can be imported by email, which can be passed via `--filter` option._
```
terraformer import sumologic --resources=user --filter 'Name=email;[email protected]'
```
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ require (
github.com/manicminer/hamilton v0.44.0
github.com/opalsecurity/opal-go v1.0.19
gopkg.in/ns1/ns1-go.v2 v2.6.5
github.com/sumovishal/sumologic-go-sdk v0.0.0-20220807145511-cf1ced70723b
github.com/sumovishal/sumologic-go-sdk v0.0.0-20230810134622-c151e5f7d9f6
)

require (
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,12 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/sumovishal/sumologic-go-sdk v0.0.0-20220806134908-7d671aa8dc0f h1:AQkmKYt+ffgcsd/n1GfJ4DWq1AtYXh5KNC8OGhV7iWI=
github.com/sumovishal/sumologic-go-sdk v0.0.0-20220806134908-7d671aa8dc0f/go.mod h1:DG0EwAJA796aYF/O79yUQ5rlp5loiPU1Sf/TRXOpCmM=
github.com/sumovishal/sumologic-go-sdk v0.0.0-20220807145511-cf1ced70723b h1:+msQGas9ER8bzezyR+hLAhdKgMURIsVd9lLs30A+cI8=
github.com/sumovishal/sumologic-go-sdk v0.0.0-20220807145511-cf1ced70723b/go.mod h1:yqBp0ZPIjBtPjYPuxg/84peHkSFfRwEYqhZu27+xmxc=
github.com/sumovishal/sumologic-go-sdk v0.0.0-20230810134622-c151e5f7d9f6 h1:JRqRDIVZRWiSPen/5SghdPOedtplPdBjoKMJ5xtnoKg=
github.com/sumovishal/sumologic-go-sdk v0.0.0-20230810134622-c151e5f7d9f6/go.mod h1:DG0EwAJA796aYF/O79yUQ5rlp5loiPU1Sf/TRXOpCmM=
github.com/svanharmelen/jsonapi v0.0.0-20180618144545-0c0828c3f16d/go.mod h1:BSTlc8jOjh0niykqEGVXOLXdi9o0r0kR8tCYiMvjFgw=
github.com/tencentcloud/tencentcloud-sdk-go v1.0.392/go.mod h1:l8PU0g9KTDkwn6R4F9WbXXSUwRgKs6k7rj52H4uAcM0=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/aa v1.0.392/go.mod h1:DTrc+dJQjeL+2b7KEkfW1RiZqssiHxIMjRWhnt6PmU4=
Expand Down
68 changes: 68 additions & 0 deletions providers/sumologic/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2021 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sumologic

import (
"fmt"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/iancoleman/strcase"
sumologic "github.com/sumovishal/sumologic-go-sdk/api"
)

type ConnectionGenerator struct {
SumoLogicService
}

func (g *ConnectionGenerator) createResources(connections []sumologic.Connection) []terraformutils.Resource {
resources := make([]terraformutils.Resource, len(connections))

for i, connection := range connections {
name := strcase.ToSnake(replaceSpaceAndDash(connection.Name))
resource := terraformutils.NewSimpleResource(
connection.Id,
fmt.Sprintf("%s-%s", name, connection.Id),
"sumologic_connection",
g.ProviderName,
[]string{})
resources[i] = resource
}

return resources
}

func (g *ConnectionGenerator) InitResources() error {
client := g.Client()
req := client.ConnectionManagementApi.ListConnections(g.AuthCtx())
req = req.Limit(100)

respBody, _, err := client.ConnectionManagementApi.ListConnectionsExecute(req)
if err != nil {
return err
}
connections := respBody.Data
for respBody.Next != nil {
req = req.Token(respBody.GetNext())
respBody, _, err = client.ConnectionManagementApi.ListConnectionsExecute(req)
if err != nil {
return err
}
connections = append(connections, respBody.Data...)
}

resources := g.createResources(connections)
g.Resources = resources
return nil
}
73 changes: 73 additions & 0 deletions providers/sumologic/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2022 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sumologic

import (
"fmt"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/iancoleman/strcase"
sumologic "github.com/sumovishal/sumologic-go-sdk/api"
)

type DashboardGenerator struct {
SumoLogicService
}

func (g *DashboardGenerator) createResources(dashboards []sumologic.Dashboard) []terraformutils.Resource {
resources := make([]terraformutils.Resource, len(dashboards))

for i, dashboard := range dashboards {
title := strcase.ToSnake(replaceSpaceAndDash(dashboard.Title))

resource := terraformutils.NewSimpleResource(
*dashboard.Id,
fmt.Sprintf("%s-%s", title, *dashboard.Id),
"sumologic_dashboard",
g.ProviderName,
[]string{})
resources[i] = resource
}

return resources
}

func (g *DashboardGenerator) InitResources() error {
client := g.Client()

var resources []terraformutils.Resource
var dashboards []sumologic.Dashboard

req := client.DashboardManagementApi.ListDashboards(g.AuthCtx())
req = req.Limit(100)

respBody, _, err := client.DashboardManagementApi.ListDashboardsExecute(req)
if err != nil {
return err
}
dashboards = respBody.Dashboards
for respBody.Next != nil {
req = req.Token(respBody.GetNext())
respBody, _, err = client.DashboardManagementApi.ListDashboardsExecute(req)
if err != nil {
return err
}
dashboards = append(dashboards, respBody.Dashboards...)
}

resources = g.createResources(dashboards)
g.Resources = resources
return nil
}
68 changes: 68 additions & 0 deletions providers/sumologic/field_extraction_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2021 The Terraformer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package sumologic

import (
"fmt"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/iancoleman/strcase"
sumologic "github.com/sumovishal/sumologic-go-sdk/api"
)

type FieldExtractionRuleGenerator struct {
SumoLogicService
}

func (g *FieldExtractionRuleGenerator) createResources(rules []sumologic.ExtractionRule) []terraformutils.Resource {
resources := make([]terraformutils.Resource, len(rules))

for i, rule := range rules {
name := strcase.ToSnake(replaceSpaceAndDash(rule.Name))
resource := terraformutils.NewSimpleResource(
rule.Id,
fmt.Sprintf("%s-%s", name, rule.Id),
"sumologic_field_extraction_rule",
g.ProviderName,
[]string{})
resources[i] = resource
}

return resources
}

func (g *FieldExtractionRuleGenerator) InitResources() error {
client := g.Client()
req := client.ExtractionRuleManagementApi.ListExtractionRules(g.AuthCtx())
req = req.Limit(100)

respBody, _, err := client.ExtractionRuleManagementApi.ListExtractionRulesExecute(req)
if err != nil {
return err
}
rules := respBody.Data
for respBody.Next != nil {
req = req.Token(respBody.GetNext())
respBody, _, err = client.ExtractionRuleManagementApi.ListExtractionRulesExecute(req)
if err != nil {
return err
}
rules = append(rules, respBody.Data...)
}

resources := g.createResources(rules)
g.Resources = resources
return nil
}