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 PagerDuty Provider (GoogleCloudPlatform#899)
* Add PagerDuty Provider * Add PagerDuty Provider Co-authored-by: David Jenniex <djenniex@benevity.com>
- Loading branch information
1 parent
2d77de6
commit e31a0f6
Showing
15 changed files
with
801 additions
and
2 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,47 @@ | ||
// Copyright 2019 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 ( | ||
pagerduty_terraforming "github.com/GoogleCloudPlatform/terraformer/providers/pagerduty" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newCmdPagerDutyImporter(options ImportOptions) *cobra.Command { | ||
token := "" | ||
cmd := &cobra.Command{ | ||
Use: "pagerduty", | ||
Short: "Import current state to Terraform configuration from PagerDuty", | ||
Long: "Import current state to Terraform configuration from PagerDuty", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
provider := newPagerDutyProvider() | ||
err := Import(provider, options, []string{token}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.AddCommand(listCmd(newPagerDutyProvider())) | ||
cmd.PersistentFlags().StringVarP(&token, "token", "t", "", "env param PAGERDUTY_TOKEN") | ||
baseProviderFlags(cmd.PersistentFlags(), &options, "user", "user=id1:id2:id4") | ||
return cmd | ||
} | ||
|
||
func newPagerDutyProvider() terraformutils.ProviderGenerator { | ||
return &pagerduty_terraforming.PagerDutyProvider{} | ||
} |
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,28 @@ | ||
### Use with PagerDuty | ||
|
||
Example: | ||
|
||
``` | ||
./terraformer import pagerduty -r team,schedule,user -t YOUR_PAGERDUTY_TOKEN // or PAGERDUTY_TOKEN in env | ||
``` | ||
Instructions to obtain a Auth Token: https://developer.pagerduty.com/docs/rest-api-v2/authentication/ | ||
|
||
List of supported PagerDuty resources: | ||
|
||
* `business_service` | ||
* `pagerduty_business_service` | ||
* `escalation_policy` | ||
* `pagerduty_escalation_policy` | ||
* `ruleset` | ||
* `pagerduty_ruleset` | ||
* `pagerduty_ruleset_rule` | ||
* `schedule` | ||
* `pagerduty_schedule` | ||
* `service` | ||
* `pagerduty_service` | ||
* `pagerduty_service_event_rule` | ||
* `team` | ||
* `pagerduty_team` | ||
* `pagerduty_team_membership` | ||
* `user` | ||
* `pagerduty_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,63 @@ | ||
// Copyright 2019 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 pagerduty | ||
|
||
import ( | ||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
pagerduty "github.com/heimweh/go-pagerduty/pagerduty" | ||
) | ||
|
||
type BusinessServiceGenerator struct { | ||
PagerDutyService | ||
} | ||
|
||
func (g *BusinessServiceGenerator) createBusinessServiceResources(client *pagerduty.Client) error { | ||
resp, _, err := client.BusinessServices.List() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, service := range resp.BusinessServices { | ||
g.Resources = append(g.Resources, terraformutils.NewSimpleResource( | ||
service.ID, | ||
service.Name, | ||
"pagerduty_business_service", | ||
g.ProviderName, | ||
[]string{}, | ||
)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (g *BusinessServiceGenerator) InitResources() error { | ||
client, err := g.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
funcs := []func(*pagerduty.Client) error{ | ||
g.createBusinessServiceResources, | ||
} | ||
|
||
for _, f := range funcs { | ||
err := f(client) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,73 @@ | ||
// Copyright 2019 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 pagerduty | ||
|
||
import ( | ||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
pagerduty "github.com/heimweh/go-pagerduty/pagerduty" | ||
) | ||
|
||
type EscalationPolicyGenerator struct { | ||
PagerDutyService | ||
} | ||
|
||
func (g *EscalationPolicyGenerator) createEscalationPolicyResources(client *pagerduty.Client) error { | ||
var offset = 0 | ||
options := pagerduty.ListEscalationPoliciesOptions{} | ||
for { | ||
options.Offset = offset | ||
resp, _, err := client.EscalationPolicies.List(&options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, policy := range resp.EscalationPolicies { | ||
g.Resources = append(g.Resources, terraformutils.NewSimpleResource( | ||
policy.ID, | ||
policy.Name, | ||
"pagerduty_escalation_policy", | ||
g.ProviderName, | ||
[]string{}, | ||
)) | ||
} | ||
|
||
if resp.More != true { | ||
break | ||
} | ||
|
||
offset += resp.Limit | ||
} | ||
return nil | ||
} | ||
|
||
func (g *EscalationPolicyGenerator) InitResources() error { | ||
client, err := g.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
funcs := []func(*pagerduty.Client) error{ | ||
g.createEscalationPolicyResources, | ||
} | ||
|
||
for _, f := range funcs { | ||
err := f(client) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,89 @@ | ||
// Copyright 2019 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 pagerduty | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
type PagerDutyProvider struct { //nolint | ||
terraformutils.Provider | ||
token string | ||
} | ||
|
||
func (p *PagerDutyProvider) Init(args []string) error { | ||
if token := os.Getenv("PAGERDUTY_TOKEN"); token != "" { | ||
p.token = os.Getenv("PAGERDUTY_TOKEN") | ||
} | ||
if len(args) > 0 { | ||
p.token = args[0] | ||
} | ||
return nil | ||
} | ||
|
||
func (p *PagerDutyProvider) GetName() string { | ||
return "pagerduty" | ||
} | ||
|
||
func (p *PagerDutyProvider) GetConfig() cty.Value { | ||
return cty.ObjectVal(map[string]cty.Value{ | ||
"token": cty.StringVal(p.token), | ||
}) | ||
} | ||
|
||
func (p *PagerDutyProvider) GetProviderData(arg ...string) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"provider": map[string]interface{}{ | ||
"pagerduty": map[string]interface{}{ | ||
"token": p.token, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (PagerDutyProvider) GetResourceConnections() map[string]map[string][]string { | ||
return map[string]map[string][]string{} | ||
} | ||
|
||
func (p *PagerDutyProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator { | ||
return map[string]terraformutils.ServiceGenerator{ | ||
"business_service": &BusinessServiceGenerator{}, | ||
"escalation_policy": &EscalationPolicyGenerator{}, | ||
"ruleset": &RulesetGenerator{}, | ||
"schedule": &ScheduleGenerator{}, | ||
"service": &ServiceGenerator{}, | ||
"team": &TeamGenerator{}, | ||
"user": &UserGenerator{}, | ||
} | ||
} | ||
|
||
func (p *PagerDutyProvider) 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{}{ | ||
"token": p.token, | ||
}) | ||
return nil | ||
} |
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,32 @@ | ||
// Copyright 2019 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 pagerduty | ||
|
||
import ( | ||
"github.com/GoogleCloudPlatform/terraformer/terraformutils" | ||
pagerduty "github.com/heimweh/go-pagerduty/pagerduty" | ||
) | ||
|
||
type PagerDutyService struct { //nolint | ||
terraformutils.Service | ||
} | ||
|
||
func (s *PagerDutyService) Client() (*pagerduty.Client, error) { | ||
client, err := pagerduty.NewClient(&pagerduty.Config{Token: s.GetArgs()["token"].(string)}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return client, nil | ||
} |
Oops, something went wrong.