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

     1  package atlas
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/atlas-go/v1"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func dataSourceAtlasArtifact() *schema.Resource {
    11  	return &schema.Resource{
    12  		Read: dataSourceArtifactRead,
    13  
    14  		Schema: map[string]*schema.Schema{
    15  			"name": &schema.Schema{
    16  				Type:     schema.TypeString,
    17  				Required: true,
    18  				ForceNew: true,
    19  			},
    20  
    21  			"type": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"build": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				ForceNew: true,
    31  			},
    32  
    33  			"version": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Optional: true,
    36  				ForceNew: true,
    37  			},
    38  
    39  			"metadata_keys": &schema.Schema{
    40  				Type:     schema.TypeSet,
    41  				Optional: true,
    42  				ForceNew: true,
    43  				Elem:     &schema.Schema{Type: schema.TypeString},
    44  				Set:      schema.HashString,
    45  			},
    46  
    47  			"metadata": &schema.Schema{
    48  				Type:     schema.TypeMap,
    49  				Optional: true,
    50  				ForceNew: true,
    51  			},
    52  
    53  			"file_url": &schema.Schema{
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  
    58  			"metadata_full": &schema.Schema{
    59  				Type:     schema.TypeMap,
    60  				Computed: true,
    61  			},
    62  
    63  			"slug": &schema.Schema{
    64  				Type:     schema.TypeString,
    65  				Computed: true,
    66  			},
    67  
    68  			"version_real": &schema.Schema{
    69  				Type:     schema.TypeString,
    70  				Computed: true,
    71  			},
    72  		},
    73  	}
    74  }
    75  
    76  func dataSourceArtifactRead(d *schema.ResourceData, meta interface{}) error {
    77  	client := meta.(*atlas.Client)
    78  
    79  	// Parse the slug from the name given of the artifact since the API
    80  	// expects these to be split.
    81  	user, name, err := atlas.ParseSlug(d.Get("name").(string))
    82  	if err != nil {
    83  		return err
    84  	}
    85  
    86  	// Filter by version or build if given
    87  	var build, version string
    88  	if v, ok := d.GetOk("version"); ok {
    89  		version = v.(string)
    90  	} else if b, ok := d.GetOk("build"); ok {
    91  		build = b.(string)
    92  	}
    93  
    94  	// If we have neither, default to latest version
    95  	if build == "" && version == "" {
    96  		version = "latest"
    97  	}
    98  
    99  	// Compile the metadata search params
   100  	md := make(map[string]string)
   101  	for _, v := range d.Get("metadata_keys").(*schema.Set).List() {
   102  		md[v.(string)] = atlas.MetadataAnyValue
   103  	}
   104  	for k, v := range d.Get("metadata").(map[string]interface{}) {
   105  		md[k] = v.(string)
   106  	}
   107  
   108  	// Do the search!
   109  	vs, err := client.ArtifactSearch(&atlas.ArtifactSearchOpts{
   110  		User:     user,
   111  		Name:     name,
   112  		Type:     d.Get("type").(string),
   113  		Build:    build,
   114  		Version:  version,
   115  		Metadata: md,
   116  	})
   117  	if err != nil {
   118  		return fmt.Errorf(
   119  			"Error searching for artifact '%s/%s': %s",
   120  			user, name, err)
   121  	}
   122  
   123  	if len(vs) == 0 {
   124  		return fmt.Errorf("No matching artifact for '%s/%s'", user, name)
   125  	} else if len(vs) > 1 {
   126  		return fmt.Errorf(
   127  			"Got %d results for '%s/%s', only one is allowed",
   128  			len(vs), user, name)
   129  	}
   130  	v := vs[0]
   131  
   132  	d.SetId(v.ID)
   133  	if v.ID == "" {
   134  		d.SetId(fmt.Sprintf("%s %d", v.Tag, v.Version))
   135  	}
   136  	d.Set("version_real", v.Version)
   137  	d.Set("metadata_full", cleanMetadata(v.Metadata))
   138  	d.Set("slug", v.Slug)
   139  
   140  	d.Set("file_url", "")
   141  	if u, err := client.ArtifactFileURL(v); err != nil {
   142  		return fmt.Errorf(
   143  			"Error reading file URL: %s", err)
   144  	} else if u != nil {
   145  		d.Set("file_url", u.String())
   146  	}
   147  
   148  	return nil
   149  }