context.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 aci
  14. import (
  15. "context"
  16. "fmt"
  17. "os"
  18. "github.com/AlecAivazis/survey/v2/terminal"
  19. "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
  20. "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
  21. "github.com/google/uuid"
  22. "github.com/pkg/errors"
  23. "github.com/docker/compose-cli/context/store"
  24. "github.com/docker/compose-cli/errdefs"
  25. "github.com/docker/compose-cli/prompt"
  26. )
  27. // ContextParams options for creating ACI context
  28. type ContextParams struct {
  29. Description string
  30. Location string
  31. SubscriptionID string
  32. ResourceGroup string
  33. }
  34. // ErrSubscriptionNotFound is returned when a required subscription is not found
  35. var ErrSubscriptionNotFound = errors.Wrapf(errdefs.ErrNotFound, "subscription")
  36. // IsSubscriptionNotFoundError returns true if the unwrapped error is IsSubscriptionNotFoundError
  37. func IsSubscriptionNotFoundError(err error) bool {
  38. return errors.Is(err, ErrSubscriptionNotFound)
  39. }
  40. type contextCreateACIHelper struct {
  41. selector prompt.UI
  42. resourceGroupHelper ResourceGroupHelper
  43. }
  44. func newContextCreateHelper() contextCreateACIHelper {
  45. return contextCreateACIHelper{
  46. selector: prompt.User{},
  47. resourceGroupHelper: aciResourceGroupHelperImpl{},
  48. }
  49. }
  50. func (helper contextCreateACIHelper) createContextData(ctx context.Context, opts ContextParams) (interface{}, string, error) {
  51. subs, err := helper.resourceGroupHelper.GetSubscriptionIDs(ctx)
  52. if err != nil {
  53. return nil, "", err
  54. }
  55. subscriptionID := ""
  56. if opts.SubscriptionID != "" {
  57. for _, sub := range subs {
  58. if *sub.SubscriptionID == opts.SubscriptionID {
  59. subscriptionID = opts.SubscriptionID
  60. }
  61. }
  62. if subscriptionID == "" {
  63. return nil, "", ErrSubscriptionNotFound
  64. }
  65. } else {
  66. subscriptionID, err = helper.chooseSub(subs)
  67. if err != nil {
  68. return nil, "", err
  69. }
  70. }
  71. var group resources.Group
  72. if opts.ResourceGroup != "" {
  73. group, err = helper.resourceGroupHelper.GetGroup(ctx, subscriptionID, opts.ResourceGroup)
  74. if err != nil {
  75. return nil, "", errors.Wrapf(err, "Could not find resource group %q", opts.ResourceGroup)
  76. }
  77. } else {
  78. groups, err := helper.resourceGroupHelper.ListGroups(ctx, subscriptionID)
  79. if err != nil {
  80. return nil, "", err
  81. }
  82. group, err = helper.chooseGroup(ctx, subscriptionID, opts, groups)
  83. if err != nil {
  84. return nil, "", err
  85. }
  86. }
  87. location := *group.Location
  88. description := fmt.Sprintf("%s@%s", *group.Name, location)
  89. if opts.Description != "" {
  90. description = fmt.Sprintf("%s (%s)", opts.Description, description)
  91. }
  92. return store.AciContext{
  93. SubscriptionID: subscriptionID,
  94. Location: location,
  95. ResourceGroup: *group.Name,
  96. }, description, nil
  97. }
  98. func (helper contextCreateACIHelper) createGroup(ctx context.Context, subscriptionID, location string) (resources.Group, error) {
  99. if location == "" {
  100. location = "eastus"
  101. }
  102. gid := uuid.New().String()
  103. g, err := helper.resourceGroupHelper.CreateOrUpdate(ctx, subscriptionID, gid, resources.Group{
  104. Location: &location,
  105. })
  106. if err != nil {
  107. return resources.Group{}, err
  108. }
  109. fmt.Printf("Resource group %q (%s) created\n", *g.Name, *g.Location)
  110. return g, nil
  111. }
  112. func (helper contextCreateACIHelper) chooseGroup(ctx context.Context, subscriptionID string, opts ContextParams, groups []resources.Group) (resources.Group, error) {
  113. groupNames := []string{"create a new resource group"}
  114. for _, g := range groups {
  115. groupNames = append(groupNames, fmt.Sprintf("%s (%s)", *g.Name, *g.Location))
  116. }
  117. group, err := helper.selector.Select("Select a resource group", groupNames)
  118. if err != nil {
  119. if err == terminal.InterruptErr {
  120. return resources.Group{}, errdefs.ErrCanceled
  121. }
  122. return resources.Group{}, err
  123. }
  124. if group == 0 {
  125. return helper.createGroup(ctx, subscriptionID, opts.Location)
  126. }
  127. return groups[group-1], nil
  128. }
  129. func (helper contextCreateACIHelper) chooseSub(subs []subscription.Model) (string, error) {
  130. if len(subs) == 1 {
  131. sub := subs[0]
  132. fmt.Println("Using only available subscription : " + display(sub))
  133. return *sub.SubscriptionID, nil
  134. }
  135. var options []string
  136. for _, sub := range subs {
  137. options = append(options, display(sub))
  138. }
  139. selected, err := helper.selector.Select("Select a subscription ID", options)
  140. if err != nil {
  141. if err == terminal.InterruptErr {
  142. os.Exit(0)
  143. }
  144. return "", err
  145. }
  146. return *subs[selected].SubscriptionID, nil
  147. }
  148. func display(sub subscription.Model) string {
  149. return fmt.Sprintf("%s (%s)", *sub.DisplayName, *sub.SubscriptionID)
  150. }