From 8d5fc9ec2cfea3504fbbc4a4e8bc736bf09f1b89 Mon Sep 17 00:00:00 2001 From: TangBin Date: Mon, 15 Oct 2018 20:00:38 +0800 Subject: [PATCH] add sercurity rule methods --- pkg/util/aws/host.go | 34 ++++++++- pkg/util/aws/securitygroup.go | 131 ++++++++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 pkg/util/aws/securitygroup.go diff --git a/pkg/util/aws/host.go b/pkg/util/aws/host.go index 1142901036..08c7205914 100644 --- a/pkg/util/aws/host.go +++ b/pkg/util/aws/host.go @@ -180,8 +180,38 @@ func (self *SHost) _createVM(name, imgId string, sysDiskSize, cpu, memMB int, networkId, ipAddr, desc, passwd, storageType string, diskSizes []int, publicKey string, secgroupId string) (string, error) { // 网络配置及安全组绑定 - // todo:// https://www.guru99.com/creating-amazon-ec2-instance.html - self.zone.getNetworkById(networkId) + net := self.zone.getNetworkById(networkId) + if net == nil { + return "", fmt.Errorf("invalid network ID %s", networkId) + } + + if net.wire == nil { + log.Errorf("network's wire is empty") + return "", fmt.Errorf("network's wire is empty") + } + + if net.wire.vpc == nil { + log.Errorf("wire's vpc is empty") + return "", fmt.Errorf("wire's vpc is empty") + } + + if len(secgroupId) == 0 { + secgroups, err := net.wire.vpc.GetISecurityGroups() + if err != nil { + return "", fmt.Errorf("get security group error %s", err) + } + + if len(secgroups) == 0 { + secId, err := self.zone.region.createDefaultSecurityGroup(net.wire.vpc.VpcId) + if err != nil { + return "", fmt.Errorf("no secgroup for vpc and failed to create a default One!!") + } else { + secgroupId = secId + } + } else { + secgroupId = secgroups[0].GetId() + } + } // 同步keypair // 镜像及硬盘配置 diff --git a/pkg/util/aws/securitygroup.go b/pkg/util/aws/securitygroup.go new file mode 100644 index 0000000000..d8e706b9be --- /dev/null +++ b/pkg/util/aws/securitygroup.go @@ -0,0 +1,131 @@ +package aws + +import ( + "time" + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/util/secrules" + "github.com/aws/aws-sdk-go/service/ec2" +) + +type SecurityGroupPermissionNicType string + +const ( + IntranetNicType SecurityGroupPermissionNicType = "intranet" + InternetNicType SecurityGroupPermissionNicType = "internet" +) + +type SPermission struct { + CreateTime time.Time + Description string + DestCidrIp string + DestGroupId string + DestGroupName string + DestGroupOwnerAccount string + Direction string + IpProtocol string + NicType SecurityGroupPermissionNicType + Policy string + PortRange string + Priority int + SourceCidrIp string + SourceGroupId string + SourceGroupName string + SourceGroupOwnerAccount string +} + +type SPermissions struct { + Permission []SPermission +} + +type Tags struct { + Tag []Tag +} + +type Tag struct { + TagKey string + TagValue string +} + +type SSecurityGroup struct { + vpc *SVpc + CreationTime time.Time + Description string + SecurityGroupId string + SecurityGroupName string + VpcId string + InnerAccessPolicy string + Permissions SPermissions + RegionId string + Tags Tags +} + +func (self *SSecurityGroup) GetId() string { + panic("implement me") +} + +func (self *SSecurityGroup) GetName() string { + panic("implement me") +} + +func (self *SSecurityGroup) GetGlobalId() string { + panic("implement me") +} + +func (self *SSecurityGroup) GetStatus() string { + panic("implement me") +} + +func (self *SSecurityGroup) Refresh() error { + panic("implement me") +} + +func (self *SSecurityGroup) IsEmulated() bool { + panic("implement me") +} + +func (self *SSecurityGroup) GetMetadata() *jsonutils.JSONDict { + panic("implement me") +} + +func (self *SSecurityGroup) GetDescription() string { + panic("implement me") +} + +func (self *SSecurityGroup) GetRules() ([]secrules.SecurityRule, error) { + panic("implement me") +} + +func (self *SRegion) addSecurityGroupRules(secGrpId string, rule *secrules.SecurityRule) error { + // todo: add sercurity rules + return nil +} + +func (self *SRegion) addSecurityGroupRule(secGrpId string, rule *secrules.SecurityRule) error { + // todo: add sercurity rules + return nil +} + +func (self *SRegion) createSecurityGroup(vpcId string, name string, desc string) (string, error) { + params := &ec2.CreateSecurityGroupInput{} + params.SetVpcId(vpcId) + params.SetDescription(desc) + params.SetGroupName(name) + + group, err := self.ec2Client.CreateSecurityGroup(params) + if err != nil { + return "", err + } + + return *group.GroupId, nil +} + +func (self *SRegion) createDefaultSecurityGroup(vpcId string) (string, error) { + secId, err := self.createSecurityGroup(vpcId, "vpc default", "vpc default group") + if err != nil { + return "", err + } + // todo : add sercurity rules + return secId, nil +} + +