This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_opentelekomcloud_compute_bms_flavor_v2.go
124 lines (104 loc) · 2.5 KB
/
data_source_opentelekomcloud_compute_bms_flavor_v2.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package opentelekomcloud
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/bms/v2/flavors"
)
func dataSourceBMSFlavorV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceBMSFlavorV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"min_ram": {
Type: schema.TypeInt,
Optional: true,
},
"ram": {
Type: schema.TypeInt,
Computed: true,
},
"vcpus": {
Type: schema.TypeInt,
Computed: true,
},
"min_disk": {
Type: schema.TypeInt,
Optional: true,
},
"disk": {
Type: schema.TypeInt,
Computed: true,
},
"swap": {
Type: schema.TypeInt,
Computed: true,
},
"rx_tx_factor": {
Type: schema.TypeFloat,
Computed: true,
},
"sort_key": {
Type: schema.TypeString,
Optional: true,
Default: "id",
},
"sort_dir": {
Type: schema.TypeString,
Optional: true,
Default: "asc",
ValidateFunc: dataSourceImagesImageV2SortDirection,
},
},
}
}
func dataSourceBMSFlavorV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
flavorClient, err := config.computeV2HWClient(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating Opentelekom bms client: %s", err)
}
listOpts := flavors.ListOpts{
MinDisk: d.Get("min_disk").(int),
MinRAM: d.Get("min_ram").(int),
Name: d.Get("name").(string),
ID: d.Id(),
SortKey: d.Get("sort_key").(string),
SortDir: d.Get("sort_dir").(string),
}
var flavor flavors.Flavor
refinedflavors, err := flavors.List(flavorClient, listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve flavors: %s", err)
}
if len(refinedflavors) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
} else {
flavor = refinedflavors[0]
}
log.Printf("[DEBUG] Single Flavor found: %s", flavor.ID)
d.SetId(flavor.ID)
d.Set("name", flavor.Name)
d.Set("disk", flavor.Disk)
d.Set("min_disk", flavor.MinDisk)
d.Set("min_ram", flavor.MinRAM)
d.Set("ram", flavor.RAM)
d.Set("rx_tx_factor", flavor.RxTxFactor)
d.Set("swap", flavor.Swap)
d.Set("vcpus", flavor.VCPUs)
return nil
}