yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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 azure
    16  
    17  import (
    18  	"net/url"
    19  	"strings"
    20  
    21  	"yunion.io/x/jsonutils"
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type PublicIPAddress struct {
    30  	ID         string
    31  	Name       string
    32  	Location   string
    33  	Properties PublicIPAddressPropertiesFormat
    34  }
    35  
    36  type InterfaceIPConfigurationPropertiesFormat struct {
    37  	PrivateIPAddress          string           `json:"privateIPAddress,omitempty"`
    38  	PrivateIPAddressVersion   string           `json:"privateIPAddressVersion,omitempty"`
    39  	PrivateIPAllocationMethod string           `json:"privateIPAllocationMethod,omitempty"`
    40  	Subnet                    SNetwork         `json:"subnet,omitempty"`
    41  	Primary                   bool             `json:"primary,omitempty"`
    42  	PublicIPAddress           *PublicIPAddress `json:"publicIPAddress,omitempty"`
    43  }
    44  
    45  type InterfaceIPConfiguration struct {
    46  	Properties InterfaceIPConfigurationPropertiesFormat `json:"properties,omitempty"`
    47  	Name       string
    48  	ID         string
    49  }
    50  
    51  type InterfacePropertiesFormat struct {
    52  	NetworkSecurityGroup SSecurityGroup             `json:"networkSecurityGroup,omitempty"`
    53  	IPConfigurations     []InterfaceIPConfiguration `json:"ipConfigurations,omitempty"`
    54  	MacAddress           string                     `json:"macAddress,omitempty"`
    55  	Primary              bool                       `json:"primary,omitempty"`
    56  	VirtualMachine       SubResource                `json:"virtualMachine,omitempty"`
    57  }
    58  
    59  type SInstanceNic struct {
    60  	multicloud.SResourceBase
    61  	AzureTags
    62  
    63  	instance   *SInstance
    64  	ID         string
    65  	Name       string
    66  	Type       string
    67  	Location   string
    68  	Properties InterfacePropertiesFormat `json:"properties,omitempty"`
    69  
    70  	cloudprovider.DummyICloudNic
    71  }
    72  
    73  func (self *SInstanceNic) GetIP() string {
    74  	if len(self.Properties.IPConfigurations) > 0 {
    75  		return self.Properties.IPConfigurations[0].Properties.PrivateIPAddress
    76  	}
    77  	return ""
    78  }
    79  
    80  func (region *SRegion) DeleteNetworkInterface(interfaceId string) error {
    81  	return region.del(interfaceId)
    82  }
    83  
    84  func (self *SInstanceNic) Delete() error {
    85  	return self.instance.host.zone.region.DeleteNetworkInterface(self.ID)
    86  }
    87  
    88  func (self *SInstanceNic) GetMAC() string {
    89  	mac := self.Properties.MacAddress
    90  	return strings.Replace(strings.ToLower(mac), "-", ":", -1)
    91  }
    92  
    93  func (self *SInstanceNic) GetDriver() string {
    94  	return "virtio"
    95  }
    96  
    97  func (self *SInstanceNic) InClassicNetwork() bool {
    98  	return false
    99  }
   100  
   101  func (self *SInstanceNic) updateSecurityGroup(secgroupId string) error {
   102  	region := self.instance.host.zone.region
   103  	if len(secgroupId) > 0 {
   104  		self.Properties.NetworkSecurityGroup = SSecurityGroup{ID: secgroupId}
   105  	}
   106  	return region.update(jsonutils.Marshal(self), nil)
   107  }
   108  
   109  func (self *SInstanceNic) revokeSecurityGroup() error {
   110  	return self.updateSecurityGroup("")
   111  }
   112  
   113  func (self *SInstanceNic) assignSecurityGroup(secgroupId string) error {
   114  	return self.updateSecurityGroup(secgroupId)
   115  }
   116  
   117  func (self *SInstanceNic) GetINetworkId() string {
   118  	if len(self.Properties.IPConfigurations) > 0 {
   119  		return strings.ToLower(self.Properties.IPConfigurations[0].Properties.Subnet.ID)
   120  	}
   121  	return ""
   122  }
   123  
   124  func (self *SRegion) GetNetworkInterface(interfaceId string) (*SInstanceNic, error) {
   125  	instancenic := SInstanceNic{}
   126  	return &instancenic, self.get(interfaceId, url.Values{}, &instancenic)
   127  }
   128  
   129  func (self *SRegion) GetNetworkInterfaces() ([]SInstanceNic, error) {
   130  	interfaces := []SInstanceNic{}
   131  	err := self.list("Microsoft.Network/networkInterfaces", url.Values{}, &interfaces)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	return interfaces, nil
   136  }
   137  
   138  func (self *SRegion) CreateNetworkInterface(resourceGroup string, nicName string, ipAddr string, subnetId string, secgrpId string) (*SInstanceNic, error) {
   139  	allocMethod := "Static"
   140  	if len(ipAddr) == 0 {
   141  		allocMethod = "Dynamic"
   142  	}
   143  	params := jsonutils.Marshal(map[string]interface{}{
   144  		"Name":     nicName,
   145  		"Location": self.Name,
   146  		"Properties": map[string]interface{}{
   147  			"IPConfigurations": []map[string]interface{}{
   148  				map[string]interface{}{
   149  					"Name": nicName,
   150  					"Properties": map[string]interface{}{
   151  						"PrivateIPAddress":          ipAddr,
   152  						"PrivateIPAddressVersion":   "IPv4",
   153  						"PrivateIPAllocationMethod": allocMethod,
   154  						"Subnet": map[string]string{
   155  							"Id": subnetId,
   156  						},
   157  					},
   158  				},
   159  			},
   160  		},
   161  		"Type": "Microsoft.Network/networkInterfaces",
   162  	}).(*jsonutils.JSONDict)
   163  	if len(secgrpId) > 0 {
   164  		params.Add(jsonutils.Marshal(map[string]string{"id": secgrpId}), "Properties", "NetworkSecurityGroup")
   165  	}
   166  	nic := SInstanceNic{}
   167  	return &nic, self.create(resourceGroup, params, &nic)
   168  }
   169  
   170  func (self *SRegion) GetINetworkInterfaces() ([]cloudprovider.ICloudNetworkInterface, error) {
   171  	nics, err := self.GetNetworkInterfaces()
   172  	if err != nil {
   173  		return nil, errors.Wrapf(err, "GetNetworkInterfaces")
   174  	}
   175  	ret := []cloudprovider.ICloudNetworkInterface{}
   176  	for i := range nics {
   177  		if len(nics[i].Properties.VirtualMachine.ID) > 0 {
   178  			continue
   179  		}
   180  		ret = append(ret, &nics[i])
   181  	}
   182  	return ret, nil
   183  }
   184  
   185  func (self *SInstanceNic) GetAssociateId() string {
   186  	return ""
   187  }
   188  
   189  func (self *SInstanceNic) GetAssociateType() string {
   190  	return ""
   191  }
   192  
   193  func (self *SInstanceNic) GetId() string {
   194  	return self.ID
   195  }
   196  
   197  func (self *SInstanceNic) GetName() string {
   198  	return self.Name
   199  }
   200  
   201  func (self *SInstanceNic) GetStatus() string {
   202  	return api.NETWORK_INTERFACE_STATUS_AVAILABLE
   203  }
   204  
   205  func (self *SInstanceNic) GetGlobalId() string {
   206  	return strings.ToLower(self.ID)
   207  }
   208  
   209  func (self *SInstanceNic) GetMacAddress() string {
   210  	return self.Properties.MacAddress
   211  }
   212  
   213  func (self *SInstanceNic) GetICloudInterfaceAddresses() ([]cloudprovider.ICloudInterfaceAddress, error) {
   214  	addrs := []cloudprovider.ICloudInterfaceAddress{}
   215  	for i := range self.Properties.IPConfigurations {
   216  		addrs = append(addrs, &self.Properties.IPConfigurations[i])
   217  	}
   218  	return addrs, nil
   219  }
   220  
   221  func (self *InterfaceIPConfiguration) GetGlobalId() string {
   222  	return strings.ToLower(self.ID)
   223  }
   224  
   225  func (self *InterfaceIPConfiguration) GetINetworkId() string {
   226  	return strings.ToLower(self.Properties.Subnet.ID)
   227  }
   228  
   229  func (self *InterfaceIPConfiguration) GetIP() string {
   230  	return self.Properties.PrivateIPAddress
   231  }
   232  
   233  func (self *InterfaceIPConfiguration) IsPrimary() bool {
   234  	return self.Properties.Primary
   235  }