context.go 4.7 KB

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