forked from xescugc/terraform-provider-flexibleengine
/
data_source_flexibleengine_dcs_product_v1.go
56 lines (47 loc) · 1.25 KB
/
data_source_flexibleengine_dcs_product_v1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package flexibleengine
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/dcs/v1/products"
)
func dataSourceDcsProductV1() *schema.Resource {
return &schema.Resource{
Read: dataSourceDcsProductV1Read,
Schema: map[string]*schema.Schema{
"spec_code": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceDcsProductV1Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
dcsV1Client, err := config.dcsV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error get dcs product client: %s", err)
}
v, err := products.Get(dcsV1Client).Extract()
if err != nil {
return err
}
log.Printf("[DEBUG] Dcs get products : %+v", v)
var FilteredPd []products.Product
for _, pd := range v.Products {
spec_code := d.Get("spec_code").(string)
if spec_code != "" && pd.SpecCode != spec_code {
continue
}
FilteredPd = append(FilteredPd, pd)
}
if len(FilteredPd) < 1 {
return fmt.Errorf("Your query returned no results. Please change your filters and try again.")
}
pd := FilteredPd[0]
d.SetId(pd.ProductID)
d.Set("spec_code", pd.SpecCode)
log.Printf("[DEBUG] Dcs product : %+v", pd)
return nil
}