context.go 4.7 KB

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