forked from GoogleCloudPlatform/terraformer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "Opsgenie" as further provider (GoogleCloudPlatform#1124)
- Loading branch information
1 parent
2b552b8
commit 789423f
Showing
10 changed files
with
263 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |