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

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/digitalocean/godo"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func dataSourceDigitalOceanImage() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourceDigitalOceanImageRead,
    14  		Schema: map[string]*schema.Schema{
    15  
    16  			"name": &schema.Schema{
    17  				Type:        schema.TypeString,
    18  				Required:    true,
    19  				Description: "name of the image",
    20  			},
    21  			// computed attributes
    22  			"image": &schema.Schema{
    23  				Type:        schema.TypeString,
    24  				Computed:    true,
    25  				Description: "slug or id of the image",
    26  			},
    27  			"min_disk_size": &schema.Schema{
    28  				Type:        schema.TypeInt,
    29  				Computed:    true,
    30  				Description: "minimum disk size required by the image",
    31  			},
    32  			"private": &schema.Schema{
    33  				Type:        schema.TypeBool,
    34  				Computed:    true,
    35  				Description: "Is the image private or non-private",
    36  			},
    37  			"regions": &schema.Schema{
    38  				Type:        schema.TypeList,
    39  				Computed:    true,
    40  				Description: "list of the regions that the image is available in",
    41  				Elem:        &schema.Schema{Type: schema.TypeString},
    42  			},
    43  			"type": &schema.Schema{
    44  				Type:        schema.TypeString,
    45  				Computed:    true,
    46  				Description: "type of the image",
    47  			},
    48  		},
    49  	}
    50  }
    51  
    52  func dataSourceDigitalOceanImageRead(d *schema.ResourceData, meta interface{}) error {
    53  	client := meta.(*godo.Client)
    54  
    55  	opts := &godo.ListOptions{}
    56  
    57  	images, _, err := client.Images.ListUser(opts)
    58  	if err != nil {
    59  		d.SetId("")
    60  		return err
    61  	}
    62  	image, err := findImageByName(images, d.Get("name").(string))
    63  
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	d.SetId(image.Name)
    69  	d.Set("name", image.Name)
    70  	d.Set("image", strconv.Itoa(image.ID))
    71  	d.Set("min_disk_size", image.MinDiskSize)
    72  	d.Set("private", !image.Public)
    73  	d.Set("regions", image.Regions)
    74  	d.Set("type", image.Type)
    75  
    76  	return nil
    77  }
    78  
    79  func findImageByName(images []godo.Image, name string) (*godo.Image, error) {
    80  	results := make([]godo.Image, 0)
    81  	for _, v := range images {
    82  		if v.Name == name {
    83  			results = append(results, v)
    84  		}
    85  	}
    86  	if len(results) == 1 {
    87  		return &results[0], nil
    88  	}
    89  	if len(results) == 0 {
    90  		return nil, fmt.Errorf("no user image found with name %s", name)
    91  	}
    92  	return nil, fmt.Errorf("too many user images found with name %s (found %d, expected 1)", name, len(results))
    93  }