0

我想使用 Softlayer java API 订购每小时一次的 Bare Metal。我从https://gist.github.com/bmpotter/fe2de7f8028d73ada4e5中获得了这个想法。这是我的步骤:

    Hardware hardware = new Hardware();
    Order orderTemplate = new Order();

    // 1. Set hostname, domain to hardware
    // 2. set Preset 
    Preset preset = new Preset();
    preset.setKeyName("S1270_8GB_2X1TBSATA_NORAID");
    hardware.setFixedConfigurationPreset(preset);
    // 3. Component setMaxSpeed, and added to hardware
    hardware.setPrimaryNetworkComponent()
    // 4. "UBUNTU_14_64"
    hardware.setOperatingSystemReferenceCode()

    // 1. Added Quantity to orderTemplate
    // 2. Added location to orderTemplate
    // 3. Added Hardware to orderTemplate
    // 4. Added Container, since I am see the exception
    orderTemplate.setContainerIdentifier("SoftLayer_Product_Package_Preset");

    Finally tried to verify the Order.

我不断收到一般错误消息:

指定的容器无效:SoftLayer_Container_Product_Order。订购服务器或服务需要特定的容器类型,而不是通用的基本订单容器。

我究竟做错了什么?我是否需要发送priceIds,类似于非每小时 Bare Metal Order?是否有故障排除指南来了解我的订单中缺少什么?

Pedro David Fuentes 你能帮忙吗?在弄清楚价格后,我尝试了这个:

https://[用户名]:[apikey]@api.softlayer.com/rest/v3/SoftLayer_Product_Order/verifyOrder

{
   "parameters": [
   {
     "complexType": "SoftLayer_Container_Product_Order_Hardware_Server",
     "quantity": 1,
     "location": "DALLAS",
     "packageId": 200,
     "useHourlyPricing": 1,
     "presetId": 66,
     "prices": [
     {
        "id": 37318
     }, 
     {
        "id": 34183
     }, 
     {
        "id": 26737
     }, 
     {
        "id": 34807
     }, 
     {
        "id": 25014
     }
  ],
  "hardware": [
    {
      "hostname": "myhostname",
      "domain": "mydomain.com"
    }
   ]
  }
 ]
}
{ 
    "error": "Unable to add a Graphics Processing Unit price (178119) because it is not valid for the package (200).", 
    "code": "SoftLayer_Exception_Public" 
}

也可以通过 JAVA 代码重现,因此也可以通过 REST 进行尝试。

带有额外日志记录的修改代码:

String username = "xxxxx";
String apiKey = "xxxxx";

Location datacenter = new Location();
datacenter.setName("seo01");

Preset preset = new Preset();
preset.setKeyName("S1270_8GB_2X1TBSATA_NORAID");

Component networkComponent = new Component();
networkComponent.setMaxSpeed(100L);

Hardware hardware = new Hardware();
hardware.setDatacenter(datacenter);
hardware.setHostname("xxxxx_xxxxx_BM_HOURLY");
hardware.setDomain("xxxx.xxx");
hardware.setHourlyBillingFlag(true);
hardware.setFixedConfigurationPreset(preset);
List<Component> networkComponents = hardware.getNetworkComponents();
networkComponents.add(networkComponent);
hardware.setOperatingSystemReferenceCode("CENTOS_LATEST");

ApiClient client = new RestApiClient().withCredentials(username, apiKey).withLoggingEnabled();
Hardware.Service hardwareService = Hardware.service(client); 
try
{  
  Gson gson = new Gson();
  Hardware hardwarePlaced = hardwareService.createObject(hardware);
  System.out.println("createObject: " + gson.toJson(hardwarePlaced));
}
catch(Exception e)
{
    System.out.println("Error: " + e);  
}

我收到一个错误:在带有正文的链接上运行 POST:{"parameters":[{"complexType":"SoftLayer_Hardware","hostname":"xxxxx_xxxxx_BM_HOURLY","domain":"xxxx.xxx","fixedConfigurationPreset":{ "complexType":"SoftLayer_Product_Package_Preset","keyName":"S1270_8GB_2X1TBSATA_NORAID"},"datacenter":{"complexType":"SoftLayer_Location","name":"seo01"},"hourlyBillingFlag":true,"networkComponents":[ {"complexType":"SoftLayer_Network_Component","maxSpeed":100}],"operatingSystemReferenceCode":"CENTOS_LATEST"}]} 在与正文的链接上得到 500:{"error":"无法添加图形处理单价 (178119),因为它对包 (200) 无效。","code":"SoftLayer_Exception_Public"} 错误:com.softlayer.api.ApiException$Internal:无法添加图形处理单价 (178119),因为它对包 (200) 无效。(代码:SoftLayer_Exception_Public,状态:500)

4

2 回答 2

0

这份关于订购 BM 服务器的文档可以帮助您http://sldn.softlayer.com/blog/bpotter/ordering-bare-metal-servers-using-softlayer-api

这是订购预设的示例:

import java.util.List;
import com.softlayer.api.*;
import com.softlayer.api.service.Hardware;
import com.softlayer.api.service.Location;
import com.softlayer.api.service.network.Component;
import com.softlayer.api.service.product.Order;
import com.softlayer.api.service.product.pkg.Preset;
import com.google.gson.Gson;

public class OrderPreSetBMS2
{
  public static void main( String[] args )
  {
    String user = "set me";
    String apiKey = "set me";

    Location datacenter = new Location();
    datacenter.setName("seo01");

    Preset preset = new Preset();
    preset.setKeyName("S1270_8GB_2X1TBSATA_NORAID");

    Component networkComponent = new Component();
    networkComponent.setMaxSpeed(100L);


    Hardware hardware = new Hardware();
    hardware.setDatacenter(datacenter);
    hardware.setHostname("simplebmi");
    hardware.setDomain("test.com");
    hardware.setHourlyBillingFlag(true);
    hardware.setFixedConfigurationPreset(preset);
    List<Component> networkComponents = hardware.getNetworkComponents();
    networkComponents.add(networkComponent);
    hardware.setOperatingSystemReferenceCode("UBUNTU_14_64");

    ApiClient client = new RestApiClient().withCredentials(user, apiKey);
    Hardware.Service hardwareService = Hardware.service(client); 
    Order.Service orderService = Order.service(client); 

    try
    {
      //Change generateOrderTemplate method by createObject when you are ready to order the server. 
      com.softlayer.api.service.container.product.Order productOrder = hardwareService.generateOrderTemplate(hardware);
      Gson gson = new Gson();
      System.out.println(gson.toJson(productOrder));
    }
    catch(Exception e)
    {
        System.out.println("Error: " + e);  
    }
  }
}
于 2016-09-13T16:21:48.807 回答
0

你可以试试这个脚本。

package SoftLayer_Java_Scripts.Examples;
import java.util.ArrayList;
import java.util.List;
import com.softlayer.api.*;
import com.softlayer.api.service.container.product.order.hardware.Server;
import com.softlayer.api.service.Hardware;
import com.softlayer.api.service.product.Order;
import com.softlayer.api.service.product.item.Price;
import com.google.gson.Gson;

/**
* Order a new server with preset configuration.
* 
 * The presets used to simplify ordering by eliminating the need
* for price ids when submitting orders.
* Also when the order contains a preset id, it is not possible
* to configure VLANs in the order.
* 
 * Important manual pages:
* http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder
* http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order_Network_Message_Queue
* http://sldn.softlayer.com/reference/datatypes/SoftLayer_Virtual_Guest
* 
 * @license <http://sldn.softlayer.com/article/License>
* @author SoftLayer Technologies, Inc. <sldn@softlayer.com>
* @version 1.0
*/
public class OrderPreSetBMS
{
  public static void main( String[] args )
  {
    // Your SoftLayer API username and key.
       String user = "set me";
       String apiKey = "set me";

       long quantity = 1;
       String location = "AMSTERDAM";
       long packageId = 200;
       long presetId = 64;
       String hostname = "test";
       String domain = "example.org";

       // Building a skeleton SoftLayer_Hardware_Server object to model the hostname and
       // domain we want for our server. If you set quantity greater then 1 then you
       // need to define one hostname/domain pair per server you wish to order.
       Hardware hardware = new Hardware();
       hardware.setHostname(hostname);
       hardware.setDomain(domain);

    // Building a skeleton SoftLayer_Product_Item_Price objects. These objects contain
    // much more than ids, but SoftLayer's ordering system only needs the price's id
    // to know what you want to order.
    // Every item in SoftLayer's product catalog is assigned an id. Use these ids
    // to tell the SoftLayer API which options you want in your new server. Use
    // the getActivePackages() method in the SoftLayer_Account API service to get
       // a list of available item and price options per available package.
       // Note: The presets already have some preconfigured items, such as
       // the server or the disks you do not need to configure the prices for those
       // items.
       //
       // Id: 44988 -> CentOS 7.x (64 bit)
    // Id: 1800  -> 0 GB Bandwidth
    // Id: 273   -> 100 Mbps Public & Private Network Uplinks
    // Id: 420   -> Unlimited SSL VPN Users & 1 PPTP VPN User per account
    // Id: 21    -> 1 IP Address
    // Id: 906   -> Reboot / KVM over IP        
    long[] priceIds = {44988, 1800, 273, 420, 21, 906};
    List<Price> prices = new ArrayList<Price>();
    for (int i = 0; i < priceIds.length; i++) {
      Price p = new Price();
      p.setId(priceIds[i]);
      prices.add(p);
    }

    // Building a skeleton SoftLayer_Container_Product_Order_Hardware_Server object
    // containing the order you wish to place.
       Server server = new Server();
       server.setQuantity(quantity);
       server.setLocation(location);
       server.setPackageId(packageId);

    List<Price> priceList = server.getPrices();
    priceList.addAll(prices);

       List<Hardware> hardwareList = server.getHardware();
       hardwareList.add(hardware);
       server.setPresetId(presetId);

       // Creating a SoftLayer API client and service objects. 
       ApiClient client = new RestApiClient().withCredentials(user, apiKey);
       Order.Service service = Order.service(client);

       try
       {
      // verifyOrder() will check your order for errors. Replace this with a call
      // to placeOrder() when you're ready to order. Both calls return a receipt
       // object that you can use for your records.
       // Once your order is placed it'll go through SoftLayer's approval and
       // provisioning process. When it's done you'll have a new
       // SoftLayer_Hardware_Server object and server ready to use.
       com.softlayer.api.service.container.product.Order verifiedOrder = service.verifyOrder(server);
         Gson gson = new Gson();
         System.out.println(gson.toJson(verifiedOrder));
       }
       catch(Exception e)
       {
              System.out.println("Error: " + e); 
       }
  }
}

下一个 REST 请求检索包裹 ID 的产品项目价格。重要的是要注意这些价格的组织方式以及它们包含的信息。例如,这些产品项目价格可能具有相同的项目 ID,这是因为价格是按位置分隔的。

 https://$username:$apiKey@api.softlayer.com/rest/v3/SoftLayer_Product_Package/200/getItemPrices.json?objectMask=mask[id,pricingLocationGroup[locations[name]],item[id,description],categories[categoryCode]]

下一个请求使用对象过滤器和主机 Ping 和 TCP 服务监控作为搜索条件。通过这个例子,人们可以意识到 Product Item Price 可以具有相同的 Item Id,这意味着它们是相同的,但应该根据正在验证订单的特定位置使用。

 https://$username:$apiKey@api.softlayer.com/rest/v3/SoftLayer_Product_Package/200/getItemPrices.json?objectMask=mask[id,pricingLocationGroup[locations[name]],item[id,description],categories[categoryCode]]&objectFilter={"itemPrices":{"item":{"description":{"operation":"Host Ping and TCP Service Monitoring"}}}} 

进一步阅读:

https://sldn.softlayer.com/article/object-masks

https://sldn.softlayer.com/article/object-filters

于 2016-09-07T19:47:11.167 回答