Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature for connection with geolocation #936

Merged
merged 5 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 145 additions & 99 deletions assets/cloudconnection.csv

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions assets/cloudlocation.csv
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,31 @@ aws,us-west-1,California,37.3500,-121.9600
aws,us-west-2,Oregon,46.1500,-123.8800
aws,us-gov-east-1,AWS GovCloud (US-East),37.0000,-81.0000
aws,us-gov-west-1,AWS GovCloud (US),40.0000,-120.0000
gcp,asia-east1,Changhua County Taiwan,24.0756,120.5451
gcp,asia-east1,Changhua County Taiwan,24.0756,120.5451
gcp,asia-east2,Hong Kong,22.3964,114.1095
gcp,asia-northeast1,Tokyo Japan,35.6895,139.6917
gcp,asia-northeast2,Osaka Japan,34.6937,135.5022
gcp,asia-northeast3,Seoul South Korea,37.2000,127.0000
gcp,asia-south1,Mumbai India,19.0761,72.8774
gcp,asia-southeast1,Jurong West Singapore,1.3400,103.7041
gcp,asia-southeast2,Jakarta Indonesia,-6.2000,106.8160
gcp,australia-southeast1,Sydney Australia,-33.8651,151.2099
gcp,europe-north1,Hamina Finland,60.5700,27.2000
gcp,europe-west1,St. Ghislain Belgium,50.4482,3.8189
gcp,europe-west2,London England UK,51.5099,-0.1181
gcp,europe-west3,Frankfurt Germany,50.1109,8.6821
gcp,europe-west4,Eemshaven Netherlands,53.4423,6.8253
gcp,europe-west6,Zurich Switzerland,47.3667,8.5500
gcp,europe-central2,Warsaw Poland,52.237049,21.017532
gcp,northamerica-northeast1,Montreal Quebec Canada,45.5089,-73.5617
gcp,southamerica-east1,Osasco (Sao Paulo) Brazil,-23.5325,-46.7917
gcp,us-central1,Council Bluffs Iowa USA,41.2522,-95.8575
gcp,us-east1,Moncks Corner South Carolina USA,33.1913,-80.0040
gcp,us-east4,Ashburn Northern Virginia USA,39.0403,-77.4852
gcp,us-west1,The Dalles Oregon USA,45.5946,-121.1787
gcp,us-west2,Los Angeles California USA,34.0522,-118.2437
gcp,us-west3,Salt Lake City Utah USA,40.7587,-111.8762
gcp,us-west4,Las Vegas Nevada USA,36.1146,-115.1728
gcp,asia-northeast1,Tokyo Japan,35.6895,139.6917
gcp,asia-northeast2,Osaka Japan,34.6937,135.5022
gcp,asia-northeast3,Seoul South Korea,37.2000,127.0000
gcp,asia-south1,Mumbai India,19.0761,72.8774
gcp,asia-southeast1,Jurong West Singapore,1.3400,103.7041
gcp,asia-southeast2,Jakarta Indonesia,-6.2000,106.8160
gcp,australia-southeast1,Sydney Australia,-33.8651,151.2099
gcp,europe-north1,Hamina Finland,60.5700,27.2000
gcp,europe-west1,St. Ghislain Belgium,50.4482,3.8189
gcp,europe-west2,London England UK,51.5099,-0.1181
gcp,europe-west3,Frankfurt Germany,50.1109,8.6821
gcp,europe-west4,Eemshaven Netherlands,53.4423,6.8253
gcp,europe-west6,Zurich Switzerland,47.3667,8.5500
gcp,europe-central2,Warsaw Poland,52.237049,21.017532
gcp,northamerica-northeast1,Montreal Quebec Canada,45.5089,-73.5617
gcp,southamerica-east1,Osasco (Sao Paulo) Brazil,-23.5325,-46.7917
gcp,us-central1,Council Bluffs Iowa USA,41.2522,-95.8575
gcp,us-east1,Moncks Corner South Carolina USA,33.1913,-80.0040
gcp,us-east4,Ashburn Northern Virginia USA,39.0403,-77.4852
gcp,us-west1,The Dalles Oregon USA,45.5946,-121.1787
gcp,us-west2,Los Angeles California USA,34.0522,-118.2437
gcp,us-west3,Salt Lake City Utah USA,40.7587,-111.8762
gcp,us-west4,Las Vegas Nevada USA,36.1146,-115.1728
alibaba,cn-qingdao,China (Qingdao),36.3000,120.2200
alibaba,cn-beijing,China (Beijing),39.5427,116.2350
alibaba,cn-zhangjiakou,China (Zhangjiakou),40.4836,114.5245
Expand Down
59 changes: 30 additions & 29 deletions src/api/grpc/request/mcisapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
pb "github.com/cloud-barista/cb-tumblebug/src/api/grpc/protobuf/cbtumblebug"
"github.com/cloud-barista/cb-tumblebug/src/api/grpc/request/mcis"

common "github.com/cloud-barista/cb-tumblebug/src/core/common"
core_mcis "github.com/cloud-barista/cb-tumblebug/src/core/mcis"

"google.golang.org/grpc"
Expand Down Expand Up @@ -81,35 +82,35 @@ type TbVmGroupCreateRequest struct {

// TbVmInfo is for MCIS VM 구조 정의
type TbVmInfo struct {
Id string `yaml:"id" json:"id"`
Name string `yaml:"name" json:"name"`
VmGroupId string `yaml:"vmGroupId" json:"vmGroupId"`
Location core_mcis.GeoLocation `yaml:"location" json:"location"`
Status string `yaml:"status" json:"status"`
TargetStatus string `yaml:"targetStatus" json:"targetStatus"`
TargetAction string `yaml:"targetAction" json:"targetAction"`
MonAgentStatus string `yaml:"monAgentStatus" json:"monAgentStatus"`
SystemMessage string `yaml:"systemMessage" json:"systemMessage"`
CreatedTime string `yaml:"createdTime" json:"createdTime"`
Label string `yaml:"label" json:"label"`
Description string `yaml:"description" json:"description"`
Region core_mcis.RegionInfo `yaml:"region" json:"region"`
PublicIP string `yaml:"publicIP" json:"publicIP"`
SSHPort string `yaml:"sshPort" json:"sshPort"`
PublicDNS string `yaml:"publicDNS" json:"publicDNS"`
PrivateIP string `yaml:"privateIP" json:"privateIP"`
PrivateDNS string `yaml:"privateDNS" json:"privateDNS"`
VMBootDisk string `yaml:"vmBootDisk" json:"vmBootDisk"`
VMBlockDisk string `yaml:"vmBlockDisk" json:"vmBlockDisk"`
ConnectionName string `yaml:"connectionName" json:"connectionName"`
SpecId string `yaml:"specId" json:"specId"`
ImageId string `yaml:"imageId" json:"imageId"`
VNetId string `yaml:"vNetId" json:"vNetId"`
SubnetId string `yaml:"subnetId" json:"subnetId"`
SecurityGroupIds []string `yaml:"securityGroupIds" json:"securityGroupIds"`
SshKeyId string `yaml:"sshKeyId" json:"sshKeyId"`
VmUserAccount string `yaml:"vmUserAccount" json:"vmUserAccount"`
VmUserPassword string `yaml:"vmUserPassword" json:"vmUserPassword"`
Id string `yaml:"id" json:"id"`
Name string `yaml:"name" json:"name"`
VmGroupId string `yaml:"vmGroupId" json:"vmGroupId"`
Location common.GeoLocation `yaml:"location" json:"location"`
Status string `yaml:"status" json:"status"`
TargetStatus string `yaml:"targetStatus" json:"targetStatus"`
TargetAction string `yaml:"targetAction" json:"targetAction"`
MonAgentStatus string `yaml:"monAgentStatus" json:"monAgentStatus"`
SystemMessage string `yaml:"systemMessage" json:"systemMessage"`
CreatedTime string `yaml:"createdTime" json:"createdTime"`
Label string `yaml:"label" json:"label"`
Description string `yaml:"description" json:"description"`
Region core_mcis.RegionInfo `yaml:"region" json:"region"`
PublicIP string `yaml:"publicIP" json:"publicIP"`
SSHPort string `yaml:"sshPort" json:"sshPort"`
PublicDNS string `yaml:"publicDNS" json:"publicDNS"`
PrivateIP string `yaml:"privateIP" json:"privateIP"`
PrivateDNS string `yaml:"privateDNS" json:"privateDNS"`
VMBootDisk string `yaml:"vmBootDisk" json:"vmBootDisk"`
VMBlockDisk string `yaml:"vmBlockDisk" json:"vmBlockDisk"`
ConnectionName string `yaml:"connectionName" json:"connectionName"`
SpecId string `yaml:"specId" json:"specId"`
ImageId string `yaml:"imageId" json:"imageId"`
VNetId string `yaml:"vNetId" json:"vNetId"`
SubnetId string `yaml:"subnetId" json:"subnetId"`
SecurityGroupIds []string `yaml:"securityGroupIds" json:"securityGroupIds"`
SshKeyId string `yaml:"sshKeyId" json:"sshKeyId"`
VmUserAccount string `yaml:"vmUserAccount" json:"vmUserAccount"`
VmUserPassword string `yaml:"vmUserPassword" json:"vmUserPassword"`

// StartTime 필드가 공백일 경우 json 객체 복사할 때 time format parsing 에러 방지
// CspViewVmDetail SpiderVMInfo `yaml:"cspViewVmDetail" json:"cspViewVmDetail"`
Expand Down
156 changes: 156 additions & 0 deletions src/core/common/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
package common

import (
"bufio"
"math/rand"
"os"
"regexp"
Expand All @@ -30,6 +31,7 @@ import (
"google.golang.org/grpc/status"
"gopkg.in/yaml.v2"

"encoding/csv"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -253,6 +255,91 @@ type ConnConfig struct { // Spider
DriverName string
CredentialName string
RegionName string
Location GeoLocation
}

// GeoLocation is struct for geographical location
type GeoLocation struct {
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
BriefAddr string `json:"briefAddr"`
CloudType string `json:"cloudType"`
NativeRegion string `json:"nativeRegion"`
}

// GetCloudLocation is to get location of clouds (need error handling)
func GetCloudLocation(cloudType string, nativeRegion string) GeoLocation {

location := GeoLocation{}

if cloudType == "" || nativeRegion == "" {

// need error handling instead of assigning default value
location.CloudType = "ufc"
location.NativeRegion = "ufc"
location.BriefAddr = "South Korea (Seoul)"
location.Latitude = "37.4767"
location.Longitude = "126.8841"

return location
}

key := "/cloudtype/" + cloudType + "/region/" + nativeRegion

fmt.Printf("[GetCloudLocation] KEY: %+v\n", key)

keyValue, err := CBStore.Get(key)

if err != nil {
CBLog.Error(err)
return location
}

if keyValue == nil {
file, fileErr := os.Open("../assets/cloudlocation.csv")
defer file.Close()
if fileErr != nil {
CBLog.Error(fileErr)
return location
}

rdr := csv.NewReader(bufio.NewReader(file))
rows, _ := rdr.ReadAll()
for i, row := range rows {
keyLoc := "/cloudtype/" + rows[i][0] + "/region/" + rows[i][1]
location.CloudType = rows[i][0]
location.NativeRegion = rows[i][1]
location.BriefAddr = rows[i][2]
location.Latitude = rows[i][3]
location.Longitude = rows[i][4]
valLoc, _ := json.Marshal(location)
dbErr := CBStore.Put(keyLoc, string(valLoc))
if dbErr != nil {
CBLog.Error(dbErr)
return location
}
for j := range row {
fmt.Printf("%s ", rows[i][j])
}
fmt.Println()
}
keyValue, err = CBStore.Get(key)
if err != nil {
CBLog.Error(err)
return location
}
}

if keyValue != nil {
fmt.Printf("[GetCloudLocation] %+v %+v\n", keyValue.Key, keyValue.Value)
err = json.Unmarshal([]byte(keyValue.Value), &location)
if err != nil {
CBLog.Error(err)
return location
}
}

return location
}

// GetConnConfig is func to get connection config from CB-Spider
Expand Down Expand Up @@ -288,6 +375,18 @@ func GetConnConfig(ConnConfigName string) (ConnConfig, error) {
}

temp, _ := resp.Result().(*ConnConfig)

// Get geolocation
nativeRegion, err := GetNativeRegion(temp.ConfigName)
if err != nil {
CBLog.Error(err)
content := ConnConfig{}
return content, err
}

location := GetCloudLocation(strings.ToLower(temp.ProviderName), strings.ToLower(nativeRegion))
temp.Location = location

return *temp, nil

} else {
Expand Down Expand Up @@ -360,6 +459,20 @@ func GetConnConfigList() (ConnConfigList, error) {
}

temp, _ := resp.Result().(*ConnConfigList)

// Get geolocations
for i, connConfig := range temp.Connectionconfig {
nativeRegion, err := GetNativeRegion(connConfig.ConfigName)
if err != nil {
CBLog.Error(err)
content := ConnConfigList{}
return content, err
}

location := GetCloudLocation(strings.ToLower(connConfig.ProviderName), strings.ToLower(nativeRegion))
temp.Connectionconfig[i].Location = location
}

return *temp, nil

} else {
Expand Down Expand Up @@ -470,6 +583,49 @@ func GetRegion(RegionName string) (Region, error) {
}
}

// GetRegion is func to get NativRegion from file
func GetNativeRegion(connectionName string) (string, error) {
// Read default resources from file and create objects
// HEADER: ProviderName, CONN_CONFIG, RegionName, NativeRegionName, RegionLocation, DriverLibFileName, DriverName
file, fileErr := os.Open("../assets/cloudconnection.csv")
defer file.Close()
if fileErr != nil {
CBLog.Error(fileErr)
return "", fileErr
}

rdr := csv.NewReader(bufio.NewReader(file))
rows, err := rdr.ReadAll()
if err != nil {
CBLog.Error(err)
return "", err
}

nativeRegionName := ""

for _, row := range rows[1:] {
if connectionName != "" {
// find only given connectionName (if not skip)
if connectionName != row[1] {
continue
}
fmt.Println("Found a line for the connectionName from file: " + row[1])
}

if connectionName != "" {
// After finish handling line for the connectionName, break
if connectionName == row[1] {
nativeRegionName = row[3]
fmt.Println("Handled for the connectionName from file: " + row[1])
break
}
}

}
return nativeRegionName, nil

}

// RegionList is array struct for Region
type RegionList struct {
Region []Region `json:"region"`
Expand Down
2 changes: 1 addition & 1 deletion src/core/mcir/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,7 @@ func LoadDefaultResource(nsId string, resType string, connectionName string) err
}

// Read default resources from file and create objects
// HEADER: ProviderName, CONN_CONFIG, RegionName, RegionLocation, DriverLibFileName, DriverName
// HEADER: ProviderName, CONN_CONFIG, RegionName, NativeRegionName, RegionLocation, DriverLibFileName, DriverName
file, fileErr := os.Open("../assets/cloudconnection.csv")
defer file.Close()
if fileErr != nil {
Expand Down
2 changes: 1 addition & 1 deletion src/core/mcis/manageInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ type TbVmStatusInfo struct {
PrivateIp string `json:"privateIp"`
SSHPort string `json:"sshPort"`

Location GeoLocation `json:"location"`
Location common.GeoLocation `json:"location"`
}

// GetVmCurrentPublicIp is func to get VM public IP
Expand Down
Loading