yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/instancenic.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 openstack
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"yunion.io/x/pkg/errors"
    21  	"yunion.io/x/pkg/util/regutils"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  )
    25  
    26  type SInstanceNic struct {
    27  	MacAddr string `json:"OS-EXT-IPS-MAC:mac_addr"`
    28  	Version int    `json:"version"`
    29  	Addr    string `json:"addr"`
    30  	Type    string `json:"OS-EXT-IPS:type"`
    31  }
    32  
    33  type SFixedIp struct {
    34  	IpAddress string
    35  	SubnetId  string
    36  }
    37  
    38  type SInstancePort struct {
    39  	region    *SRegion
    40  	FixedIps  []SFixedIp
    41  	MacAddr   string
    42  	NetId     string
    43  	PortId    string
    44  	PortState string
    45  
    46  	cloudprovider.DummyICloudNic
    47  }
    48  
    49  func (region *SRegion) GetInstancePorts(instanceId string) ([]SInstancePort, error) {
    50  	resource := fmt.Sprintf("/servers/%s/os-interface", instanceId)
    51  	resp, err := region.ecsList(resource, nil)
    52  	if err != nil {
    53  		return nil, errors.Wrap(err, "ecsList")
    54  	}
    55  	ports := []SInstancePort{}
    56  	err = resp.Unmarshal(&ports, "interfaceAttachments")
    57  	if err != nil {
    58  		return nil, errors.Wrap(err, "resp.Unmarshal")
    59  	}
    60  	return ports, nil
    61  }
    62  
    63  func (nic *SInstancePort) GetId() string {
    64  	return ""
    65  }
    66  
    67  func (nic *SInstancePort) GetIP() string {
    68  	for i := range nic.FixedIps {
    69  		if regutils.MatchIPAddr(nic.FixedIps[i].IpAddress) {
    70  			return nic.FixedIps[i].IpAddress
    71  		}
    72  	}
    73  	return ""
    74  }
    75  
    76  func (nic *SInstancePort) GetMAC() string {
    77  	return nic.MacAddr
    78  }
    79  
    80  func (nic *SInstancePort) GetDriver() string {
    81  	return "virtio"
    82  }
    83  
    84  func (nic *SInstancePort) InClassicNetwork() bool {
    85  	return false
    86  }
    87  
    88  func (nic *SInstancePort) GetINetworkId() string {
    89  	for i := range nic.FixedIps {
    90  		if regutils.MatchIPAddr(nic.FixedIps[i].IpAddress) {
    91  			network, err := nic.region.GetNetwork(nic.FixedIps[i].SubnetId)
    92  			if err != nil {
    93  				return nic.FixedIps[i].SubnetId
    94  			}
    95  			return network.GetGlobalId()
    96  		}
    97  	}
    98  	return ""
    99  }