From 89f06f2f1ffbb6c3c2f5d560a076ed5923ae4132 Mon Sep 17 00:00:00 2001 From: Qu Xuan Date: Mon, 21 Jun 2021 11:28:34 +0800 Subject: [PATCH] fix(region): add azure sql server --- pkg/apis/compute/dbinstance_const.go | 3 + pkg/compute/models/dbinstances.go | 1 + pkg/multicloud/azure/azure.go | 3 + pkg/multicloud/azure/dbinstance.go | 27 ++- pkg/multicloud/azure/dbinstance_database.go | 2 +- pkg/multicloud/azure/rds_managed_sqlserver.go | 172 +++++++++++++++++ .../azure/rds_managed_sqlserver_account.go | 40 ++++ .../azure/rds_managed_sqlserver_database.go | 91 +++++++++ pkg/multicloud/azure/rds_sqlserver.go | 182 ++++++++++++++++++ pkg/multicloud/azure/rds_sqlserver_account.go | 40 ++++ pkg/multicloud/azure/rds_sqlserver_backup.go | 120 ++++++++++++ .../azure/rds_sqlserver_database.go | 120 ++++++++++++ pkg/multicloud/azure/shell/dbinstance.go | 4 +- 13 files changed, 798 insertions(+), 7 deletions(-) create mode 100644 pkg/multicloud/azure/rds_managed_sqlserver.go create mode 100644 pkg/multicloud/azure/rds_managed_sqlserver_account.go create mode 100644 pkg/multicloud/azure/rds_managed_sqlserver_database.go create mode 100644 pkg/multicloud/azure/rds_sqlserver.go create mode 100644 pkg/multicloud/azure/rds_sqlserver_account.go create mode 100644 pkg/multicloud/azure/rds_sqlserver_backup.go create mode 100644 pkg/multicloud/azure/rds_sqlserver_database.go diff --git a/pkg/apis/compute/dbinstance_const.go b/pkg/apis/compute/dbinstance_const.go index 7fb2828402..7a4d04f5ab 100644 --- a/pkg/apis/compute/dbinstance_const.go +++ b/pkg/apis/compute/dbinstance_const.go @@ -123,6 +123,9 @@ const ( GOOGLE_DBINSTANCE_CATEGORY_REGIONAL = "Regional" // 高可用性(区域级) GOOGLE_DBINSTANCE_CATEGORY_ZONAL = "Zonal" // 单个地区 + // Azure + AZURE_DBINSTANCE_CATEGORY_BASIC = "basic" + //阿里云存储类型 ALIYUN_DBINSTANCE_STORAGE_TYPE_LOCAL_SSD = "local_ssd" //本地盘SSD盘 ALIYUN_DBINSTANCE_STORAGE_TYPE_CLOUD_ESSD = "cloud_essd" //ESSD云盘 diff --git a/pkg/compute/models/dbinstances.go b/pkg/compute/models/dbinstances.go index 332aedea1c..c486951b21 100644 --- a/pkg/compute/models/dbinstances.go +++ b/pkg/compute/models/dbinstances.go @@ -1615,6 +1615,7 @@ func (self *SDBInstance) SyncWithCloudDBInstance(ctx context.Context, userCred m self.VmemSizeMb = extInstance.GetVmemSizeMB() self.DiskSizeGB = extInstance.GetDiskSizeGB() self.StorageType = extInstance.GetStorageType() + self.Category = extInstance.GetCategory() self.Status = extInstance.GetStatus() self.Port = extInstance.GetPort() diff --git a/pkg/multicloud/azure/azure.go b/pkg/multicloud/azure/azure.go index 6b9295f68a..8332ba9c29 100644 --- a/pkg/multicloud/azure/azure.go +++ b/pkg/multicloud/azure/azure.go @@ -412,6 +412,9 @@ func (self *SAzureClient) _apiVersion(resource string, params url.Values) string } return "2017-12-01" } + if utils.IsInStringArray("microsoft.sql", info) { + return "2020-08-01-preview" + } if utils.IsInStringArray("microsoft.compute", info) { if utils.IsInStringArray("tags", info) { return "2020-06-01" diff --git a/pkg/multicloud/azure/dbinstance.go b/pkg/multicloud/azure/dbinstance.go index c0661584a4..f1a8eb0028 100644 --- a/pkg/multicloud/azure/dbinstance.go +++ b/pkg/multicloud/azure/dbinstance.go @@ -106,12 +106,35 @@ func (self *SRegion) GetIDBInstances() ([]cloudprovider.ICloudDBInstance, error) } result := []cloudprovider.ICloudDBInstance{} for i := range DBInstances { + DBInstances[i].region = self result = append(result, &DBInstances[i]) } + sqlServers, err := self.ListSQLServer() + if err != nil { + return nil, errors.Wrapf(err, "ListSQLServer") + } + for i := range sqlServers { + sqlServers[i].region = self + result = append(result, &sqlServers[i]) + } + managedSQLServers, err := self.ListManagedSQLServer() + if err != nil { + return nil, errors.Wrapf(err, "ListManagedSQLServer") + } + for i := range managedSQLServers { + managedSQLServers[i].region = self + result = append(result, &managedSQLServers[i]) + } return result, nil } func (self *SRegion) GetIDBInstanceById(instanceId string) (cloudprovider.ICloudDBInstance, error) { + if strings.Index(strings.ToLower(instanceId), "microsoft.sql/servers") > 0 { + return self.GetSQLServer(instanceId) + } + if strings.Index(strings.ToLower(instanceId), "microsoft.sql/managedinstances") > 0 { + return self.GetManagedSQLServer(instanceId) + } rds, err := self.GetDBInstanceById(instanceId) if err != nil { return nil, errors.Wrapf(err, "self.get(%s, url.Values{}, &newrds)", instanceId) @@ -365,10 +388,6 @@ func (rds *SDBInstance) Delete() error { return cloudprovider.ErrNotImplemented } -func (self *SDBInstance) GetTags() (map[string]string, error) { - return self.Tags, nil -} - func (self *SDBInstance) SetTags(tags map[string]string, replace bool) error { if !replace { for k, v := range self.Tags { diff --git a/pkg/multicloud/azure/dbinstance_database.go b/pkg/multicloud/azure/dbinstance_database.go index 6e88638577..b2fa24fb7a 100644 --- a/pkg/multicloud/azure/dbinstance_database.go +++ b/pkg/multicloud/azure/dbinstance_database.go @@ -43,7 +43,7 @@ type SDBInstanceDatabaseProperties struct { func (self *SRegion) ListSDBInstanceDatabase(Id string) ([]SDBInstanceDatabase, error) { type databases struct { - Value []SDBInstanceDatabase `json:"collation"` + Value []SDBInstanceDatabase } result := databases{} diff --git a/pkg/multicloud/azure/rds_managed_sqlserver.go b/pkg/multicloud/azure/rds_managed_sqlserver.go new file mode 100644 index 0000000000..b302afb46e --- /dev/null +++ b/pkg/multicloud/azure/rds_managed_sqlserver.go @@ -0,0 +1,172 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "net/url" + "strings" + + "yunion.io/x/pkg/errors" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/multicloud" +) + +type SManagedSQLServer struct { + region *SRegion + multicloud.SDBInstanceBase + multicloud.AzureTags + + Location string `json:"location"` + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Sku struct { + Name string `json:"name"` + Tier string `json:"tier"` + Capacity int `json:"capacity"` + Family string `json:"family"` + } `json:"sku"` + Properties struct { + Fullyqualifieddomainname string `json:"fullyQualifiedDomainName"` + Administratorlogin string `json:"administratorLogin"` + Subnetid string `json:"subnetId"` + State string `json:"state"` + Provisioningstate string `json:"provisioningState"` + Vcores int `json:"vCores"` + Storagesizeingb int `json:"storageSizeInGB"` + Licensetype string `json:"licenseType"` + Collation string `json:"collation"` + Publicdataendpointenabled bool `json:"publicDataEndpointEnabled"` + Proxyoverride string `json:"proxyOverride"` + Minimaltlsversion string `json:"minimalTlsVersion"` + Dnszone string `json:"dnsZone"` + Maintenanceconfigurationid string `json:"maintenanceConfigurationId"` + Storageaccounttype string `json:"storageAccountType"` + } `json:"properties"` +} + +func (self *SRegion) ListManagedSQLServer() ([]SManagedSQLServer, error) { + rds := []SManagedSQLServer{} + err := self.list("Microsoft.Sql/managedInstances", url.Values{}, &rds) + if err != nil { + return nil, errors.Wrapf(err, "list") + } + return rds, nil +} + +func (self *SRegion) GetManagedSQLServer(id string) (*SManagedSQLServer, error) { + rds := &SManagedSQLServer{region: self} + return rds, self.get(id, url.Values{}, rds) +} + +func (self *SManagedSQLServer) GetDiskSizeGB() int { + return self.Properties.Storagesizeingb +} + +func (self *SManagedSQLServer) GetEngine() string { + return api.DBINSTANCE_TYPE_SQLSERVER +} + +func (self *SManagedSQLServer) GetEngineVersion() string { + return "latest" +} + +func (self *SManagedSQLServer) GetGlobalId() string { + return strings.ToLower(self.ID) +} + +func (self *SManagedSQLServer) GetCategory() string { + return self.Sku.Family +} + +func (self *SManagedSQLServer) GetIVpcId() string { + if len(self.Properties.Subnetid) > 0 { + info := strings.Split(self.Properties.Subnetid, "/") + if len(info) > 2 { + return strings.Join(info[:len(info)-2], "/") + } + } + return "" +} + +func (self *SManagedSQLServer) GetId() string { + return self.ID +} + +func (self *SManagedSQLServer) GetInstanceType() string { + return self.Sku.Name +} + +func (self *SManagedSQLServer) GetMaintainTime() string { + return "" +} + +func (self *SManagedSQLServer) GetName() string { + return self.Name +} + +func (self *SManagedSQLServer) GetPort() int { + return 1433 +} + +func (self *SManagedSQLServer) GetStatus() string { + switch self.Properties.State { + case "Succeeded", "Running", "Ready": + return api.DBINSTANCE_RUNNING + case "Creating", "Created": + return api.DBINSTANCE_DEPLOYING + case "Deleted", "Deleting": + return api.DBINSTANCE_BACKUP_DELETING + case "Failed": + return api.DBINSTANCE_CREATE_FAILED + default: + return strings.ToLower(self.Properties.State) + } +} + +func (self *SManagedSQLServer) GetStorageType() string { + return self.Properties.Storageaccounttype +} + +func (self *SManagedSQLServer) GetVcpuCount() int { + return self.Properties.Vcores +} + +func (self *SManagedSQLServer) GetVmemSizeMB() int { + switch self.Sku.Family { + case "Gen5": + return int(float32(self.Properties.Vcores) * 5.1 * 1024) + case "Gen4": + return self.Properties.Vcores * 7 * 1024 + } + return 0 +} + +func (self *SManagedSQLServer) GetZone1Id() string { + return "" +} + +func (self *SManagedSQLServer) GetZone2Id() string { + return "" +} + +func (self *SManagedSQLServer) GetZone3Id() string { + return "" +} + +func (self *SManagedSQLServer) GetConnectionStr() string { + return self.Properties.Fullyqualifieddomainname +} diff --git a/pkg/multicloud/azure/rds_managed_sqlserver_account.go b/pkg/multicloud/azure/rds_managed_sqlserver_account.go new file mode 100644 index 0000000000..0f132a0186 --- /dev/null +++ b/pkg/multicloud/azure/rds_managed_sqlserver_account.go @@ -0,0 +1,40 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" +) + +type SManagedSQLServerAccount struct { + multicloud.SDBInstanceAccountBase + rds *SManagedSQLServer + + Name string +} + +func (self *SManagedSQLServerAccount) GetName() string { + return self.Name +} + +func (self *SManagedSQLServer) GetIDBInstanceAccounts() ([]cloudprovider.ICloudDBInstanceAccount, error) { + accounts := []cloudprovider.ICloudDBInstanceAccount{} + if len(self.Properties.Administratorlogin) > 0 { + account := &SManagedSQLServerAccount{rds: self, Name: self.Properties.Administratorlogin} + accounts = append(accounts, account) + } + return accounts, nil +} diff --git a/pkg/multicloud/azure/rds_managed_sqlserver_database.go b/pkg/multicloud/azure/rds_managed_sqlserver_database.go new file mode 100644 index 0000000000..5f856f56d4 --- /dev/null +++ b/pkg/multicloud/azure/rds_managed_sqlserver_database.go @@ -0,0 +1,91 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "net/url" + "strings" + "time" + + "yunion.io/x/pkg/errors" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" +) + +type SManagedSQLServerDatabase struct { + multicloud.SDBInstanceDatabaseBase + multicloud.AzureTags + rds *SManagedSQLServer + + ID string `json:"id"` + Location string `json:"location"` + Name string `json:"name"` + Properties struct { + Collation string `json:"collation"` + Creationdate time.Time `json:"creationDate"` + Defaultsecondarylocation string `json:"defaultSecondaryLocation"` + Status string `json:"status"` + } `json:"properties"` + Type string `json:"type"` +} + +func (self *SManagedSQLServerDatabase) GetName() string { + return self.Name +} + +func (self *SManagedSQLServerDatabase) GetId() string { + return self.ID +} + +func (self *SManagedSQLServerDatabase) GetStatus() string { + switch self.Properties.Status { + case "Online": + return api.DBINSTANCE_DATABASE_RUNNING + case "Creating": + return api.DBINSTANCE_DATABASE_CREATING + default: + return strings.ToLower(self.Properties.Status) + } +} + +func (self *SManagedSQLServerDatabase) GetGlobalId() string { + return strings.ToLower(self.Name) +} + +func (self *SManagedSQLServerDatabase) GetCharacterSet() string { + return self.Properties.Collation +} + +func (self *SRegion) GetManagedSQLServerDatabases(id string) ([]SManagedSQLServerDatabase, error) { + result := struct { + Value []SManagedSQLServerDatabase + }{} + return result.Value, self.get(id+"/databases", url.Values{}, &result) +} + +func (self *SManagedSQLServer) GetIDBInstanceDatabases() ([]cloudprovider.ICloudDBInstanceDatabase, error) { + dbs, err := self.region.GetManagedSQLServerDatabases(self.ID) + if err != nil { + return nil, errors.Wrapf(err, "GetSQLServerDatabases") + } + ret := []cloudprovider.ICloudDBInstanceDatabase{} + for i := range dbs { + dbs[i].rds = self + ret = append(ret, &dbs[i]) + } + return ret, nil +} diff --git a/pkg/multicloud/azure/rds_sqlserver.go b/pkg/multicloud/azure/rds_sqlserver.go new file mode 100644 index 0000000000..698aeb3db5 --- /dev/null +++ b/pkg/multicloud/azure/rds_sqlserver.go @@ -0,0 +1,182 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "net/url" + "strings" + + "yunion.io/x/pkg/errors" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/multicloud" +) + +type SPrivateendpointconnection struct { + ID string `json:"id"` + Properties struct { + Provisioningstate string `json:"provisioningState"` + Privateendpoint struct { + ID string `json:"id"` + } `json:"privateEndpoint"` + Privatelinkserviceconnectionstate struct { + Status string `json:"status"` + Description string `json:"description"` + Actionsrequired string `json:"actionsRequired"` + } `json:"privateLinkServiceConnectionState"` + } `json:"properties"` +} + +type SSQLServer struct { + region *SRegion + multicloud.SDBInstanceBase + multicloud.AzureTags + + dbs []SSQLServerDatabase + + Kind string `json:"kind"` + Properties struct { + Administratorlogin string `json:"administratorLogin"` + Version string `json:"version"` + State string `json:"state"` + Fullyqualifieddomainname string `json:"fullyQualifiedDomainName"` + Privateendpointconnections []SPrivateendpointconnection `json:"privateEndpointConnections"` + Publicnetworkaccess string `json:"publicNetworkAccess"` + } `json:"properties"` + Location string `json:"location"` + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` +} + +func (self *SRegion) ListSQLServer() ([]SSQLServer, error) { + rds := []SSQLServer{} + err := self.list("Microsoft.Sql/servers", url.Values{}, &rds) + if err != nil { + return nil, errors.Wrapf(err, "list") + } + return rds, nil +} + +func (self *SRegion) GetSQLServer(id string) (*SSQLServer, error) { + rds := &SSQLServer{region: self} + return rds, self.get(id, url.Values{}, rds) +} + +func (self *SSQLServer) GetDiskSizeGB() int { + dbs, err := self.fetchDatabase() + if err != nil { + return 0 + } + sizeMb := 0 + for _, db := range dbs { + sizeMb += db.GetDiskSizeMb() + } + return sizeMb / 1024 +} + +func (self *SSQLServer) GetEngine() string { + return api.DBINSTANCE_TYPE_SQLSERVER +} + +func (self *SSQLServer) GetEngineVersion() string { + return self.Properties.Version +} + +func (self *SSQLServer) GetGlobalId() string { + return strings.ToLower(self.ID) +} + +func (self *SSQLServer) GetCategory() string { + return api.AZURE_DBINSTANCE_CATEGORY_BASIC +} + +func (self *SSQLServer) GetIVpcId() string { + return "" +} + +func (self *SSQLServer) GetId() string { + return self.ID +} + +func (self *SSQLServer) GetInstanceType() string { + return "" +} + +func (self *SSQLServer) GetMaintainTime() string { + return "" +} + +func (self *SSQLServer) GetName() string { + return self.Name +} + +func (self *SSQLServer) GetPort() int { + return 1433 +} + +func (self *SSQLServer) GetStatus() string { + switch self.Properties.State { + case "Ready": + return api.DBINSTANCE_RUNNING + default: + return self.Properties.State + } +} + +func (self *SSQLServer) GetStorageType() string { + return "" +} + +func (self *SSQLServer) fetchDatabase() ([]SSQLServerDatabase, error) { + if len(self.dbs) > 0 { + return self.dbs, nil + } + var err error + self.dbs, err = self.region.GetSQLServerDatabases(self.ID) + return self.dbs, err +} + +func (self *SSQLServer) GetVcpuCount() int { + dbs, err := self.fetchDatabase() + if err != nil { + return 0 + } + vcpu := 0 + for _, db := range dbs { + vcpu += db.GetVcpuCount() + } + return vcpu +} + +func (self *SSQLServer) GetVmemSizeMB() int { + return self.GetVcpuCount() * 3 * 1023 +} + +func (self *SSQLServer) GetZone1Id() string { + return "" +} + +func (self *SSQLServer) GetZone2Id() string { + return "" +} + +func (self *SSQLServer) GetZone3Id() string { + return "" +} + +func (self *SSQLServer) GetConnectionStr() string { + return self.Properties.Fullyqualifieddomainname +} diff --git a/pkg/multicloud/azure/rds_sqlserver_account.go b/pkg/multicloud/azure/rds_sqlserver_account.go new file mode 100644 index 0000000000..f8f21ab82d --- /dev/null +++ b/pkg/multicloud/azure/rds_sqlserver_account.go @@ -0,0 +1,40 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" +) + +type SSQLServerAccount struct { + multicloud.SDBInstanceAccountBase + rds *SSQLServer + + Name string +} + +func (self *SSQLServerAccount) GetName() string { + return self.Name +} + +func (self *SSQLServer) GetIDBInstanceAccounts() ([]cloudprovider.ICloudDBInstanceAccount, error) { + accounts := []cloudprovider.ICloudDBInstanceAccount{} + if len(self.Properties.Administratorlogin) > 0 { + account := &SSQLServerAccount{rds: self, Name: self.Properties.Administratorlogin} + accounts = append(accounts, account) + } + return accounts, nil +} diff --git a/pkg/multicloud/azure/rds_sqlserver_backup.go b/pkg/multicloud/azure/rds_sqlserver_backup.go new file mode 100644 index 0000000000..b1bc713357 --- /dev/null +++ b/pkg/multicloud/azure/rds_sqlserver_backup.go @@ -0,0 +1,120 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "fmt" + "net/url" + "strings" + "time" + + "yunion.io/x/pkg/errors" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" +) + +type SSQLServerBackup struct { + rds *SSQLServer + multicloud.SDBInstanceBackupBase + multicloud.AzureTags + + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Properties struct { + Servername string `json:"serverName"` + Servercreatetime time.Time `json:"serverCreateTime"` + Databasename string `json:"databaseName"` + Databasedeletiontime string `json:"databaseDeletionTime"` + Backuptime time.Time `json:"backupTime"` + Backupstorageredundancy string `json:"backupStorageRedundancy"` + } `json:"properties"` +} + +func (self *SSQLServerBackup) GetBackupMode() string { + return api.BACKUP_MODE_AUTOMATED +} + +func (self *SSQLServerBackup) GetBackupMethod() cloudprovider.TBackupMethod { + return cloudprovider.BackupMethodUnknown +} + +func (self *SSQLServerBackup) GetBackupSizeMb() int { + return 0 +} + +func (self *SSQLServerBackup) GetDBInstanceId() string { + return self.rds.GetGlobalId() +} + +func (self *SSQLServerBackup) GetName() string { + return self.Name +} + +func (self *SSQLServerBackup) GetDBNames() string { + return self.Properties.Databasename +} + +func (self *SSQLServerBackup) GetStatus() string { + return api.DBINSTANCE_BACKUP_READY +} + +func (self *SSQLServerBackup) GetId() string { + return self.ID +} + +func (self *SSQLServerBackup) GetGlobalId() string { + return strings.ToLower(self.Name) +} + +func (self *SSQLServerBackup) GetStartTime() time.Time { + return self.Properties.Backuptime +} + +func (self *SSQLServerBackup) GetEndTime() time.Time { + return time.Time{} +} + +func (self *SSQLServerBackup) GetEngine() string { + return self.rds.GetEngine() +} + +func (self *SSQLServerBackup) GetEngineVersion() string { + return self.rds.GetEngineVersion() +} + +func (self *SRegion) ListSQLServerBackups(id, location string) ([]SSQLServerBackup, error) { + ret := struct { + Value []SSQLServerBackup + }{} + r := fmt.Sprintf("Microsoft.Sql/locations/%s/longTermRetentionServers", location) + prefix := strings.Replace(strings.ToLower(id), "microsoft.sql/servers", r, -1) + return ret.Value, self.get(prefix+"/longTermRetentionBackups", url.Values{}, &ret) +} + +func (self *SSQLServer) GetIDBInstanceBackups() ([]cloudprovider.ICloudDBInstanceBackup, error) { + backups, err := self.region.ListSQLServerBackups(self.ID, self.Location) + if err != nil { + return nil, errors.Wrapf(err, "ListSQLServerBackups") + } + ret := []cloudprovider.ICloudDBInstanceBackup{} + for i := range backups { + backups[i].rds = self + ret = append(ret, &backups[i]) + } + return ret, nil +} diff --git a/pkg/multicloud/azure/rds_sqlserver_database.go b/pkg/multicloud/azure/rds_sqlserver_database.go new file mode 100644 index 0000000000..5cca325fa0 --- /dev/null +++ b/pkg/multicloud/azure/rds_sqlserver_database.go @@ -0,0 +1,120 @@ +// Copyright 2019 Yunion +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package azure + +import ( + "net/url" + "strings" + "time" + + "yunion.io/x/pkg/errors" + + api "yunion.io/x/onecloud/pkg/apis/compute" + "yunion.io/x/onecloud/pkg/cloudprovider" + "yunion.io/x/onecloud/pkg/multicloud" +) + +type SSQLServerDatabase struct { + multicloud.SDBInstanceDatabaseBase + multicloud.AzureTags + rds *SSQLServer + Sku struct { + Name string `json:"name"` + Tier string `json:"tier"` + Capacity int `json:"capacity"` + } `json:"sku"` + Kind string `json:"kind"` + Properties struct { + Collation string `json:"collation"` + Maxsizebytes int64 `json:"maxSizeBytes"` + Status string `json:"status"` + Databaseid string `json:"databaseId"` + Creationdate time.Time `json:"creationDate"` + Currentserviceobjectivename string `json:"currentServiceObjectiveName"` + Requestedserviceobjectivename string `json:"requestedServiceObjectiveName"` + Defaultsecondarylocation string `json:"defaultSecondaryLocation"` + Catalogcollation string `json:"catalogCollation"` + Licensetype string `json:"licenseType"` + Maxlogsizebytes int `json:"maxLogSizeBytes"` + Storageaccounttype string `json:"storageAccountType"` + Zoneredundant bool `json:"zoneRedundant"` + Readscale string `json:"readScale"` + Earliestrestoredate time.Time `json:"earliestRestoreDate"` + Currentsku struct { + Name string `json:"name"` + Tier string `json:"tier"` + Capacity int `json:"capacity"` + } `json:"currentSku"` + } `json:"properties"` + Location string `json:"location"` + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` +} + +func (self *SSQLServerDatabase) GetName() string { + return self.Name +} + +func (self *SSQLServerDatabase) GetId() string { + return self.ID +} + +func (self *SSQLServerDatabase) GetStatus() string { + switch self.Properties.Status { + case "Online": + return api.DBINSTANCE_DATABASE_RUNNING + case "Creating": + return api.DBINSTANCE_DATABASE_CREATING + default: + return strings.ToLower(self.Properties.Status) + } +} + +func (self *SSQLServerDatabase) GetDiskSizeMb() int { + return int(self.Properties.Maxsizebytes / 1024 / 1024) +} + +func (self *SSQLServerDatabase) GetVcpuCount() int { + return self.Properties.Currentsku.Capacity +} + +func (self *SSQLServerDatabase) GetGlobalId() string { + return strings.ToLower(self.Name) +} + +func (self *SSQLServerDatabase) GetCharacterSet() string { + return self.Properties.Collation +} + +func (self *SRegion) GetSQLServerDatabases(id string) ([]SSQLServerDatabase, error) { + result := struct { + Value []SSQLServerDatabase + }{} + return result.Value, self.get(id+"/databases", url.Values{}, &result) +} + +func (self *SSQLServer) GetIDBInstanceDatabases() ([]cloudprovider.ICloudDBInstanceDatabase, error) { + dbs, err := self.fetchDatabase() + if err != nil { + return nil, errors.Wrapf(err, "fetchDatabase") + } + ret := []cloudprovider.ICloudDBInstanceDatabase{} + for i := range dbs { + dbs[i].rds = self + ret = append(ret, &dbs[i]) + } + return ret, nil +} diff --git a/pkg/multicloud/azure/shell/dbinstance.go b/pkg/multicloud/azure/shell/dbinstance.go index 087e3050fe..6fc5706345 100644 --- a/pkg/multicloud/azure/shell/dbinstance.go +++ b/pkg/multicloud/azure/shell/dbinstance.go @@ -21,10 +21,10 @@ import ( func init() { type DBInstanceListOptions struct { - Type string `help:"dbinstance type" choices:"Microsoft.DBForMariaDB/servers|Microsoft.DBforMySQL/servers|Microsoft.DBforMySQL/flexibleServers|Microsoft.DBforPostgreSQL/servers|Microsoft.DBforPostgreSQL/flexibleServers"` + TYPE string `help:"dbinstance type" choices:"Microsoft.DBForMariaDB/servers|Microsoft.DBforMySQL/servers|Microsoft.DBforMySQL/flexibleServers|Microsoft.DBforPostgreSQL/servers|Microsoft.DBforPostgreSQL/flexibleServers|Microsoft.Sql/servers|Microsoft.Sql/managedInstances"` } shellutils.R(&DBInstanceListOptions{}, "dbinstance-list", "List rds intances", func(cli *azure.SRegion, args *DBInstanceListOptions) error { - instances, err := cli.ListDBInstance(args.Type) + instances, err := cli.ListDBInstance(args.TYPE) if err != nil { return err }