Skip to content

Commit 767f426

Browse files
committed
fix: ovh_vps: make sure resource is saved in state before performing rebuild that can fail
Fixes #1118 Signed-off-by: Romain Beuque <[email protected]>
1 parent c8a199f commit 767f426

File tree

5 files changed

+14
-32
lines changed

5 files changed

+14
-32
lines changed

docs/resources/vps.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,23 @@ Creates an OVHcloud Virtual Private Server (VPS).
1212

1313
~> **WARNING** `BANK_ACCOUNT` is not supported anymore, please update your default payment method to `SEPA_DIRECT_DEBIT`
1414

15-
~> **NOTE** During VPS creation, the OS must be configured through the `plan.configuration` argument, using `vps_os` as key, and values from [catalog](https://eu.api.ovh.com/console/?section=%2Forder&branch=v1#get-/order/catalog/public/vps) data). The `image_id` argument should be used only when reinstalling a server. The available values for `image_id` can be found using this [API call](https://eu.api.ovh.com/console/?section=%2Fvps&branch=v1#get-/vps/-serviceName-/images/available)
15+
~> **NOTE** During VPS creation, the OS must be configured through the `plan.configuration` argument, using `vps_os` as key, and values from [catalog](https://eu.api.ovh.com/console/?section=%2Forder&branch=v1#get-/order/catalog/public/vps) data). If you provide a `public_ssh_key` during VPS creation, you will have to provide `image_id` as well. The `image_id` argument should be used only when reinstalling a server. The available values for `image_id` can be found using this [API call](https://eu.api.ovh.com/console/?section=%2Fvps&branch=v1#get-/vps/-serviceName-/images/available)
1616

1717
## Example Usage
1818

1919
```terraform
2020
data "ovh_me" "my_account" {}
2121
22-
data "ovh_order_cart" "mycart" {
23-
ovh_subsidiary = data.ovh_me.my_account.ovh_subsidiary
24-
}
25-
26-
data "ovh_order_cart_product_plan" "vps" {
27-
cart_id = data.ovh_order_cart.mycart.id
28-
price_capacity = "renew"
29-
product = "vps"
30-
plan_code = "vps-le-2-2-40"
31-
}
32-
3322
resource "ovh_vps" "my_vps" {
3423
display_name = "dev_vps"
3524
3625
image_id = "45b2f222-ab10-44ed-863f-720942762b6f"
3726
38-
ovh_subsidiary = data.ovh_order_cart.mycart.ovh_subsidiary
27+
ovh_subsidiary = data.ovh_me.my_account.ovh_subsidiary
3928
plan = [
4029
{
4130
duration = "P1M"
42-
plan_code = data.ovh_order_cart_product_plan.vps.plan_code
31+
plan_code = "vps-le-2-2-40"
4332
pricing_mode = "default"
4433
4534
configuration = [
@@ -49,7 +38,7 @@ resource "ovh_vps" "my_vps" {
4938
},
5039
{
5140
label = "vps_os"
52-
value = "Debian 10"
41+
value = "Debian 13"
5342
}
5443
]
5544
}
@@ -68,7 +57,7 @@ output "vps_display_name" {
6857
The following arguments are supported:
6958

7059
* `display_name` - Custom display name
71-
* `image_id` - (String) Id of the image to install on the VPS. This attribute is only useful to trigger a VPS reinstallation. The available values can be found using this [API call](https://eu.api.ovh.com/console/?section=%2Fvps&branch=v1#get-/vps/-serviceName-/images/available)
60+
* `image_id` - (String) Id of the image to install on the VPS. This attribute is useful to trigger a VPS reinstallation, and during VPS creation if you want to configure a `public_ssh_key`. The available values can be found using this [API call](https://eu.api.ovh.com/console/?section=%2Fvps&branch=v1#get-/vps/-serviceName-/images/available)
7261
* `netboot_mode` - VPS netboot mode (local┃rescue)
7362
* `ovh_subsidiary` - (Required) OVHcloud Subsidiary. Country of OVHcloud legal entity you'll be billed by. List of supported subsidiaries available on API at [/1.0/me.json](https://eu.api.ovh.com/console-preview/?section=%2Fme&branch=v1#get-/me)
7463
* `plan` - (Required) Product Plan to order

examples/resources/vps/example_1.tf

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
data "ovh_me" "my_account" {}
22

3-
data "ovh_order_cart" "mycart" {
4-
ovh_subsidiary = data.ovh_me.my_account.ovh_subsidiary
5-
}
6-
7-
data "ovh_order_cart_product_plan" "vps" {
8-
cart_id = data.ovh_order_cart.mycart.id
9-
price_capacity = "renew"
10-
product = "vps"
11-
plan_code = "vps-le-2-2-40"
12-
}
13-
143
resource "ovh_vps" "my_vps" {
154
display_name = "dev_vps"
165

176
image_id = "45b2f222-ab10-44ed-863f-720942762b6f"
187

19-
ovh_subsidiary = data.ovh_order_cart.mycart.ovh_subsidiary
8+
ovh_subsidiary = data.ovh_me.my_account.ovh_subsidiary
209
plan = [
2110
{
2211
duration = "P1M"
23-
plan_code = data.ovh_order_cart_product_plan.vps.plan_code
12+
plan_code = "vps-le-2-2-40"
2413
pricing_mode = "default"
2514

2615
configuration = [
@@ -30,7 +19,7 @@ resource "ovh_vps" "my_vps" {
3019
},
3120
{
3221
label = "vps_os"
33-
value = "Debian 10"
22+
value = "Debian 13"
3423
}
3524
]
3625
}

ovh/resource_vps.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func (r *vpsResource) Create(ctx context.Context, req resource.CreateRequest, re
9898
StringValue: basetypes.NewStringValue(serviceName),
9999
}
100100

101+
// Save early data into Terraform state, to make sure a reapply will not reorder a new VPS
102+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
103+
101104
// Update resource
102105
endpoint := "/vps/" + url.PathEscape(data.ServiceName.ValueString())
103106
if err := r.config.OVHClient.Put(endpoint, data.ToUpdate(), nil); err != nil {

ovh/resource_vps_gen.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/resources/vps.md.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Creates an OVHcloud Virtual Private Server (VPS).
1616

1717
~> **WARNING** `BANK_ACCOUNT` is not supported anymore, please update your default payment method to `SEPA_DIRECT_DEBIT`
1818

19-
~> **NOTE** During VPS creation, the OS must be configured through the `plan.configuration` argument, using `vps_os` as key, and values from [catalog](https://eu.api.ovh.com/console/?section=%2Forder&branch=v1#get-/order/catalog/public/vps) data). The `image_id` argument should be used only when reinstalling a server. The available values for `image_id` can be found using this [API call](https://eu.api.ovh.com/console/?section=%2Fvps&branch=v1#get-/vps/-serviceName-/images/available)
19+
~> **NOTE** During VPS creation, the OS must be configured through the `plan.configuration` argument, using `vps_os` as key, and values from [catalog](https://eu.api.ovh.com/console/?section=%2Forder&branch=v1#get-/order/catalog/public/vps) data). If you provide a `public_ssh_key` during VPS creation, you will have to provide `image_id` as well. The `image_id` argument should be used only when reinstalling a server. The available values for `image_id` can be found using this [API call](https://eu.api.ovh.com/console/?section=%2Fvps&branch=v1#get-/vps/-serviceName-/images/available)
2020

2121
## Example Usage
2222

@@ -27,7 +27,7 @@ Creates an OVHcloud Virtual Private Server (VPS).
2727
The following arguments are supported:
2828

2929
* `display_name` - Custom display name
30-
* `image_id` - (String) Id of the image to install on the VPS. This attribute is only useful to trigger a VPS reinstallation. The available values can be found using this [API call](https://eu.api.ovh.com/console/?section=%2Fvps&branch=v1#get-/vps/-serviceName-/images/available)
30+
* `image_id` - (String) Id of the image to install on the VPS. This attribute is useful to trigger a VPS reinstallation, and during VPS creation if you want to configure a `public_ssh_key`. The available values can be found using this [API call](https://eu.api.ovh.com/console/?section=%2Fvps&branch=v1#get-/vps/-serviceName-/images/available)
3131
* `netboot_mode` - VPS netboot mode (local┃rescue)
3232
* `ovh_subsidiary` - (Required) OVHcloud Subsidiary. Country of OVHcloud legal entity you'll be billed by. List of supported subsidiaries available on API at [/1.0/me.json](https://eu.api.ovh.com/console-preview/?section=%2Fme&branch=v1#get-/me)
3333
* `plan` - (Required) Product Plan to order

0 commit comments

Comments
 (0)