如何使用 api's 在 SoftLayer 上订购和删除 iSCSI 块存储?

How to order and delete a iscsi block storage on SoftLayer using the api's?

我正在尝试为 iSCSI 卷(块存储)创建 Endurance 存储。我正在使用 SoftLayer api。创建删除存储的过程是什么?

要创建块存储 - Endurance,您可以尝试以下脚本:

// Order Block Storage - Endurance
// This script creates an order for Block Storage - Endurance
//
// See below references for more details.
// important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order
// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder/
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>

package main

import (
    "fmt"
    "github.com/softlayer/softlayer-go/datatypes"
    "github.com/softlayer/softlayer-go/services"
    "github.com/softlayer/softlayer-go/session"
    "github.com/softlayer/softlayer-go/sl"
    "encoding/json"
)

func main() {
  username    := "set me"
  apikey      := "set me"

  complexType := "SoftLayer_Container_Product_Order_Network_Storage_Enterprise"
  quantity    := 1
  location    := "265592"
  packageId   := 240
  osKeyName   := "LINUX"
  quote       := false

  // 1. Create a session
  sess := session.New(username, apikey)

  // 2. Get a service
  service := services.GetProductOrderService(sess)

  osFormatType := &datatypes.Network_Storage_Iscsi_OS_Type{
    KeyName: sl.String(osKeyName),    
  }

  prices := []datatypes.Product_Item_Price{
    datatypes.Product_Item_Price{ Id: sl.Int(45098) },  // Block Storage
    datatypes.Product_Item_Price{ Id: sl.Int(45058) },  // Endurance Storage
    datatypes.Product_Item_Price{ Id: sl.Int(45148) },  // 40 GB Storage Space
    datatypes.Product_Item_Price{ Id: sl.Int(45068) },  // 0.25 IOPS per GB
  }

  cpo := datatypes.Container_Product_Order {
    ComplexType   : sl.String(complexType),
    Quantity      : sl.Int(quantity),
    Location      : sl.String(location),
    PackageId     : sl.Int(packageId),         
    Prices        : prices,
  }

  cponp := datatypes.Container_Product_Order_Network_Storage_Enterprise {
    Container_Product_Order : cpo,
    OsFormatType            : osFormatType,
  }


  // 4. Invoke a method
    containerProductOrder, err := service.PlaceOrder(&cponp,&quote)
    if err != nil {
        fmt.Printf("%s\n", err)
        return
    } 

  cpoMarshed, errMarsh := json.Marshal(containerProductOrder)
  if errMarsh != nil {
    fmt.Println(errMarsh)
    return
  }

  fmt.Println(string(cpoMarshed))
}

如果您需要价格方面的帮助,此方法可以提供帮助:

价格的重要参考:

取消存储

// Cancel Storage
//
// This script cancels the storage, passing a storage billing ID to
// SoftLayer_Billing_Item::cancelService method.
//
// See below references for more details.
// Important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Billing_Item/cancelService
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>

package main

import (
    "fmt"
    "github.com/softlayer/softlayer-go/services"
    "github.com/softlayer/softlayer-go/session"
)

func main() {
  username    := "set me"
  apikey      := "set me"

  storageId := 19771755

  sess := session.New(username, apikey)

  networkService := services.GetNetworkStorageService(sess)
  billingService := services.GetBillingItemService(sess)

  mask := "billingItem" 

  network, err := networkService.Id(storageId).Mask(mask).GetObject()
    if err != nil {
        fmt.Printf("%s\n", err)
        return
    } 

  result, err := billingService.Id(*network.BillingItem.Id).CancelService()
    if err != nil {
        fmt.Printf("%s\n", err)
        return
    }

  fmt.Println(result)
}

参考文献:


通过storage的orderId获取storage lun名称和目标地址

// Get Storage through orderId
//
// See below references for more details.
// Important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNetworkStorage
//
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>

package main

import (
    "fmt"
    "github.com/softlayer/softlayer-go/services"
    "github.com/softlayer/softlayer-go/session"
    "encoding/json"
)

func main() {
  username    := "set me"
  apikey      := "set me"
  orderId     := "6199311"

  sess := session.New(username, apikey)

  accountService := services.GetAccountService(sess)

  filter := `{"networkStorage": {"billingItem": {"orderItem": {"order": {"id": {"operation": `+orderId+`}}}}}}`
  mask   := "username;serviceResourceBackendIpAddress"

  result, err := accountService.Mask(mask).Filter(filter).GetNetworkStorage()
    if err != nil {
        fmt.Printf("%s\n", err)
        return
    } 

  storage, errMarsh := json.Marshal(result)
  if errMarsh != nil {
    fmt.Println(errMarsh)
    return
  }

  fmt.Println(string(storage))
}


    // Get Storage through orderId
    //
    // See below references for more details.
    // Important manual pages:
    // http://sldn.softlayer.com/reference/services/SoftLayer_Account/getNetworkStorage
    //
    // @License: http://sldn.softlayer.com/article/License
    // @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>

    package main

    import (
        "fmt"
        "github.com/softlayer/softlayer-go/services"
        "github.com/softlayer/softlayer-go/session"
        "encoding/json"
    )

    func main() {
      username    := "set me"
      apikey      := "set me"
      orderId     := "6199311"

      sess := session.New(username, apikey)

      accountService := services.GetAccountService(sess)

      filter := `{"networkStorage": {"billingItem": {"orderItem": {"order": {"id": {"operation": `+orderId+`}}}}}}`
      mask   := "username;serviceResourceBackendIpAddress"

      result, err := accountService.Mask(mask).Filter(filter).GetNetworkStorage()
        if err != nil {
            fmt.Printf("%s\n", err)
            return
        } 

      storage, errMarsh := json.Marshal(result)
      if errMarsh != nil {
        fmt.Println(errMarsh)
        return
      }

      fmt.Println(string(storage))
    }

将:用户名、apikey和6199311替换为自己的信息

参考文献:


获取标准价格(位置组 = null)

// Retrieve product item prices which location group is null
//
// See below references for more details.
// important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getItemPrices
//
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>

package main

import (
    "fmt"
    "github.com/softlayer/softlayer-go/services"
    "github.com/softlayer/softlayer-go/session"
    "encoding/json"
)

func main() {
  username    := "set me"
  apikey      := "set me"

  packageId   := 240

  // 1. Create a session
  sess := session.New(username, apikey)

  // 2. Get a service
  packageService := services.GetProductPackageService(sess)

  // Declare a filter to get standard prices
  filter := `{"itemPrices":{"locationGroupId":{"operation": "is null"}}}`

  packageResult, err := packageService.Id(packageId).Filter(filter).GetItemPrices()
  if err != nil {
    fmt.Print("%s\n", err)
    return
  }

  prices, errMarsh := json.Marshal(packageResult)
  if errMarsh != nil {
    fmt.Println(errMarsh)
    return
  }

  fmt.Println(string(prices))
}

检索性能存储的产品项目价格(标准)space

// Retrieve product item prices for performance storage space
//
// See below references for more details.
// important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getItemPrices
//
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>

package main

import (
    "fmt"
    "github.com/softlayer/softlayer-go/services"
    "github.com/softlayer/softlayer-go/session"
    "encoding/json"
)

func main() {
  username    := "set me"
  apikey      := "set me"

  packageId   := 240

  // 1. Create a session
  sess := session.New(username, apikey)

  // 2. Get a service
  packageService := services.GetProductPackageService(sess)

  // Declare a filter to get standard prices
  filter := `{"itemPrices":{"item":{"capacity":{"operation":40}}, "attributes":{"value":{"operation":300}}, "locationGroupId":{"operation": "is null"}, "categories":{"categoryCode":{"operation":"performance_storage_space"}}}}`

  packageResult, err := packageService.Id(packageId).Filter(filter).GetItemPrices()
  if err != nil {
    fmt.Print("%s\n", err)
    return
  }

  prices, errMarsh := json.Marshal(packageResult)
  if errMarsh != nil {
    fmt.Println(errMarsh)
    return
  }

  fmt.Println(string(prices))
}

SoftLayer_Account/getObject 不使用 softlayer-go

也许这样的事情可以帮助你:

我试过了,我创建了以下脚本来从 SoftLayer_Account::getObject 响应中获取部分数据:

package main

import (
    "encoding/json"
    "fmt"
    "log"
    "net/http"
)

type Account struct {
    Id int `json:"id"`
    Address1 string `json:"address1"`
    FirstName string `json:"firstName"`
}

func main() {

    url := fmt.Sprintf("https://username:apiKey@api.softlayer.com/rest/v3/SoftLayer_Account/getObject")

    req, err := http.NewRequest("GET", url, nil)
    if err != nil {
        log.Fatal("NewRequest: ", err)
        return
    }

    client := &http.Client{}

    resp, err := client.Do(req)
    if err != nil {
        log.Fatal("Do: ", err)
        return
    }

    defer resp.Body.Close()

    var record Account

    // Use json.Decode for reading streams of JSON data
    if err := json.NewDecoder(resp.Body).Decode(&record); err != nil {
        log.Println(err)
    }

    fmt.Println("Response = ", record)
    fmt.Println("FirstName = ", record.FirstName)

}

将:用户名apiKey 替换为您的凭据

据我了解 Golang,有必要定义响应的结构(我不确定,但这是我成功解码响应的唯一方法)参见 type Account struct 在脚本中,您可以查看 softlayer-go 中的以下数据类型(结构),以清楚地了解它:

SoftLayer_Product_Order::verifyOrder - POST 没有 softlayer-go

的请求

最后我找到了一个简单的方法来解码响应:),这里是 post 对 SoftLayer_Product_Order::verifyOrder 的请求,我在 [=57] 中定义了 json =]Str 如你所见:

// Verify Order for VSI
//
// This script verifies an order for vsi
//
// important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/verifyOrder
// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Order/placeOrder
//
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>


package main

import (
    "fmt"
    "net/http"
    "bytes"
    "io/ioutil"
)


func main() {
    url := "https://username:apiKey@api.softlayer.com/rest/v3/SoftLayer_Product_Order/verifyOrder"
    fmt.Println("URL:>", url)

    // Define the json to send
    var jsonStr = []byte(`{"parameters":[{"location":"AMSTERDAM","packageId":46,"quantity":1,"hardware":[{"domain":"softlayer.com","hostname":"rcvtest"}],"prices":[{"id":50231},{"id":52135},{"id":51639},{"id":51569},{"id":52363},{"id":61019},{"id":55},{"id":57},{"id":175777},{"id":273},{"id":21},{"id":51481},{"id":905},{"id":58},{"id":420},{"id":418}]}]}`)
    req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
    req.Header.Set("Content-Type", "application/json")

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }

    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))
}

: username 和 apiKey 替换成自己在请求中的信息


SoftLayer_Account::getVirtualGuests - 带有对象过滤器和对象掩码的脚本

这里是一个在 SoftLayer_Account::getVirtualGuests 方法请求中定义了 objectFilter 和 objectMasks 的脚本,用于获取虚拟访客:

// This script get virtual guest through id using object filter and mask
//
// important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Account/getVirtualGuests
// http://sldn.softlayer.com/article/object-filters
// http://sldn.softlayer.com/article/object-masks
//
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>


package main

import (
    "fmt"
    "net/http"
     "io/ioutil"
)

func main() {
    url := `https://username:apiKey@api.softlayer.com/rest/v3/SoftLayer_Account/getVirtualGuests?objectFilter={"virtualGuests":{"id":{"operation":27497599}}}&objectMask=mask[billingItem]`
    fmt.Println("URL:>", url)

    req, err := http.NewRequest("GET", url, nil)


    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))
}

: username, apiKey 和 27497599 替换成自己在请求中的信息


SoftLayer_Product_Package::getItemPrices - 使用 objetFilter 请求

这里是一个带有 objectFilter 的脚本 SoftLayer_Product_Package::getItemPrices 检索性能存储的产品项目价格(标准)space(脚本在我的其他答案中使用softlayer-go),

如您所见,我设置了 is+null 因为 space 引发了异常,但它工作正常

// Retrieve product item prices for performance storage space using objectFilter
//
// important manual pages:
// http://sldn.softlayer.com/reference/services/SoftLayer_Product_Package/getItemPrices
// http://sldn.softlayer.com/article/object-filters
//
// @License: http://sldn.softlayer.com/article/License
// @Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>

package main

import (
    "fmt"
    "log"
    "net/http"
     "io/ioutil"
)

func main() {
    url := `https://username:apiKey@api.softlayer.com/rest/v3/SoftLayer_Product_Package/240/getItemPrices?objectFilter={"itemPrices":{"item":{"capacity":{"operation":40}},"attributes":{"value":{"operation":300}},"categories":{"categoryCode":{"operation":"performance_storage_space"}},"locationGroupId":{"operation":"is+null"}}}`
    fmt.Println("URL:>", url)

    req, err := http.NewRequest("GET", url, nil)

    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal("Do: ", err)
        return
    }

    defer resp.Body.Close()

    fmt.Println("response Status:", resp.Status)
    fmt.Println("response Headers:", resp.Header)
    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println("response Body:", string(body))
}

替换:请求中的用户名和apiKey

希望这些脚本能帮到你,如果你有任何问题或疑问,请告诉我