github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 architecture := d.Get("architecture") 84 if name, ok := d.GetOk("name"); ok { 85 isMatch = func(s api.ScalewayBootscript) bool { 86 architectureMatch := true 87 if architecture != "" { 88 architectureMatch = architecture == s.Arch 89 } 90 return s.Title == name.(string) && architectureMatch 91 } 92 } else if nameFilter, ok := d.GetOk("name_filter"); ok { 93 exp, err := regexp.Compile(nameFilter.(string)) 94 if err != nil { 95 return err 96 } 97 98 isMatch = func(s api.ScalewayBootscript) bool { 99 nameMatch := exp.MatchString(s.Title) 100 architectureMatch := true 101 if architecture != "" { 102 architectureMatch = architecture == s.Arch 103 } 104 return nameMatch && architectureMatch 105 } 106 } 107 108 var matches []api.ScalewayBootscript 109 for _, script := range *scripts { 110 if isMatch(script) { 111 matches = append(matches, script) 112 } 113 } 114 115 if len(matches) > 1 { 116 return fmt.Errorf("The query returned more than one result. Please refine your query.") 117 } 118 if len(matches) == 0 { 119 return fmt.Errorf("The query returned no result. Please refine your query.") 120 } 121 122 return bootscriptDescriptionAttributes(d, matches[0]) 123 }