@@ -20,6 +20,7 @@ import (
2020 "time"
2121
2222 "yunion.io/x/jsonutils"
23+ "yunion.io/x/log"
2324 "yunion.io/x/pkg/errors"
2425
2526 billing_api "yunion.io/x/cloudmux/pkg/apis/billing"
@@ -120,6 +121,10 @@ func (eip *SEip) GetTags() (map[string]string, error) {
120121 return tags .GetTags (), nil
121122}
122123
124+ func (eip * SEip ) SetTags (tags map [string ]string , replace bool ) error {
125+ return eip .region .SetResourceTags ("eip" , eip .AllocationId , tags , replace )
126+ }
127+
123128func (eip * SEip ) GetStatus () string {
124129 switch eip .State {
125130 case "associate" :
@@ -265,23 +270,45 @@ func (region *SRegion) CreateEip(opts *cloudprovider.SEip) (*SEip, error) {
265270 return nil , errors .Wrap (errors .ErrNotFound , "No bgp lines found" )
266271 }
267272 params := map [string ]interface {}{
268- "LineId" : lineId ,
269- "BandWidth" : fmt .Sprintf ("%d" , opts .BandwidthMbps ),
270- "ChargeType" : "TrafficMonthly" ,
273+ "LineId" : lineId ,
274+ "BandWidth" : fmt .Sprintf ("%d" , opts .BandwidthMbps ),
271275 }
276+
277+ chargeTypes := []string {}
272278 if opts .ChargeType == api .EIP_CHARGE_TYPE_BY_BANDWIDTH {
273- params [ "ChargeType" ] = "Monthlys"
279+ chargeTypes = append ( chargeTypes , "Monthly" )
274280 params ["PurchaseTime" ] = "1"
281+ } else {
282+ chargeTypes = append (chargeTypes , "TrafficMonthly" )
283+ chargeTypes = append (chargeTypes , "DailyPaidByTransfer" )
275284 }
276285
277- body , err := region .eipRequest ("AllocateAddress" , params )
286+ ret := & SEip {region : region }
287+ var body jsonutils.JSONObject
288+ for _ , chargeType := range chargeTypes {
289+ params ["ChargeType" ] = chargeType
290+ body , err = region .eipRequest ("AllocateAddress" , params )
291+ if err != nil {
292+ log .Debugf ("not support charge type %s, error: %v" , chargeType , err )
293+ if e , ok := err .(* sKsyunError ); ok && e .ErrorMsg .Code == "PackageNotExists" {
294+ continue
295+ }
296+ return nil , errors .Wrap (err , "AllocateAddress" )
297+ }
298+ err = body .Unmarshal (ret )
299+ if err != nil {
300+ return nil , errors .Wrap (err , "Unmarshal" )
301+ }
302+ }
278303 if err != nil {
279304 return nil , errors .Wrap (err , "AllocateAddress" )
280305 }
281- ret := & SEip {region : region }
282- err = body .Unmarshal (ret )
283- if err != nil {
284- return nil , errors .Wrap (err , "Unmarshal" )
306+
307+ if len (opts .Tags ) > 0 {
308+ err = region .CreateTags ("eip" , ret .AllocationId , opts .Tags )
309+ if err != nil {
310+ log .Errorf ("set tags %s to eip %s failed: %v" , opts .Tags , ret .AllocationId , err )
311+ }
285312 }
286313 return ret , nil
287314}
0 commit comments