github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/newrelic/alert.go (about) 1 // Copyright 2019 The Terraformer Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package newrelic 16 17 import ( 18 "fmt" 19 20 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 21 newrelic "github.com/paultyng/go-newrelic/v4/api" 22 ) 23 24 type AlertGenerator struct { 25 NewRelicService 26 } 27 28 func (g *AlertGenerator) createAlertChannelResources(client *newrelic.Client) error { 29 alertChannels, err := client.ListAlertChannels() 30 if err != nil { 31 return err 32 } 33 34 for _, channel := range alertChannels { 35 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 36 fmt.Sprintf("%d", channel.ID), 37 fmt.Sprintf("%s-%d", normalizeResourceName(channel.Name), channel.ID), 38 "newrelic_alert_channel", 39 g.ProviderName, 40 []string{}, 41 )) 42 } 43 44 return nil 45 } 46 47 func (g *AlertGenerator) createAlertConditionResources(client *newrelic.Client) error { 48 alertPolicies, err := client.ListAlertPolicies() 49 if err != nil { 50 return err 51 } 52 53 for _, alertPolicy := range alertPolicies { 54 alertConditions, err := client.ListAlertConditions(alertPolicy.ID) 55 if err != nil { 56 return err 57 } 58 59 for _, alertCondition := range alertConditions { 60 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 61 fmt.Sprintf("%d:%d", alertPolicy.ID, alertCondition.ID), 62 fmt.Sprintf("%s-%d", normalizeResourceName(alertCondition.Name), alertCondition.ID), 63 "newrelic_alert_condition", 64 g.ProviderName, 65 []string{})) 66 } 67 } 68 return nil 69 } 70 71 func (g *AlertGenerator) createAlertPolicyResources(client *newrelic.Client) error { 72 alertPolicies, err := client.ListAlertPolicies() 73 if err != nil { 74 return err 75 } 76 77 for _, alertPolicy := range alertPolicies { 78 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 79 fmt.Sprintf("%d", alertPolicy.ID), 80 fmt.Sprintf("%s-%d", normalizeResourceName(alertPolicy.Name), alertPolicy.ID), 81 "newrelic_alert_policy", 82 g.ProviderName, 83 []string{})) 84 } 85 86 return nil 87 } 88 89 func (g *AlertGenerator) InitResources() error { 90 client, err := g.Client() 91 if err != nil { 92 return err 93 } 94 95 funcs := []func(*newrelic.Client) error{ 96 g.createAlertChannelResources, 97 g.createAlertConditionResources, 98 g.createAlertPolicyResources, 99 } 100 101 for _, f := range funcs { 102 err := f(client) 103 if err != nil { 104 return err 105 } 106 } 107 108 return nil 109 } 110 111 func (g *AlertGenerator) PostConvertHook() error { 112 for i, resource := range g.Resources { 113 if resource.InstanceInfo.Type == "newrelic_alert_condition" { 114 if resource.Item["violation_close_timer"] == "0" { 115 delete(g.Resources[i].Item, "violation_close_timer") 116 } 117 } 118 } 119 120 return nil 121 }