Skip to content

Commit

Permalink
Add "Opsgenie" as further provider (GoogleCloudPlatform#1124)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-muehle authored Nov 9, 2021
1 parent 2b552b8 commit 789423f
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ A CLI tool that generates `tf`/`json` and `tfstate` files based on existing infr
* [Datadog](/docs/datadog.md)
* [New Relic](/docs/relic.md)
* [PagerDuty](/docs/pagerduty.md)
* [Opsgenie](/docs/opsgenie.md)
* Community
* [Keycloak](/docs/keycloak.md)
* [Logz.io](/docs/logz.md)
Expand Down Expand Up @@ -277,6 +278,7 @@ Links to download Terraform Providers:
* 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/)
* Pagerduty >=1.9 - [here](https://releases.hashicorp.com/terraform-provider-pagerduty/)
* Opsgenie >= 0.6.0 [here](https://releases.hashicorp.com/terraform-provider-opsgenie/)
* Community
* Keycloak provider >=1.19.0 - [here](https://github.com/mrparkers/terraform-provider-keycloak/)
* Logz.io provider >=1.1.1 - [here](https://github.com/jonboydell/logzio_terraform_provider/)
Expand Down
33 changes: 33 additions & 0 deletions cmd/provider_cmd_opsgenie.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"github.com/spf13/cobra"

opsgenie_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/opsgenie"
"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)

func newCmdOpsgenieImporter(options ImportOptions) *cobra.Command {
var apiKey string
cmd := &cobra.Command{
Use: "opsgenie",
Short: "Import current state to Terraform configuration from Opsgenie",
Long: "Import current state to Terraform configuration from Opsgenie",
RunE: func(cmd *cobra.Command, args []string) error {
provider := newOpsgenieProvider()
err := Import(provider, options, []string{apiKey})
if err != nil {
return err
}
return nil
},
}
cmd.AddCommand(listCmd(newOpsgenieProvider()))
baseProviderFlags(cmd.PersistentFlags(), &options, "user,team", "")
cmd.PersistentFlags().StringVarP(&apiKey, "api-key", "", "", "YOUR_OPSGENIE_API_KEY or env param OPSGENIE_API_KEY")
return cmd
}

func newOpsgenieProvider() terraformutils.ProviderGenerator {
return &opsgenie_terraforming.OpsgenieProvider{}
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func providerImporterSubcommands() []func(options ImportOptions) *cobra.Command
newCmdNewRelicImporter,
newCmdGrafanaImporter,
newCmdPagerDutyImporter,
newCmdOpsgenieImporter,
// Community
newCmdKeycloakImporter,
newCmdLogzioImporter,
Expand Down
14 changes: 14 additions & 0 deletions docs/opsgenie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### Use with Opsgenie

Example:

```
terraformer import opsgenie --resources=team,user --api-key=YOUR_API_KEY // or OPSGENIE_API_KEY in env
```

List of supported Opsgenie services:

* `team`
* `opsgenie_team`
* `user`
* `opsgenie_user`
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ require (
github.com/oklog/run v1.0.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.8 // indirect
github.com/patrickmn/go-cache v0.0.0-20180815053127-5633e0862627 // indirect
github.com/pborman/uuid v1.2.0 // indirect
github.com/pelletier/go-toml v1.7.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ github.com/hashicorp/go-plugin v1.0.1/go.mod h1:++UyYGoz3o5w9ZzAdZxtQKrWWP+iqPBn
github.com/hashicorp/go-plugin v1.3.0/go.mod h1:F9eH4LrE/ZsRdbwhfjs9k9HoDUwAHnYtXdgmf1AVNs0=
github.com/hashicorp/go-plugin v1.4.1 h1:6UltRQlLN9iZO513VveELp5xyaFxVD2+1OVylE+2E+w=
github.com/hashicorp/go-plugin v1.4.1/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ=
github.com/hashicorp/go-retryablehttp v0.5.1/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.5.2/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
github.com/hashicorp/go-retryablehttp v0.6.2/go.mod h1:gEx6HMUGxYYhJScX7W1Il64m6cc2C1mDaW3NQ9sY1FY=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
Expand Down Expand Up @@ -1041,6 +1042,8 @@ github.com/opencontainers/runc v1.0.1 h1:G18PGckGdAm3yVQRWDVQ1rLSLntiniKJ0cNRT2T
github.com/opencontainers/runc v1.0.1/go.mod h1:aTaHFFwQXuA71CiyxOdFFIorAoemI04suvGRQFzWTD0=
github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.8 h1:qF/rRi8GSU2mjBXfJIyMj9GGmjedsV3Gm1uYbiGlCRk=
github.com/opsgenie/opsgenie-go-sdk-v2 v1.2.8/go.mod h1:4OjcxgwdXzezqytxN534MooNmrxRD50geWZxTD7845s=
github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA=
github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
github.com/packer-community/winrmcp v0.0.0-20180102160824-81144009af58/go.mod h1:f6Izs6JvFTdnRbziASagjZ2vmf55NSIkC/weStxCHqk=
Expand Down
70 changes: 70 additions & 0 deletions providers/opsgenie/opsgenie_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package opsgenie

import (
"errors"
"os"

"github.com/zclconf/go-cty/cty"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)

type OpsgenieProvider struct { //nolint
terraformutils.Provider

APIKey string
}

func (p *OpsgenieProvider) Init(args []string) error {
if apiKey := os.Getenv("OPSGENIE_API_KEY"); apiKey != "" {
p.APIKey = os.Getenv("OPSGENIE_API_KEY")
}
if args[0] != "" {
p.APIKey = args[0]
}
if p.APIKey == "" {
return errors.New("required API Key missing")
}

return nil
}

func (p *OpsgenieProvider) InitService(serviceName string, verbose bool) error {
var isSupported bool
if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
return errors.New(p.GetName() + ": " + serviceName + " not supported service")
}
p.Service = p.GetSupportedService()[serviceName]
p.Service.SetName(serviceName)
p.Service.SetVerbose(verbose)
p.Service.SetProviderName(p.GetName())
p.Service.SetArgs(map[string]interface{}{
"api-key": p.APIKey,
})
return nil
}

func (p *OpsgenieProvider) GetConfig() cty.Value {
return cty.ObjectVal(map[string]cty.Value{
"api_key": cty.StringVal(p.APIKey),
})
}

func (p *OpsgenieProvider) GetProviderData(arg ...string) map[string]interface{} {
return map[string]interface{}{}
}

func (p *OpsgenieProvider) GetResourceConnections() map[string]map[string][]string {
return map[string]map[string][]string{}
}

func (p *OpsgenieProvider) GetName() string {
return "opsgenie"
}

func (p *OpsgenieProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
return map[string]terraformutils.ServiceGenerator{
"user": &UserGenerator{},
"team": &TeamGenerator{},
}
}
21 changes: 21 additions & 0 deletions providers/opsgenie/opsgenie_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package opsgenie

import (
"github.com/opsgenie/opsgenie-go-sdk-v2/client"
"github.com/opsgenie/opsgenie-go-sdk-v2/team"
"github.com/opsgenie/opsgenie-go-sdk-v2/user"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)

type OpsgenieService struct { //nolint
terraformutils.Service
}

func (s *OpsgenieService) UserClient() (*user.Client, error) {
return user.NewClient(&client.Config{ApiKey: s.GetArgs()["api-key"].(string)})
}

func (s *OpsgenieService) TeamClient() (*team.Client, error) {
return team.NewClient(&client.Config{ApiKey: s.GetArgs()["api-key"].(string)})
}
50 changes: 50 additions & 0 deletions providers/opsgenie/team.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package opsgenie

import (
"context"
"time"

"github.com/opsgenie/opsgenie-go-sdk-v2/team"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)

type TeamGenerator struct {
OpsgenieService
}

func (g *TeamGenerator) InitResources() error {
client, err := g.TeamClient()
if err != nil {
return err
}

ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFunc()

result, err := client.List(ctx, &team.ListTeamRequest{})
if err != nil {
return err
}

g.Resources = g.createResources(result.Teams)
return nil
}

func (g *TeamGenerator) createResources(teams []team.ListedTeams) []terraformutils.Resource {
var resources []terraformutils.Resource

for _, t := range teams {
resources = append(resources, terraformutils.NewResource(
t.Id,
t.Name,
"opsgenie_team",
g.ProviderName,
map[string]string{},
[]string{},
map[string]interface{}{},
))
}

return resources
}
68 changes: 68 additions & 0 deletions providers/opsgenie/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package opsgenie

import (
"context"
"fmt"
"time"

"github.com/opsgenie/opsgenie-go-sdk-v2/user"

"github.com/GoogleCloudPlatform/terraformer/terraformutils"
)

type UserGenerator struct {
OpsgenieService
}

func (g *UserGenerator) InitResources() error {
client, err := g.UserClient()
if err != nil {
return err
}

limit := 50
offset := 0

var users []user.User

for {
result, err := func(limit, offset int) (*user.ListResult, error) {
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
defer cancelFunc()

return client.List(ctx, &user.ListRequest{Limit: limit, Offset: offset})
}(limit, offset)

if err != nil {
return err
}

users = append(users, result.Users...)
offset += limit

if offset >= result.TotalCount {
break
}
}

g.Resources = g.createResources(users)
return nil
}

func (g *UserGenerator) createResources(users []user.User) []terraformutils.Resource {
var resources []terraformutils.Resource

for _, u := range users {
resources = append(resources, terraformutils.NewResource(
u.Id,
fmt.Sprintf("%s-%s", u.Id, u.Username),
"opsgenie_user",
g.ProviderName,
map[string]string{},
[]string{},
map[string]interface{}{},
))
}

return resources
}

0 comments on commit 789423f

Please sign in to comment.