cloudgroup.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/pkg/util/pinyinutils"
  21. api "yunion.io/x/cloudmux/pkg/apis/cloudid"
  22. "yunion.io/x/cloudmux/pkg/cloudprovider"
  23. )
  24. type SCloudgroup struct {
  25. client *SAzureClient
  26. Id string
  27. DeletionTimestamp string
  28. Description string
  29. DirSyncEnabled string
  30. DisplayName string
  31. LastDirSyncTime string
  32. Mail string
  33. MailNickname string
  34. MailEnabled bool
  35. ProxyAddresses []string
  36. }
  37. func (group *SCloudgroup) GetName() string {
  38. return group.DisplayName
  39. }
  40. func (group *SCloudgroup) GetGlobalId() string {
  41. return group.Id
  42. }
  43. func (group *SCloudgroup) GetDescription() string {
  44. return group.Description
  45. }
  46. func (group *SCloudgroup) GetICloudpolicies() ([]cloudprovider.ICloudpolicy, error) {
  47. policies, err := group.client.GetPrincipalPolicy(group.Id)
  48. if err != nil {
  49. return nil, errors.Wrapf(err, "GetCloudpolicies(%s)", group.Id)
  50. }
  51. ret := []cloudprovider.ICloudpolicy{}
  52. for i := range policies {
  53. ret = append(ret, &SCloudpolicy{Id: policies[i].RoleDefinitionId})
  54. }
  55. return ret, nil
  56. }
  57. func (group *SCloudgroup) GetICloudusers() ([]cloudprovider.IClouduser, error) {
  58. users, err := group.client.ListGroupMemebers(group.Id)
  59. if err != nil {
  60. return nil, errors.Wrap(err, "ListGroupMemebers")
  61. }
  62. ret := []cloudprovider.IClouduser{}
  63. for i := range users {
  64. users[i].client = group.client
  65. ret = append(ret, &users[i])
  66. }
  67. return ret, nil
  68. }
  69. func (group *SCloudgroup) AddUser(name string) error {
  70. return group.client.AddGroupUser(group.Id, name)
  71. }
  72. func (group *SCloudgroup) RemoveUser(name string) error {
  73. return group.client.RemoveGroupUser(group.Id, name)
  74. }
  75. func (group *SCloudgroup) AttachPolicy(policyId string, policyType api.TPolicyType) error {
  76. return group.client.AssignPolicy(group.Id, policyId)
  77. }
  78. func (group *SCloudgroup) DetachPolicy(policyId string, policyType api.TPolicyType) error {
  79. policys, err := group.client.GetPrincipalPolicy(group.Id)
  80. if err != nil {
  81. return err
  82. }
  83. for _, policy := range policys {
  84. if policy.RoleDefinitionId == policyId {
  85. return group.client.DeletePrincipalPolicy(policy.Id)
  86. }
  87. }
  88. return nil
  89. }
  90. func (group *SCloudgroup) Delete() error {
  91. return group.client.DeleteGroup(group.Id)
  92. }
  93. func (self *SAzureClient) GetCloudgroups(name string) ([]SCloudgroup, error) {
  94. params := url.Values{}
  95. if len(name) > 0 {
  96. params.Set("$filter", fmt.Sprintf("displayName eq '%s'", name))
  97. }
  98. resp, err := self._list_v2(SERVICE_GRAPH, "groups", "", params)
  99. if err != nil {
  100. return nil, err
  101. }
  102. groups := []SCloudgroup{}
  103. err = resp.Unmarshal(&groups, "value")
  104. if err != nil {
  105. return nil, err
  106. }
  107. return groups, nil
  108. }
  109. func (self *SAzureClient) GetICloudgroups() ([]cloudprovider.ICloudgroup, error) {
  110. groups, err := self.GetCloudgroups("")
  111. if err != nil {
  112. return nil, errors.Wrap(err, "GetCloudgroups")
  113. }
  114. ret := []cloudprovider.ICloudgroup{}
  115. for i := range groups {
  116. groups[i].client = self
  117. ret = append(ret, &groups[i])
  118. }
  119. return ret, nil
  120. }
  121. func (self *SAzureClient) GetICloudgroupByName(name string) (cloudprovider.ICloudgroup, error) {
  122. groups, err := self.GetCloudgroups(name)
  123. if err != nil {
  124. return nil, errors.Wrapf(err, "GetCloudgroups(%s)", name)
  125. }
  126. if len(groups) == 0 {
  127. return nil, cloudprovider.ErrNotFound
  128. }
  129. if len(groups) > 1 {
  130. return nil, cloudprovider.ErrDuplicateId
  131. }
  132. groups[0].client = self
  133. return &groups[0], nil
  134. }
  135. func (self *SAzureClient) ListGroupMemebers(id string) ([]SClouduser, error) {
  136. resource := fmt.Sprintf("groups/%s/members", id)
  137. resp, err := self._list_v2(SERVICE_GRAPH, resource, "", nil)
  138. if err != nil {
  139. return nil, err
  140. }
  141. users := []SClouduser{}
  142. err = resp.Unmarshal(&users, "value")
  143. if err != nil {
  144. return nil, err
  145. }
  146. return users, nil
  147. }
  148. func (self *SAzureClient) DeleteGroup(id string) error {
  149. _, err := self._delete_v2(SERVICE_GRAPH, "groups/"+id, "")
  150. return err
  151. }
  152. func (self *SAzureClient) CreateGroup(name, desc string) (*SCloudgroup, error) {
  153. params := map[string]interface{}{
  154. "displayName": name,
  155. "mailNickname": pinyinutils.Text2Pinyin(name),
  156. "mailEnabled": false,
  157. "securityEnabled": true,
  158. }
  159. if len(desc) > 0 {
  160. params["Description"] = desc
  161. }
  162. resp, err := self._post_v2(SERVICE_GRAPH, "groups", "", params)
  163. if err != nil {
  164. return nil, err
  165. }
  166. group := &SCloudgroup{client: self}
  167. err = resp.Unmarshal(group)
  168. if err != nil {
  169. return nil, err
  170. }
  171. return group, nil
  172. }
  173. func (self *SAzureClient) RemoveGroupUser(id, userName string) error {
  174. user, err := self.GetClouduser(userName)
  175. if err != nil {
  176. return errors.Wrapf(err, "GetCloudusers(%s)", userName)
  177. }
  178. resource := fmt.Sprintf("/groups/%s/members/%s/$ref", id, user.Id)
  179. _, err = self._delete_v2(SERVICE_GRAPH, resource, "")
  180. return err
  181. }
  182. func (self *SAzureClient) CreateICloudgroup(name, desc string) (cloudprovider.ICloudgroup, error) {
  183. group, err := self.CreateGroup(name, desc)
  184. if err != nil {
  185. return nil, errors.Wrap(err, "CreateGroup")
  186. }
  187. group.client = self
  188. return group, nil
  189. }
  190. func (self *SAzureClient) AddGroupUser(id, userName string) error {
  191. user, err := self.GetClouduser(userName)
  192. if err != nil {
  193. return errors.Wrapf(err, "GetCloudusers(%s)", userName)
  194. }
  195. params := map[string]interface{}{
  196. "@odata.id": fmt.Sprintf("https://graph.microsoft.com/v1.0/directoryObjects/%s", user.Id),
  197. }
  198. if self.envName == "AzureChinaCloud" {
  199. params["@odata.id"] = fmt.Sprintf("https://microsoftgraph.chinacloudapi.cn/v1.0/directoryObjects/%s", user.Id)
  200. }
  201. resource := fmt.Sprintf("groups/%s/members/$ref", id)
  202. _, err = self._post_v2(SERVICE_GRAPH, resource, "", params)
  203. if err != nil && !strings.Contains(err.Error(), "One or more added object references already exist for the following modified properties") {
  204. return err
  205. }
  206. return nil
  207. }