instancenic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. package cloudpods
  15. import (
  16. "fmt"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. "yunion.io/x/cloudmux/pkg/multicloud"
  19. "yunion.io/x/jsonutils"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  22. )
  23. type SInstanceNic struct {
  24. multicloud.SResourceBase
  25. ins *SInstance
  26. api.GuestnetworkDetails
  27. }
  28. func (self *SInstanceNic) GetId() string {
  29. return fmt.Sprintf("%d", self.RowId)
  30. }
  31. func (self *SInstanceNic) GetIP() string {
  32. return self.IpAddr
  33. }
  34. func (self *SInstanceNic) GetIP6() string {
  35. return self.Ip6Addr
  36. }
  37. func (self *SInstanceNic) GetDriver() string {
  38. return self.Driver
  39. }
  40. func (self *SInstanceNic) InClassicNetwork() bool {
  41. return false
  42. }
  43. func (self *SInstanceNic) GetMAC() string {
  44. return self.MacAddr
  45. }
  46. func (self *SInstanceNic) GetSubAddress() ([]string, error) {
  47. return []string{}, nil
  48. }
  49. func (self *SInstanceNic) AssignNAddress(count int) ([]string, error) {
  50. return nil, cloudprovider.ErrNotImplemented
  51. }
  52. func (self *SInstanceNic) AssignAddress(ipAddrs []string) error {
  53. return cloudprovider.ErrNotImplemented
  54. }
  55. func (self *SInstanceNic) UnassignAddress(IpAddrs []string) error {
  56. return cloudprovider.ErrNotImplemented
  57. }
  58. func (self *SInstanceNic) GetINetworkId() string {
  59. return self.NetworkId
  60. }
  61. func (self *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) {
  62. nics, err := self.host.zone.region.GetGuestnetworks(self.Id)
  63. if err != nil {
  64. return nil, err
  65. }
  66. ret := []cloudprovider.ICloudNic{}
  67. for i := range nics {
  68. nics[i].ins = self
  69. ret = append(ret, &nics[i])
  70. }
  71. return ret, nil
  72. }
  73. func (self *SRegion) GetGuestnetworks(serverId string) ([]SInstanceNic, error) {
  74. ret := []SInstanceNic{}
  75. params := map[string]interface{}{
  76. "scope": "system",
  77. }
  78. if len(serverId) > 0 {
  79. params["server_id"] = serverId
  80. }
  81. resp, err := modules.Servernetworks.List(self.cli.s, jsonutils.Marshal(params))
  82. if err != nil {
  83. return nil, err
  84. }
  85. return ret, jsonutils.Update(&ret, resp.Data)
  86. }