Skip to content

Commit

Permalink
Add createSubnet() func. and Apply it to CreateVPC() and AddSubnet()
Browse files Browse the repository at this point in the history
  • Loading branch information
innodreamer committed Aug 12, 2024
1 parent a1f001d commit c76fd67
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ func (vpcHandler *KTVpcVPCHandler) CreateVPC(vpcReqInfo irs.VPCReqInfo) (irs.VPC

// Create the Requested Subnets
for _, subnetReqInfo := range vpcReqInfo.SubnetInfoList {
_, err := vpcHandler.AddSubnet(irs.IID{SystemId: vpcList[0].ID}, subnetReqInfo)
_, err := vpcHandler.createSubnet(&subnetReqInfo)
if err != nil {
newErr := fmt.Errorf("Failed to Create New Subnet : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr // Caution!!) D1 Platform Abnormal Error
return irs.VPCInfo{}, newErr
}
}

Expand Down Expand Up @@ -265,68 +265,26 @@ func (vpcHandler *KTVpcVPCHandler) AddSubnet(vpcIID irs.IID, subnetReqInfo irs.S
cblogger.Info("KT Cloud VPC driver: called AddSubnet()!!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, subnetReqInfo.IId.NameId, "AddSubnet()")

if subnetReqInfo.IId.NameId == "" {
newErr := fmt.Errorf("Invalid Sunbet NameId!!")
if strings.EqualFold(vpcIID.SystemId, "") {
newErr := fmt.Errorf("Invalid VPC ID!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}

// KT Cloud D1 platform API guide - Tier : https://cloud.kt.com/docs/open-api-guide/d/computing/tier
cidrBlock := strings.Split(subnetReqInfo.IPv4_CIDR, ".")
vmStartIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "6"
vmEndIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "180"
lbStartIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "181"
lbEndIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "199"
bmStartIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "201"
bmEndIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "250"
gatewayIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "1"

detailTierInfo := subnets.DetailInfo {
CIDR: subnetReqInfo.IPv4_CIDR,
StartIP: vmStartIP, // For VM
EndIP: vmEndIP,
LBStartIP: lbStartIP, // For NLB
LBEndIP: lbEndIP,
BMStartIP: bmStartIP, // For BareMetal Machine
BMEndIP: bmEndIP,
Gateway: gatewayIP,
if subnetReqInfo.IId.NameId == "" {
newErr := fmt.Errorf("Invalid Sunbet NameId!!")
cblogger.Error(newErr.Error())
return irs.VPCInfo{}, newErr
}

// Create Subnet
createOpts := subnets.CreateOpts{
Name: subnetReqInfo.IId.NameId, // Required
Zone: vpcHandler.RegionInfo.Zone, // Required
Type: "tier", // Required
UserCustom: "y", // Required
Detail: detailTierInfo,
}
// cblogger.Info("\n### Subnet createOpts : ")
// spew.Dump(createOpts)
// cblogger.Info("\n")

cblogger.Info("\n### Adding New Subnet Now!!")
start := call.Start()
_, err := subnets.Create(vpcHandler.NetworkClient, createOpts).Extract()
// subnet, err := subnets.Create(vpcHandler.NetworkClient, createOpts).Extract()
_, err := vpcHandler.createSubnet(&subnetReqInfo)
if err != nil {
if !strings.Contains(err.Error(), ":true") { // Cauton!! : Abnormal Error when creating a subnet on D1 Platform
newErr := fmt.Errorf("Failed to Add the Subnet on KT Coud : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
} else {
cblogger.Info("\n### Waiting for Adding the Subnet!!")
time.Sleep(time.Second * 20)

// cblogger.Infof("Succeeded in Adding the Subnet : [%s]", subnet.ID) // To prevent 'panic: runtime error', maded this line as a comment.
newErr := fmt.Errorf("Failed to Add New Subnet : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return irs.VPCInfo{}, newErr
}
loggingInfo(callLogInfo, start)

// subnetInfo := vpcHandler.setterSubnet(*subnet)
// return *subnetInfo, nil

vpcInfo, err := vpcHandler.GetVPC(irs.IID{SystemId: vpcIID.SystemId})
if err != nil {
newErr := fmt.Errorf("Failed to Get the VPC Info!! : [%v] ", err)
Expand Down Expand Up @@ -393,7 +351,7 @@ func (vpcHandler *KTVpcVPCHandler) mappingVpcInfo(nvpc *networks.Network) (*irs.
if !strings.EqualFold(subnet.Name, "Private_Sub") && !strings.EqualFold(subnet.Name, "DMZ_Sub") && !strings.EqualFold(subnet.Name, "external"){
// # When apply filtering

cblogger.Info("# Subnet Name : [%s]", subnet.Name)
// cblogger.Infof("# Subnet Name : [%s]", subnet.Name)
// if strings.EqualFold(subnet.Name, "NLB-SUBNET_Sub"){ // Note) '_Sub' is automatically appended to the original subnet name

subnetInfo := vpcHandler.mappingSubnetInfo(subnet)
Expand Down Expand Up @@ -433,6 +391,75 @@ func (vpcHandler *KTVpcVPCHandler) mappingSubnetInfo(subnet subnets.Subnet) *irs
return &subnetInfo
}

// Create New Subnet (Tire) and Return OS Network ID
func (vpcHandler *KTVpcVPCHandler) createSubnet(subnetReqInfo *irs.SubnetInfo) (string, error) {
cblogger.Info("KT Cloud VPC driver: called createSubnet()!!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, subnetReqInfo.IId.NameId, "createSubnet()")

if subnetReqInfo.IId.NameId == "" {
newErr := fmt.Errorf("Invalid Sunbet NameId!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
}

// KT Cloud D1 platform API guide - Tier : https://cloud.kt.com/docs/open-api-guide/d/computing/tier
cidrBlock := strings.Split(subnetReqInfo.IPv4_CIDR, ".")
vmStartIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "6"
vmEndIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "180"
lbStartIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "181"
lbEndIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "199"
bmStartIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "201"
bmEndIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "250"
gatewayIP := cidrBlock[0] + "." + cidrBlock[1] + "." + cidrBlock[2] + "." + "1"

detailTierInfo := subnets.DetailInfo {
CIDR: subnetReqInfo.IPv4_CIDR,
StartIP: vmStartIP, // For VM
EndIP: vmEndIP,
LBStartIP: lbStartIP, // For NLB
LBEndIP: lbEndIP,
BMStartIP: bmStartIP, // For BareMetal Machine
BMEndIP: bmEndIP,
Gateway: gatewayIP,
}

// Create Subnet
createOpts := subnets.CreateOpts{
Name: subnetReqInfo.IId.NameId, // Required
Zone: vpcHandler.RegionInfo.Zone, // Required
Type: "tier", // Required
UserCustom: "y", // Required
Detail: detailTierInfo,
}
// cblogger.Info("\n### Subnet createOpts : ")
// spew.Dump(createOpts)
// cblogger.Info("\n")

cblogger.Info("\n### Adding New Subnet Now!!")
start := call.Start()
result, err := subnets.Create(vpcHandler.NetworkClient, createOpts).ExtractCreateInfo()
if err != nil {
if !strings.Contains(err.Error(), ":true") {
newErr := fmt.Errorf("Failed to Create the Subnet : [%v]", err)
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
}
} else if strings.EqualFold(result.NetworkID, "") {
newErr := fmt.Errorf("Failed to Create the Subnet!!")
cblogger.Error(newErr.Error())
loggingError(callLogInfo, newErr)
return "", newErr
} else {
cblogger.Info("\n### Waiting for Creating the Subnet!!")
time.Sleep(time.Second * 20)
}
loggingInfo(callLogInfo, start)

return result.NetworkID, nil
}

func (vpcHandler *KTVpcVPCHandler) getKtCloudVpc(vpcId string) (*networks.Network, error) {
cblogger.Info("KT Cloud VPC Driver: called getKtCloudVpc()!")
callLogInfo := getCallLogScheme(vpcHandler.RegionInfo.Zone, call.VPCSUBNET, vpcId, "getKtCloudVpc()")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require (
github.com/alibabacloud-go/tea v1.2.2
github.com/alibabacloud-go/vpc-20160428/v6 v6.4.0
github.com/cloud-barista/ktcloud-sdk-go v0.2.1-0.20240123114820-8684cfc5deeb
github.com/cloud-barista/ktcloudvpc-sdk-go v0.0.0-20240524035950-e31ad87778e0
github.com/cloud-barista/ktcloudvpc-sdk-go v0.0.0-20240812121947-01daa5e57030
github.com/cloud-barista/nhncloud-sdk-go v0.0.0-20240709053216-536bc37fa6c9
github.com/go-openapi/strfmt v0.21.3
github.com/hashicorp/go-version v1.6.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ github.com/cloud-barista/cb-log v0.8.2 h1:hPCbLj6TW6m9UWlq002sDuGgxKFVp68w4V3k49
github.com/cloud-barista/cb-log v0.8.2/go.mod h1:nGgfTFMPwl1MpCO3FBjexUkNdOYA0BNJoyM9Pd0lMms=
github.com/cloud-barista/ktcloud-sdk-go v0.2.1-0.20240123114820-8684cfc5deeb h1:JAQQ/wBdkcoGCeim+J84F8hIuxbtn/XTe/iDYRsr+t0=
github.com/cloud-barista/ktcloud-sdk-go v0.2.1-0.20240123114820-8684cfc5deeb/go.mod h1:ZIrUxItUvMIGZyX6Re7tUdcS3cwBIzBPcL+Pk/6lt/8=
github.com/cloud-barista/ktcloudvpc-sdk-go v0.0.0-20240524035950-e31ad87778e0 h1:mnQnZHMwt8V/WihpgnIDcqK9fsLE8A+uTdVSGdPaQ+Q=
github.com/cloud-barista/ktcloudvpc-sdk-go v0.0.0-20240524035950-e31ad87778e0/go.mod h1:SDTNI+0ZyoEWhQKHASGwhS1a9kad+mxsXCKSfnpWnSA=
github.com/cloud-barista/ktcloudvpc-sdk-go v0.0.0-20240812121947-01daa5e57030 h1:KZSxMftDrIKpk0+qW0rt3UX/e2QsHolvesazvl2av4s=
github.com/cloud-barista/ktcloudvpc-sdk-go v0.0.0-20240812121947-01daa5e57030/go.mod h1:SDTNI+0ZyoEWhQKHASGwhS1a9kad+mxsXCKSfnpWnSA=
github.com/cloud-barista/nhncloud-sdk-go v0.0.0-20240709053216-536bc37fa6c9 h1:br7+CBDJEXofGPWUj79sldxuvrj4GNktmF6T8zVrVOk=
github.com/cloud-barista/nhncloud-sdk-go v0.0.0-20240709053216-536bc37fa6c9/go.mod h1:G+VjfmHi5IUt0oe14A5UXmbrGFlMTMQjLonBuvjy8Yc=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand Down

0 comments on commit c76fd67

Please sign in to comment.