-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_dataplatform_node_pool.go
200 lines (182 loc) · 6.53 KB
/
data_source_dataplatform_node_pool.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package ionoscloud
import (
"context"
"errors"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
dataplatform "github.com/ionos-cloud/sdk-go-dataplatform"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
dataplatformService "github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/dataplatform"
)
func dataSourceDataplatformNodePool() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDataplatformReadNodePool,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.IsUUID),
},
"name": {
Type: schema.TypeString,
Description: "The name of your node pool.",
Optional: true,
},
"partial_match": {
Type: schema.TypeBool,
Description: "Whether partial matching is allowed or not when using name argument.",
Default: false,
Optional: true,
},
"version": {
Type: schema.TypeString,
Description: "The version of the Data Platform.",
Computed: true,
},
"datacenter_id": {
Type: schema.TypeString,
Description: "The UUID of the virtual data center (VDC) in which the node pool is provisioned",
Computed: true,
},
"node_count": {
Type: schema.TypeInt,
Description: "The number of nodes that make up the node pool.",
Computed: true,
},
"cpu_family": {
Type: schema.TypeString,
Description: "A valid CPU family name or `AUTO` if the platform shall choose the best fitting option. Available CPU architectures can be retrieved from the datacenter resource.",
Computed: true,
},
"cores_count": {
Type: schema.TypeInt,
Description: "The number of CPU cores per node.",
Computed: true,
},
"ram_size": {
Type: schema.TypeInt,
Description: "The RAM size for one node in MB. Must be set in multiples of 1024 MB, with a minimum size is of 2048 MB.",
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Description: "The availability zone of the virtual datacenter region where the node pool resources should be provisioned.",
Computed: true,
},
"storage_type": {
Type: schema.TypeString,
Description: "The type of hardware for the volume.",
Computed: true,
},
"storage_size": {
Type: schema.TypeInt,
Description: "The size of the volume in GB. The size must be greater than 10GB.",
Computed: true,
},
"maintenance_window": {
Type: schema.TypeList,
Description: "Starting time of a weekly 4 hour-long window, during which maintenance might occur in hh:mm:ss format",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"time": {
Type: schema.TypeString,
Description: "Time at which the maintenance should start. Must conform to the 'HH:MM:SS' 24-hour format.",
Computed: true,
},
"day_of_the_week": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"labels": {
Type: schema.TypeMap,
Description: "Key-value pairs attached to the node pool resource as kubernetes labels",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"annotations": {
Type: schema.TypeMap,
Description: "Key-value pairs attached to node pool resource as kubernetes annotations",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: "The UUID of an existing Dataplatform cluster",
ValidateDiagFunc: validation.ToDiagFunc(validation.IsUUID),
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func dataSourceDataplatformReadNodePool(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).DataplatformClient
clusterId := d.Get("cluster_id").(string)
idValue, idOk := d.GetOk("id")
nameValue, nameOk := d.GetOk("name")
id := idValue.(string)
name := nameValue.(string)
if idOk && nameOk {
return diag.FromErr(errors.New("id and name cannot be both specified in the same time"))
}
if !idOk && !nameOk {
return diag.FromErr(errors.New("please provide either the Dataplatform Node Pool id or name"))
}
var nodePool dataplatform.NodePoolResponseData
var err error
if idOk {
/* search by ID */
nodePool, _, err = client.GetNodePool(ctx, clusterId, id)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occurred while fetching the Dataplatform Node Pool with ID %s: %w", id, err))
}
} else {
/* search by name */
results, diagErr := filterNodePools(ctx, d, client, name)
if diagErr != nil {
return diagErr
}
if results == nil || len(results) == 0 {
return diag.FromErr(fmt.Errorf("no Dataplatform NodePool found with the specified name = %s", name))
} else if len(results) > 1 {
return diag.FromErr(fmt.Errorf("more than one Dataplatform NodePool found with the specified criteria name = %s", name))
} else {
nodePool = results[0]
}
}
if err = dataplatformService.SetDataplatformNodePoolData(d, nodePool); err != nil {
return diag.FromErr(err)
}
return nil
}
func filterNodePools(ctx context.Context, d *schema.ResourceData, client *dataplatformService.Client, name string) ([]dataplatform.NodePoolResponseData, diag.Diagnostics) {
clusterId := d.Get("cluster_id").(string)
var results []dataplatform.NodePoolResponseData
partialMatch := d.Get("partial_match").(bool)
log.Printf("[INFO] Using data source for Dataplatform Node Pool by name with partial_match %t and name: %s", partialMatch, name)
nodePools, _, err := client.ListNodePools(ctx, clusterId)
if err != nil {
diags := diag.FromErr(fmt.Errorf("an error occurred while fetching Dataplatform NodePools: %w", err))
return nil, diags
}
if nodePools.Items != nil && len(*nodePools.Items) > 0 {
for _, nodePoolItem := range *nodePools.Items {
if nodePoolItem.Properties != nil && nodePoolItem.Properties.Name != nil && (partialMatch && strings.Contains(*nodePoolItem.Properties.Name, name) ||
!partialMatch && strings.EqualFold(*nodePoolItem.Properties.Name, name)) {
if len(results) > 1 {
break
}
results = append(results, nodePoolItem)
}
}
}
return results, nil
}