resourcegroup.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. DeleteAsync(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. // DeleteAsync deletes a resource group. Does not wait for full deletion to return (long operation)
  73. func (mgt aciResourceGroupHelperImpl) DeleteAsync(ctx context.Context, subscriptionID string, resourceGroupName string) (err error) {
  74. gc, err := getGroupsClient(subscriptionID)
  75. if err != nil {
  76. return err
  77. }
  78. _, err = gc.Delete(ctx, resourceGroupName)
  79. return err
  80. }
  81. // GetSubscriptionIDs Return available subscription IDs based on azure login
  82. func (mgt aciResourceGroupHelperImpl) GetSubscriptionIDs(ctx context.Context) ([]subscription.Model, error) {
  83. c, err := getSubscriptionsClient()
  84. if err != nil {
  85. return nil, err
  86. }
  87. res, err := c.List(ctx)
  88. if err != nil {
  89. return nil, err
  90. }
  91. subs := res.Values()
  92. if len(subs) == 0 {
  93. return nil, errors.New("no subscriptions found")
  94. }
  95. for res.NotDone() {
  96. err = res.NextWithContext(ctx)
  97. if err != nil {
  98. return nil, err
  99. }
  100. subs = append(subs, res.Values()...)
  101. }
  102. return subs, nil
  103. }
  104. func getSubscriptionsClient() (subscription.SubscriptionsClient, error) {
  105. subc := subscription.NewSubscriptionsClient()
  106. err := setupClient(&subc.Client)
  107. if err != nil {
  108. return subscription.SubscriptionsClient{}, errors.Wrap(errdefs.ErrLoginFailed, err.Error())
  109. }
  110. return subc, nil
  111. }
  112. func getGroupsClient(subscriptionID string) (resources.GroupsClient, error) {
  113. groupsClient := resources.NewGroupsClient(subscriptionID)
  114. err := setupClient(&groupsClient.Client)
  115. if err != nil {
  116. return resources.GroupsClient{}, err
  117. }
  118. return groupsClient, nil
  119. }