civo/terraform-provider-civo

Check if provided network ID exists in region before creating volume

zulh-civo opened this issue · 0 comments

When the provider has no region, it will use NYC1 region as default (I believe this is being done by Civo API server and not by civogo).

When user did a data query for network, it will return a network from NYC1 (note: network data source can't accept both label and region at this moment).

Now, when user wants to create a civo_volume in LON1 region, the passed down network id is incorrect/not exists in LON1. The id is network ID in NYC1 region.

Hence, the volume creation failed (see traces below). What we should do here is, we should check if the provided network ID exists in region before creating volume.

Also, we need to allow user to query network data source by label and region and not just limited to label. I'll create a separate issue for this one.


Case 1

provider "civo" {
    token = "api-token"
}

data "civo_network" "default_network" {
    label = "Default"
}

resource "civo_volume" "db" {
    name = "backup-data"
    size_gb = 5
    network_id = data.civo_network.default_network.id
    depends_on = [
      data.civo_network.default_network
    ]
}

Result:

backup-data volume created successfully in NYC1 region (I guess Civo API uses NYC1 as default region)


Case 2

provider "civo" {
    token = "api-token"
    region = "LON1"
}

data "civo_network" "default_network" {
    label = "Default"
}

resource "civo_volume" "db" {
    name = "backup-data"
    size_gb = 5
    network_id = data.civo_network.default_network.id
    depends_on = [
      data.civo_network.default_network
    ]
}

Result:

backup-data volume created successfully in LON1 region


Case 3

provider "civo" {
    token = "api-token"
}

data "civo_network" "default_network" {
    label = "Default"
}

resource "civo_volume" "db" {
    name = "backup-data"
    size_gb = 5
    network_id = data.civo_network.default_network.id
    depends_on = [
      data.civo_network.default_network
    ]
    region = "LON1"
}

Result:

The volume didn't created. The following error occured:

tf apply --auto-approve
civo_volume.db: Refreshing state... [id=f71a6e92-8372-4d26-91d3-d756168ce917]

Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the last "terraform apply":

  # civo_volume.db has been deleted
  - resource "civo_volume" "db" {
      - id         = "f71a6e92-8372-4d26-91d3-d756168ce917" -> null
      - name       = "backup-data" -> null
      - network_id = "5c16ab17-933a-46ed-96c6-8a093a0179e1" -> null
      - size_gb    = 5 -> null
    }

Unless you have made equivalent changes to your configuration, or ignored the relevant attributes using ignore_changes, the following plan may include actions to undo or respond to these changes.

─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # civo_volume.db will be created
  + resource "civo_volume" "db" {
      + id          = (known after apply)
      + mount_point = (known after apply)
      + name        = "backup-data"
      + network_id  = "fb3cdfff-66ba-4048-9f37-8625237cbe67"
      + region      = "LON1"
      + size_gb     = 5
    }

Plan: 1 to add, 0 to change, 0 to destroy.
civo_volume.db: Creating...
╷
│ Error: [ERR] failed to create a new volume: Error: Unknown error response - status: 400 Bad Request, code: 400, reason: {"code":"parameter_network_id_invalid","result":"failed","reason":"The network_id supplied was empty"}
│
│   with civo_volume.db,
│   on main.tf line 7, in resource "civo_volume" "db":
│    7: resource "civo_volume" "db" {
│
╵