如何配置ipv4和amp; ipv6地址

时间:2017-10-27 12:50:46

标签: ibm-cloud-infrastructure

如何通过softlayer sdk java配置ipv4和ipv6地址?

我执行下面的示例代码来配置两个ip版本,但它只配置了ipv4。你能否在提交createObject请求时帮我配置ipv6地址。

softlayer sdk java版本:0.2.3

    Image image = new Image();
    image.setCapacity(50L);

    Guest guest = new Guest();
    guest.setHostname("malya-vm-4");
    guest.setDomain("test.com");
    guest.setStartCpus(1l);
    guest.setMaxMemory(1024l);
    guest.setDatacenter(new Location());
    guest.getDatacenter().setName("che01");

    guest.setLocalDiskFlag(false);
    guest.setHourlyBillingFlag(true);

    guest.setOperatingSystemReferenceCode("UBUNTU_LATEST");

    Component privateComponent = new Component();
    Component publicComponent = new Component();

    Vlan networkVlan1 = new Vlan();
    networkVlan1.setVlanNumber(1365L);
    networkVlan1.setId(2117919L);

    Subnet privateSubnet = new Subnet();
    privateSubnet.setId(1059121L);

    privateComponent.setNetworkVlan(networkVlan1);
    privateComponent.setPrimarySubnet(privateSubnet);
    guest.setPrimaryBackendNetworkComponent(privateComponent);

    Vlan networkVlan2 = new Vlan();
    networkVlan2.setId(2117917L);
    networkVlan2.setVlanNumber(1290L);

    Subnet primarySubnetVersion4 = new Subnet();
    primarySubnetVersion4.setVersion(4L);
    primarySubnetVersion4.setId(1547547L);

    Subnet ipv6 = Subnet.service(client, 1512739L).getObject();
    Subnet primarySubnetVersion6 = new Subnet();
    primarySubnetVersion6.setVersion(6L);
    primarySubnetVersion6.setId(1512739L);
    primarySubnetVersion6.setSubnetType("PRIMARY_6");

     networkVlan2.setPrimarySubnet(primarySubnetVersion4);
    networkVlan2.setPrimarySubnetVersion6(ipv6);
    networkVlan2.setPrimarySubnetCount(2L);

    publicComponent.setNetworkVlan(networkVlan2);
    guest.setPrimaryNetworkComponent(publicComponent);
    Guest.service(client).createObject(guest);

1 个答案:

答案 0 :(得分:1)

我担心您无法使用两个ip版本配置VSI,createObject方法是订购VSI的简化替代方法,这意味着无法使用此方法设置某些其他配置。实际上,如果执行方法SoftLayer_Virtual_Guest::getCreateObjectOptions,您将无法看到ipv6的任何选项。

如果您想使用ip地址v6订购VSI,我建议使用placeOrder方法。以下是一个示例,请不要忘记使用您自己的数据更改值,例如用户名,apiKey,位置和价格。

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.softlayer.api.ApiClient;
import com.softlayer.api.RestApiClient;
import com.softlayer.api.service.container.product.order.Receipt;
import com.softlayer.api.service.network.Vlan;
import com.softlayer.api.service.product.Order;
import com.softlayer.api.service.product.item.Price;
import com.softlayer.api.service.virtual.Guest;
import com.softlayer.api.service.virtual.guest.network.Component;
import java.util.ArrayList;
import java.util.List;

public class orderVSI {

    public orderVSI(){
        // Declare username and api key
        String username = "set me";
        String apiKey = "set me";

        // Declare required variables to place an order
        long quantity = 1;
        String location = "MEXICO";
        long packageId = 46;

        // Declare Vlans you want to use
        Vlan publicVlan = new Vlan();
        publicVlan.setId(1133383L);

        Vlan privateVlan = new Vlan();
        privateVlan.setId(1133381L);

        // Vlans need to be into network components
        Component primaryNetworkComponent = new Component();
        primaryNetworkComponent.setNetworkVlan(publicVlan);

        Component backendNetworkComponent = new Component();
        backendNetworkComponent.setNetworkVlan(privateVlan);

        // Declare hostname, domain and network components of guests you want to order, add more guest objects if
        // quantity is greater than 1
        List<Guest> virtualGuests = new ArrayList<Guest>();
        Guest guest1 = new Guest();
        guest1.setHostname("hostname-test");
        guest1.setDomain("sl-domain.com");

        guest1.setPrimaryNetworkComponent(primaryNetworkComponent);
        guest1.setPrimaryBackendNetworkComponent(backendNetworkComponent);

        virtualGuests.add(guest1);

        // Declare the list of prices. To get list of all available prices you can use the method
        // SoftLayer_Product_Package::getItemPrices
        long[] pricesIds = {
                52283,    // 1 x 2.0 GHz Cores
                51209,    // 1 GB RAM
                45466,    // CentOS 7.x - Minimal Install (64 bit)
                2202,     // "25 GB (SAN) as First disk
                1800,     // 0 GB Bandwidth
                17129,    // Primary IPv6 Addresses
                273,      // 100 Mbps Public & Private Network Uplinks
                55,       // Monitoring - Host Ping
                57,       // Email and Ticket
                58,       // Automated Notification
                21,       // Primary IP Addresses
                418,      // Nessus Vulnerability Assessment & Reportin
                420,      // VPN Management - Private Network
                905       // Remote Management
        };

        // Building the skeleton of SoftLayer_Product_Item_Price objects
        List<Price> prices = new ArrayList<Price>();
        for (int i = 0; i < pricesIds.length; i++) {
            Price p = new Price();
            p.setId(pricesIds[i]);
            prices.add(p);
        }

        // Build a skeleton SoftLayer_Container_Product_Order object template that will be used to order VSIs.
        com.softlayer.api.service.container.product.Order orderTemplate = new com.softlayer.api.service.container.product.Order();
        orderTemplate.setLocation(location);
        orderTemplate.setPackageId(packageId);
        orderTemplate.setQuantity(quantity);
        orderTemplate.getVirtualGuests().addAll(virtualGuests);
        orderTemplate.getPrices().addAll(prices);

        // Get Api Client and service
        ApiClient client = new RestApiClient().withCredentials(username, apiKey);
        Order.Service orderService = Order.service(client);

        try{
            // verifyOrder() will check your order for errors. Replace this with placeOrder() when
            // you're ready to order.
            Receipt receipt = orderService.placeOrder(orderTemplate, Boolean.FALSE);
            // com.softlayer.api.service.container.product.Order receipt = orderService.verifyOrder(orderTemplate);

            // Print response in JSON format
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            System.out.println(gson.toJson(receipt));

        } catch (Exception e)
        {
            System.out.println("Error: " + e);
        }
    }
    public static void main(String [] args)
    {
        new orderVSI();
    }
}