github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/newrelic/dashboard.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 DashboardGenerator struct {
    25  	NewRelicService
    26  }
    27  
    28  func (g *DashboardGenerator) createDashboardResources(client *newrelic.Client) error {
    29  	dashboards, err := client.ListDashboards()
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	for _, dashboard := range dashboards {
    35  		resource := terraformutils.NewSimpleResource(
    36  			fmt.Sprintf("%d", dashboard.ID),
    37  			fmt.Sprintf("%s-%d", normalizeResourceName(dashboard.Title), dashboard.ID),
    38  			"newrelic_dashboard",
    39  			g.ProviderName,
    40  			[]string{})
    41  		resource.SlowQueryRequired = true
    42  		g.Resources = append(g.Resources, resource)
    43  	}
    44  
    45  	return nil
    46  }
    47  
    48  func (g *DashboardGenerator) InitResources() error {
    49  	client, err := g.Client()
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	err = g.createDashboardResources(client)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  func (g *DashboardGenerator) PostConvertHook() error {
    63  	// Widget's title is a `required` field
    64  	// Setting title to empty string for those resources missing this property
    65  	for i, resource := range g.Resources {
    66  		widgets := resource.Item["widget"]
    67  		if widgets == nil {
    68  			continue
    69  		}
    70  		for wIdx, w := range resource.Item["widget"].([]interface{}) {
    71  			if _, ok := w.(map[string]interface{})["title"]; !ok {
    72  				g.Resources[i].Item["widget"].([]interface{})[wIdx].(map[string]interface{})["title"] = ""
    73  			}
    74  		}
    75  	}
    76  
    77  	return nil
    78  }