Skip to content

Commit 89e2fc8

Browse files
authored
Merge pull request #189 from ovh/k8s
feat(k8s): add vrack support, fix missing attributes in kube resources
2 parents ce7db06 + 4708242 commit 89e2fc8

7 files changed

+241
-119
lines changed

ovh/data_source_ovh_cloud_project_kube.go

+11-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"log"
66
"net/url"
7-
"strings"
87

98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
109
)
@@ -30,6 +29,10 @@ func dataSourceCloudProjectKube() *schema.Resource {
3029
Type: schema.TypeString,
3130
Optional: true,
3231
},
32+
"private_network_id": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
3336
"control_plane_is_up_to_date": {
3437
Type: schema.TypeBool,
3538
Computed: true,
@@ -87,17 +90,13 @@ func dataSourceCloudProjectKubeRead(d *schema.ResourceData, meta interface{}) er
8790
return fmt.Errorf("Error calling %s:\n\t %q", endpoint, err)
8891
}
8992

90-
d.SetId(res.Id)
91-
d.Set("control_plane_is_up_to_date", res.ControlPlaneIsUpToDate)
92-
d.Set("is_up_to_date", res.IsUpToDate)
93-
d.Set("name", res.Name)
94-
d.Set("next_upgrade_versions", res.NextUpgradeVersions)
95-
d.Set("nodes_url", res.NodesUrl)
96-
d.Set("region", res.Region)
97-
d.Set("status", res.Status)
98-
d.Set("update_policy", res.UpdatePolicy)
99-
d.Set("url", res.Url)
100-
d.Set("version", res.Version[:strings.LastIndex(res.Version, ".")])
93+
for k, v := range res.ToMap() {
94+
if k != "id" {
95+
d.Set(k, v)
96+
} else {
97+
d.SetId(fmt.Sprint(v))
98+
}
99+
}
101100

102101
return nil
103102
}

ovh/resource_ovh_cloud_project_kube.go

+20-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package ovh
33
import (
44
"fmt"
55
"log"
6-
"strings"
76
"time"
87

98
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -43,6 +42,18 @@ func resourceCloudProjectKube() *schema.Resource {
4342
Optional: true,
4443
ForceNew: true,
4544
},
45+
"private_network_id": {
46+
Type: schema.TypeString,
47+
Optional: true,
48+
ForceNew: true,
49+
},
50+
"region": {
51+
Type: schema.TypeString,
52+
Required: true,
53+
ForceNew: true,
54+
},
55+
56+
// Computed
4657
"control_plane_is_up_to_date": {
4758
Type: schema.TypeBool,
4859
Computed: true,
@@ -62,11 +73,6 @@ func resourceCloudProjectKube() *schema.Resource {
6273
Type: schema.TypeString,
6374
Computed: true,
6475
},
65-
"region": {
66-
Type: schema.TypeString,
67-
Required: true,
68-
ForceNew: true,
69-
},
7076
"status": {
7177
Type: schema.TypeString,
7278
Computed: true,
@@ -93,11 +99,7 @@ func resourceCloudProjectKubeCreate(d *schema.ResourceData, meta interface{}) er
9399
serviceName := d.Get("service_name").(string)
94100

95101
endpoint := fmt.Sprintf("/cloud/project/%s/kube", serviceName)
96-
params := &CloudProjectKubeCreateOpts{
97-
Name: d.Get("name").(string),
98-
Region: d.Get("region").(string),
99-
Version: d.Get("version").(string),
100-
}
102+
params := (&CloudProjectKubeCreateOpts{}).FromResource(d)
101103
res := &CloudProjectKubeResponse{}
102104

103105
log.Printf("[DEBUG] Will create kube: %+v", params)
@@ -137,18 +139,13 @@ func resourceCloudProjectKubeRead(d *schema.ResourceData, meta interface{}) erro
137139
if err := config.OVHClient.Get(endpoint, res); err != nil {
138140
return helpers.CheckDeleted(d, err, endpoint)
139141
}
140-
141-
d.SetId(res.Id)
142-
d.Set("control_plane_is_up_to_date", res.ControlPlaneIsUpToDate)
143-
d.Set("is_up_to_date", res.IsUpToDate)
144-
d.Set("name", res.Name)
145-
d.Set("next_upgrade_versions", res.NextUpgradeVersions)
146-
d.Set("nodes_url", res.NodesUrl)
147-
d.Set("region", res.Region)
148-
d.Set("status", res.Status)
149-
d.Set("update_policy", res.UpdatePolicy)
150-
d.Set("url", res.Url)
151-
d.Set("version", res.Version[:strings.LastIndex(res.Version, ".")])
142+
for k, v := range res.ToMap() {
143+
if k != "id" {
144+
d.Set(k, v)
145+
} else {
146+
d.SetId(fmt.Sprint(v))
147+
}
148+
}
152149

153150
if d.IsNewResource() {
154151
kubeconfigRaw := CloudProjectKubeKubeConfigResponse{}

ovh/resource_ovh_cloud_project_kube_nodepool.go

+98-53
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,111 @@ func resourceCloudProjectKubeNodePool() *schema.Resource {
3030
Schema: map[string]*schema.Schema{
3131
"service_name": {
3232
Type: schema.TypeString,
33+
Description: "Service name",
3334
Required: true,
3435
ForceNew: true,
3536
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
3637
},
3738
"kube_id": {
38-
Type: schema.TypeString,
39-
Required: true,
40-
ForceNew: true,
39+
Type: schema.TypeString,
40+
Description: "Kube ID",
41+
Required: true,
42+
ForceNew: true,
4143
},
42-
"name": {
43-
Type: schema.TypeString,
44-
Optional: true,
45-
ForceNew: true,
44+
"autoscale": {
45+
Type: schema.TypeBool,
46+
Description: "Enable auto-scaling for the pool",
47+
Optional: true,
48+
ForceNew: true,
49+
Default: "false",
50+
},
51+
"anti_affinity": {
52+
Type: schema.TypeBool,
53+
Description: "Enable anti affinity groups for nodes in the pool",
54+
Optional: true,
55+
ForceNew: true,
56+
Default: "false",
4657
},
4758
"flavor_name": {
48-
Type: schema.TypeString,
49-
Required: true,
50-
ForceNew: true,
59+
Type: schema.TypeString,
60+
Description: "Flavor name",
61+
Required: true,
62+
ForceNew: true,
5163
},
5264
"desired_nodes": {
53-
Type: schema.TypeInt,
54-
Optional: true,
65+
Type: schema.TypeInt,
66+
Description: "Number of nodes you desire in the pool",
67+
Optional: true,
68+
},
69+
"name": {
70+
Type: schema.TypeString,
71+
Description: "NodePool resource name",
72+
Optional: true,
73+
ForceNew: true,
5574
},
5675
"max_nodes": {
57-
Type: schema.TypeInt,
58-
Optional: true,
76+
Type: schema.TypeInt,
77+
Description: "Number of nodes you desire in the pool",
78+
Optional: true,
5979
},
6080
"min_nodes": {
61-
Type: schema.TypeInt,
62-
Optional: true,
81+
Type: schema.TypeInt,
82+
Description: "Number of nodes you desire in the pool",
83+
Optional: true,
6384
},
6485
"monthly_billed": {
65-
Type: schema.TypeBool,
66-
Optional: true,
67-
ForceNew: true,
68-
Default: "false",
86+
Type: schema.TypeBool,
87+
Description: "Enable monthly billing on all nodes in the pool",
88+
Optional: true,
89+
ForceNew: true,
90+
Default: "false",
6991
},
70-
"anti_affinity": {
71-
Type: schema.TypeBool,
72-
Optional: true,
73-
ForceNew: true,
74-
Default: "false",
92+
93+
// computed
94+
"available_nodes": {
95+
Type: schema.TypeInt,
96+
Description: "Number of nodes which are actually ready in the pool",
97+
Computed: true,
98+
},
99+
"created_at": {
100+
Type: schema.TypeString,
101+
Description: "Creation date",
102+
Computed: true,
103+
},
104+
"current_nodes": {
105+
Type: schema.TypeInt,
106+
Description: "Number of nodes present in the pool",
107+
Computed: true,
108+
},
109+
"flavor": {
110+
Type: schema.TypeString,
111+
Description: "Flavor name",
112+
Computed: true,
113+
},
114+
"project_id": {
115+
Type: schema.TypeString,
116+
Description: "Project id",
117+
Computed: true,
118+
},
119+
"size_status": {
120+
Type: schema.TypeString,
121+
Description: "Status describing the state between number of nodes wanted and available ones",
122+
Computed: true,
75123
},
76124
"status": {
77-
Type: schema.TypeString,
78-
Computed: true,
125+
Type: schema.TypeString,
126+
Description: "Current status",
127+
Computed: true,
128+
},
129+
"up_to_date_nodes": {
130+
Type: schema.TypeInt,
131+
Description: "Number of nodes with latest version installed in the pool",
132+
Computed: true,
133+
},
134+
"updated_at": {
135+
Type: schema.TypeString,
136+
Description: "Last update date",
137+
Computed: true,
79138
},
80139
},
81140
}
@@ -87,15 +146,7 @@ func resourceCloudProjectKubeNodePoolCreate(d *schema.ResourceData, meta interfa
87146
kubeId := d.Get("kube_id").(string)
88147

89148
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s/nodepool", serviceName, kubeId)
90-
params := &CloudProjectKubeNodePoolCreateOpts{
91-
Name: d.Get("name").(string),
92-
FlavorName: d.Get("flavor_name").(string),
93-
DesiredNodes: d.Get("desired_nodes").(int),
94-
MaxNodes: d.Get("max_nodes").(int),
95-
MinNodes: d.Get("min_nodes").(int),
96-
MonthlyBilled: d.Get("monthly_billed").(bool),
97-
AntiAffinity: d.Get("anti_affinity").(bool),
98-
}
149+
params := (&CloudProjectKubeNodePoolCreateOpts{}).FromResource(d)
99150
res := &CloudProjectKubeNodePoolResponse{}
100151

101152
log.Printf("[DEBUG] Will create nodepool: %+v", params)
@@ -137,15 +188,13 @@ func resourceCloudProjectKubeNodePoolRead(d *schema.ResourceData, meta interface
137188
return helpers.CheckDeleted(d, err, endpoint)
138189
}
139190

140-
d.SetId(res.Id)
141-
d.Set("name", res.Name)
142-
d.Set("flavor_name", res.Flavor)
143-
d.Set("desired_nodes", res.DesiredNodes)
144-
d.Set("max_nodes", res.MaxNodes)
145-
d.Set("min_nodes", res.MinNodes)
146-
d.Set("monthly_billed", res.MonthlyBilled)
147-
d.Set("anti_affinity", res.AntiAffinity)
148-
d.Set("status", res.Status)
191+
for k, v := range res.ToMap() {
192+
if k != "id" {
193+
d.Set(k, v)
194+
} else {
195+
d.SetId(fmt.Sprint(v))
196+
}
197+
}
149198

150199
log.Printf("[DEBUG] Read nodepool: %+v", res)
151200
return nil
@@ -157,11 +206,7 @@ func resourceCloudProjectKubeNodePoolUpdate(d *schema.ResourceData, meta interfa
157206
kubeId := d.Get("kube_id").(string)
158207

159208
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s/nodepool/%s", serviceName, kubeId, d.Id())
160-
params := &CloudProjectKubeNodePoolUpdateOpts{
161-
DesiredNodes: d.Get("desired_nodes").(int),
162-
MaxNodes: d.Get("max_nodes").(int),
163-
MinNodes: d.Get("min_nodes").(int),
164-
}
209+
params := (&CloudProjectKubeNodePoolUpdateOpts{}).FromResource(d)
165210

166211
log.Printf("[DEBUG] Will update nodepool: %+v", params)
167212
err := config.OVHClient.Put(endpoint, params, nil)
@@ -205,7 +250,7 @@ func resourceCloudProjectKubeNodePoolDelete(d *schema.ResourceData, meta interfa
205250
}
206251

207252
func cloudProjectKubeNodePoolExists(serviceName, kubeId, id string, client *ovh.Client) error {
208-
res := &CloudProjectKubeResponse{}
253+
res := &CloudProjectKubeNodePoolResponse{}
209254

210255
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s/nodepool/%s", serviceName, kubeId, id)
211256
return client.Get(endpoint, res)
@@ -216,7 +261,7 @@ func waitForCloudProjectKubeNodePoolReady(client *ovh.Client, serviceName, kubeI
216261
Pending: []string{"INSTALLING", "UPDATING", "REDEPLOYING", "RESIZING"},
217262
Target: []string{"READY"},
218263
Refresh: func() (interface{}, string, error) {
219-
res := &CloudProjectKubeResponse{}
264+
res := &CloudProjectKubeNodePoolResponse{}
220265
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s/nodepool/%s", serviceName, kubeId, id)
221266
err := client.Get(endpoint, res)
222267
if err != nil {
@@ -239,7 +284,7 @@ func waitForCloudProjectKubeNodePoolDeleted(client *ovh.Client, serviceName, kub
239284
Pending: []string{"DELETING"},
240285
Target: []string{"DELETED"},
241286
Refresh: func() (interface{}, string, error) {
242-
res := &CloudProjectKubeResponse{}
287+
res := &CloudProjectKubeNodePoolResponse{}
243288
endpoint := fmt.Sprintf("/cloud/project/%s/kube/%s/nodepool/%s", serviceName, kubeId, id)
244289
err := client.Get(endpoint, res)
245290
if err != nil {

0 commit comments

Comments
 (0)