Skip to content

Commit

Permalink
Merge pull request cloud-barista#1238 from ish-hcc/tag_openstack
Browse files Browse the repository at this point in the history
OpenStack:  Add tagging feature for supported resources
  • Loading branch information
powerkimhub authored Jul 12, 2024
2 parents 493a9ab + 1e84512 commit 83ff13b
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (OpenStackDriver) GetDriverCapability() idrv.DriverCapabilityInfo {
drvCapabilityInfo.MyImageHandler = true
drvCapabilityInfo.RegionZoneHandler = true
drvCapabilityInfo.PriceInfoHandler = false
drvCapabilityInfo.TagHandler = true

return drvCapabilityInfo
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,25 @@ func (cloudConn *OpenStackCloudConnection) CreateImageHandler() (irs.ImageHandle

func (cloudConn *OpenStackCloudConnection) CreateVPCHandler() (irs.VPCHandler, error) {
cblogger.Info("OpenStack Cloud Driver: called CreateVPCHandler()!")
vpcHandler := osrs.OpenStackVPCHandler{Client: cloudConn.NetworkClient, VMClient: cloudConn.ComputeClient}
vpcHandler := osrs.OpenStackVPCHandler{
CredentialInfo: cloudConn.CredentialInfo,
IdentityClient: cloudConn.IdentityClient,
ComputeClient: cloudConn.ComputeClient,
NetworkClient: cloudConn.NetworkClient,
NLBClient: cloudConn.NLBClient,
}
return &vpcHandler, nil
}

func (cloudConn OpenStackCloudConnection) CreateSecurityHandler() (irs.SecurityHandler, error) {
cblogger.Info("OpenStack Cloud Driver: called CreateSecurityHandler()!")
securityHandler := osrs.OpenStackSecurityHandler{Client: cloudConn.ComputeClient, NetworkClient: cloudConn.NetworkClient}
securityHandler := osrs.OpenStackSecurityHandler{
CredentialInfo: cloudConn.CredentialInfo,
IdentityClient: cloudConn.IdentityClient,
ComputeClient: cloudConn.ComputeClient,
NetworkClient: cloudConn.NetworkClient,
NLBClient: cloudConn.NLBClient,
}
return &securityHandler, nil
}

Expand All @@ -68,7 +80,15 @@ func (cloudConn *OpenStackCloudConnection) CreateKeyPairHandler() (irs.KeyPairHa

func (cloudConn *OpenStackCloudConnection) CreateVMHandler() (irs.VMHandler, error) {
cblogger.Info("OpenStack Cloud Driver: called CreateVMHandler()!")
vmHandler := osrs.OpenStackVMHandler{Region: cloudConn.Region, ComputeClient: cloudConn.ComputeClient, NetworkClient: cloudConn.NetworkClient, VolumeClient: cloudConn.Volume3Client}
vmHandler := osrs.OpenStackVMHandler{
Region: cloudConn.Region,
CredentialInfo: cloudConn.CredentialInfo,
IdentityClient: cloudConn.IdentityClient,
ComputeClient: cloudConn.ComputeClient,
NetworkClient: cloudConn.NetworkClient,
NLBClient: cloudConn.NLBClient,
VolumeClient: cloudConn.Volume3Client,
}
if vmHandler.VolumeClient == nil {
vmHandler.VolumeClient = cloudConn.Volume2Client
}
Expand All @@ -83,7 +103,14 @@ func (cloudConn *OpenStackCloudConnection) CreateVMSpecHandler() (irs.VMSpecHand

func (cloudConn *OpenStackCloudConnection) CreateNLBHandler() (irs.NLBHandler, error) {
cblogger.Info("OpenStack Cloud Driver: called CreateNLBHandler()!")
nlbHandler := osrs.OpenStackNLBHandler{CredentialInfo: cloudConn.CredentialInfo, Region: cloudConn.Region, VMClient: cloudConn.ComputeClient, NetworkClient: cloudConn.NetworkClient, NLBClient: cloudConn.NLBClient}
nlbHandler := osrs.OpenStackNLBHandler{
CredentialInfo: cloudConn.CredentialInfo,
Region: cloudConn.Region,
IdentityClient: cloudConn.IdentityClient,
ComputeClient: cloudConn.ComputeClient,
NetworkClient: cloudConn.NetworkClient,
NLBClient: cloudConn.NLBClient,
}
return &nlbHandler, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,18 @@ Loop:
fmt.Println("Finish GetSecurity()")
case 3:
fmt.Println("Start CreateSecurity() ...")
var tags []irs.KeyValue
for _, tag := range config.Openstack.Resources.Security.Tags {
tags = append(tags, irs.KeyValue{
Key: tag.Key,
Value: tag.Value,
})
}
reqInfo := irs.SecurityReqInfo{
IId: securityIId,
SecurityRules: &securityRulesInfos,
VpcIID: targetVPCIId,
TagList: tags,
}
security, err := securityHandler.CreateSecurity(reqInfo)
if err != nil {
Expand Down Expand Up @@ -403,19 +411,35 @@ func testVPCHandler(config Config) {
subnetLists := config.Openstack.Resources.VPC.Subnets
var subnetInfoList []irs.SubnetInfo
for _, sb := range subnetLists {
var tags []irs.KeyValue
for _, tag := range sb.Tags {
tags = append(tags, irs.KeyValue{
Key: tag.Key,
Value: tag.Value,
})
}
info := irs.SubnetInfo{
IId: irs.IID{
NameId: sb.NameId,
},
IPv4_CIDR: sb.IPv4CIDR,
TagList: tags,
}
subnetInfoList = append(subnetInfoList, info)
}
var tags []irs.KeyValue
for _, tag := range config.Openstack.Resources.VPC.Tags {
tags = append(tags, irs.KeyValue{
Key: tag.Key,
Value: tag.Value,
})
}

VPCReqInfo := irs.VPCReqInfo{
IId: vpcIID,
IPv4_CIDR: config.Openstack.Resources.VPC.IPv4CIDR,
SubnetInfoList: subnetInfoList,
TagList: tags,
}
addSubnet := config.Openstack.Resources.VPC.AddSubnet
addSubnetInfo := irs.SubnetInfo{
Expand Down Expand Up @@ -546,6 +570,13 @@ func testVMHandler(config Config) {
if config.Openstack.Resources.Vm.ImageType == "MyImage" {
imageType = irs.MyImage
}
var tags []irs.KeyValue
for _, tag := range config.Openstack.Resources.Vm.Tags {
tags = append(tags, irs.KeyValue{
Key: tag.Key,
Value: tag.Value,
})
}
vmReqInfo := irs.VMReqInfo{
IId: irs.IID{
NameId: config.Openstack.Resources.Vm.IID.NameId,
Expand All @@ -565,13 +596,15 @@ func testVMHandler(config Config) {
},
VMSpecName: config.Openstack.Resources.Vm.VmSpecName,
KeyPairIID: irs.IID{
NameId: config.Openstack.Resources.Vm.KeyPairIID.NameId,
NameId: config.Openstack.Resources.Vm.KeyPairIID.NameId,
SystemId: config.Openstack.Resources.Vm.KeyPairIID.SystemId,
},
RootDiskSize: config.Openstack.Resources.Vm.RootDiskSize,
RootDiskType: config.Openstack.Resources.Vm.RootDiskType,
SecurityGroupIIDs: SecurityGroupIIDs,
VMUserId: config.Openstack.Resources.Vm.VMUserId,
VMUserPasswd: config.Openstack.Resources.Vm.VMUserPasswd,
TagList: tags,
}

Loop:
Expand Down Expand Up @@ -780,6 +813,9 @@ func testNLBHandler(config Config) {
Timeout: 4,
Threshold: 3,
},
TagList: []irs.KeyValue{
{Key: "nlb-tag-key", Value: "nlb-tag-value"},
},
}
updateListener := irs.ListenerInfo{
Protocol: "TCP",
Expand Down Expand Up @@ -1552,6 +1588,10 @@ type Config struct {
CIDR string `yaml:"CIDR"`
Direction string `yaml:"Direction"`
} `yaml:"removeRules"`
Tags []struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
} `yaml:"tags"`
} `yaml:"security"`
KeyPair struct {
NameId string `yaml:"nameId"`
Expand All @@ -1568,11 +1608,19 @@ type Config struct {
Subnets []struct {
NameId string `yaml:"nameId"`
IPv4CIDR string `yaml:"ipv4CIDR"`
Tags []struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
} `yaml:"tags"`
} `yaml:"subnets"`
AddSubnet struct {
NameId string `yaml:"nameId"`
IPv4CIDR string `yaml:"ipv4CIDR"`
} `yaml:"addSubnet"`
Tags []struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
} `yaml:"tags"`
} `yaml:"vpc"`
Vm struct {
IID struct {
Expand All @@ -1586,7 +1634,8 @@ type Config struct {
ImageType string `yaml:"ImageType"`
VmSpecName string `yaml:"VmSpecName"`
KeyPairIID struct {
NameId string `yaml:"nameId"`
NameId string `yaml:"nameId"`
SystemId string `yaml:"systemId"`
} `yaml:"KeyPairIID"`
VpcIID struct {
NameId string `yaml:"nameId"`
Expand All @@ -1604,6 +1653,10 @@ type Config struct {
RootDiskType string `yaml:"RootDiskType"`
VMUserId string `yaml:"VMUserId"`
VMUserPasswd string `yaml:"VMUserPasswd"`
Tags []struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
} `yaml:"tags"`
} `yaml:"vm"`
} `yaml:"resources"`
} `yaml:"openstack"`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
openstack:
identity_endpoint: TBD
project_id: TBD
project_id: TBD
domain_name: TBD
username: TBD
password: TBD
Expand Down Expand Up @@ -33,24 +33,36 @@ openstack:
IPProtocol: "tcp"
CIDR: "0.0.0.0/0"
Direction: "inbound"
tags:
- key: sg-tag-key
value: sg-tag-value
keyPair:
nameId: mcb-test-key
systemId:
vmSpec:
nameId: m1.small
systemId:
vpc:
nameId: mcb-test-vpc
nameId: nlb-tester-vpc
systemId:
ipv4CIDR: 180.0.0.0/16
subnets:
- nameId: mcb-test-vpc-subnet1
ipv4CIDR: 180.0.40.0/24
tags:
- key: subnet1-tag-key
value: subnet1-tag-value
- nameId: mcb-test-vpc-subnet2
ipv4CIDR: 180.0.30.0/24
tags:
- key: subnet2-tag-key
value: subnet2-tag-value
addSubnet:
nameId: mcb-test-vpc-subnet3
ipv4CIDR: 180.0.50.0/24
tags:
- key: vpc-tag-key
value: vpc-tag-value
vm:
IID:
nameId: mcb-test-vm
Expand All @@ -71,3 +83,6 @@ openstack:
SecurityGroupIIDs:
- nameId: mcb-test-security
systemId:
tags:
- key: vm-tag-key
value: vm-tag-value
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,25 @@ func CheckIIDValidation(IId irs.IID) bool {
}
return true
}

func tagsToKeyValue(tagString string) irs.KeyValue {
split := strings.Split(tagString, "=")

if len(split) == 2 {
return irs.KeyValue{
Key: split[0],
Value: split[1],
}
}

return irs.KeyValue{}
}

func returnTaggingError(errTags []irs.KeyValue, originalErrMsg string) error {
errMsg := "TaggingError: "
for _, tag := range errTags {
errMsg = errMsg + "{" + tag.Key + ", " + tag.Value + "}, "
}

return errors.New(errMsg[:len(errMsg)-2] + ": " + originalErrMsg)
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
type OpenStackNLBHandler struct {
CredentialInfo idrv.CredentialInfo
Region idrv.RegionInfo
VMClient *gophercloud.ServiceClient
IdentityClient *gophercloud.ServiceClient
ComputeClient *gophercloud.ServiceClient
NetworkClient *gophercloud.ServiceClient
NLBClient *gophercloud.ServiceClient
}
Expand Down Expand Up @@ -781,6 +782,12 @@ func (nlbHandler *OpenStackNLBHandler) ChangeHealthCheckerInfo(nlbIID irs.IID, h
}

func (nlbHandler *OpenStackNLBHandler) setterNLB(rawNLB loadbalancers.LoadBalancer) (irs.NLBInfo, error) {
var tags []irs.KeyValue

for _, tag := range rawNLB.Tags {
tags = append(tags, tagsToKeyValue(tag))
}

nlbInfo := irs.NLBInfo{
IId: irs.IID{
NameId: rawNLB.Name,
Expand All @@ -789,6 +796,7 @@ func (nlbHandler *OpenStackNLBHandler) setterNLB(rawNLB loadbalancers.LoadBalanc
Scope: string(NLBRegionType),
Type: string(NLBPublicType),
CreatedTime: rawNLB.CreatedAt,
TagList: tags,
}
vpcIId, err := nlbHandler.getVPCIID(rawNLB)
if err == nil {
Expand Down Expand Up @@ -867,7 +875,7 @@ func (nlbHandler *OpenStackNLBHandler) getRawVMByName(name string) (*servers.Ser
opts := servers.ListOpts{
Name: name,
}
pager, err := servers.List(nlbHandler.VMClient, opts).AllPages()
pager, err := servers.List(nlbHandler.ComputeClient, opts).AllPages()
if err != nil {
return nil, err
}
Expand All @@ -882,7 +890,7 @@ func (nlbHandler *OpenStackNLBHandler) getRawVMByIP(ip string) (*servers.Server,
opts := servers.ListOpts{
IP: ip,
}
pager, err := servers.List(nlbHandler.VMClient, opts).AllPages()
pager, err := servers.List(nlbHandler.ComputeClient, opts).AllPages()
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 83ff13b

Please sign in to comment.