From 7d8f8822e252d704063861e70ac6710fef523e41 Mon Sep 17 00:00:00 2001 From: Yunkon Kim Date: Fri, 7 Jun 2024 18:41:09 +0900 Subject: [PATCH] Sync cloud models with CB-Tumblebug --- api/docs.go | 66 +++++++++---- api/swagger.json | 66 +++++++++---- api/swagger.yaml | 49 ++++++---- pkg/core/common/config.go | 31 ++++++ pkg/core/common/utility.go | 189 ++++++++++++++++++++++--------------- 5 files changed, 269 insertions(+), 132 deletions(-) diff --git a/api/docs.go b/api/docs.go index 04ce3f4..a617526 100644 --- a/api/docs.go +++ b/api/docs.go @@ -1076,40 +1076,32 @@ const docTemplate = `{ "configName": { "type": "string" }, + "credentialHolder": { + "type": "string" + }, "credentialName": { "type": "string" }, "driverName": { "type": "string" }, - "location": { - "$ref": "#/definitions/common.GeoLocation" - }, "providerName": { "type": "string" }, - "regionName": { - "type": "string" - } - } - }, - "common.GeoLocation": { - "type": "object", - "properties": { - "briefAddr": { - "type": "string" + "regionDetail": { + "$ref": "#/definitions/common.RegionDetail" }, - "cloudType": { - "type": "string" + "regionRepresentative": { + "type": "boolean" }, - "latitude": { - "type": "string" + "regionZoneInfo": { + "$ref": "#/definitions/common.RegionZoneInfo" }, - "longitude": { + "regionZoneInfoName": { "type": "string" }, - "nativeRegion": { - "type": "string" + "verified": { + "type": "boolean" } } }, @@ -1195,6 +1187,40 @@ const docTemplate = `{ } } }, + "common.RegionDetail": { + "type": "object", + "properties": { + "description": { + "type": "string" + }, + "location": { + "$ref": "#/definitions/common.Location" + }, + "regionId": { + "type": "string" + }, + "regionName": { + "type": "string" + }, + "zones": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "common.RegionZoneInfo": { + "type": "object", + "properties": { + "assignedRegion": { + "type": "string" + }, + "assignedZone": { + "type": "string" + } + } + }, "common.RestGetAllNsResponse": { "type": "object", "properties": { diff --git a/api/swagger.json b/api/swagger.json index 62cb40f..ba2dfad 100644 --- a/api/swagger.json +++ b/api/swagger.json @@ -1069,40 +1069,32 @@ "configName": { "type": "string" }, + "credentialHolder": { + "type": "string" + }, "credentialName": { "type": "string" }, "driverName": { "type": "string" }, - "location": { - "$ref": "#/definitions/common.GeoLocation" - }, "providerName": { "type": "string" }, - "regionName": { - "type": "string" - } - } - }, - "common.GeoLocation": { - "type": "object", - "properties": { - "briefAddr": { - "type": "string" + "regionDetail": { + "$ref": "#/definitions/common.RegionDetail" }, - "cloudType": { - "type": "string" + "regionRepresentative": { + "type": "boolean" }, - "latitude": { - "type": "string" + "regionZoneInfo": { + "$ref": "#/definitions/common.RegionZoneInfo" }, - "longitude": { + "regionZoneInfoName": { "type": "string" }, - "nativeRegion": { - "type": "string" + "verified": { + "type": "boolean" } } }, @@ -1188,6 +1180,40 @@ } } }, + "common.RegionDetail": { + "type": "object", + "properties": { + "description": { + "type": "string" + }, + "location": { + "$ref": "#/definitions/common.Location" + }, + "regionId": { + "type": "string" + }, + "regionName": { + "type": "string" + }, + "zones": { + "type": "array", + "items": { + "type": "string" + } + } + } + }, + "common.RegionZoneInfo": { + "type": "object", + "properties": { + "assignedRegion": { + "type": "string" + }, + "assignedZone": { + "type": "string" + } + } + }, "common.RestGetAllNsResponse": { "type": "object", "properties": { diff --git a/api/swagger.yaml b/api/swagger.yaml index b601027..a00d72d 100644 --- a/api/swagger.yaml +++ b/api/swagger.yaml @@ -276,29 +276,24 @@ definitions: properties: configName: type: string + credentialHolder: + type: string credentialName: type: string driverName: type: string - location: - $ref: '#/definitions/common.GeoLocation' providerName: type: string - regionName: - type: string - type: object - common.GeoLocation: - properties: - briefAddr: - type: string - cloudType: - type: string - latitude: - type: string - longitude: - type: string - nativeRegion: + regionDetail: + $ref: '#/definitions/common.RegionDetail' + regionRepresentative: + type: boolean + regionZoneInfo: + $ref: '#/definitions/common.RegionZoneInfo' + regionZoneInfoName: type: string + verified: + type: boolean type: object common.IID: properties: @@ -355,6 +350,28 @@ definitions: example: ns01 type: string type: object + common.RegionDetail: + properties: + description: + type: string + location: + $ref: '#/definitions/common.Location' + regionId: + type: string + regionName: + type: string + zones: + items: + type: string + type: array + type: object + common.RegionZoneInfo: + properties: + assignedRegion: + type: string + assignedZone: + type: string + type: object common.RestGetAllNsResponse: properties: ns: diff --git a/pkg/core/common/config.go b/pkg/core/common/config.go index a3a2464..d954dab 100644 --- a/pkg/core/common/config.go +++ b/pkg/core/common/config.go @@ -23,6 +23,28 @@ import ( cbstore_utils "github.com/cloud-barista/cb-store/utils" ) +// CloudInfo is structure for cloud information +type CloudInfo struct { + CSPs map[string]CSPDetail `mapstructure:"cloud" json:"csps"` +} + +// CSPDetail is structure for CSP information +type CSPDetail struct { + Description string `mapstructure:"description" json:"description"` + Driver string `mapstructure:"driver" json:"driver"` + Links []string `mapstructure:"link" json:"links"` + Regions map[string]RegionDetail `mapstructure:"region" json:"regions"` +} + +// RegionDetail is structure for region information +type RegionDetail struct { + RegionId string `mapstructure:"id" json:"regionId"` + RegionName string `mapstructure:"regionName" json:"regionName"` + Description string `mapstructure:"description" json:"description"` + Location Location `mapstructure:"location" json:"location"` + Zones []string `mapstructure:"zone" json:"zones"` +} + // Location is structure for location information type Location struct { Display string `mapstructure:"display" json:"display"` @@ -30,6 +52,15 @@ type Location struct { Longitude float64 `mapstructure:"longitude" json:"longitude"` } +// RuntimeCloudInfo is global variable for CloudInfo +var RuntimeCloudInfo = CloudInfo{} + +type Credential struct { + Credentialholder map[string]map[string]map[string]string `yaml:"credentialholder"` +} + +var RuntimeCredential = Credential{} + // RuntimeLatancyMap is global variable for LatancyMap var RuntimeLatancyMap = [][]string{} diff --git a/pkg/core/common/utility.go b/pkg/core/common/utility.go index 0b00272..d79a605 100644 --- a/pkg/core/common/utility.go +++ b/pkg/core/common/utility.go @@ -15,9 +15,7 @@ limitations under the License. package common import ( - "bufio" "math/rand" - "os" "regexp" "runtime" "strings" @@ -28,7 +26,6 @@ import ( "gopkg.in/yaml.v2" - "encoding/csv" "encoding/json" "fmt" ) @@ -217,7 +214,7 @@ func GenChildResourceKey(nsId string, resourceType string, parentResourceId stri } // mcirIds is struct for containing id and name of each MCIR type -type mcirIds struct { // Beetle +type mcirIds struct { // Tumblebug CspImageId string CspImageName string CspCustomImageId string @@ -308,99 +305,139 @@ func GetCspResourceId(nsId string, resourceType string, resourceId string) (stri } } -// ConnConfig is struct for containing a CB-Spider struct for connection config -type ConnConfig struct { // Spider +// ConnConfig is struct for containing modified CB-Spider struct for connection config +type ConnConfig struct { + ConfigName string `json:"configName"` + ProviderName string `json:"providerName"` + DriverName string `json:"driverName"` + CredentialName string `json:"credentialName"` + CredentialHolder string `json:"credentialHolder"` + RegionZoneInfoName string `json:"regionZoneInfoName"` + RegionZoneInfo RegionZoneInfo `json:"regionZoneInfo"` + RegionDetail RegionDetail `json:"regionDetail"` + RegionRepresentative bool `json:"regionRepresentative"` + Verified bool `json:"verified"` +} + +// SpiderConnConfig is struct for containing a CB-Spider struct for connection config +type SpiderConnConfig struct { ConfigName string ProviderName string 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"` +// CloudDriverInfo is struct for containing a CB-Spider struct for cloud driver info +type CloudDriverInfo struct { + DriverName string + ProviderName string + DriverLibFileName string } -// GetCloudLocation is to get location of clouds (need error handling) -func GetCloudLocation(cloudType string, nativeRegion string) GeoLocation { +// CredentialReq is struct for containing a struct for credential request +type CredentialReq struct { + CredentialHolder string `json:"credentialHolder"` + ProviderName string `json:"providerName"` + KeyValueInfoList []KeyValue `json:"keyValueInfoList"` +} - location := GeoLocation{} +// CredentialInfo is struct for containing a struct for credential info +type CredentialInfo struct { + CredentialName string `json:"credentialName"` + CredentialHolder string `json:"credentialHolder"` + ProviderName string `json:"providerName"` + KeyValueInfoList []KeyValue `json:"keyValueInfoList"` +} - if cloudType == "" || nativeRegion == "" { +// SpiderRegionZoneInfo is struct for containing region struct of CB-Spider +type SpiderRegionZoneInfo struct { + RegionName string // ex) "region01" + ProviderName string // ex) "GCP" + KeyValueInfoList []KeyValue // ex) { {region, us-east1}, {zone, us-east1-c} } + AvailableZoneList []string +} - // 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" +// RegionZoneInfo is struct for containing region struct +type RegionZoneInfo struct { + AssignedRegion string `json:"assignedRegion"` + AssignedZone string `json:"assignedZone"` +} - return location - } +// // GetCloudLocation is to get location of clouds (need error handling) +// func GetCloudLocation(cloudType string, nativeRegion string) GeoLocation { - key := "/cloudtype/" + cloudType + "/region/" + nativeRegion +// location := GeoLocation{} - //fmt.Printf("[GetCloudLocation] KEY: %+v\n", key) +// if cloudType == "" || nativeRegion == "" { - keyValue, err := CBStore.Get(key) +// // 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" - if err != nil { - CBLog.Error(err) - return location - } +// return location +// } - if keyValue == nil { - file, fileErr := os.Open("../assets/cloudlocation.csv") - defer file.Close() - if fileErr != nil { - CBLog.Error(fileErr) - return location - } +// key := "/cloudtype/" + cloudType + "/region/" + nativeRegion - 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 - } - } +// //fmt.Printf("[GetCloudLocation] KEY: %+v\n", key) - 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 - } - } +// keyValue, err := CBStore.Get(key) - return location -} +// 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 // func GetConnConfig(ConnConfigName string) (ConnConfig, error) {