cloudaccount.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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 compute
  15. import (
  16. "fmt"
  17. "reflect"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/gotypes"
  21. "yunion.io/x/pkg/utils"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. proxyapi "yunion.io/x/onecloud/pkg/apis/cloudcommon/proxy"
  24. "yunion.io/x/onecloud/pkg/httperrors"
  25. )
  26. type CloudenvResourceInfo struct {
  27. // 云平台名称
  28. // example: Google
  29. Provider string `json:"provider,omitempty"`
  30. // 云平台品牌
  31. // example: Google
  32. Brand string `json:"brand,omitempty"`
  33. // 云环境
  34. // example: public
  35. CloudEnv string `json:"cloud_env,omitempty"`
  36. // Environment
  37. Environment string `json:"environment,omitempty"`
  38. }
  39. type CloudenvResourceListInput struct {
  40. // 列出指定云平台的资源,支持的云平台如下
  41. //
  42. // | Provider | 开始支持版本 | 平台 |
  43. // |-----------|------------|-------------------------------------|
  44. // | OneCloud | 0.0 | OneCloud内置私有云,包括KVM和裸金属管理 |
  45. // | VMware | 1.2 | VMware vCenter |
  46. // | OpenStack | 2.6 | OpenStack M版本以上私有云 |
  47. // | ZStack | 2.10 | ZStack私有云 |
  48. // | Aliyun | 2.0 | 阿里云 |
  49. // | Aws | 2.3 | Amazon AWS |
  50. // | Azure | 2.2 | Microsoft Azure |
  51. // | Google | 2.13 | Google Cloud Platform |
  52. // | Qcloud | 2.3 | 腾讯云 |
  53. // | Huawei | 2.5 | 华为公有云 |
  54. // | Ucloud | 2.7 | UCLOUD |
  55. // | Ctyun | 2.13 | 天翼云 |
  56. // | S3 | 2.11 | 通用s3对象存储 |
  57. // | Ceph | 2.11 | Ceph对象存储 |
  58. // | Xsky | 2.11 | XSKY启明星辰Ceph对象存储 |
  59. //
  60. // enum: ["OneCloud","VMware","Aliyun","Qcloud","Azure","Aws","Huawei","OpenStack","Ucloud","ZStack","Google","Ctyun","S3","Ceph","Xsky"]
  61. Providers []string `json:"providers"`
  62. // swagger:ignore
  63. // Deprecated
  64. Provider []string `json:"provider" yunion-deprecated-by:"providers"`
  65. Brands []string `json:"brands"`
  66. // swagger:ignore
  67. // Deprecated
  68. Brand []string `json:"brand" yunion-deprecated-by:"brands"`
  69. // 列出指定云环境的资源,支持云环境如下:
  70. //
  71. // | CloudEnv | 说明 |
  72. // |-----------|--------|
  73. // | public | 公有云 |
  74. // | private | 私有云 |
  75. // | onpremise | 本地IDC |
  76. //
  77. // enum: ["public","private","onpremise"]
  78. CloudEnv string `json:"cloud_env"`
  79. // swagger:ignore
  80. // Deprecated
  81. // description: this param will be deprecate at 3.0
  82. PublicCloud *bool `json:"public_cloud"`
  83. // swagger:ignore
  84. // Deprecated
  85. // description: this param will be deprecate at 3.0
  86. IsPublic *bool `json:"is_public"`
  87. // swagger:ignore
  88. // Deprecated
  89. // description: this param will be deprecate at 3.0
  90. PrivateCloud *bool `json:"private_cloud"`
  91. // swagger:ignore
  92. // Deprecated
  93. // description: this param will be deprecate at 3.0
  94. IsPrivate *bool `json:"is_private"`
  95. // swagger:ignore
  96. // Deprecated
  97. // description: this param will be deprecate at 3.0
  98. IsOnPremise *bool `json:"is_on_premise"`
  99. // 以平台名称排序
  100. // pattern:asc|desc
  101. OrderByProvider string `json:"order_by_provider"`
  102. // 以平台品牌排序
  103. // pattern:asc|desc
  104. OrderByBrand string `json:"order_by_brand"`
  105. }
  106. type CloudaccountResourceInfo struct {
  107. CloudenvResourceInfo
  108. // 云账号名称
  109. // example: google-account
  110. Account string `json:"account,omitempty"`
  111. // 云账号状态
  112. AccountStatus string `json:"account_status,omitempty"`
  113. // 云账号监控状态
  114. AccountHealthStatus string `json:"account_health_status,omitempty"`
  115. AccountReadOnly bool `json:"account_read_only,omitempty"`
  116. }
  117. type CloudaccountCreateInput struct {
  118. apis.EnabledStatusInfrasResourceBaseCreateInput
  119. // 指定云平台
  120. // Qcloud: 腾讯云
  121. // Ctyun: 天翼云
  122. // enum: ["VMware", "Aliyun", "Qcloud", "Azure", "Aws", "Huawei", "OpenStack", "Ucloud", "ZStack", "Google", "Ctyun", "JDcloud"]
  123. Provider string `json:"provider"`
  124. // swagger:ignore
  125. AccountId string `json:"account_id"`
  126. // 跳过重复账号注册检查
  127. SkipDuplicateAccountCheck bool `json:"skip_duplicate_account_check"`
  128. // 指定云平台品牌, 此参数默认和provider相同
  129. // requried: false
  130. //
  131. //
  132. //
  133. // | provider | 支持的参数 |
  134. // | -------- | ---------- |
  135. // | VMware | VMware |
  136. // | Aliyun | Aliyun |
  137. // | Qcloud | Qcloud |
  138. // | Azure | Azure |
  139. // | Aws | Aws |
  140. // | Huawei | Huawei |
  141. // | OpenStack | OpenStack |
  142. // | Ucloud | Ucloud |
  143. // | ZStack | ZStack |
  144. // | Google | Google |
  145. // | Ctyun | Ctyun |
  146. Brand string `json:"brand"`
  147. // swagger:ignore
  148. IsPublicCloud bool `json:"is_public_cloud"`
  149. // swagger:ignore
  150. IsOnPremise bool `json:"is_on_premise"`
  151. // 指定云账号所属的项目
  152. // Tenant string `json:"tenant"`
  153. // swagger:ignore
  154. // TenantId string
  155. apis.ProjectizedResourceInput
  156. // 自动根据云上项目或订阅创建本地项目, OpenStack此参数为true
  157. // default: false
  158. AutoCreateProject *bool `json:"auto_create_project"`
  159. // 额外信息,例如账单的access key
  160. Options *jsonutils.JSONDict `json:"options"`
  161. proxyapi.ProxySettingResourceInput
  162. cloudprovider.SCloudaccount
  163. cloudprovider.SCloudaccountCredential
  164. // 是否启用SAML认证
  165. // default: false
  166. SAMLAuth *bool `json:"saml_auth"`
  167. // VMware 账号有zone属性
  168. Zone string `json:"zone"`
  169. // 仅当show_sub_accounts=true并且dry_run=true时才返回sub accounts 信息, 且不会创建云账号
  170. ShowSubAccounts bool `json:"show_sub_accounts"`
  171. // swagger:ignore
  172. SubAccounts *cloudprovider.SubAccounts
  173. ReadOnly bool `json:"read_only"`
  174. SProjectMappingResourceInput
  175. // 是否立即开始同步资源
  176. StartSync *bool `json:"start_sync"`
  177. // 跳过指定资源同步
  178. SkipSyncResources SkipSyncResources `json:"skip_sync_resources"`
  179. // 货币类型
  180. // enmu: CNY, USD
  181. Currency string `json:"currency"`
  182. EnableAutoSyncResource *bool `json:"enable_auto_sync_resource"`
  183. }
  184. type SProjectMappingResourceInput struct {
  185. ProjectMappingId string `json:"project_mapping_id"`
  186. EnableProjectSync bool `json:"enable_project_sync"`
  187. EnableResourceSync bool `json:"enable_resource_sync"`
  188. }
  189. type CloudaccountShareModeInput struct {
  190. apis.Meta
  191. ShareMode string `json:"share_mode"`
  192. }
  193. func (i CloudaccountShareModeInput) Validate() error {
  194. if len(i.ShareMode) == 0 {
  195. return httperrors.NewMissingParameterError("share_mode")
  196. }
  197. if !utils.IsInStringArray(i.ShareMode, CLOUD_ACCOUNT_SHARE_MODES) {
  198. return httperrors.NewInputParameterError("invalid share_mode %s", i.ShareMode)
  199. }
  200. return nil
  201. }
  202. type CloudaccountListInput struct {
  203. apis.EnabledStatusInfrasResourceBaseListInput
  204. ManagedResourceListInput
  205. CapabilityListInput
  206. SyncableBaseResourceListInput
  207. // 账号健康状态
  208. HealthStatus []string `json:"health_status"`
  209. ReadOnly *bool `json:"read_only"`
  210. // 共享模式
  211. ShareMode []string `json:"share_mode"`
  212. // 代理
  213. ProxySetting string `json:"proxy_setting"`
  214. // swagger:ignore
  215. ProxySettingId string `json:"proxy_setting_id" yunion-deprecated-by:"proxy_setting"`
  216. // 按宿主机数量排序
  217. // pattern:asc|desc
  218. OrderByHostCount string `json:"order_by_host_count"`
  219. // 按虚拟机数量排序
  220. // pattern:asc|desc
  221. OrderByGuestCount string `json:"order_by_guest_count"`
  222. }
  223. type ProviderProject struct {
  224. // 子订阅项目名称
  225. // example: system
  226. Tenant string `json:"tenant"`
  227. // 子订阅项目Id
  228. // 9a48383a-467a-4542-8b50-4e15b0a8715f
  229. TenantId string `json:"tenant_id"`
  230. }
  231. type SAccountUsage struct {
  232. // 弹性公网Ip数量
  233. // example: 2
  234. EipCount int `json:"eip_count,allowempty"`
  235. // 虚拟私有网络数量
  236. // example: 4
  237. VpcCount int `json:"vpc_count,allowempty"`
  238. // 云盘数量
  239. // example: 12
  240. DiskCount int `json:"disk_count,allowempty"`
  241. // 宿主机数量(不计算虚拟机宿主机数量)
  242. // example: 0
  243. HostCount int `json:"host_count,allowempty"`
  244. // 云主机数量
  245. // example: 4
  246. GuestCount int `json:"guest_count,allowempty"`
  247. // 块存储数量
  248. // example: 12
  249. StorageCount int `json:"storage_count,allowempty"`
  250. // 子订阅数量
  251. // example: 1
  252. ProviderCount int `json:"provider_count,allowempty"`
  253. // 启用的子订阅数量
  254. // example: 1
  255. EnabledProviderCount int `json:"enabled_provider_count,allowempty"`
  256. // 路由表数量
  257. // example: 0
  258. RoutetableCount int `json:"routetable_count,allowempty"`
  259. // 存储缓存数量
  260. // example: 10
  261. StoragecacheCount int `json:"storagecache_count,allowempty"`
  262. // 并发同步数量
  263. SyncCount int `json:"sync_count,allowempty"`
  264. }
  265. type CloudaccountDetail struct {
  266. apis.EnabledStatusInfrasResourceBaseDetails
  267. SCloudaccount
  268. // 同步时间间隔
  269. // example: 3600
  270. SyncIntervalSeconds int `json:"sync_interval_seconds"`
  271. // 同步状态
  272. SyncStatus2 string `json:"sync_status2"`
  273. // 云账号环境类型
  274. // public: 公有云
  275. // private: 私有云
  276. // onpremise: 本地IDC
  277. // example: public
  278. CloudEnv string `json:"cloud_env"`
  279. apis.ProjectizedResourceInfo
  280. SAccountUsage
  281. ProxySetting proxyapi.SProxySetting `json:"proxy_setting"`
  282. ProjectMappingResourceInfo
  283. // 上次同步耗时
  284. LastSyncCost string `json:"last_sync_cost"`
  285. }
  286. func (self CloudaccountDetail) GetMetricTags() map[string]string {
  287. ret := map[string]string{
  288. "id": self.Id,
  289. "cloudaccount_id": self.Id,
  290. "cloudaccount_name": self.Name,
  291. "brand": self.Brand,
  292. "domain_id": self.DomainId,
  293. "project_domain": self.ProjectDomain,
  294. "currency": self.Currency,
  295. "tenant_id": self.ProjectId,
  296. "tenant": self.Project,
  297. "status": self.Status,
  298. }
  299. return AppendMetricTags(ret, self.MetadataResourceInfo, self.ProjectizedResourceInfo)
  300. }
  301. func (self CloudaccountDetail) GetMetricPairs() map[string]string {
  302. ret := map[string]string{
  303. "balance": fmt.Sprintf("%.2f", self.Balance),
  304. }
  305. return ret
  306. }
  307. type CloudaccountUpdateInput struct {
  308. apis.EnabledStatusInfrasResourceBaseUpdateInput
  309. // 待更新的options key/value
  310. Options *jsonutils.JSONDict `json:"options"`
  311. // 带删除的options key
  312. RemoveOptions []string `json:"remove_options"`
  313. SAMLAuth *bool `json:"saml_auth"`
  314. proxyapi.ProxySettingResourceInput
  315. // 临时清除缺失的权限提示,云账号权限缺失依然会自动刷新
  316. CleanLakeOfPermissions bool `json:"clean_lake_of_permissions"`
  317. SkipSyncResources *SkipSyncResources `json:"skip_sync_resources"`
  318. AddSkipSyncResources []string `json:"add_skip_sync_resources"`
  319. RemoveSkipSyncResources []string `json:"remove_skip_sync_resources"`
  320. ReadOnly bool `json:"read_only"`
  321. Currency string `json:"currency"`
  322. EnableAutoSyncResource *bool `json:"enable_auto_sync_resource"`
  323. }
  324. type CloudaccountPerformPublicInput struct {
  325. apis.PerformPublicDomainInput
  326. // 共享模式,可能值为provider_domain, system
  327. // example: provider_domain
  328. ShareMode string `json:"share_mode"`
  329. }
  330. type CloudaccountPerformPrepareNetsInput struct {
  331. CloudaccountCreateInput
  332. WireLevelForVmware string `json:"wire_level_for_vmware"`
  333. Dvs bool `json:"dvs"`
  334. }
  335. type CloudaccountPerformPrepareNetsOutput struct {
  336. CAWireNets []CAWireNet `json:"wire_networks"`
  337. Hosts []CAGuestNet `json:"hosts"`
  338. // Guests []CAGuestNet `json:"guests"`
  339. Wires []CAPWire `json:"wires"`
  340. VSwitchs []VSwitch `json:"vswitchs"`
  341. }
  342. type CloudaccountSyncVMwareNetworkInput struct {
  343. Zone string `help:"zone Id or Name" json:"zone"`
  344. }
  345. type CAPWire struct {
  346. Id string `json:"id"`
  347. Name string `json:"name"`
  348. Distributed bool `json:"distributed"`
  349. Hosts []SimpleHost `json:"hosts"`
  350. HostNetworks []CANetConf `json:"host_networks"`
  351. GuestNetworks []CANetConf `json:"guest_networks"`
  352. }
  353. type VSwitch struct {
  354. Id string `json:"id"`
  355. Name string `json:"name"`
  356. Distributed bool `json:"distributed"`
  357. Hosts []SimpleHost `json:"hosts"`
  358. HostNetworks []CANetConf `json:"host_networks"`
  359. GuestNetworks []CANetConf `json:"guest_networks"`
  360. }
  361. type SimpleHost struct {
  362. Id string `json:"id"`
  363. Name string `json:"name"`
  364. }
  365. type CAWireNet struct {
  366. SuggestedWire CAWireConf `json:"suggested_wire"`
  367. SuitableWire string `json:"suitable_wire,allowempty"`
  368. Hosts []CAHostNet `json:"hosts"`
  369. // description: 没有合适的已有网络,推荐的网络配置
  370. HostSuggestedNetworks []CANetConf `json:"host_suggested_networks"`
  371. // Guests []CAGuestNet `json:"guests"`
  372. // description: 没有合适的已有网络,推荐的网络配置
  373. // GuestSuggestedNetworks []CANetConf `json:"guest_suggested_networks"`
  374. }
  375. type CAWireConf struct {
  376. // Zoneids to be selected
  377. ZoneIds []string `json:"zone_ids"`
  378. // description: wire name
  379. Name string `json:"name"`
  380. // description: wire description
  381. Description string `json:"description"`
  382. }
  383. type CAHostNet struct {
  384. // description: Host 的 Name
  385. Name string `json:"name"`
  386. // description: IP
  387. IP string `json:"ip"`
  388. // description: 合适的已有网络
  389. SuitableNetwork string `json:"suitable_network,allowempty"`
  390. }
  391. type CAGuestNet struct {
  392. // description: Host 的 Name
  393. Name string `json:"name"`
  394. IPNets []CAIPNet `json:"ip_nets"`
  395. }
  396. type CAIPNet struct {
  397. // description: IP
  398. IP string `json:"ip"`
  399. VlanID int32 `json:"vlan_id"`
  400. // description: 合适的已有网络
  401. SuitableNetwork string `json:"suitable_network,allowempty"`
  402. }
  403. type CASimpleNetConf struct {
  404. GuestIpStart string `json:"guest_ip_start"`
  405. GuestIpEnd string `json:"guest_ip_end"`
  406. GuestIpMask int8 `json:"guest_ip_mask"`
  407. GuestGateway string `json:"guest_gateway"`
  408. VlanID int32 `json:"vlan_id"`
  409. }
  410. type CANetConf struct {
  411. Name string `json:"name"`
  412. Description string `json:"description"`
  413. CASimpleNetConf
  414. }
  415. type SubscriptonCreateInput struct {
  416. // EA 账号id, 可通过 cloud-account-enrollment-accounts接口获取里面的id字段
  417. EnrollmentAccountId string `json:"enrollment_account_id"`
  418. // 订阅名称
  419. Name string `json:"name"`
  420. // 可选值: MS-AZR-0017P(生产用途), MS-AZR-0148P(开发测试)
  421. OfferType string `json:"offer_type"`
  422. }
  423. type EnrollmentAccountQuery struct {
  424. }
  425. type CloudaccountSyncSkusInput struct {
  426. Resource string `json:"resource"`
  427. Force bool `json:"force"`
  428. CloudregionResourceInput
  429. CloudproviderResourceInput
  430. }
  431. type CloudaccountProjectMappingInput struct {
  432. AutoCreateProject bool `json:"auto_create_project"`
  433. AutoCreateProjectForProvider bool `json:"auto_create_project_for_provider"`
  434. ProjectId string `json:"project_id"`
  435. // 同步策略Id, 若不传此参数则解绑
  436. // 绑定同步策略要求当前云账号此刻未绑定其他同步策略
  437. ProjectMappingId string `json:"project_mapping_id"`
  438. EnableProjectSync *bool `json:"enable_project_sync"`
  439. EnableResourceSync *bool `json:"enable_resource_sync"`
  440. }
  441. type SyncRangeInput struct {
  442. Force bool `json:"force"`
  443. FullSync bool `json:"full_sync"`
  444. DeepSync bool `json:"deep_sync"`
  445. // 极速模式(差量更新)
  446. Xor bool `json:"xor"`
  447. Region []string `json:"region"`
  448. Zone []string `json:"zone"`
  449. Host []string `json:"host"`
  450. // swagger:ignore
  451. SkipSyncResources []string `json:"skip_sync_resources"`
  452. // 按资源类型同步,可输入多个
  453. // enmu: project, compute, network, eip, loadbalancer, objectstore, rds, cache, event, cloudid, dnszone, public_ip, intervpcnetwork, saml_auth, quota, nat, nas, waf, mongodb, es, kafka, app, cdn, container, ipv6_gateway, tablestore, modelarts, vpcpeer, misc
  454. Resources []string `json:"resources" choices:"project|compute|network|eip|loadbalancer|objectstore|rds|cache|event|cloudid|dnszone|public_ip|intervpcnetwork|saml_auth|quota|nat|nas|waf|mongodb|es|kafka|app|cdn|container|ipv6_gateway|tablestore|modelarts|vpcpeer|misc|image"`
  455. }
  456. type iRes interface {
  457. Keyword() string
  458. }
  459. func (self *SyncRangeInput) IsNotSkipSyncResource(res iRes) bool {
  460. return !utils.IsInStringArray(res.Keyword(), self.SkipSyncResources)
  461. }
  462. type SAccountPermission struct {
  463. Permissions []string `json:"permissions"`
  464. }
  465. type SkipSyncResources []string
  466. func (s SkipSyncResources) String() string {
  467. return jsonutils.Marshal(s).String()
  468. }
  469. func (s *SkipSyncResources) Add(res string) {
  470. if !utils.IsInStringArray(res, *s) {
  471. *s = append(*s, res)
  472. }
  473. }
  474. func (s *SkipSyncResources) Remove(res string) {
  475. ret := []string{}
  476. for _, r := range *s {
  477. if r != res {
  478. ret = append(ret, r)
  479. }
  480. }
  481. *s = ret
  482. }
  483. func (s SkipSyncResources) IsZero() bool {
  484. return len(s) == 0
  485. }
  486. type SAccountPermissions map[string]SAccountPermission
  487. func (s SAccountPermissions) String() string {
  488. return jsonutils.Marshal(s).String()
  489. }
  490. func (s SAccountPermissions) IsZero() bool {
  491. return len(s) == 0
  492. }
  493. func init() {
  494. gotypes.RegisterSerializable(reflect.TypeOf(&SAccountPermissions{}), func() gotypes.ISerializable {
  495. return &SAccountPermissions{}
  496. })
  497. gotypes.RegisterSerializable(reflect.TypeOf(&SkipSyncResources{}), func() gotypes.ISerializable {
  498. return &SkipSyncResources{}
  499. })
  500. }