github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/monitoring.go (about) 1 // Copyright 2018 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 gcp 16 17 import ( 18 "context" 19 "log" 20 21 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 22 23 "google.golang.org/api/iterator" 24 25 monitoring "cloud.google.com/go/monitoring/apiv3" // nolint 26 monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" 27 ) 28 29 var monitoringAllowEmptyValues = []string{} 30 31 var monitoringAdditionalFields = map[string]interface{}{} 32 33 type MonitoringGenerator struct { 34 GCPService 35 } 36 37 func (g *MonitoringGenerator) loadAlerts(ctx context.Context, project string) error { 38 client, err := monitoring.NewAlertPolicyClient(ctx) 39 if err != nil { 40 return err 41 } 42 43 req := &monitoringpb.ListAlertPoliciesRequest{ 44 Name: "projects/" + project, 45 } 46 47 alertIterator := client.ListAlertPolicies(ctx, req) 48 49 for { 50 alert, err := alertIterator.Next() 51 if err == iterator.Done { 52 break 53 } 54 if err != nil { 55 log.Println("error with alert:", err) 56 continue 57 } 58 g.Resources = append(g.Resources, terraformutils.NewResource( 59 alert.Name, 60 alert.Name, 61 "google_monitoring_alert_policy", 62 g.ProviderName, 63 map[string]string{ 64 "name": alert.Name, 65 "project": project, 66 }, 67 monitoringAllowEmptyValues, 68 monitoringAdditionalFields, 69 )) 70 } 71 return nil 72 } 73 74 func (g *MonitoringGenerator) loadGroups(ctx context.Context, project string) error { 75 client, err := monitoring.NewGroupClient(ctx) 76 if err != nil { 77 return err 78 } 79 80 req := &monitoringpb.ListGroupsRequest{ 81 Name: "projects/" + project, 82 } 83 84 groupsIterator := client.ListGroups(ctx, req) 85 for { 86 group, err := groupsIterator.Next() 87 if err == iterator.Done { 88 break 89 } 90 if err != nil { 91 log.Println("error with group:", err) 92 continue 93 } 94 g.Resources = append(g.Resources, terraformutils.NewResource( 95 group.Name, 96 group.Name, 97 "google_monitoring_group", 98 g.ProviderName, 99 map[string]string{ 100 "name": group.Name, 101 "project": project, 102 }, 103 monitoringAllowEmptyValues, 104 monitoringAdditionalFields, 105 )) 106 } 107 return nil 108 } 109 110 func (g *MonitoringGenerator) loadNotificationChannel(ctx context.Context, project string) error { 111 client, err := monitoring.NewNotificationChannelClient(ctx) 112 if err != nil { 113 return err 114 } 115 116 req := &monitoringpb.ListNotificationChannelsRequest{ 117 Name: "projects/" + project, 118 } 119 120 notificationChannelIterator := client.ListNotificationChannels(ctx, req) 121 for { 122 notificationChannel, err := notificationChannelIterator.Next() 123 if err == iterator.Done { 124 break 125 } 126 if err != nil { 127 log.Println("error with notification Channel:", err) 128 continue 129 } 130 g.Resources = append(g.Resources, terraformutils.NewResource( 131 notificationChannel.Name, 132 notificationChannel.Name, 133 "google_monitoring_notification_channel", 134 g.ProviderName, 135 map[string]string{ 136 "name": notificationChannel.Name, 137 "project": project, 138 }, 139 monitoringAllowEmptyValues, 140 monitoringAdditionalFields, 141 )) 142 } 143 return nil 144 } 145 func (g *MonitoringGenerator) loadUptimeCheck(ctx context.Context, project string) error { 146 client, err := monitoring.NewUptimeCheckClient(ctx) 147 if err != nil { 148 return err 149 } 150 151 req := &monitoringpb.ListUptimeCheckConfigsRequest{ 152 Parent: "projects/" + project, 153 } 154 155 uptimeCheckConfigsIterator := client.ListUptimeCheckConfigs(ctx, req) 156 for { 157 uptimeCheckConfigs, err := uptimeCheckConfigsIterator.Next() 158 if err == iterator.Done { 159 break 160 } 161 if err != nil { 162 log.Println("error with uptimeCheckConfigs:", err) 163 continue 164 } 165 g.Resources = append(g.Resources, terraformutils.NewResource( 166 uptimeCheckConfigs.Name, 167 uptimeCheckConfigs.Name, 168 "google_monitoring_uptime_check_config", 169 g.ProviderName, 170 map[string]string{ 171 "name": uptimeCheckConfigs.Name, 172 "project": project, 173 }, 174 monitoringAllowEmptyValues, 175 monitoringAdditionalFields, 176 )) 177 } 178 return nil 179 } 180 181 // Generate TerraformResources from GCP API, 182 // from each alert create 1 TerraformResource 183 // Need alert name as ID for terraform resource 184 func (g *MonitoringGenerator) InitResources() error { 185 project := g.GetArgs()["project"].(string) 186 ctx := context.Background() 187 188 if err := g.loadAlerts(ctx, project); err != nil { 189 return err 190 } 191 192 if err := g.loadGroups(ctx, project); err != nil { 193 return err 194 } 195 196 if err := g.loadNotificationChannel(ctx, project); err != nil { 197 return err 198 } 199 200 if err := g.loadUptimeCheck(ctx, project); err != nil { 201 return err 202 } 203 204 return nil 205 }