resourcegroup.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Copyright 2020 Docker, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package azure
  14. import (
  15. "context"
  16. "github.com/Azure/azure-sdk-for-go/profiles/2019-03-01/resources/mgmt/resources"
  17. "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
  18. "github.com/pkg/errors"
  19. "github.com/docker/api/errdefs"
  20. )
  21. // ACIResourceGroupHelper interface to manage resource groups and subscription IDs
  22. type ACIResourceGroupHelper interface {
  23. GetSubscriptionIDs(ctx context.Context) ([]subscription.Model, error)
  24. ListGroups(ctx context.Context, subscriptionID string) ([]resources.Group, error)
  25. GetGroup(ctx context.Context, subscriptionID string, groupName string) (resources.Group, error)
  26. CreateOrUpdate(ctx context.Context, subscriptionID string, resourceGroupName string, parameters resources.Group) (result resources.Group, err error)
  27. Delete(ctx context.Context, subscriptionID string, resourceGroupName string) error
  28. }
  29. type aciResourceGroupHelperImpl struct {
  30. }
  31. // NewACIResourceGroupHelper create a new ACIResourceGroupHelper
  32. func NewACIResourceGroupHelper() ACIResourceGroupHelper {
  33. return aciResourceGroupHelperImpl{}
  34. }
  35. // GetGroup get a resource group from its name
  36. func (mgt aciResourceGroupHelperImpl) GetGroup(ctx context.Context, subscriptionID string, groupName string) (resources.Group, error) {
  37. gc, err := getGroupsClient(subscriptionID)
  38. if err != nil {
  39. return resources.Group{}, err
  40. }
  41. return gc.Get(ctx, groupName)
  42. }
  43. // ListGroups list resource groups
  44. func (mgt aciResourceGroupHelperImpl) ListGroups(ctx context.Context, subscriptionID string) ([]resources.Group, error) {
  45. gc, err := getGroupsClient(subscriptionID)
  46. if err != nil {
  47. return nil, err
  48. }
  49. groupResponse, err := gc.List(ctx, "", nil)
  50. if err != nil {
  51. return nil, err
  52. }
  53. groups := groupResponse.Values()
  54. for groupResponse.NotDone() {
  55. err = groupResponse.NextWithContext(ctx)
  56. if err != nil {
  57. return nil, err
  58. }
  59. newValues := groupResponse.Values()
  60. groups = append(groups, newValues...)
  61. }
  62. return groups, nil
  63. }
  64. // CreateOrUpdate create or update a resource group
  65. func (mgt aciResourceGroupHelperImpl) CreateOrUpdate(ctx context.Context, subscriptionID string, resourceGroupName string, parameters resources.Group) (result resources.Group, err error) {
  66. gc, err := getGroupsClient(subscriptionID)
  67. if err != nil {
  68. return resources.Group{}, err
  69. }
  70. return gc.CreateOrUpdate(ctx, resourceGroupName, parameters)
  71. }
  72. // Delete deletes a resource group
  73. func (mgt aciResourceGroupHelperImpl) Delete(ctx context.Context, subscriptionID string, resourceGroupName string) (err error) {
  74. gc, err := getGroupsClient(subscriptionID)
  75. if err != nil {
  76. return err
  77. }
  78. future, err := gc.Delete(ctx, resourceGroupName)
  79. if err != nil {
  80. return err
  81. }
  82. return future.WaitForCompletionRef(ctx, gc.Client)
  83. }
  84. // GetSubscriptionIDs Return available subscription IDs based on azure login
  85. func (mgt aciResourceGroupHelperImpl) GetSubscriptionIDs(ctx context.Context) ([]subscription.Model, error) {
  86. c, err := getSubscriptionsClient()
  87. if err != nil {
  88. return nil, err
  89. }
  90. res, err := c.List(ctx)
  91. if err != nil {
  92. return nil, err
  93. }
  94. subs := res.Values()
  95. if len(subs) == 0 {
  96. return nil, errors.New("no subscriptions found")
  97. }
  98. for res.NotDone() {
  99. err = res.NextWithContext(ctx)
  100. if err != nil {
  101. return nil, err
  102. }
  103. subs = append(subs, res.Values()...)
  104. }
  105. return subs, nil
  106. }
  107. func getSubscriptionsClient() (subscription.SubscriptionsClient, error) {
  108. subc := subscription.NewSubscriptionsClient()
  109. err := setupClient(&subc.Client)
  110. if err != nil {
  111. return subscription.SubscriptionsClient{}, errors.Wrap(errdefs.ErrLoginFailed, err.Error())
  112. }
  113. return subc, nil
  114. }
  115. func getGroupsClient(subscriptionID string) (resources.GroupsClient, error) {
  116. groupsClient := resources.NewGroupsClient(subscriptionID)
  117. err := setupClient(&groupsClient.Client)
  118. if err != nil {
  119. return resources.GroupsClient{}, err
  120. }
  121. return groupsClient, nil
  122. }