user.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // Copyright 2023 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 volcengine
  15. import (
  16. "fmt"
  17. api "yunion.io/x/cloudmux/pkg/apis/cloudid"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/utils"
  22. )
  23. type SUser struct {
  24. multicloud.SBaseClouduser
  25. client *SVolcEngineClient
  26. Id int
  27. CreateDate string
  28. UpdateDate string
  29. Status string
  30. AccountId string
  31. UserName string
  32. Description string
  33. DisplayName string
  34. Email string
  35. EmailIsVerify bool
  36. MobilePhone string
  37. MobilePhoneIsVerify bool
  38. Trn string
  39. Source string
  40. }
  41. func (user *SUser) GetGlobalId() string {
  42. return user.UserName
  43. }
  44. func (user *SUser) GetName() string {
  45. return user.UserName
  46. }
  47. func (user *SUser) GetEmailAddr() string {
  48. return user.Email
  49. }
  50. func (user *SUser) GetInviteUrl() string {
  51. return ""
  52. }
  53. func (user *SUser) Delete() error {
  54. return user.client.DeleteUser(user.UserName)
  55. }
  56. func (user *SUser) GetICloudgroups() ([]cloudprovider.ICloudgroup, error) {
  57. groups, err := user.client.ListGroupsForUser(user.UserName)
  58. if err != nil {
  59. return nil, err
  60. }
  61. ret := []cloudprovider.ICloudgroup{}
  62. for i := range groups {
  63. groups[i].client = user.client
  64. ret = append(ret, &groups[i])
  65. }
  66. return ret, nil
  67. }
  68. func (user *SUser) GetICloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
  69. policies, err := user.client.ListAttachedUserPolicies(user.UserName)
  70. if err != nil {
  71. return nil, err
  72. }
  73. ret := []cloudprovider.ICloudpolicy{}
  74. for i := range policies {
  75. policies[i].client = user.client
  76. ret = append(ret, &policies[i])
  77. }
  78. return ret, nil
  79. }
  80. func (user *SUser) SetDisable() error {
  81. return user.client.DeleteLoginProfile(user.UserName)
  82. }
  83. func (user *SUser) SetEnable(opts *cloudprovider.SClouduserEnableOptions) error {
  84. login := true
  85. return user.client.UpdateLoginProfile(user.UserName, opts.Password, &login, &opts.PasswordResetRequired, &opts.EnableMfa)
  86. }
  87. func (user *SUser) IsConsoleLogin() bool {
  88. profile, err := user.client.GetLoginProfile(user.UserName)
  89. if err != nil {
  90. return false
  91. }
  92. return profile.LoginAllowed
  93. }
  94. func (user *SUser) ResetPassword(password string) error {
  95. return user.client.UpdateLoginProfile(user.UserName, password, nil, nil, nil)
  96. }
  97. func (user *SUser) AttachPolicy(policyName string, policyType api.TPolicyType) error {
  98. return user.client.AttachUserPolicy(user.UserName, policyName, utils.Capitalize(string(policyType)))
  99. }
  100. func (user *SUser) DetachPolicy(policyName string, policyType api.TPolicyType) error {
  101. return user.client.DetachUserPolicy(user.UserName, policyName, utils.Capitalize(string(policyType)))
  102. }
  103. func (client *SVolcEngineClient) GetUsers() ([]SUser, error) {
  104. params := map[string]string{
  105. "Limit": "50",
  106. }
  107. offset := 0
  108. ret := []SUser{}
  109. for {
  110. params["Offset"] = fmt.Sprintf("%d", offset)
  111. resp, err := client.iamRequest("", "ListUsers", params)
  112. if err != nil {
  113. return nil, err
  114. }
  115. part := struct {
  116. UserMetadata []SUser
  117. Total int
  118. }{}
  119. err = resp.Unmarshal(&part)
  120. if err != nil {
  121. return nil, err
  122. }
  123. ret = append(ret, part.UserMetadata...)
  124. if len(part.UserMetadata) == 0 || len(ret) >= part.Total {
  125. break
  126. }
  127. offset = len(ret)
  128. }
  129. return ret, nil
  130. }
  131. func (self *SVolcEngineClient) DeleteUser(name string) error {
  132. params := map[string]string{
  133. "UserName": name,
  134. }
  135. _, err := self.iamRequest("", "DeleteUser", params)
  136. return err
  137. }
  138. func (client *SVolcEngineClient) GetICloudusers() ([]cloudprovider.IClouduser, error) {
  139. users, err := client.GetUsers()
  140. if err != nil {
  141. return nil, err
  142. }
  143. ret := []cloudprovider.IClouduser{}
  144. for i := range users {
  145. users[i].client = client
  146. ret = append(ret, &users[i])
  147. }
  148. return ret, nil
  149. }
  150. func (client *SVolcEngineClient) CreateIClouduser(opts *cloudprovider.SClouduserCreateConfig) (cloudprovider.IClouduser, error) {
  151. user, err := client.CreateUser(opts)
  152. if err != nil {
  153. return nil, err
  154. }
  155. err = client.CreateLoginProfile(user.UserName, opts.Password, &opts.IsConsoleLogin)
  156. if err != nil {
  157. return nil, errors.Wrapf(err, "CreateLoginProfile")
  158. }
  159. return user, nil
  160. }
  161. func (self *SVolcEngineClient) CreateUser(opts *cloudprovider.SClouduserCreateConfig) (*SUser, error) {
  162. params := map[string]string{
  163. "UserName": opts.Name,
  164. "Description": opts.Desc,
  165. "Email": opts.Email,
  166. "MobilePhone": opts.MobilePhone,
  167. }
  168. resp, err := self.iamRequest("", "CreateUser", params)
  169. if err != nil {
  170. return nil, err
  171. }
  172. ret := &SUser{client: self}
  173. err = resp.Unmarshal(ret, "User")
  174. if err != nil {
  175. return nil, err
  176. }
  177. return ret, nil
  178. }
  179. type LoginProfile struct {
  180. PasswordResetRequired bool
  181. LoginAllowed bool
  182. LastLoginDate string
  183. }
  184. func (self *SVolcEngineClient) GetLoginProfile(name string) (*LoginProfile, error) {
  185. params := map[string]string{
  186. "UserName": name,
  187. }
  188. resp, err := self.iamRequest("", "GetLoginProfile", params)
  189. if err != nil {
  190. return nil, err
  191. }
  192. ret := &LoginProfile{}
  193. err = resp.Unmarshal(ret, "LoginProfile")
  194. if err != nil {
  195. return nil, err
  196. }
  197. return ret, nil
  198. }
  199. func (self *SVolcEngineClient) CreateLoginProfile(name, password string, loginAllowd *bool) error {
  200. params := map[string]string{
  201. "UserName": name,
  202. "Password": password,
  203. }
  204. if loginAllowd != nil {
  205. params["LoginAllowed"] = fmt.Sprintf("%v", *loginAllowd)
  206. }
  207. _, err := self.iamRequest("", "CreateLoginProfile", params)
  208. return err
  209. }
  210. func (self *SVolcEngineClient) DeleteLoginProfile(name string) error {
  211. params := map[string]string{
  212. "UserName": name,
  213. }
  214. _, err := self.iamRequest("", "DeleteLoginProfile", params)
  215. return err
  216. }
  217. func (self *SVolcEngineClient) UpdateLoginProfile(name, password string, loginAllowd *bool, reset, mfa *bool) error {
  218. params := map[string]string{
  219. "UserName": name,
  220. "Password": password,
  221. }
  222. if loginAllowd != nil {
  223. params["LoginAllowed"] = fmt.Sprintf("%v", *loginAllowd)
  224. }
  225. if reset != nil {
  226. params["PasswordResetRequired"] = "false"
  227. if *reset {
  228. params["PasswordResetRequired"] = "true"
  229. }
  230. }
  231. if mfa != nil {
  232. params["SafeAuthFlag"] = "false"
  233. if *mfa {
  234. params["SafeAuthFlag"] = "true"
  235. params["SafeAuthType"] = "vmfa"
  236. }
  237. }
  238. _, err := self.iamRequest("", "UpdateLoginProfile", params)
  239. return err
  240. }
  241. func (client *SVolcEngineClient) ListGroupsForUser(name string) ([]SGroup, error) {
  242. params := map[string]string{
  243. "Limit": "50",
  244. "UserName": name,
  245. }
  246. offset := 0
  247. ret := []SGroup{}
  248. for {
  249. params["Offset"] = fmt.Sprintf("%d", offset)
  250. resp, err := client.iamRequest("", "ListGroupsForUser", params)
  251. if err != nil {
  252. return nil, err
  253. }
  254. part := struct {
  255. UserGroups []SGroup
  256. Total int
  257. }{}
  258. err = resp.Unmarshal(&part)
  259. if err != nil {
  260. return nil, err
  261. }
  262. ret = append(ret, part.UserGroups...)
  263. if len(part.UserGroups) == 0 || len(ret) >= part.Total {
  264. break
  265. }
  266. offset = len(ret)
  267. }
  268. return ret, nil
  269. }
  270. func (client *SVolcEngineClient) ListAttachedUserPolicies(name string) ([]SPolicy, error) {
  271. params := map[string]string{
  272. "UserName": name,
  273. }
  274. resp, err := client.iamRequest("", "ListAttachedUserPolicies", params)
  275. if err != nil {
  276. return nil, err
  277. }
  278. ret := []SPolicy{}
  279. err = resp.Unmarshal(&ret, "AttachedPolicyMetadata")
  280. if err != nil {
  281. return nil, err
  282. }
  283. return ret, nil
  284. }
  285. func (client *SVolcEngineClient) AttachUserPolicy(name, policy, policyType string) error {
  286. params := map[string]string{
  287. "UserName": name,
  288. "PolicyName": policy,
  289. "PolicyType": policyType,
  290. }
  291. _, err := client.iamRequest("", "AttachUserPolicy", params)
  292. return err
  293. }
  294. func (client *SVolcEngineClient) DetachUserPolicy(name, policy, policyType string) error {
  295. params := map[string]string{
  296. "UserName": name,
  297. "PolicyName": policy,
  298. "PolicyType": policyType,
  299. }
  300. _, err := client.iamRequest("", "DetachUserPolicy", params)
  301. return err
  302. }
  303. func (client *SVolcEngineClient) GetIClouduserByName(name string) (cloudprovider.IClouduser, error) {
  304. user, err := client.GetUser(name)
  305. if err != nil {
  306. return nil, err
  307. }
  308. return user, nil
  309. }
  310. func (client *SVolcEngineClient) GetUser(name string) (*SUser, error) {
  311. params := map[string]string{
  312. "UserName": name,
  313. }
  314. resp, err := client.iamRequest("", "GetUser", params)
  315. if err != nil {
  316. return nil, err
  317. }
  318. ret := &SUser{client: client}
  319. err = resp.Unmarshal(ret, "User")
  320. if err != nil {
  321. return nil, err
  322. }
  323. return ret, nil
  324. }