context.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 ContextParams) (interface{}, string, error) {
  37. var subscriptionID string
  38. if opts.SubscriptionID != "" {
  39. subscriptionID = opts.SubscriptionID
  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.ResourceGroup != "" {
  53. group, err = helper.resourceGroupHelper.GetGroup(ctx, subscriptionID, opts.ResourceGroup)
  54. if err != nil {
  55. return nil, "", errors.Wrapf(err, "Could not find resource group %q", opts.ResourceGroup)
  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 := *group.Location
  68. description := fmt.Sprintf("%s@%s", *group.Name, location)
  69. if opts.Description != "" {
  70. description = fmt.Sprintf("%s (%s)", opts.Description, description)
  71. }
  72. return store.AciContext{
  73. SubscriptionID: subscriptionID,
  74. Location: location,
  75. ResourceGroup: *group.Name,
  76. }, description, nil
  77. }
  78. func (helper contextCreateACIHelper) createGroup(ctx context.Context, subscriptionID, location string) (resources.Group, error) {
  79. if location == "" {
  80. location = "eastus"
  81. }
  82. gid := uuid.New().String()
  83. g, err := helper.resourceGroupHelper.CreateOrUpdate(ctx, subscriptionID, gid, resources.Group{
  84. Location: &location,
  85. })
  86. if err != nil {
  87. return resources.Group{}, err
  88. }
  89. fmt.Printf("Resource group %q (%s) created\n", *g.Name, *g.Location)
  90. return g, nil
  91. }
  92. func (helper contextCreateACIHelper) chooseGroup(ctx context.Context, subscriptionID string, opts ContextParams, groups []resources.Group) (resources.Group, error) {
  93. groupNames := []string{"create a new resource group"}
  94. for _, g := range groups {
  95. groupNames = append(groupNames, fmt.Sprintf("%s (%s)", *g.Name, *g.Location))
  96. }
  97. group, err := helper.selector.userSelect("Select a resource group", groupNames)
  98. if err != nil {
  99. if err == terminal.InterruptErr {
  100. os.Exit(0)
  101. }
  102. return resources.Group{}, err
  103. }
  104. if group == 0 {
  105. return helper.createGroup(ctx, subscriptionID, opts.Location)
  106. }
  107. return groups[group-1], nil
  108. }
  109. func (helper contextCreateACIHelper) chooseSub(subs []subscription.Model) (string, error) {
  110. if len(subs) == 1 {
  111. sub := subs[0]
  112. fmt.Println("Using only available subscription : " + display(sub))
  113. return *sub.SubscriptionID, nil
  114. }
  115. var options []string
  116. for _, sub := range subs {
  117. options = append(options, display(sub))
  118. }
  119. selected, err := helper.selector.userSelect("Select a subscription ID", options)
  120. if err != nil {
  121. if err == terminal.InterruptErr {
  122. os.Exit(0)
  123. }
  124. return "", err
  125. }
  126. return *subs[selected].SubscriptionID, nil
  127. }
  128. func display(sub subscription.Model) string {
  129. return fmt.Sprintf("%s (%s)", *sub.DisplayName, *sub.SubscriptionID)
  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. }