| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package cloudpods
- import (
- "time"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- "yunion.io/x/cloudmux/pkg/multicloud"
- "yunion.io/x/jsonutils"
- billing_api "yunion.io/x/onecloud/pkg/apis/billing"
- api "yunion.io/x/onecloud/pkg/apis/compute"
- modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
- )
- type SEip struct {
- multicloud.SVirtualResourceBase
- multicloud.SBillingBase
- CloudpodsTags
- region *SRegion
- api.ElasticipDetails
- }
- func (self *SEip) GetName() string {
- return self.Name
- }
- func (self *SEip) GetId() string {
- return self.Id
- }
- func (self *SEip) GetGlobalId() string {
- return self.Id
- }
- func (self *SEip) GetProjectId() string {
- return self.TenantId
- }
- func (self *SEip) GetStatus() string {
- return self.Status
- }
- func (self *SEip) Refresh() error {
- eip, err := self.region.GetEip(self.Id)
- if err != nil {
- return err
- }
- return jsonutils.Update(self, eip)
- }
- func (self *SEip) GetBillingType() string {
- return self.BillingType
- }
- func (self *SEip) GetIpAddr() string {
- return self.IpAddr
- }
- func (self *SEip) GetINetworkId() string {
- return self.NetworkId
- }
- func (self *SEip) GetAssociationType() string {
- return self.AssociateType
- }
- func (self *SEip) GetAssociationExternalId() string {
- return self.AssociateId
- }
- func (self *SEip) GetBandwidth() int {
- return self.Bandwidth
- }
- func (self *SEip) GetInternetChargeType() string {
- return self.ChargeType
- }
- func (self *SEip) Delete() error {
- return self.region.cli.delete(&modules.Elasticips, self.Id)
- }
- func (self *SEip) IsAutoRenew() bool {
- return self.AutoRenew
- }
- func (self *SEip) Associate(opts *cloudprovider.AssociateConfig) error {
- input := api.ElasticipAssociateInput{}
- input.InstanceType = opts.AssociateType
- input.InstanceId = opts.InstanceId
- switch opts.AssociateType {
- case api.EIP_ASSOCIATE_TYPE_SERVER:
- default:
- return cloudprovider.ErrNotImplemented
- }
- _, err := self.region.perform(&modules.Elasticips, self.Id, "associate", input)
- return err
- }
- func (self *SEip) Dissociate() error {
- _, err := self.region.perform(&modules.Elasticips, self.Id, "dissociate", nil)
- return err
- }
- func (self *SEip) GetCreatedAt() time.Time {
- return self.CreatedAt
- }
- func (self *SEip) GetExpiredAt() time.Time {
- return self.ExpiredAt
- }
- func (self *SEip) GetMode() string {
- return self.Mode
- }
- func (self *SEip) ChangeBandwidth(bw int) error {
- return cloudprovider.ErrNotImplemented
- }
- func (self *SRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
- eip, err := self.GetEip(id)
- if err != nil {
- return nil, err
- }
- return eip, nil
- }
- func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
- eips, err := self.GetEips("")
- if err != nil {
- return nil, err
- }
- ret := []cloudprovider.ICloudEIP{}
- for i := range eips {
- eips[i].region = self
- ret = append(ret, &eips[i])
- }
- return ret, nil
- }
- func (self *SRegion) CreateEIP(opts *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
- input := api.SElasticipCreateInput{}
- input.Name = opts.Name
- input.CloudregionId = self.Id
- input.ChargeType = billing_api.TNetChargeType(opts.ChargeType)
- input.BandwidthMb = opts.BandwidthMbps
- input.ProjectId = opts.ProjectId
- input.NetworkId = opts.NetworkExternalId
- input.BgpType = opts.BGPType
- input.IpAddr = opts.Ip
- eip := &SEip{region: self}
- return eip, self.create(&modules.Elasticips, input, eip)
- }
- func (self *SRegion) GetEips(associateId string) ([]SEip, error) {
- eips := []SEip{}
- params := map[string]interface{}{}
- if len(associateId) > 0 {
- params["associate_id"] = associateId
- }
- return eips, self.list(&modules.Elasticips, params, &eips)
- }
- func (self *SRegion) GetEip(id string) (*SEip, error) {
- eip := &SEip{region: self}
- return eip, self.cli.get(&modules.Elasticips, id, nil, eip)
- }
|