Skip to content

Commit

Permalink
Add "Mackerel" as further provider (GoogleCloudPlatform#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gompei authored Nov 9, 2021
1 parent 789423f commit 1746214
Show file tree
Hide file tree
Showing 16 changed files with 697 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ A CLI tool that generates `tf`/`json` and `tfstate` files based on existing infr
* Monitoring & System Management
* [Datadog](/docs/datadog.md)
* [New Relic](/docs/relic.md)
* [Mackerel](/docs/mackerel.md)
* [PagerDuty](/docs/pagerduty.md)
* [Opsgenie](/docs/opsgenie.md)
* Community
Expand Down Expand Up @@ -277,6 +278,7 @@ Links to download Terraform Providers:
* Monitoring & System Management
* Datadog provider >2.1.0 - [here](https://releases.hashicorp.com/terraform-provider-datadog/)
* New Relic provider >2.0.0 - [here](https://releases.hashicorp.com/terraform-provider-newrelic/)
* Mackerel provider > 0.0.6 - [here](https://github.com/mackerelio-labs/terraform-provider-mackerel)
* Pagerduty >=1.9 - [here](https://releases.hashicorp.com/terraform-provider-pagerduty/)
* Opsgenie >= 0.6.0 [here](https://releases.hashicorp.com/terraform-provider-opsgenie/)
* Community
Expand Down
46 changes: 46 additions & 0 deletions cmd/provider_cmd_mackerel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 cmd

import (
mackerel_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/mackerel"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/spf13/cobra"
)

func newCmdMackerelImporter(options ImportOptions) *cobra.Command {
var apiKey string
cmd := &cobra.Command{
Use: "mackerel",
Short: "Import current state to Terraform configuration from Mackerel",
Long: "Import current state to Terraform configuration from Mackerel",
RunE: func(cmd *cobra.Command, args []string) error {
provider := newMackerelProvider()
err := Import(provider, options, []string{apiKey})
if err != nil {
return err
}
return nil
},
}
cmd.AddCommand(listCmd(newMackerelProvider()))
baseProviderFlags(cmd.PersistentFlags(), &options, "service,role,aws_integration", "aws_integration=id1:id2:id4")
cmd.PersistentFlags().StringVarP(&apiKey, "api-key", "", "", "YOUR_MACKEREL_API_KEY or env param MACKEREL_API_KEY")
return cmd
}

func newMackerelProvider() terraformutils.ProviderGenerator {
return &mackerel_terraforming.MackerelProvider{}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func providerImporterSubcommands() []func(options ImportOptions) *cobra.Command
// Monitoring & System Management
newCmdDatadogImporter,
newCmdNewRelicImporter,
newCmdMackerelImporter,
newCmdGrafanaImporter,
newCmdPagerDutyImporter,
newCmdOpsgenieImporter,
Expand Down
30 changes: 30 additions & 0 deletions docs/mackerel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
### Use with Mackerel

Example:

```bash
./terraformer import mackerel --resources=service --api-key=YOUR_MACKEREL_API_KEY // or MACKEREL_API_KEY in env --app-key=YOUR_MACKEREL_API_KEY
./terraformer import mackerel --resources=service --filter=service=name1:name2:name4 --api-key=YOUR_MACKEREL_API_KEY // or MACKEREL_API_KEY in env --app-key=YOUR_MACKEREL_API_KEY
./terraformer import mackerel --resources=aws_integration --filter=aws_integration=id1:id2:id4 --api-key=YOUR_MACKEREL_API_KEY // or MACKEREL_API_KEY in env --app-key=YOUR_MACKEREL_API_KEY
```

List of supported Mackerel services:

* `alert_group_setting`
* `mackerel_alert_group_setting`
* `aws_integration`
* `mackerel_aws_integration`
* Sensitive field `secret_key` is not generated and needs to be manually set
* Sensitive field `external_id` is not generated and needs to be manually set
* `channel`
* `mackerel_channel`
* `downtime`
* `mackerel_downtime`
* `monitor`
* `mackerel_monitor`
* `notification_group`
* `mackerel_notification_group`
* `role`
* `mackerel_role`
* `service`
* `mackerel_service`
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ require (
github.com/IBM/go-sdk-core v1.1.0
github.com/aws/aws-sdk-go-v2/internal/ini v1.2.2 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/mackerelio/mackerel-client-go v0.19.0
github.com/okta/terraform-provider-okta v0.0.0-20210924173942-a5a664459d3b
github.com/zclconf/go-cty-yaml v1.0.2 // indirect
)
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,8 @@ github.com/likexian/simplejson-go v0.0.0-20190502021454-d8787b4bfa0b/go.mod h1:3
github.com/linode/linodego v0.24.1 h1:3s7/F54z9XZfefzrFqnHMD9DIEYVyOddPm+gyTEFFzc=
github.com/linode/linodego v0.24.1/go.mod h1:GSBKPpjoQfxEfryoCRcgkuUOCuVtGHWhzI8OMdycNTE=
github.com/lusis/go-artifactory v0.0.0-20160115162124-7e4ce345df82/go.mod h1:y54tfGmO3NKssKveTEFFzH8C/akrSOy/iW9qEAUDV84=
github.com/mackerelio/mackerel-client-go v0.19.0 h1:DkYVD07fmklFTMKLaHcjtkU53Nt+nhvXNUSEeKfRSZs=
github.com/mackerelio/mackerel-client-go v0.19.0/go.mod h1:/GNOj+y1eFsd3CK8c6IQ/uS38/GT0+NWImk5YGJs5Lk=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
Expand Down Expand Up @@ -1531,6 +1533,7 @@ golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roY
golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200601175630-2caf76543d99/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200612022331-742c5eb664c2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200624163319-25775e59acb7/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
Expand Down
58 changes: 58 additions & 0 deletions providers/mackerel/alert_group_setting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 mackerel

import (
"fmt"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/mackerelio/mackerel-client-go"
)

// AlertGroupSettingGenerator ...
type AlertGroupSettingGenerator struct {
MackerelService
}

func (g *AlertGroupSettingGenerator) createResources(alertGroupSettings []*mackerel.AlertGroupSetting) []terraformutils.Resource {
resources := []terraformutils.Resource{}
for _, alertGroupSetting := range alertGroupSettings {
resources = append(resources, g.createResource(alertGroupSetting.ID))
}
return resources
}

func (g *AlertGroupSettingGenerator) createResource(alertGroupSettingID string) terraformutils.Resource {
return terraformutils.NewSimpleResource(
alertGroupSettingID,
fmt.Sprintf("alert_group_setting_%s", alertGroupSettingID),
"mackerel_alert_group_setting",
"mackerel",
[]string{},
)
}

// InitResources Generate TerraformResources from Mackerel API,
// from each alert group setting create 1 TerraformResource.
// Need Alert Group Setting ID as ID for terraform resource
func (g *AlertGroupSettingGenerator) InitResources() error {
client := g.Args["mackerelClient"].(*mackerel.Client)
alertGroupSettings, err := client.FindAlertGroupSettings()
if err != nil {
return err
}
g.Resources = append(g.Resources, g.createResources(alertGroupSettings)...)
return nil
}
58 changes: 58 additions & 0 deletions providers/mackerel/aws_integration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 mackerel

import (
"fmt"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/mackerelio/mackerel-client-go"
)

// AWSIntegrationGenerator ...
type AWSIntegrationGenerator struct {
MackerelService
}

func (g *AWSIntegrationGenerator) createResources(awsIntegrations []*mackerel.AWSIntegration) []terraformutils.Resource {
resources := []terraformutils.Resource{}
for _, awsIntegration := range awsIntegrations {
resources = append(resources, g.createResource(awsIntegration.ID))
}
return resources
}

func (g *AWSIntegrationGenerator) createResource(awsIntegrationID string) terraformutils.Resource {
return terraformutils.NewSimpleResource(
awsIntegrationID,
fmt.Sprintf("aws_integration_%s", awsIntegrationID),
"mackerel_aws_integration",
"mackerel",
[]string{},
)
}

// InitResources Generate TerraformResources from Mackerel API,
// from each aws integration create 1 TerraformResource.
// Need AWS Integration ID as ID for terraform resource
func (g *AWSIntegrationGenerator) InitResources() error {
client := g.Args["mackerelClient"].(*mackerel.Client)
awsIntegrations, err := client.FindAWSIntegrations()
if err != nil {
return err
}
g.Resources = append(g.Resources, g.createResources(awsIntegrations)...)
return nil
}
75 changes: 75 additions & 0 deletions providers/mackerel/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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 mackerel

import (
"fmt"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/mackerelio/mackerel-client-go"
)

// ChannelGenerator ...
type ChannelGenerator struct {
MackerelService
}

func (g *ChannelGenerator) createResources(channels []*mackerel.Channel) []terraformutils.Resource {
resources := []terraformutils.Resource{}
for _, channel := range channels {
if channel.Type != "email" && channel.Type != "slack" && channel.Type != "webhook" {
continue
}

if channel.Type == "email" {
if channel.Events != nil {
events := *channel.Events
for _, event := range events {
if event != "alert" && event != "alertGroup" {
continue
}
}
} else {
continue
}
}

resources = append(resources, g.createResource(channel.ID))
}
return resources
}

func (g *ChannelGenerator) createResource(channelID string) terraformutils.Resource {
return terraformutils.NewSimpleResource(
channelID,
fmt.Sprintf("channel_%s", channelID),
"mackerel_channel",
"mackerel",
[]string{},
)
}

// InitResources Generate TerraformResources from Mackerel API,
// from each channel create 1 TerraformResource.
// Need Channel ID as ID for terraform resource
func (g *ChannelGenerator) InitResources() error {
client := g.Args["mackerelClient"].(*mackerel.Client)
channels, err := client.FindChannels()
if err != nil {
return err
}
g.Resources = append(g.Resources, g.createResources(channels)...)
return nil
}
58 changes: 58 additions & 0 deletions providers/mackerel/downtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 mackerel

import (
"fmt"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
"github.com/mackerelio/mackerel-client-go"
)

// DowntimeGenerator ...
type DowntimeGenerator struct {
MackerelService
}

func (g *DowntimeGenerator) createResources(downtimes []*mackerel.Downtime) []terraformutils.Resource {
resources := []terraformutils.Resource{}
for _, downtime := range downtimes {
resources = append(resources, g.createResource(downtime.ID))
}
return resources
}

func (g *DowntimeGenerator) createResource(downtimeID string) terraformutils.Resource {
return terraformutils.NewSimpleResource(
downtimeID,
fmt.Sprintf("downtime_%s", downtimeID),
"mackerel_downtime",
"mackerel",
[]string{},
)
}

// InitResources Generate TerraformResources from Mackerel API,
// from each downtime create 1 TerraformResource.
// Need Downtime ID as ID for terraform resource
func (g *DowntimeGenerator) InitResources() error {
client := g.Args["mackerelClient"].(*mackerel.Client)
downtimes, err := client.FindDowntimes()
if err != nil {
return err
}
g.Resources = append(g.Resources, g.createResources(downtimes)...)
return nil
}
Loading

0 comments on commit 1746214

Please sign in to comment.