0
votes

I want to create an instance in openstack with a pre-defined network interface attached only. I have access to openstack, I know the network interface id/name.

Post creation of an instance I can simply attach the interface. This way it will get a randomly assigned IP from the pool and afterwards get the network interface attached. That's not what I want.

As stated in the beginning I want to attach the interface while I build the instance.

Edit - Example code:

Host Creation:

resource "openstack_compute_instance_v2" "example_host" {
  count =  1
  name = example-host
  image_name = var.centos_7_name
  flavor_id = "2"
  key_pair = "key"
}

Interface attaching:

resource "openstack_compute_interface_attach_v2" "example_interface_attach" {
  instance_id = openstack_compute_instance_v2.example_host[0].id
  port_id = "bd858b4c-d6de-4739-b125-314f1e7041ed"
} 

This won't work. Terraform returns an error:

Error: Error creating OpenStack server: Expected HTTP response code [] when accessing [POST servers], but got 409 instead {"conflictingRequest": {"message": "Multiple possible networks found, use a Network ID to be more specific.", "code": 409}}

Back to my initial query: I want to deploy a new host and attach a network interface. The result should be a host with only one IP-Address, the one I've attached to it.

2
And what is the problem? Do you have any TF code to show?Marcin
Sure, I'll be providing some example code, but issue remains the same: I want to deploy a new host and attach an interface. The goal in the end is for the host to only have the IP-Address associated with the attached interface and no other ip.Roman

2 Answers

0
votes

The error seems to be generated by the instance launch. OpenStack (not Terraform) insists on a network if more than one network is available. From an OpenStack perspective, you have several solutions. Off the cuff, I see three:

Since microversion 2.37, the Nova API allows you to specify "none" as a network, in which case the instance runs, but is not connected after the launch.

Or launch the instance on a port instead of a network, after putting an IP address on the port. Using the openstack client:

openstack port create --network <network> --fixed-ip subnet=<subnet>,ip-address=<ip-address> 
openstack server create ... --port <port-ip> ...

I consider that the best solution.

Another solution would be specifying a network and a fixed IP address while launching the instance. CLI:

openstack server create ... --nic NET-UUID,v4-fixed-ip=172.16.7.8 ...

Unfortunately, I can't tell whether Terraform supports any of these solutions. I would try adding the port_id to the resource "openstack_compute_instance_v2" "example_host" block.

0
votes

I've found the solution and it's incredibly simple. You can plainly add the port id to the network block. I've tried it previously already but it failed. Chances are I've provided the wrong ID.

##Create hosts
resource "openstack_compute_instance_v2" "test_host" {
  count =  1
  name = format("test-host-%02d", count.index + 1)
  image_name = var.centos_7_name
  flavor_id = "2"
  key_pair = "key"

  network {
    port = "<port-id>"
  }
 }

Here's an additional solution removing the potential of providing a wrong id.

##Create hosts
resource "openstack_compute_instance_v2" "test_host" {
  count =  1
  name = format("test-host-%02d", count.index + 1)
  image_name = var.centos_7_name
  flavor_id = "2"
  key_pair = "key"

  network {
    port = data.openstack_networking_port_v2.port_1.id
  }
 }

data openstack_networking_port_v2 "port_1" {
  name = "switch-port-208.37"
}