dbinstance.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package azure
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. "yunion.io/x/cloudmux/pkg/multicloud"
  25. )
  26. type SDBInstanceVirtualNetworkRule struct {
  27. ID string `json:"id"`
  28. Name string `json:"name"`
  29. Type string `json:"type"`
  30. Properties SDBInstanceVirtualNetworkRuleProperties `json:"properties"`
  31. }
  32. type SDBInstanceVirtualNetworkRuleProperties struct {
  33. IgnoreMissingVnetServiceEndpoint bool `json:"ignoreMissingVnetServiceEndpoint"`
  34. State string `json:"state"`
  35. VirtualNetworkSubnetID string `json:"virtualNetworkSubnetId"`
  36. }
  37. type SDBInstanceSku struct {
  38. Name string `json:"name"`
  39. Tier string `json:"tier"`
  40. Family string `json:"family"`
  41. Capacity int `json:"capacity"`
  42. }
  43. type SDBInstanceStorageProfile struct {
  44. StorageMB int `json:"storageMB"`
  45. BackupRetentionDays int `json:"backupRetentionDays"`
  46. StorageIops int `json:"storageIops"`
  47. GeoRedundantBackup string `json:"geoRedundantBackup"`
  48. }
  49. type SDBInstanceDelegatedSubnetArguments struct {
  50. SubnetArmResourceId string `json:"subnetArmResourceId"`
  51. }
  52. type SDBInstanceProperties struct {
  53. AdministratorLogin string `json:"administratorLogin"`
  54. StorageProfile SDBInstanceStorageProfile `json:"storageProfile"`
  55. Version string `json:"version"`
  56. SslEnforcement string `json:"sslEnforcement"`
  57. UserVisibleState string `json:"userVisibleState"`
  58. FullyQualifiedDomainName string `json:"fullyQualifiedDomainName"`
  59. EarliestRestoreDate time.Time `json:"earliestRestoreDate"`
  60. ReplicationRole string `json:"replicationRole"`
  61. MasterServerId string `json:"masterServerId"`
  62. ReplicaCapacity int `json:"replicaCapacity"`
  63. DelegatedSubnetArguments SDBInstanceDelegatedSubnetArguments `json:"delegatedSubnetArguments"`
  64. }
  65. type SDBInstance struct {
  66. region *SRegion
  67. multicloud.SDBInstanceBase
  68. AzureTags
  69. Sku SDBInstanceSku `json:"sku"`
  70. Properties SDBInstanceProperties `json:"properties"`
  71. Location string `json:"location"`
  72. ID string `json:"id"`
  73. Name string `json:"name"`
  74. Type string `json:"type"`
  75. }
  76. func (self *SRegion) GetIDBInstances() ([]cloudprovider.ICloudDBInstance, error) {
  77. instanceTypes := []string{
  78. "Microsoft.DBForMariaDB/servers",
  79. "Microsoft.DBforMySQL/servers",
  80. "Microsoft.DBforMySQL/flexibleServers",
  81. "Microsoft.DBforPostgreSQL/servers",
  82. "Microsoft.DBforPostgreSQL/flexibleServers",
  83. }
  84. DBInstances := []SDBInstance{}
  85. for i := range instanceTypes {
  86. instances, err := self.ListDBInstance(instanceTypes[i])
  87. if err != nil {
  88. if errors.Cause(err) != cloudprovider.ErrNotFound {
  89. return nil, errors.Wrap(err, "self.ListDBInstance()")
  90. }
  91. }
  92. DBInstances = append(DBInstances, instances...)
  93. }
  94. result := []cloudprovider.ICloudDBInstance{}
  95. for i := range DBInstances {
  96. DBInstances[i].region = self
  97. result = append(result, &DBInstances[i])
  98. }
  99. sqlServers, err := self.ListSQLServer()
  100. if err != nil {
  101. return nil, errors.Wrapf(err, "ListSQLServer")
  102. }
  103. for i := range sqlServers {
  104. sqlServers[i].region = self
  105. result = append(result, &sqlServers[i])
  106. }
  107. managedSQLServers, err := self.ListManagedSQLServer()
  108. if err != nil {
  109. return nil, errors.Wrapf(err, "ListManagedSQLServer")
  110. }
  111. for i := range managedSQLServers {
  112. managedSQLServers[i].region = self
  113. result = append(result, &managedSQLServers[i])
  114. }
  115. return result, nil
  116. }
  117. func (self *SRegion) GetIDBInstanceById(instanceId string) (cloudprovider.ICloudDBInstance, error) {
  118. if strings.Index(strings.ToLower(instanceId), "microsoft.sql/servers") > 0 {
  119. return self.GetSQLServer(instanceId)
  120. }
  121. if strings.Index(strings.ToLower(instanceId), "microsoft.sql/managedinstances") > 0 {
  122. return self.GetManagedSQLServer(instanceId)
  123. }
  124. rds, err := self.GetDBInstanceById(instanceId)
  125. if err != nil {
  126. return nil, errors.Wrapf(err, "self.get(%s, url.Values{}, &newrds)", instanceId)
  127. }
  128. return rds, nil
  129. }
  130. func (self *SRegion) GetDBInstanceById(instanceId string) (*SDBInstance, error) {
  131. rds := SDBInstance{}
  132. err := self.get(instanceId, url.Values{}, &rds)
  133. if err != nil {
  134. return nil, errors.Wrapf(err, "self.get(%s, url.Values{}, &newrds)", instanceId)
  135. }
  136. rds.region = self
  137. return &rds, nil
  138. }
  139. func (self *SRegion) ListDBInstance(instanceType string) ([]SDBInstance, error) {
  140. result := []SDBInstance{}
  141. err := self.list(instanceType, url.Values{}, &result)
  142. if err != nil {
  143. return nil, errors.Wrapf(err, "list(%s)", instanceType)
  144. }
  145. for i := range result {
  146. result[i].region = self
  147. }
  148. return result, nil
  149. }
  150. func (self *SRegion) ListDBInstanceReplica(Id string) ([]SDBInstance, error) {
  151. type replicas struct {
  152. Value []SDBInstance
  153. }
  154. result := replicas{}
  155. err := self.get(fmt.Sprintf("%s/replicas", Id), url.Values{}, &result)
  156. if err != nil {
  157. return nil, errors.Wrapf(err, "get(%s)", Id)
  158. }
  159. for i := range result.Value {
  160. result.Value[i].region = self
  161. }
  162. return result.Value, nil
  163. }
  164. func (self *SRegion) ListDBInstanceVirtualNetworkRule(Id string) ([]SDBInstanceVirtualNetworkRule, error) {
  165. type networksRules struct {
  166. Value []SDBInstanceVirtualNetworkRule
  167. }
  168. result := networksRules{}
  169. err := self.get(fmt.Sprintf("%s/virtualNetworkRules", Id), url.Values{}, &result)
  170. if err != nil {
  171. return nil, errors.Wrapf(err, "get(%s)", Id)
  172. }
  173. return result.Value, nil
  174. }
  175. func (rds *SDBInstance) GetName() string {
  176. return rds.Name
  177. }
  178. func (rds *SDBInstance) GetId() string {
  179. return strings.ToLower(rds.ID)
  180. }
  181. func (rds *SDBInstance) GetGlobalId() string {
  182. return rds.GetId()
  183. }
  184. func (rds *SDBInstance) GetStatus() string {
  185. return api.DBINSTANCE_RUNNING
  186. }
  187. func (self *SDBInstance) GetProjectId() string {
  188. return getResourceGroup(self.ID)
  189. }
  190. func (rds *SDBInstance) GetExpiredAt() time.Time {
  191. return time.Time{}
  192. }
  193. func (rds *SDBInstance) GetStorageType() string {
  194. switch rds.Sku.Tier {
  195. case "Basic":
  196. return api.STORAGE_AZURE_BASIC
  197. case "General Purpose":
  198. return api.STORAGE_AZURE_GENERAL_PURPOSE
  199. case "Memory Optimized":
  200. return api.STORAGE_AZURE_GENERAL_PURPOSE
  201. default:
  202. return api.STORAGE_AZURE_BASIC
  203. }
  204. }
  205. func (rds *SDBInstance) Refresh() error {
  206. newrds := SDBInstance{}
  207. err := rds.region.get(rds.ID, url.Values{}, &newrds)
  208. if err != nil {
  209. return errors.Wrapf(err, "rds.region.get(%s, url.Values{}, &newdb)", rds.ID)
  210. }
  211. err = jsonutils.Update(rds, newrds)
  212. if err != nil {
  213. return err
  214. }
  215. rds.Tags = newrds.Tags
  216. return nil
  217. }
  218. func (rds *SDBInstance) GetMasterInstanceId() string {
  219. if len(rds.Properties.MasterServerId) > 0 {
  220. return strings.ToLower(rds.Properties.MasterServerId)
  221. }
  222. return ""
  223. }
  224. func (rds *SDBInstance) GetPort() int {
  225. switch rds.GetEngine() {
  226. case api.DBINSTANCE_TYPE_POSTGRESQL:
  227. return 5432
  228. case api.DBINSTANCE_TYPE_MYSQL:
  229. return 3306
  230. case api.DBINSTANCE_TYPE_MARIADB:
  231. return 3306
  232. default:
  233. return 0
  234. }
  235. }
  236. func (rds *SDBInstance) GetEngine() string {
  237. databaseType := strings.Split(rds.Type, "/")
  238. switch databaseType[0] {
  239. case "Microsoft.DBforPostgreSQL":
  240. return api.DBINSTANCE_TYPE_POSTGRESQL
  241. case "Microsoft.DBforMySQL":
  242. return api.DBINSTANCE_TYPE_MYSQL
  243. case "Microsoft.DBforMariaDB":
  244. return api.DBINSTANCE_TYPE_MARIADB
  245. default:
  246. return ""
  247. }
  248. }
  249. func (rds *SDBInstance) GetEngineVersion() string {
  250. return rds.Properties.Version
  251. }
  252. func (rds *SDBInstance) GetInstanceType() string {
  253. return rds.Sku.Name
  254. }
  255. func (rds *SDBInstance) GetVcpuCount() int {
  256. return rds.Sku.Capacity
  257. }
  258. func (rds *SDBInstance) GetVmemSizeMB() int {
  259. if strings.Contains(rds.Type, "server") {
  260. switch rds.Sku.Tier {
  261. case "Basic":
  262. return rds.Sku.Capacity * 2 * 1024
  263. case "General Purpose":
  264. return rds.Sku.Capacity * 5 * 1024
  265. case "GeneralPurpose":
  266. return int(float32(rds.Sku.Capacity) * 5.2 * 1024)
  267. case "Memory Optimized":
  268. return rds.Sku.Capacity * 10 * 1024
  269. default:
  270. return 0
  271. }
  272. }
  273. return 0
  274. }
  275. func (rds *SDBInstance) GetDiskSizeGB() int {
  276. return rds.Properties.StorageProfile.StorageMB / 1024
  277. }
  278. func (rds *SDBInstance) GetCategory() string {
  279. return rds.Sku.Tier
  280. }
  281. func (rds *SDBInstance) GetMaintainTime() string {
  282. return ""
  283. }
  284. func (rds *SDBInstance) GetConnectionStr() string {
  285. return rds.Properties.FullyQualifiedDomainName
  286. }
  287. // func (rds *SDBInstance) GetInternalConnectionStr() string
  288. func (rds *SDBInstance) GetIVpcId() string {
  289. splited := strings.Split(rds.Properties.DelegatedSubnetArguments.SubnetArmResourceId, "/subnets")
  290. return splited[0]
  291. }
  292. func (rds *SDBInstance) GetDBNetworks() ([]cloudprovider.SDBInstanceNetwork, error) {
  293. result := []cloudprovider.SDBInstanceNetwork{}
  294. delegateNet := cloudprovider.SDBInstanceNetwork{NetworkId: rds.Properties.DelegatedSubnetArguments.SubnetArmResourceId}
  295. result = append(result, delegateNet)
  296. return result, nil
  297. }
  298. func (rds *SDBInstance) GetZone1Id() string {
  299. return ""
  300. }
  301. func (rds *SDBInstance) GetZone2Id() string {
  302. return ""
  303. }
  304. func (rds *SDBInstance) GetZone3Id() string {
  305. return ""
  306. }
  307. func (rds *SDBInstance) GetIDBInstanceParameters() ([]cloudprovider.ICloudDBInstanceParameter, error) {
  308. configs, err := rds.region.ListDBInstanceConfiguration(rds.ID)
  309. if err != nil {
  310. return nil, errors.Wrapf(err, "rds.region.ListDBInstanceConfiguration(%s)", rds.ID)
  311. }
  312. result := []cloudprovider.ICloudDBInstanceParameter{}
  313. for i := range configs {
  314. result = append(result, &configs[i])
  315. }
  316. return result, nil
  317. }
  318. func (rds *SDBInstance) GetIDBInstanceDatabases() ([]cloudprovider.ICloudDBInstanceDatabase, error) {
  319. db, err := rds.region.ListSDBInstanceDatabase(rds.ID)
  320. if err != nil {
  321. return nil, errors.Wrapf(err, "rds.region.ListSDBInstanceDatabase(%s)", rds.ID)
  322. }
  323. result := []cloudprovider.ICloudDBInstanceDatabase{}
  324. for i := range db {
  325. result = append(result, &db[i])
  326. }
  327. return result, nil
  328. }
  329. func (rds *SDBInstance) GetIDBInstanceAccounts() ([]cloudprovider.ICloudDBInstanceAccount, error) {
  330. accounts := []cloudprovider.ICloudDBInstanceAccount{}
  331. if len(rds.Properties.AdministratorLogin) > 0 {
  332. account := &SDBInstanceAccount{instance: rds, AccountName: rds.Properties.AdministratorLogin}
  333. accounts = append(accounts, account)
  334. }
  335. return accounts, nil
  336. }
  337. func (rds *SDBInstance) GetIDBInstanceBackups() ([]cloudprovider.ICloudDBInstanceBackup, error) {
  338. return []cloudprovider.ICloudDBInstanceBackup{}, nil
  339. }
  340. func (rds *SDBInstance) Delete() error {
  341. return cloudprovider.ErrNotImplemented
  342. }
  343. func (self *SDBInstance) SetTags(tags map[string]string, replace bool) error {
  344. if !replace {
  345. for k, v := range self.Tags {
  346. if _, ok := tags[k]; !ok {
  347. tags[k] = v
  348. }
  349. }
  350. }
  351. _, err := self.region.client.SetTags(self.ID, tags)
  352. if err != nil {
  353. return errors.Wrapf(err, "self.region.client.SetTags(%s,%s)", self.ID, jsonutils.Marshal(tags).String())
  354. }
  355. return nil
  356. }