github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/profitbricks/data_source_image.go (about)

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  	"github.com/profitbricks/profitbricks-sdk-go"
     7  	"strings"
     8  )
     9  
    10  func dataSourceImage() *schema.Resource {
    11  	return &schema.Resource{
    12  		Read: dataSourceImageRead,
    13  		Schema: map[string]*schema.Schema{
    14  			"name": {
    15  				Type:     schema.TypeString,
    16  				Optional: true,
    17  			},
    18  			"type": {
    19  				Type:     schema.TypeString,
    20  				Optional: true,
    21  			},
    22  			"location": {
    23  				Type:     schema.TypeString,
    24  				Optional: true,
    25  			},
    26  			"version": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  			},
    30  		},
    31  	}
    32  }
    33  
    34  func dataSourceImageRead(d *schema.ResourceData, meta interface{}) error {
    35  	profitbricks.SetDepth("5")
    36  
    37  	images := profitbricks.ListImages()
    38  
    39  	if images.StatusCode > 299 {
    40  		return fmt.Errorf("An error occured while fetching ProfitBricks locations %s", images.Response)
    41  	}
    42  
    43  	name := d.Get("name").(string)
    44  	imageType, imageTypeOk := d.GetOk("type")
    45  	location, locationOk := d.GetOk("location")
    46  	version, versionOk := d.GetOk("version")
    47  
    48  	results := []profitbricks.Image{}
    49  
    50  	// if version value is present then concatenate name - version
    51  	// otherwise search by name or part of the name
    52  	if versionOk {
    53  		name_ver := fmt.Sprintf("%s-%s", name, version.(string))
    54  		for _, img := range images.Items {
    55  			if strings.Contains(strings.ToLower(img.Properties.Name), strings.ToLower(name_ver)) {
    56  				results = append(results, img)
    57  			}
    58  		}
    59  	} else {
    60  		for _, img := range images.Items {
    61  			if strings.Contains(strings.ToLower(img.Properties.Name), strings.ToLower(name)) {
    62  				results = append(results, img)
    63  			}
    64  		}
    65  	}
    66  
    67  	if imageTypeOk {
    68  		imageTypeResults := []profitbricks.Image{}
    69  		for _, img := range results {
    70  			if img.Properties.ImageType == imageType.(string) {
    71  				imageTypeResults = append(imageTypeResults, img)
    72  			}
    73  
    74  		}
    75  		results = imageTypeResults
    76  	}
    77  
    78  	if locationOk {
    79  		locationResults := []profitbricks.Image{}
    80  		for _, img := range results {
    81  			if img.Properties.Location == location.(string) {
    82  				locationResults = append(locationResults, img)
    83  			}
    84  
    85  		}
    86  		results = locationResults
    87  	}
    88  
    89  	if len(results) > 1 {
    90  		return fmt.Errorf("There is more than one image that match the search criteria")
    91  	}
    92  
    93  	if len(results) == 0 {
    94  		return fmt.Errorf("There are no images that match the search criteria")
    95  	}
    96  
    97  	d.Set("name", results[0].Properties.Name)
    98  
    99  	d.SetId(results[0].Id)
   100  
   101  	return nil
   102  }