Associating a public IP for an Openstack-nova instance

891 views
Skip to first unread message

Nirmal Fernando

unread,
Nov 8, 2012, 11:36:06 PM11/8/12
to jcl...@googlegroups.com
Hi,

I want to do $subject.

I found following code does floating IP allocation.

novaClient.getFloatingIPExtensionForZone(region).get().allocate()

But does this means that this IP associated to the particular node that is just started? It seems not. In that case, how can I associate the IP?

--

Thanks & regards,
Nirmal

Software Engineer- Platform Technologies Team, WSO2 Inc.
Mobile: +94715779733
Blog: http://nirmalfdo.blogspot.com/

Adrian Cole

unread,
Nov 9, 2012, 1:30:54 AM11/9/12
to jcl...@googlegroups.com

You can set a property to enable auto up allocation.  Check out below:

https://github.com/jclouds/jclouds/blob/master/providers/hpcloud-compute/src/main/java/org/jclouds/hpcloud/compute/HPCloudComputeProviderMetadata.java

--
You received this message because you are subscribed to the Google Groups "jclouds" group.
To post to this group, send email to jcl...@googlegroups.com.
To unsubscribe from this group, send email to jclouds+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jclouds?hl=en.

Nirmal Fernando

unread,
Nov 9, 2012, 1:57:21 AM11/9/12
to jcl...@googlegroups.com
Hi Adrian,


On Fri, Nov 9, 2012 at 12:00 PM, Adrian Cole <adrian...@gmail.com> wrote:

You can set a property to enable auto up allocation.  Check out below:


I want to get rid of auto allocation of IPs.

https://github.com/jclouds/jclouds/blob/master/providers/hpcloud-compute/src/main/java/org/jclouds/hpcloud/compute/HPCloudComputeProviderMetadata.java

On Nov 9, 2012 1:36 AM, "Nirmal Fernando" <nir...@wso2.com> wrote:
Hi,

I want to do $subject.

I found following code does floating IP allocation.

novaClient.getFloatingIPExtensionForZone(region).get().allocate()

Is only this what I needed to do? or is there another API call to make this IP associated with the node?
 

But does this means that this IP associated to the particular node that is just started? It seems not. In that case, how can I associate the IP?

--

Thanks & regards,
Nirmal

Software Engineer- Platform Technologies Team, WSO2 Inc.
Mobile: +94715779733
Blog: http://nirmalfdo.blogspot.com/

--
You received this message because you are subscribed to the Google Groups "jclouds" group.
To post to this group, send email to jcl...@googlegroups.com.
To unsubscribe from this group, send email to jclouds+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jclouds?hl=en.

--
You received this message because you are subscribed to the Google Groups "jclouds" group.
To post to this group, send email to jcl...@googlegroups.com.
To unsubscribe from this group, send email to jclouds+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/jclouds?hl=en.

Everett Toews

unread,
Nov 9, 2012, 9:19:20 AM11/9/12
to jcl...@googlegroups.com
To disable auto allocation of IPs you need to override the property.

Properties overrides = new Properties();
overrides.setProperty(NovaProperties.AUTO_ALLOCATE_FLOATING_IPS, "false");

ComputeServiceContext context = ContextBuilder.newBuilder(provider)
  .credentials(identity, key)
  .overrides(overrides)
  .modules(modules)
  .buildView(ComputeServiceContext.class);
compute = context.getComputeService();
nova = context.unwrap();

To learn how to use the FloatingIPApi first have a look at the Javadoc [1] to see what you can do with the API.

Regards,
Everett

Nirmal Fernando

unread,
Nov 19, 2012, 1:23:41 AM11/19/12
to jcl...@googlegroups.com
I am using JClouds 1.5.0 and here's how I've done the IP association.

public String associateAddress(IaasProvider iaasInfo, NodeMetadata node) {

        ComputeServiceContext context = iaasInfo.getComputeService().getContext();
        @SuppressWarnings("unchecked")
        RestContext<NovaClient, NovaAsyncClient> restContext = context.unwrap(RestContext.class);
       
        NovaClient novaClient = restContext.getApi();
        String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo);
       
        FloatingIPClient floatingIp = novaClient.getFloatingIPExtensionForZone(region).get();
       
        String ip;
        try {
            ip = floatingIp.allocate().getIp();

        } catch (InsufficientResourcesException e) {
            ArrayList<FloatingIP> unassignedIps =
                                                  Lists.newArrayList(Iterables.filter(floatingIp.listFloatingIPs(),
                                                                                      new Predicate<FloatingIP>() {

                                                                                          @Override
                                                                                          public boolean apply(FloatingIP arg0) {
                                                                                              return arg0.getFixedIp() == null;
                                                                                          }

                                                                                      }));
            // try to prevent multiple parallel launches from choosing the same
            // ip.
            Collections.shuffle(unassignedIps);
            ip = Iterables.getLast(unassignedIps).getIp();
        }

        // wait till the fixed IP address gets assigned - this is needed before we associate a public IP
        do{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ignore) {}
        }
        while(node.getPrivateAddresses() == null);
       
        floatingIp.addFloatingIPToServer(ip, node.getProviderId());
       
        NodeMetadataBuilder.fromNodeMetadata(node).publicAddresses(ImmutableSet.of(ip)).build();
     
        log.info("Successfully associated an IP address "+ip+" for node with id: "+node.getId());
       
        return ip;
    }


===========
Note that above 'wait' is needed or else, you'll encounter following issue.

Caused by: org.jclouds.http.HttpResponseException: command: POST http://192.168.16.20:8774/v2/8cxxxxxxxxxxxx40a821exxxxxxxx/servers/01ec695c-547a-442d-aa23-xxxxxxxxxx/action HTTP/1.1 failed with response: HTTP/1.1 400 Bad Request; content: [{"badRequest": {"message": "No fixed ips associated to instance", "code": 400}}]
    at org.jclouds.openstack.nova.v2_0.handlers.NovaErrorHandler.handleError(NovaErrorHandler.java:48)
    at org.jclouds.http.handlers.DelegatingErrorHandler.handleError(DelegatingErrorHandler.java:69)
    at org.jclouds.http.internal.BaseHttpCommandExecutorService$HttpResponseCallable.shouldContinue(BaseHttpCommandExecutorService.java:197)
    at org.jclouds.http.internal.BaseHttpCommandExecutorService$HttpResponseCallable.call(BaseHttpCommandExecutorService.java:167)
    at org.jclouds.http.internal.BaseHttpCommandExecutorService$HttpResponseCallable.call(BaseHttpCommandExecutorService.java:135)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

One suggestion : this floating API's Java doc needs a revisit.. Its wording is bit confusing. (My personal thought, just for your attention)

Thanks everyone for all the tips!! :-)

Adrian Cole

unread,
Nov 24, 2012, 1:36:04 PM11/24/12
to jcl...@googlegroups.com
thanks for posting the solution!
Reply all
Reply to author
Forward
0 new messages