resourcegroup.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/jsonutils"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. )
  23. type GroupProperties struct {
  24. ProvisioningState string
  25. }
  26. type SResourceGroup struct {
  27. multicloud.SProjectBase
  28. AzureTags
  29. client *SAzureClient
  30. ID string
  31. Name string
  32. Location string
  33. Properties GroupProperties
  34. ManagedBy string
  35. subId string
  36. }
  37. func (self *SRegion) GetResourceGroupDetail(groupName string) (*SResourceGroup, error) {
  38. resourceGroup := SResourceGroup{client: self.client, subId: self.client._subscriptionId()}
  39. idStr := fmt.Sprintf("subscriptions/%s/resourcegroups/%s", self.client._subscriptionId(), groupName)
  40. return &resourceGroup, self.get(idStr, url.Values{}, &resourceGroup)
  41. }
  42. // not support update, resource group name is immutable???
  43. func (self *SRegion) UpdateResourceGroup(groupName string, newName string) error {
  44. resourceGroup := SResourceGroup{Name: newName, client: self.client, subId: self.client._subscriptionId()}
  45. resource := fmt.Sprintf("subscriptions/%s/resourcegroups/%s", self.client.subscriptionId, groupName)
  46. _, err := self.client.patch(resource, jsonutils.Marshal(&resourceGroup))
  47. return err
  48. }
  49. func (self *SRegion) CreateResourceGroup(groupName string) (jsonutils.JSONObject, error) {
  50. resourceGroup := SResourceGroup{Location: self.Name, client: self.client, subId: self.client._subscriptionId()}
  51. idStr := fmt.Sprintf("subscriptions/%s/resourcegroups/%s", self.client._subscriptionId(), groupName)
  52. return self.client.put(idStr, jsonutils.Marshal(resourceGroup))
  53. }
  54. func (self *SRegion) DeleteResourceGroup(groupName string) error {
  55. idStr := fmt.Sprintf("subscriptions/%s/resourcegroups/%s", self.client._subscriptionId(), groupName)
  56. return self.del(idStr)
  57. }
  58. func (r *SResourceGroup) GetName() string {
  59. return r.Name
  60. }
  61. func (r *SResourceGroup) GetId() string {
  62. return r.ID
  63. }
  64. func (self *SResourceGroup) GetAccountId() string {
  65. return fmt.Sprintf("%s/%s", self.client.tenantId, self.subId)
  66. }
  67. func (r *SResourceGroup) GetGlobalId() string {
  68. return strings.ToLower(fmt.Sprintf("%s/%s", r.subId, r.Name))
  69. }
  70. func (r *SResourceGroup) GetStatus() string {
  71. return api.EXTERNAL_PROJECT_STATUS_AVAILABLE
  72. }