context.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright (c) 2020 Docker Inc.
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use, copy,
  7. modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be
  11. included in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. EXPRESS OR IMPLIED,
  14. INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM,
  18. DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT,
  20. TORT OR OTHERWISE,
  21. ARISING FROM, OUT OF OR IN CONNECTION WITH
  22. THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. package azure
  25. import (
  26. "context"
  27. "fmt"
  28. "os"
  29. "github.com/AlecAivazis/survey/v2"
  30. "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/subscription/mgmt/subscription"
  31. "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources"
  32. "github.com/google/uuid"
  33. "github.com/pkg/errors"
  34. "github.com/tj/survey/terminal"
  35. "github.com/docker/api/context/store"
  36. )
  37. type contextCreateACIHelper struct {
  38. selector userSelector
  39. resourceGroupHelper ACIResourceGroupHelper
  40. }
  41. func newContextCreateHelper() contextCreateACIHelper {
  42. return contextCreateACIHelper{
  43. selector: cliUserSelector{},
  44. resourceGroupHelper: aciResourceGroupHelperImpl{},
  45. }
  46. }
  47. func (helper contextCreateACIHelper) createContextData(ctx context.Context, opts map[string]string) (interface{}, string, error) {
  48. var subscriptionID string
  49. if opts["aciSubscriptionID"] != "" {
  50. subscriptionID = opts["aciSubscriptionID"]
  51. } else {
  52. subs, err := helper.resourceGroupHelper.GetSubscriptionIDs(ctx)
  53. if err != nil {
  54. return nil, "", err
  55. }
  56. subscriptionID, err = helper.chooseSub(subs)
  57. if err != nil {
  58. return nil, "", err
  59. }
  60. }
  61. var group resources.Group
  62. var err error
  63. if opts["aciResourceGroup"] != "" {
  64. group, err = helper.resourceGroupHelper.GetGroup(ctx, subscriptionID, opts["aciResourceGroup"])
  65. if err != nil {
  66. return nil, "", errors.Wrapf(err, "Could not find resource group %q", opts["aciResourceGroup"])
  67. }
  68. } else {
  69. groups, err := helper.resourceGroupHelper.ListGroups(ctx, subscriptionID)
  70. if err != nil {
  71. return nil, "", err
  72. }
  73. group, err = helper.chooseGroup(ctx, subscriptionID, opts, groups)
  74. if err != nil {
  75. return nil, "", err
  76. }
  77. }
  78. location := opts["aciLocation"]
  79. if location == "" {
  80. location = *group.Location
  81. }
  82. description := fmt.Sprintf("%s@%s", *group.Name, location)
  83. if opts["description"] != "" {
  84. description = fmt.Sprintf("%s (%s)", opts["description"], description)
  85. }
  86. return store.AciContext{
  87. SubscriptionID: subscriptionID,
  88. Location: location,
  89. ResourceGroup: *group.Name,
  90. }, description, nil
  91. }
  92. func (helper contextCreateACIHelper) createGroup(ctx context.Context, subscriptionID, location string) (resources.Group, error) {
  93. if location == "" {
  94. location = "eastus"
  95. }
  96. gid := uuid.New().String()
  97. g, err := helper.resourceGroupHelper.CreateOrUpdate(ctx, subscriptionID, gid, resources.Group{
  98. Location: &location,
  99. })
  100. if err != nil {
  101. return resources.Group{}, err
  102. }
  103. fmt.Printf("Resource group %q (%s) created\n", *g.Name, *g.Location)
  104. return g, nil
  105. }
  106. func (helper contextCreateACIHelper) chooseGroup(ctx context.Context, subscriptionID string, opts map[string]string, groups []resources.Group) (resources.Group, error) {
  107. groupNames := []string{"create a new resource group"}
  108. for _, g := range groups {
  109. groupNames = append(groupNames, fmt.Sprintf("%s (%s)", *g.Name, *g.Location))
  110. }
  111. group, err := helper.selector.userSelect("Select a resource group", groupNames)
  112. if err != nil {
  113. if err == terminal.InterruptErr {
  114. os.Exit(0)
  115. }
  116. return resources.Group{}, err
  117. }
  118. if group == 0 {
  119. return helper.createGroup(ctx, subscriptionID, opts["aciLocation"])
  120. }
  121. return groups[group-1], nil
  122. }
  123. func (helper contextCreateACIHelper) chooseSub(subs []subscription.Model) (string, error) {
  124. if len(subs) == 1 {
  125. sub := subs[0]
  126. fmt.Println("Using only available subscription : " + display(sub))
  127. return *sub.SubscriptionID, nil
  128. }
  129. var options []string
  130. for _, sub := range subs {
  131. options = append(options, display(sub))
  132. }
  133. selected, err := helper.selector.userSelect("Select a subscription ID", options)
  134. if err != nil {
  135. if err == terminal.InterruptErr {
  136. os.Exit(0)
  137. }
  138. return "", err
  139. }
  140. return *subs[selected].SubscriptionID, nil
  141. }
  142. func display(sub subscription.Model) string {
  143. return fmt.Sprintf("%s (%s)", *sub.DisplayName, *sub.SubscriptionID)
  144. }
  145. type userSelector interface {
  146. userSelect(message string, options []string) (int, error)
  147. }
  148. type cliUserSelector struct{}
  149. func (us cliUserSelector) userSelect(message string, options []string) (int, error) {
  150. qs := &survey.Select{
  151. Message: message,
  152. Options: options,
  153. }
  154. var selected int
  155. err := survey.AskOne(qs, &selected, nil)
  156. return selected, err
  157. }