yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/shell/skubilling.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 shell
    16  
    17  import (
    18  	"fmt"
    19  	"io/ioutil"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/multicloud/google"
    25  	"yunion.io/x/onecloud/pkg/util/shellutils"
    26  )
    27  
    28  func init() {
    29  	type SkuBillingListOptions struct {
    30  		PageSize  int
    31  		PageToken string
    32  	}
    33  	shellutils.R(&SkuBillingListOptions{}, "sku-billing-list", "List sku billing", func(cli *google.SRegion, args *SkuBillingListOptions) error {
    34  		billings, err := cli.ListSkuBilling(args.PageSize, args.PageToken)
    35  		if err != nil {
    36  			return err
    37  		}
    38  		printList(billings, 0, 0, 0, nil)
    39  		return nil
    40  	})
    41  
    42  	shellutils.R(&SkuBillingListOptions{}, "compute-sku-billing-list", "List sku billing", func(cli *google.SRegion, args *SkuBillingListOptions) error {
    43  		billings, err := cli.ListSkuBilling(args.PageSize, args.PageToken)
    44  		if err != nil {
    45  			return err
    46  		}
    47  		info := cli.GetSkuRateInfo(billings)
    48  		fmt.Println(jsonutils.Marshal(info).PrettyString())
    49  		return nil
    50  	})
    51  
    52  	type SkuEstimate struct {
    53  		RATE_FAILE string
    54  		SKU        string
    55  		REGION     string
    56  		CPU        int
    57  		MEMORY_MB  int
    58  	}
    59  
    60  	shellutils.R(&SkuEstimate{}, "sku-estimate", "Estimate sku price", func(cli *google.SRegion, args *SkuEstimate) error {
    61  		data, err := ioutil.ReadFile(args.RATE_FAILE)
    62  		if err != nil {
    63  			return errors.Wrap(err, "ioutil.ReadFile")
    64  		}
    65  		rate := google.SRateInfo{}
    66  		j, err := jsonutils.Parse(data)
    67  		if err != nil {
    68  			return errors.Wrap(err, "jsonutils.Parse")
    69  		}
    70  		err = jsonutils.Update(&rate, j)
    71  		if err != nil {
    72  			return errors.Wrap(err, "jsonutils.Update")
    73  		}
    74  		result, err := rate.GetSkuPrice(args.REGION, args.SKU, args.CPU, args.MEMORY_MB)
    75  		if err != nil {
    76  			return errors.Wrap(err, "GetSkuPrice")
    77  		}
    78  		fmt.Printf("result: %s\n", jsonutils.Marshal(result).PrettyString())
    79  		return nil
    80  	})
    81  
    82  }