From 396da0de612d33da8875dd223e2ec9a1d815ce18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 28 Nov 2018 16:04:22 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E7=A7=81=E7=BD=91IP=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E5=8F=8Astorage=E5=90=8D=E7=A7=B0=E7=BB=9F=E4=B8=80=E5=B0=8F?= =?UTF-8?q?=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/qcloud/instance.go | 4 ++++ pkg/util/qcloud/storage.go | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pkg/util/qcloud/instance.go b/pkg/util/qcloud/instance.go index 294ad1fef1..dc2e91547a 100644 --- a/pkg/util/qcloud/instance.go +++ b/pkg/util/qcloud/instance.go @@ -224,6 +224,10 @@ func (self *SInstance) GetINics() ([]cloudprovider.ICloudNic, error) { nic := SInstanceNic{instance: self, ipAddr: ip} nics = append(nics, &nic) } + for _, ip := range self.PrivateIpAddresses { + nic := SInstanceNic{instance: self, ipAddr: ip} + nics = append(nics, &nic) + } return nics, nil } diff --git a/pkg/util/qcloud/storage.go b/pkg/util/qcloud/storage.go index a9ebca1bc4..2e47490800 100644 --- a/pkg/util/qcloud/storage.go +++ b/pkg/util/qcloud/storage.go @@ -21,15 +21,15 @@ func (self *SStorage) GetMetadata() *jsonutils.JSONDict { } func (self *SStorage) GetId() string { - return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerId, self.zone.GetId(), self.storageType) + return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerId, self.zone.GetId(), strings.ToLower(self.storageType)) } func (self *SStorage) GetName() string { - return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerName, self.zone.GetId(), self.storageType) + return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerName, self.zone.GetId(), strings.ToLower(self.storageType)) } func (self *SStorage) GetGlobalId() string { - return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerId, self.zone.GetGlobalId(), self.storageType) + return fmt.Sprintf("%s-%s-%s", self.zone.region.client.providerId, self.zone.GetGlobalId(), strings.ToLower(self.storageType)) } func (self *SStorage) IsEmulated() bool { From 46a4cbd4a4c7351d9db48b841babc0e770beb168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 28 Nov 2018 16:47:04 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=85=B3=E8=81=94=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/qcloud/instance.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/util/qcloud/instance.go b/pkg/util/qcloud/instance.go index dc2e91547a..67d5d46062 100644 --- a/pkg/util/qcloud/instance.go +++ b/pkg/util/qcloud/instance.go @@ -140,6 +140,12 @@ func (self *SInstance) GetMetadata() *jsonutils.JSONDict { data.Add(jsonutils.NewString(self.image.OsName), "os_distribution") } + secgroupIds := jsonutils.NewArray() + for _, secgroupId := range self.SecurityGroupIds { + data.Add(jsonutils.NewString(secgroupId), "secgroupId") + secgroupIds.Add(jsonutils.NewString(secgroupId)) + } + data.Add(secgroupIds, "secgroupIds") return data } From 87a1cce5ebe85d9ea91376e88e1f520c22782a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 28 Nov 2018 16:50:20 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0disk=20hypervisor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/qcloud/disk.go | 8 +++----- pkg/util/qcloud/localdisk.go | 4 +++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/util/qcloud/disk.go b/pkg/util/qcloud/disk.go index 61377bc936..33884aaa78 100644 --- a/pkg/util/qcloud/disk.go +++ b/pkg/util/qcloud/disk.go @@ -65,16 +65,14 @@ func (v SDiskSet) Less(i, j int) bool { } func (self *SDisk) GetMetadata() *jsonutils.JSONDict { - // data := jsonutils.NewDict() + data := jsonutils.NewDict() // // The pricingInfo key structure is 'RegionId::DiskCategory::DiskType // priceKey := fmt.Sprintf("%s::%s::%s", self.RegionId, self.Category, self.Type) // data.Add(jsonutils.NewString(priceKey), "price_key") - // data.Add(jsonutils.NewString(models.HYPERVISOR_ALIYUN), "hypervisor") - - // return data - return nil + data.Add(jsonutils.NewString(models.HYPERVISOR_QCLOUD), "hypervisor") + return data } func (self *SRegion) GetDisks(instanceId string, zoneId string, category string, diskIds []string, offset int, limit int) ([]SDisk, int, error) { diff --git a/pkg/util/qcloud/localdisk.go b/pkg/util/qcloud/localdisk.go index ad91c8a764..864338ed89 100644 --- a/pkg/util/qcloud/localdisk.go +++ b/pkg/util/qcloud/localdisk.go @@ -17,7 +17,9 @@ type SLocalDisk struct { } func (self *SLocalDisk) GetMetadata() *jsonutils.JSONDict { - return nil + data := jsonutils.NewDict() + data.Add(jsonutils.NewString(models.HYPERVISOR_QCLOUD), "hypervisor") + return data } func (self *SLocalDisk) CreateISnapshot(name, desc string) (cloudprovider.ICloudSnapshot, error) { From 48974ebd489517873f8b9f71113da7e1682a20ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 28 Nov 2018 16:52:06 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=B5=8B=E5=80=BC?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/aws/instance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/util/aws/instance.go b/pkg/util/aws/instance.go index 44b7f7e96d..532f223443 100644 --- a/pkg/util/aws/instance.go +++ b/pkg/util/aws/instance.go @@ -269,7 +269,7 @@ func (self *SInstance) GetMachine() string { } func (self *SInstance) AssignSecurityGroup(secgroupId string) error { - return self.host.zone.region.assignSecurityGroup(self.InstanceId, secgroupId) + return self.host.zone.region.assignSecurityGroup(secgroupId, self.InstanceId) } func (self *SInstance) GetHypervisor() string { From fbc3fc5889cc93f96506555f9b4ea2d4ae8289b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 28 Nov 2018 18:43:09 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E8=85=BE=E8=AE=AF=E4=BA=91=E5=AE=89=E5=85=A8=E7=BB=84=E8=A7=84?= =?UTF-8?q?=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/util/qcloud/securitygroup.go | 64 +++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/pkg/util/qcloud/securitygroup.go b/pkg/util/qcloud/securitygroup.go index 870edae808..9e977adb8a 100644 --- a/pkg/util/qcloud/securitygroup.go +++ b/pkg/util/qcloud/securitygroup.go @@ -319,8 +319,70 @@ func (self *SRegion) SyncSecurityGroup(secgroupId string, vpcId string, name str return self.syncSecgroupRules(secgroupId, rules) } +func (self *SRegion) deleteAllRules(secgroupid string) error { + params := map[string]string{"SecurityGroupId": secgroupid, "SecurityGroupPolicySet.Version": "0"} + _, err := self.vpcRequest("ModifySecurityGroupPolicies", params) + return err +} + func (self *SRegion) syncSecgroupRules(secgroupid string, rules []secrules.SecurityRule) (string, error) { - return "", cloudprovider.ErrNotImplemented + if err := self.deleteAllRules(secgroupid); err != nil { + return "", err + } + egressIndex, ingressIndex := -1, -1 + for _, rule := range rules { + params := map[string]string{} + params["SecurityGroupId"] = secgroupid + policyIndex := 0 + direction := "Egress" + action := "accept" + if rule.Action == secrules.SecurityRuleDeny { + action = "drop" + } + protocol := "ALL" + if rule.Protocol != secrules.PROTO_ANY { + protocol = rule.Protocol + } + if rule.Direction == secrules.DIR_IN { + ingressIndex++ + policyIndex = ingressIndex + direction = "Ingress" + } else { + egressIndex++ + policyIndex = egressIndex + } + params[fmt.Sprintf("SecurityGroupPolicySet.%s.0.PolicyIndex", direction)] = fmt.Sprintf("%d", policyIndex) + params[fmt.Sprintf("SecurityGroupPolicySet.%s.0.Action", direction)] = action + params[fmt.Sprintf("SecurityGroupPolicySet.%s.0.PolicyDescription", direction)] = rule.Description + params[fmt.Sprintf("SecurityGroupPolicySet.%s.0.Protocol", direction)] = protocol + params[fmt.Sprintf("SecurityGroupPolicySet.%s.0.CidrBlock", direction)] = rule.IPNet.String() + if rule.Protocol == secrules.PROTO_TCP || rule.Protocol == secrules.PROTO_UDP { + port := "ALL" + if rule.PortEnd > 0 && rule.PortStart > 0 { + if rule.PortStart == rule.PortEnd { + port = fmt.Sprintf("%d", rule.PortStart) + } else { + port = fmt.Sprintf("%d-%d", rule.PortStart, rule.PortEnd) + } + } else if len(rule.Ports) > 0 { + ports := []string{} + for _, _port := range rule.Ports { + ports = append(ports, fmt.Sprintf("%d", _port)) + } + port = strings.Join(ports, ",") + } + params[fmt.Sprintf("SecurityGroupPolicySet.%s.0.Port", direction)] = port + } + //为什么不一次创建完成? + //答: 因为如果只有入方向安全组规则,创建时会提示缺少出方向规则。 + //为什么不分两次,一次创建入方向规则,一次创建出方向规则? + //答: 因为这样就不能设置优先级了,一次性创建的出或入方向的优先级必须一样。 + _, err := self.vpcRequest("CreateSecurityGroupPolicies", params) + if err != nil { + return "", err + } + } + return secgroupid, nil } func (self *SRegion) GetSecurityGroupDetails(secGroupId string) (*SSecurityGroup, error) {