yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/sku.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 aws
    16  
    17  import (
    18  	"github.com/aws/aws-sdk-go/service/pricing"
    19  
    20  	"yunion.io/x/jsonutils"
    21  	"yunion.io/x/pkg/errors"
    22  )
    23  
    24  type Product struct {
    25  	ProductFamily string     `json:"productFamily"`
    26  	Attributes    Attributes `json:"attributes"`
    27  	Sku           string     `json:"sku"`
    28  }
    29  
    30  type Attributes struct {
    31  	EnhancedNetworkingSupported string `json:"enhancedNetworkingSupported"`
    32  	IntelTurboAvailable         string `json:"intelTurboAvailable"`
    33  	Memory                      string `json:"memory"`
    34  	DedicatedEbsThroughput      string `json:"dedicatedEbsThroughput"`
    35  	Vcpu                        int    `json:"vcpu"`
    36  	Gpu                         int    `json:"gpu"`
    37  	Capacitystatus              string `json:"capacitystatus"`
    38  	LocationType                string `json:"locationType"`
    39  	Storage                     string `json:"storage"`
    40  	InstanceFamily              string `json:"instanceFamily"`
    41  	OperatingSystem             string `json:"operatingSystem"`
    42  	IntelAvx2Available          string `json:"intelAvx2Available"`
    43  	PhysicalProcessor           string `json:"physicalProcessor"`
    44  	ClockSpeed                  string `json:"clockSpeed"`
    45  	Ecu                         string `json:"ecu"`
    46  	NetworkPerformance          string `json:"networkPerformance"`
    47  	Servicename                 string `json:"servicename"`
    48  	InstanceType                string `json:"instanceType"`
    49  	InstanceSku                 string `json:"instancesku"`
    50  	Tenancy                     string `json:"tenancy"`
    51  	Usagetype                   string `json:"usagetype"`
    52  	NormalizationSizeFactor     string `json:"normalizationSizeFactor"`
    53  	IntelAvxAvailable           string `json:"intelAvxAvailable"`
    54  	ProcessorFeatures           string `json:"processorFeatures"`
    55  	Servicecode                 string `json:"servicecode"`
    56  	LicenseModel                string `json:"licenseModel"`
    57  	CurrentGeneration           string `json:"currentGeneration"`
    58  	PreInstalledSw              string `json:"preInstalledSw"`
    59  	Location                    string `json:"location"`
    60  	ProcessorArchitecture       string `json:"processorArchitecture"`
    61  	Operation                   string `json:"operation"`
    62  	VolumeApiName               string `json:"volumeApiName"`
    63  }
    64  
    65  type Terms struct {
    66  	OnDemand map[string]Term `json:"OnDemand"`
    67  	Reserved map[string]Term `json:"Reserved"`
    68  }
    69  
    70  type Term struct {
    71  	PriceDimensions map[string]Dimension `json:"priceDimensions"`
    72  	Sku             string               `json:"sku"`
    73  	EffectiveDate   string               `json:"effectiveDate"`
    74  	OfferTermCode   string               `json:"offerTermCode"`
    75  	TermAttributes  TermAttributes       `json:"termAttributes"`
    76  }
    77  
    78  type Dimension struct {
    79  	Unit         string       `json:"unit"`
    80  	EndRange     string       `json:"endRange"`
    81  	Description  string       `json:"description"`
    82  	AppliesTo    []string     `json:"appliesTo"`
    83  	RateCode     string       `json:"rateCode"`
    84  	BeginRange   string       `json:"beginRange"`
    85  	PricePerUnit PricePerUnit `json:"pricePerUnit"`
    86  }
    87  
    88  type TermAttributes struct {
    89  	LeaseContractLength string `json:"LeaseContractLength"`
    90  	OfferingClass       string `json:"OfferingClass"`
    91  	PurchaseOption      string `json:"PurchaseOption"`
    92  }
    93  
    94  type PricePerUnit struct {
    95  	Usd float64 `json:"USD"`
    96  	CNY float64 `json:"CNY"`
    97  }
    98  
    99  type SInstnaceType struct {
   100  	Product         Product `json:"product"`
   101  	ServiceCode     string  `json:"serviceCode"`
   102  	Terms           Terms   `json:"terms"`
   103  	Version         string  `json:"version"`
   104  	PublicationDate string  `json:"publicationDate"`
   105  }
   106  
   107  func (self *SRegion) GetInstanceTypes(nextToken string) ([]SInstnaceType, string, error) {
   108  	input := &pricing.GetProductsInput{}
   109  	input.SetServiceCode("AmazonEC2")
   110  	if len(nextToken) > 0 {
   111  		input.SetNextToken(nextToken)
   112  	}
   113  	GetFilter := func(k, v string) pricing.Filter {
   114  		filter := pricing.Filter{}
   115  		filter.SetType("TERM_MATCH")
   116  		filter.SetField(k)
   117  		filter.SetValue(v)
   118  		return filter
   119  	}
   120  	f1 := GetFilter("regionCode", self.RegionId)
   121  	f2 := GetFilter("operatingSystem", "Linux")
   122  	f3 := GetFilter("licenseModel", "No License required")
   123  	f4 := GetFilter("productFamily", "Compute Instance")
   124  	f5 := GetFilter("operation", "RunInstances")
   125  	f6 := GetFilter("preInstalledSw", "NA")
   126  	f7 := GetFilter("tenancy", "Shared")
   127  	f8 := GetFilter("capacitystatus", "Used")
   128  	input.SetFilters([]*pricing.Filter{&f1, &f2, &f3, &f4, &f5, &f6, &f7, &f8})
   129  	s, err := self.client.getAwsSession("ap-south-1", false)
   130  	if err != nil {
   131  		return nil, "", err
   132  	}
   133  
   134  	cli := pricing.New(s)
   135  	output, err := cli.GetProducts(input)
   136  	if err != nil {
   137  		return nil, "", errors.Wrap(err, "SZone.GetServerSku.GetProducts")
   138  	}
   139  	if output.NextToken != nil {
   140  		nextToken = *output.NextToken
   141  	}
   142  	ret := []SInstnaceType{}
   143  	return ret, nextToken, jsonutils.Update(&ret, output.PriceList)
   144  }