github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/scaleway/data_source_scaleway_bootscript.go (about)

     1  package scaleway
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/scaleway/scaleway-cli/pkg/api"
     9  )
    10  
    11  func dataSourceScalewayBootscript() *schema.Resource {
    12  	return &schema.Resource{
    13  		Read: dataSourceScalewayBootscriptRead,
    14  
    15  		Schema: map[string]*schema.Schema{
    16  			"name": &schema.Schema{
    17  				Type:     schema.TypeString,
    18  				Optional: true,
    19  				ForceNew: true,
    20  			},
    21  			"name_filter": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Optional: true,
    24  				ForceNew: true,
    25  			},
    26  			"architecture": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Computed: true,
    29  				Optional: true,
    30  			},
    31  			// Computed values.
    32  			"organization": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  			"public": &schema.Schema{
    37  				Type:     schema.TypeBool,
    38  				Computed: true,
    39  			},
    40  			"boot_cmd_args": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Computed: true,
    43  			},
    44  			"dtb": &schema.Schema{
    45  				Type:     schema.TypeString,
    46  				Computed: true,
    47  			},
    48  			"initrd": &schema.Schema{
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			},
    52  			"kernel": &schema.Schema{
    53  				Type:     schema.TypeString,
    54  				Computed: true,
    55  			},
    56  		},
    57  	}
    58  }
    59  
    60  func bootscriptDescriptionAttributes(d *schema.ResourceData, script api.ScalewayBootscript) error {
    61  	d.Set("architecture", script.Arch)
    62  	d.Set("organization", script.Organization)
    63  	d.Set("public", script.Public)
    64  	d.Set("boot_cmd_args", script.Bootcmdargs)
    65  	d.Set("dtb", script.Dtb)
    66  	d.Set("initrd", script.Initrd)
    67  	d.Set("kernel", script.Kernel)
    68  	d.SetId(script.Identifier)
    69  
    70  	return nil
    71  }
    72  
    73  func dataSourceScalewayBootscriptRead(d *schema.ResourceData, meta interface{}) error {
    74  	scaleway := meta.(*Client).scaleway
    75  
    76  	scripts, err := scaleway.GetBootscripts()
    77  	if err != nil {
    78  		return err
    79  	}
    80  
    81  	var isMatch func(api.ScalewayBootscript) bool
    82  
    83  	if name, ok := d.GetOk("name"); ok {
    84  		isMatch = func(s api.ScalewayBootscript) bool {
    85  			return s.Title == name.(string)
    86  		}
    87  	} else if nameFilter, ok := d.GetOk("name_filter"); ok {
    88  		architecture := d.Get("architecture")
    89  		exp, err := regexp.Compile(nameFilter.(string))
    90  		if err != nil {
    91  			return err
    92  		}
    93  
    94  		isMatch = func(s api.ScalewayBootscript) bool {
    95  			nameMatch := exp.MatchString(s.Title)
    96  			architectureMatch := true
    97  			if architecture != "" {
    98  				architectureMatch = architecture == s.Arch
    99  			}
   100  			return nameMatch && architectureMatch
   101  		}
   102  	}
   103  
   104  	var matches []api.ScalewayBootscript
   105  	for _, script := range *scripts {
   106  		if isMatch(script) {
   107  			matches = append(matches, script)
   108  		}
   109  	}
   110  
   111  	if len(matches) > 1 {
   112  		return fmt.Errorf("The query returned more than one result. Please refine your query.")
   113  	}
   114  	if len(matches) == 0 {
   115  		return fmt.Errorf("The query returned no result. Please refine your query.")
   116  	}
   117  
   118  	return bootscriptDescriptionAttributes(d, matches[0])
   119  }