github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/newrelic/synthetics.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  	synthetics "github.com/dollarshaveclub/new-relic-synthetics-go"
    22  	newrelic "github.com/paultyng/go-newrelic/v4/api"
    23  )
    24  
    25  type SyntheticsGenerator struct {
    26  	NewRelicService
    27  }
    28  
    29  func (g *SyntheticsGenerator) createSyntheticsAlertConditionResources(client *newrelic.Client) error {
    30  	alertPolicies, err := client.ListAlertPolicies()
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	for _, alertPolicy := range alertPolicies {
    36  		syntheticsAlertConditions, err := client.ListAlertSyntheticsConditions(alertPolicy.ID)
    37  		if err != nil {
    38  			return err
    39  		}
    40  		for _, syntheticsAlertCondition := range syntheticsAlertConditions {
    41  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    42  				fmt.Sprintf("%d:%d", alertPolicy.ID, syntheticsAlertCondition.ID),
    43  				fmt.Sprintf("%s-%d", normalizeResourceName(alertPolicy.Name), alertPolicy.ID),
    44  				"newrelic_synthetics_alert_condition",
    45  				g.ProviderName,
    46  				[]string{}))
    47  		}
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  func (g *SyntheticsGenerator) createSyntheticsMonitorResources(client *synthetics.Client) error {
    54  	var offset uint
    55  	offset = 0
    56  
    57  	allMonitors, err := client.GetAllMonitors(offset, 100)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	for allMonitors.Count > 0 {
    63  		for _, monitor := range allMonitors.Monitors {
    64  			g.Resources = append(g.Resources, terraformutils.NewSimpleResource(
    65  				fmt.Sprint(monitor.ID),
    66  				fmt.Sprintf("%s-%s", normalizeResourceName(monitor.Name), monitor.ID),
    67  				"newrelic_synthetics_monitor",
    68  				g.ProviderName,
    69  				[]string{}))
    70  		}
    71  
    72  		offset += allMonitors.Count
    73  		allMonitors, err = client.GetAllMonitors(offset, 100)
    74  		if err != nil {
    75  			return err
    76  		}
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  func (g *SyntheticsGenerator) InitResources() error {
    83  	synctheticClient, err := g.SyntheticsClient()
    84  	if err != nil {
    85  		return err
    86  	}
    87  	client, err := g.Client()
    88  	if err != nil {
    89  		return err
    90  	}
    91  
    92  	err = g.createSyntheticsMonitorResources(synctheticClient)
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	err = g.createSyntheticsAlertConditionResources(client)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	return nil
   103  }