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

时间:2017-02-08 10:18:29

标签: ibm-cloud-infrastructure iscsi block-storage

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

2 个答案:

答案 0 :(得分:1)

要创建Block Storage - 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)
}

<强>参考文献:

通过存储&lt; orderId

获取存储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以及您自己的信息

<强>参考

获取标准价格(location group = 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))
}

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

// 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,有必要定义响应的结构(我不确定,但这是我成功解码响应的唯一方法)请参阅脚本中的类型帐户结构,您可以在softlayer-go中查看以下数据类型(结构),以便清楚地了解相关内容:

答案 1 :(得分:0)

SoftLayer_Product_Order :: verifyOrder - 没有softlayer-go的POST请求

最后我发现了一种解码响应的简单方法:),这里是对SoftLayer_Product_Order::verifyOrder的帖子请求,我在 jsonStr 中定义了json,如你所见:

// 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))
}

替换:用户名,apiKey和27497599,并在请求中提供您自己的信息

SoftLayer_Product_Package :: getItemPrices - 使用objetFilter的请求

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

正如您所看到的,我设置是+ null ,因为该空格引发了异常,但它工作正常

// 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

我希望这些脚本可以帮助您,如果您有任何问题或疑问,请告诉我