github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/digitalocean/datasource_digitalocean_image.go (about)

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