![](/img/trans.png)
[英]How can I save API user authentication tokens within IBM cloud functions using Node?
[英]How to add user metadata to IBM Cloud virtual device order?
我通過發布以下所示的JSON成功使用NodeJS(JavaScript)在IBM Cloud(ex SoftLayer)上訂購了新設備。 現在,我試圖修改訂單並將用戶元數據添加到每個訂購的設備,但是找不到輕松添加它的方法。
這是我的工作順序(在一個API調用中請求3個Ubuntu框):
var ibm_order = {
imageTemplateGlobalIdentifier: 'a1237539-c54d-46dd-bece-c17c2329c607',
imageTemplateId: '',
location: 138124,
packageId: 46,
presetId: '',
quantity: 3,
sourceVirtualGuestId: '',
useHourlyPricing: true,
complexType: 'SoftLayer_Container_Product_Order_Virtual_Guest',
prices:
[{id: 1641}, // description: 2 x 2.0 GHz Cores
{id: 1647}, // description: 8 GB
{id: 13899}, // description: 25 GB (LOCAL)
{id: 905}, // description: Reboot / Remote Console
{id: 274}, // description: 1 Gbps Public & Private Network Uplinks
{id: 1800}, // description: 0 GB Bandwidth
{id: 21}, // description: 1 IP Address
{id: 55}, // description: Host Ping
{id: 57}, // description: Email and Ticket
{id: 58}, // description: Automated Notification
{id: 420}, // description: Unlimited SSL VPN Users & 1 PPTP VPN User per account
{id: 418}], // description: Nessus Vulnerability Assessment & Reporting
sshKeys: [{sshKeyIds: [4512]}],
virtualGuests: [
{domain: 'mydomain.com', hostname: 'rw-ff-dal05-1a'},
{domain: 'mydomain.com', hostname: 'rw-ff-dal05-1b'},
{domain: 'mydomain.com', hostname: 'rw-ff-dal05-1c'}
]
}
此代碼使用softlayer-node npm發布以上命令:
var SoftLayer = require('softlayer-node');
var softlayer_client = new SoftLayer();
// actual SL API call
softlayer_client
.auth(softlayer_username, softlayer_api_key)
.path('Product_Order', 'placeOrder')
.parameters(ibm_order)
.post()
.then(function(result:any) {
console.log('success! result: ' + JSON.stringify(result, null, 4) );
}, function(error:any) {
console.log('error: details: ' + JSON.stringify(error, null, 4) );
});
問題:如何將用戶元數據添加到每個請求的設備? 用戶元數據是一個字符串數組,大概應該是virtualGuests數組元素的一部分(每個“虛擬來賓”都有其自己的單個user_metadata數組),但是當我嘗試指定“ UserMetadata”時,出現錯誤:
error: details: {
"message": {
"error": "The property 'UserMetadata' is not valid for 'SoftLayer_Virtual_Guest'.",
"code": "SoftLayer_Exception_Public"
},
"statusCode": 500
}
我找到了這些SoftLayer API參考頁:
1)參考»數據類型» SoftLayer_Container_Product_Order_Virtual_Guest
2)參考»服務» SoftLayer_Virtual_Guest
但是它們並不是100%相關的(他們建議使用setUserMetadata方法,但我所擁有的只是一個低級的“預烘焙” JSON!)
還是有其他更優雅的方法可以從NodeJS訂購IBM Cloud(ex SoftLayer)的新設備?
沒錯,但是錯誤是由於發送的屬性“ UserMetaData”造成的,這不是virtualGuests的有效屬性,您要查找的特定屬性稱為“ userData”,而是在數據類型Virtual_Guest中 。
請嘗試以下操作:
var ibm_order = {
imageTemplateGlobalIdentifier: 'a1237539-c54d-46dd-bece-c17c2329c607',
imageTemplateId: '',
location: 138124,
packageId: 46,
presetId: '',
quantity: 3,
sourceVirtualGuestId: '',
useHourlyPricing: true,
complexType: 'SoftLayer_Container_Product_Order_Virtual_Guest',
prices:
[{id: 1641}, // description: 2 x 2.0 GHz Cores
{id: 1647}, // description: 8 GB
{id: 13899}, // description: 25 GB (LOCAL)
{id: 905}, // description: Reboot / Remote Console
{id: 274}, // description: 1 Gbps Public & Private Network Uplinks
{id: 1800}, // description: 0 GB Bandwidth
{id: 21}, // description: 1 IP Address
{id: 55}, // description: Host Ping
{id: 57}, // description: Email and Ticket
{id: 58}, // description: Automated Notification
{id: 420}, // description: Unlimited SSL VPN Users & 1 PPTP VPN User per account
{id: 418}], // description: Nessus Vulnerability Assessment & Reporting
sshKeys: [{sshKeyIds: [4512]}],
virtualGuests: [
{domain: 'mydomain.com', hostname: 'rw-ff-dal05-1a', userData: [{value: 'someValue1'}]},
{domain: 'mydomain.com', hostname: 'rw-ff-dal05-1b', userData: [{value: 'someValue2'}]},
{domain: 'mydomain.com', hostname: 'rw-ff-dal05-1c', userData: [{value: 'someValue3'}]}
]
}
有關更多信息,您可能會在下面看到:
http://sldn.softlayer.com/blog/jarteche/getting-started-user-data-and-post-provisioning-scripts
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.