github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/newrelic/data_source_newrelic_application.go (about)

     1  package newrelic
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	newrelic "github.com/paultyng/go-newrelic/api"
    10  )
    11  
    12  func dataSourceNewRelicApplication() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceNewRelicApplicationRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"name": {
    18  				Type:     schema.TypeString,
    19  				Required: true,
    20  			},
    21  			"instance_ids": {
    22  				Type:     schema.TypeList,
    23  				Elem:     &schema.Schema{Type: schema.TypeInt},
    24  				Computed: true,
    25  			},
    26  			"host_ids": {
    27  				Type:     schema.TypeList,
    28  				Elem:     &schema.Schema{Type: schema.TypeInt},
    29  				Computed: true,
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  func dataSourceNewRelicApplicationRead(d *schema.ResourceData, meta interface{}) error {
    36  	client := meta.(*newrelic.Client)
    37  
    38  	log.Printf("[INFO] Reading New Relic applications")
    39  
    40  	applications, err := client.ListApplications()
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	var application *newrelic.Application
    46  	name := d.Get("name").(string)
    47  
    48  	for _, a := range applications {
    49  		if a.Name == name {
    50  			application = &a
    51  			break
    52  		}
    53  	}
    54  
    55  	if application == nil {
    56  		return fmt.Errorf("The name '%s' does not match any New Relic applications.", name)
    57  	}
    58  
    59  	d.SetId(strconv.Itoa(application.ID))
    60  	d.Set("name", application.Name)
    61  	d.Set("instance_ids", application.Links.InstanceIDs)
    62  	d.Set("host_ids", application.Links.HostIDs)
    63  
    64  	return nil
    65  }