-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_container_dedicated_host_flavor.go
119 lines (107 loc) · 3.48 KB
/
data_source_ibm_container_dedicated_host_flavor.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
// Copyright IBM Corp. 2017, 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package kubernetes
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
v2 "github.com/IBM-Cloud/bluemix-go/api/container/containerv2"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
)
func DataSourceIBMContainerDedicatedHostFlavor() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMContainerDedicatedHostFlavorRead,
Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Required: true,
Description: "The zone of the dedicated host flavor",
},
"host_flavor_id": {
Type: schema.TypeString,
Required: true,
Description: "The id of the dedicated host flavor",
},
"flavor_class": {
Type: schema.TypeString,
Computed: true,
Description: "The class of the dedicated host flavor",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "The region of the dedicated host flavor",
},
"deprecated": {
Type: schema.TypeBool,
Computed: true,
Description: "Describes if the dedicated host flavor is deprecated",
},
"max_vcpus": {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum available vcpus in the dedicated host flavor",
},
"max_memory": {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum available memory in the dedicated host flavor",
},
"instance_storage": {
Type: schema.TypeList,
Computed: true,
Description: "The instance storage of the dedicated host flavor",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"count": {
Type: schema.TypeInt,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
},
}
}
func dataSourceIBMContainerDedicatedHostFlavorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
flavorID := d.Get("host_flavor_id").(string)
client, err := meta.(conns.ClientSession).VpcContainerAPI()
if err != nil {
return diag.FromErr(err)
}
dedicatedHostFlavorAPI := client.DedicatedHostFlavor()
targetEnv := v2.ClusterTargetHeader{}
dedicatedHostFlavors, err := dedicatedHostFlavorAPI.ListDedicatedHostFlavors(d.Get("zone").(string), targetEnv)
if err != nil {
return diag.Errorf("[ERROR] Listing dedicated host flavors in zone %s failed: %v", d.Get("zone").(string), err)
}
var dedicatedHostFlavor *v2.GetDedicatedHostFlavor
for _, dh := range dedicatedHostFlavors {
if dh.ID == flavorID {
dedicatedHostFlavor = &dh
break
}
}
if dedicatedHostFlavor == nil {
return diag.Errorf("[ERROR] Dedicated host flavor is not found, id : %s", d.Get("id").(string))
}
d.SetId(dedicatedHostFlavor.ID)
d.Set("flavor_class", dedicatedHostFlavor.FlavorClass)
d.Set("region", dedicatedHostFlavor.Region)
d.Set("deprecated", dedicatedHostFlavor.Deprecated)
d.Set("max_vcpus", dedicatedHostFlavor.MaxVCPUs)
d.Set("max_memory", dedicatedHostFlavor.MaxMemory)
instanceStorage := make([]map[string]interface{}, len(dedicatedHostFlavor.InstanceStorage))
for i, is := range dedicatedHostFlavor.InstanceStorage {
instanceStorage[i] = map[string]interface{}{
"count": is.Count,
"size": is.Size,
}
}
d.Set("instance_storage", instanceStorage)
return nil
}