yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/disk_offering.go (about)

     1  // Copyright 2019 Yunion
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package zstack
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"yunion.io/x/jsonutils"
    22  )
    23  
    24  type SDiskOffering struct {
    25  	ZStackBasic
    26  	DiskSize          int    `json:"diskSize"`
    27  	Type              string `json:"type"`
    28  	State             string `json:"state"`
    29  	AllocatorStrategy string `json:"allocatorStrategy"`
    30  }
    31  
    32  func (region *SRegion) GetDiskOfferings(diskSizeGB int) ([]SDiskOffering, error) {
    33  	offerings := []SDiskOffering{}
    34  	params := url.Values{}
    35  	if diskSizeGB != 0 {
    36  		params.Add("q", "diskSize="+fmt.Sprintf("%d", diskSizeGB*1024*1024*1024))
    37  	}
    38  	return offerings, region.client.listAll("disk-offerings", params, &offerings)
    39  }
    40  
    41  func (region *SRegion) CreateDiskOffering(diskSizeGB int) (*SDiskOffering, error) {
    42  	params := map[string]interface{}{
    43  		"params": map[string]interface{}{
    44  			"name":     fmt.Sprintf("temp-disk-offering-%dGB", diskSizeGB),
    45  			"diskSize": diskSizeGB * 1024 * 1024 * 1024,
    46  		},
    47  	}
    48  	resp, err := region.client.post("disk-offerings", jsonutils.Marshal(params))
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	offer := &SDiskOffering{}
    53  	return offer, resp.Unmarshal(offer, "inventory")
    54  }
    55  
    56  func (region *SRegion) DeleteDiskOffering(offerId string) error {
    57  	return region.client.delete("disk-offerings", offerId, "")
    58  }