how to assign network to vm using jclouds in opens

2019-09-17 06:42发布

问题:

Can someone help me understanding how to assign a specific network to a newly created VM using the JClouds API in OpenStack? I'm using following code to create the real server:

CreateServerOptions[] serverOptions = new CreateServerOptions[0];

ServerCreated serverCreated = serverApi.create(clonedVmName, imageId, FlavourId, serverOptions);

Also I'm using v1.0-quantum APIs to fetch all the networks as follows but it's not returning any. I'm not sure what's wrong with the following code:

Iterable<Module> modules = ImmutableSet.<Module> of(
              new SLF4JLoggingModule());

      quantum = ContextBuilder.newBuilder(provider)
            .endpoint("http://10.37.53.229:5000/v2.0/")
            .credentials(identity, password)
            .modules(modules)
            .build();


      QuantumApi quanApi =  (QuantumApi)quantum.getApi();
      for(String zone : quanApi.getConfiguredZones())
      {
          System.out.println("Zone : "+ zone);
      }

      NetworkApi netApi = quanApi.getNetworkApiForZone("RegionOne");
      FluentIterable<? extends Network> nets = netApi.list();

      for(Network net : nets)
      {
          System.out.println(net.getName() +" Id : "+net.getId());
      }

回答1:

I use version 1.7.3 of JClouds and I build the options in the following way, giving network ids as parameters:

 CreateServerOptions options = CreateServerOptions.Builder
       .networks("00000000-0000-0000-0000-000000000000", "11111111-1111-1111-1111-111111111111")

As for listing networks here are good examples.