resourcegroup.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. return groups, nil
  55. }
  56. // CreateOrUpdate create or update a resource group
  57. func (mgt aciResourceGroupHelperImpl) CreateOrUpdate(ctx context.Context, subscriptionID string, resourceGroupName string, parameters resources.Group) (result resources.Group, err error) {
  58. gc, err := getGroupsClient(subscriptionID)
  59. if err != nil {
  60. return resources.Group{}, err
  61. }
  62. return gc.CreateOrUpdate(ctx, resourceGroupName, parameters)
  63. }
  64. // Delete deletes a resource group
  65. func (mgt aciResourceGroupHelperImpl) Delete(ctx context.Context, subscriptionID string, resourceGroupName string) (err error) {
  66. gc, err := getGroupsClient(subscriptionID)
  67. if err != nil {
  68. return err
  69. }
  70. future, err := gc.Delete(ctx, resourceGroupName)
  71. if err != nil {
  72. return err
  73. }
  74. return future.WaitForCompletionRef(ctx, gc.Client)
  75. }
  76. // GetSubscriptionIDs Return available subscription IDs based on azure login
  77. func (mgt aciResourceGroupHelperImpl) GetSubscriptionIDs(ctx context.Context) ([]subscription.Model, error) {
  78. c, err := getSubscriptionsClient()
  79. if err != nil {
  80. return nil, err
  81. }
  82. res, err := c.List(ctx)
  83. if err != nil {
  84. return nil, err
  85. }
  86. subs := res.Values()
  87. if len(subs) == 0 {
  88. return nil, errors.New("no subscriptions found")
  89. }
  90. for res.NotDone() {
  91. err = res.NextWithContext(ctx)
  92. if err != nil {
  93. return nil, err
  94. }
  95. subs = append(subs, res.Values()...)
  96. }
  97. return subs, nil
  98. }
  99. func getSubscriptionsClient() (subscription.SubscriptionsClient, error) {
  100. subc := subscription.NewSubscriptionsClient()
  101. err := setupClient(&subc.Client)
  102. if err != nil {
  103. return subscription.SubscriptionsClient{}, errors.Wrap(errdefs.ErrLoginFailed, err.Error())
  104. }
  105. return subc, nil
  106. }
  107. func getGroupsClient(subscriptionID string) (resources.GroupsClient, error) {
  108. groupsClient := resources.NewGroupsClient(subscriptionID)
  109. err := setupClient(&groupsClient.Client)
  110. if err != nil {
  111. return resources.GroupsClient{}, err
  112. }
  113. return groupsClient, nil
  114. }