context.go 4.9 KB

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