From 2e868ed7c118cce8b18f387e19580288b5429ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 16 Nov 2018 15:28:32 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=81=BF=E5=85=8D=E5=85=AC=E6=9C=89?= =?UTF-8?q?=E4=BA=91=E6=89=BE=E4=B8=8D=E5=88=B0=E5=AF=B9=E5=BA=94=E7=9A=84?= =?UTF-8?q?cloudregion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/compute/guestdrivers/managedvirtual.go | 7 ++++++- pkg/compute/models/snapshots.go | 8 ++++++++ pkg/util/aliyun/snapshot.go | 2 +- pkg/util/azure/snapshot.go | 4 +++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/pkg/compute/guestdrivers/managedvirtual.go b/pkg/compute/guestdrivers/managedvirtual.go index f3e7458173..36d1f7f73d 100644 --- a/pkg/compute/guestdrivers/managedvirtual.go +++ b/pkg/compute/guestdrivers/managedvirtual.go @@ -369,7 +369,12 @@ func (self *SManagedVirtualizedGuestDriver) RequestDiskSnapshot(ctx context.Cont res := jsonutils.NewDict() res.Set("snapshot_id", jsonutils.NewString(cloudSnapshot.GetId())) res.Set("manager_id", jsonutils.NewString(cloudSnapshot.GetManagerId())) - res.Set("cloudregion_id", jsonutils.NewString(cloudSnapshot.GetRegionId())) + extId := cloudSnapshot.GetRegionId() + cloudregion, err := models.CloudregionManager.FetchByExternalId(extId) + if err != nil { + return nil, err + } + res.Set("cloudregion_id", jsonutils.NewString(cloudregion.GetId())) return res, nil }) return nil diff --git a/pkg/compute/models/snapshots.go b/pkg/compute/models/snapshots.go index 491381fe77..bc0e70fef6 100644 --- a/pkg/compute/models/snapshots.go +++ b/pkg/compute/models/snapshots.go @@ -439,8 +439,16 @@ func totalSnapshotCount(projectId string) int { func (self *SSnapshot) SyncWithCloudSnapshot(userCred mcclient.TokenCredential, ext cloudprovider.ICloudSnapshot) error { _, err := self.GetModelManager().TableSpec().Update(self, func() error { + self.Name = ext.GetName() self.Status = ext.GetStatus() self.DiskType = ext.GetDiskType() + + cloudregionId := ext.GetRegionId() + region, err := CloudregionManager.FetchByExternalId(cloudregionId) + if err != nil { + return err + } + self.CloudregionId = region.GetId() return nil }) if err != nil { diff --git a/pkg/util/aliyun/snapshot.go b/pkg/util/aliyun/snapshot.go index e0cada0372..515115e8fb 100644 --- a/pkg/util/aliyun/snapshot.go +++ b/pkg/util/aliyun/snapshot.go @@ -56,7 +56,7 @@ func (self *SSnapshot) GetManagerId() string { } func (self *SSnapshot) GetRegionId() string { - return self.region.GetId() + return self.region.GetGlobalId() } func (self *SSnapshot) GetSize() int32 { diff --git a/pkg/util/azure/snapshot.go b/pkg/util/azure/snapshot.go index 429b1451c8..8804fb7832 100644 --- a/pkg/util/azure/snapshot.go +++ b/pkg/util/azure/snapshot.go @@ -154,9 +154,11 @@ func (self *SRegion) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) { classicSnapshots = append(classicSnapshots, _classicSnapshots...) isnapshots := make([]cloudprovider.ICloudSnapshot, len(snapshots)+len(classicSnapshots)) for i := 0; i < len(snapshots); i++ { + snapshots[i].region = self isnapshots[i] = &snapshots[i] } for i := 0; i < len(classicSnapshots); i++ { + classicSnapshots[i].region = self isnapshots[len(snapshots)+i] = &classicSnapshots[i] } return isnapshots, nil @@ -171,7 +173,7 @@ func (self *SSnapshot) GetManagerId() string { } func (self *SSnapshot) GetRegionId() string { - return self.region.GetId() + return self.region.GetGlobalId() } func (self *SSnapshot) GetDiskType() string { From 503bec34c86f2b00ed54736ec988ab5a4f11bc9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 16 Nov 2018 16:55:17 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E4=B8=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/cloudprovider/resources.go | 2 -- pkg/compute/guestdrivers/managedvirtual.go | 7 ------- pkg/compute/models/snapshots.go | 21 ++++++++----------- pkg/compute/tasks/guest_disk_snapshot_task.go | 13 +++--------- pkg/util/aliyun/snapshot.go | 8 ------- pkg/util/azure/snapshot.go | 8 ------- 6 files changed, 12 insertions(+), 47 deletions(-) diff --git a/pkg/cloudprovider/resources.go b/pkg/cloudprovider/resources.go index 0935ffb38f..2a9df62487 100644 --- a/pkg/cloudprovider/resources.go +++ b/pkg/cloudprovider/resources.go @@ -249,12 +249,10 @@ type ICloudDisk interface { type ICloudSnapshot interface { ICloudResource - GetManagerId() string GetSize() int32 GetDiskId() string GetDiskType() string Delete() error - GetRegionId() string } type ICloudVpc interface { diff --git a/pkg/compute/guestdrivers/managedvirtual.go b/pkg/compute/guestdrivers/managedvirtual.go index 36d1f7f73d..30dbc10618 100644 --- a/pkg/compute/guestdrivers/managedvirtual.go +++ b/pkg/compute/guestdrivers/managedvirtual.go @@ -368,13 +368,6 @@ func (self *SManagedVirtualizedGuestDriver) RequestDiskSnapshot(ctx context.Cont } res := jsonutils.NewDict() res.Set("snapshot_id", jsonutils.NewString(cloudSnapshot.GetId())) - res.Set("manager_id", jsonutils.NewString(cloudSnapshot.GetManagerId())) - extId := cloudSnapshot.GetRegionId() - cloudregion, err := models.CloudregionManager.FetchByExternalId(extId) - if err != nil { - return nil, err - } - res.Set("cloudregion_id", jsonutils.NewString(cloudregion.GetId())) return res, nil }) return nil diff --git a/pkg/compute/models/snapshots.go b/pkg/compute/models/snapshots.go index bc0e70fef6..367f05bb71 100644 --- a/pkg/compute/models/snapshots.go +++ b/pkg/compute/models/snapshots.go @@ -273,6 +273,7 @@ func (self *SSnapshotManager) CreateSnapshot(ctx context.Context, userCred mccli return nil, err } disk := iDisk.(*SDisk) + storage := disk.GetStorage() snapshot := &SSnapshot{} snapshot.SetModelManager(self) snapshot.ProjectId = userCred.GetProjectId() @@ -282,6 +283,8 @@ func (self *SSnapshotManager) CreateSnapshot(ctx context.Context, userCred mccli snapshot.DiskType = disk.DiskType snapshot.Location = location snapshot.CreatedBy = createdBy + snapshot.ManagerId = storage.ManagerId + snapshot.CloudregionId = storage.getZone().GetRegion().GetId() snapshot.Name = name snapshot.Status = SNAPSHOT_CREATING err = SnapshotManager.TableSpec().Insert(snapshot) @@ -437,18 +440,12 @@ func totalSnapshotCount(projectId string) int { return count } -func (self *SSnapshot) SyncWithCloudSnapshot(userCred mcclient.TokenCredential, ext cloudprovider.ICloudSnapshot) error { +func (self *SSnapshot) SyncWithCloudSnapshot(userCred mcclient.TokenCredential, ext cloudprovider.ICloudSnapshot, region *SCloudregion) error { _, err := self.GetModelManager().TableSpec().Update(self, func() error { self.Name = ext.GetName() self.Status = ext.GetStatus() self.DiskType = ext.GetDiskType() - - cloudregionId := ext.GetRegionId() - region, err := CloudregionManager.FetchByExternalId(cloudregionId) - if err != nil { - return err - } - self.CloudregionId = region.GetId() + self.CloudregionId = region.Id return nil }) if err != nil { @@ -457,7 +454,7 @@ func (self *SSnapshot) SyncWithCloudSnapshot(userCred mcclient.TokenCredential, return err } -func (manager *SSnapshotManager) newFromCloudSnapshot(userCred mcclient.TokenCredential, extSnapshot cloudprovider.ICloudSnapshot, region *SCloudregion) (*SSnapshot, error) { +func (manager *SSnapshotManager) newFromCloudSnapshot(userCred mcclient.TokenCredential, extSnapshot cloudprovider.ICloudSnapshot, region *SCloudregion, provider *SCloudprovider) (*SSnapshot, error) { snapshot := SSnapshot{} snapshot.SetModelManager(manager) @@ -475,7 +472,7 @@ func (manager *SSnapshotManager) newFromCloudSnapshot(userCred mcclient.TokenCre snapshot.DiskType = extSnapshot.GetDiskType() snapshot.Size = int(extSnapshot.GetSize()) * 1024 - snapshot.ManagerId = extSnapshot.GetManagerId() + snapshot.ManagerId = provider.Id snapshot.CloudregionId = region.Id snapshot.ProjectId = userCred.GetProjectId() @@ -526,7 +523,7 @@ func (manager *SSnapshotManager) SyncSnapshots(ctx context.Context, userCred mcc } } for i := 0; i < len(commondb); i += 1 { - err = commondb[i].SyncWithCloudSnapshot(userCred, commonext[i]) + err = commondb[i].SyncWithCloudSnapshot(userCred, commonext[i], region) if err != nil { syncResult.UpdateError(err) } else { @@ -534,7 +531,7 @@ func (manager *SSnapshotManager) SyncSnapshots(ctx context.Context, userCred mcc } } for i := 0; i < len(added); i += 1 { - _, err := manager.newFromCloudSnapshot(userCred, added[i], region) + _, err := manager.newFromCloudSnapshot(userCred, added[i], region, provider) if err != nil { syncResult.AddError(err) } else { diff --git a/pkg/compute/tasks/guest_disk_snapshot_task.go b/pkg/compute/tasks/guest_disk_snapshot_task.go index 9c9e7aa7ea..4f5f230ef2 100644 --- a/pkg/compute/tasks/guest_disk_snapshot_task.go +++ b/pkg/compute/tasks/guest_disk_snapshot_task.go @@ -52,32 +52,25 @@ func (self *GuestDiskSnapshotTask) DoDiskSnapshot(ctx context.Context, guest *mo func (self *GuestDiskSnapshotTask) OnDiskSnapshotComplete(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) { res := data.(*jsonutils.JSONDict) + snapshotId, _ := self.Params.GetString("snapshot_id") + iSnapshot, _ := models.SnapshotManager.FetchById(snapshotId) + snapshot := iSnapshot.(*models.SSnapshot) if guest.Hypervisor == models.HYPERVISOR_KVM { location, err := res.GetString("location") if err != nil { log.Infof("OnDiskSnapshotComplete called with data no location") return } - snapshotId, _ := self.Params.GetString("snapshot_id") - iSnapshot, _ := models.SnapshotManager.FetchById(snapshotId) - snapshot := iSnapshot.(*models.SSnapshot) models.SnapshotManager.TableSpec().Update(snapshot, func() error { snapshot.Location = location snapshot.Status = models.SNAPSHOT_READY return nil }) } else { - snapshotId, _ := self.Params.GetString("snapshot_id") - iSnapshot, _ := models.SnapshotManager.FetchById(snapshotId) - snapshot := iSnapshot.(*models.SSnapshot) extSnapshotId, _ := data.GetString("snapshot_id") - cloudregionId, _ := data.GetString("cloudregion_id") - managerId, _ := data.GetString("manager_id") models.SnapshotManager.TableSpec().Update(snapshot, func() error { - snapshot.CloudregionId = cloudregionId snapshot.ExternalId = extSnapshotId snapshot.Status = models.SNAPSHOT_READY - snapshot.ManagerId = managerId return nil }) } diff --git a/pkg/util/aliyun/snapshot.go b/pkg/util/aliyun/snapshot.go index 515115e8fb..87d9523970 100644 --- a/pkg/util/aliyun/snapshot.go +++ b/pkg/util/aliyun/snapshot.go @@ -51,14 +51,6 @@ func (self *SSnapshot) GetStatus() string { } } -func (self *SSnapshot) GetManagerId() string { - return self.region.client.providerId -} - -func (self *SSnapshot) GetRegionId() string { - return self.region.GetGlobalId() -} - func (self *SSnapshot) GetSize() int32 { return self.SourceDiskSize } diff --git a/pkg/util/azure/snapshot.go b/pkg/util/azure/snapshot.go index 8804fb7832..ccc78658bd 100644 --- a/pkg/util/azure/snapshot.go +++ b/pkg/util/azure/snapshot.go @@ -168,14 +168,6 @@ func (self *SSnapshot) GetDiskId() string { return self.Properties.CreationData.SourceResourceID } -func (self *SSnapshot) GetManagerId() string { - return self.region.client.providerId -} - -func (self *SSnapshot) GetRegionId() string { - return self.region.GetGlobalId() -} - func (self *SSnapshot) GetDiskType() string { return "" } From 54dda9496ffdefc5f331d1b0d8bd3c203542583b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Fri, 16 Nov 2018 20:42:30 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E9=87=8D=E5=A4=8D=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=B7=B2=E5=88=86=E9=85=8D=E7=9A=84=E6=9C=AA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E7=BD=91=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/azure/host.go | 52 +++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/pkg/util/azure/host.go b/pkg/util/azure/host.go index 089e714fd3..a32a5ca6df 100644 --- a/pkg/util/azure/host.go +++ b/pkg/util/azure/host.go @@ -42,18 +42,48 @@ func (self *SHost) Refresh() error { return nil } -func (self *SHost) CreateVM(name string, imgId string, sysDiskSize int, cpu int, memMB int, networkId string, ipAddr string, desc string, passwd string, storageType string, diskSizes []int, publicKey string, secgroupId string) (cloudprovider.ICloudVM, error) { - nicId := "" - if net := self.zone.getNetworkById(networkId); net == nil { - return nil, fmt.Errorf("invalid network ID %s", networkId) - } else if nic, err := self.zone.region.CreateNetworkInterface(fmt.Sprintf("%s-ipconfig", name), ipAddr, net.GetId(), secgroupId); err != nil { - return nil, err - } else { - nicId = nic.ID - } - vmId, err := self._createVM(name, imgId, int32(sysDiskSize), cpu, memMB, nicId, ipAddr, desc, passwd, storageType, diskSizes, publicKey) +func (self *SHost) searchNetorkInterface(IPAddr string, networkId string, secgroupId string) (*SInstanceNic, error) { + interfaces, err := self.zone.region.GetNetworkInterfaces() if err != nil { - self.zone.region.DeleteNetworkInterface(nicId) + return nil, err + } + for i, nic := range interfaces { + for _, ipConf := range nic.Properties.IPConfigurations { + if ipConf.Properties.PrivateIPAddress == IPAddr && networkId == ipConf.Properties.Subnet.ID && ipConf.Properties.PrivateIPAllocationMethod == "Static" { + if nic.Properties.NetworkSecurityGroup == nil || nic.Properties.NetworkSecurityGroup.ID != secgroupId { + nic.Properties.NetworkSecurityGroup = &SSecurityGroup{ID: secgroupId} + if err := self.zone.region.client.Update(jsonutils.Marshal(nic), nil); err != nil { + log.Errorf("assign secgroup %s for nic %s failed %d") + return nil, err + } + } + return &interfaces[i], nil + } + } + } + return nil, cloudprovider.ErrNotFound +} + +func (self *SHost) CreateVM(name string, imgId string, sysDiskSize int, cpu int, memMB int, networkId string, ipAddr string, desc string, passwd string, storageType string, diskSizes []int, publicKey string, secgroupId string) (cloudprovider.ICloudVM, error) { + net := self.zone.getNetworkById(networkId) + if net == nil { + return nil, fmt.Errorf("invalid network ID %s", networkId) + } + nic, err := self.searchNetorkInterface(ipAddr, net.GetId(), secgroupId) + if err != nil { + if err == cloudprovider.ErrNotFound { + nic, err = self.zone.region.CreateNetworkInterface(fmt.Sprintf("%s-ipconfig", name), ipAddr, net.GetId(), secgroupId) + if err != nil { + return nil, err + } + } else { + return nil, err + } + } + + vmId, err := self._createVM(name, imgId, int32(sysDiskSize), cpu, memMB, nic.ID, ipAddr, desc, passwd, storageType, diskSizes, publicKey) + if err != nil { + self.zone.region.DeleteNetworkInterface(nic.ID) return nil, err } if vm, err := self.zone.region.GetInstance(vmId); err != nil {