func AskSecurityGroups()

in pkg/question/question.go [728:802]


func AskSecurityGroups(groups []*ec2.SecurityGroup, addedGroups []string) string {
	question := "Security Group(s)"
	data := [][]string{}
	indexedOptions := []string{}
	var defaultOptionRepr, defaultOptionValue *string = nil, nil

	// Add security groups to the data for table
	if groups != nil {
		counter := 0
		for _, group := range groups {
			// If this security group is already added, just don't display it here
			isFound := false
			for _, addedGroupId := range addedGroups {
				if addedGroupId == *group.GroupId {
					isFound = true
					break
				}
			}
			if isFound {
				continue
			}

			indexedOptions = append(indexedOptions, *group.GroupId)

			groupName := *group.GroupId
			groupTagName := ec2helper.GetTagName(group.Tags)
			if groupTagName != nil {
				groupName = fmt.Sprintf("%s(%s)", *groupTagName, *group.GroupId)
			}

			if *group.GroupName == "default" {
				defaultOptionRepr, defaultOptionValue = &groupName, group.GroupId
			}

			data = append(data, []string{fmt.Sprintf("%d.", counter+1), groupName,
				*group.Description})
			counter++
		}
	}

	// If no security group is available, simply don't ask
	if len(data) <= 0 {
		return cli.ResponseNo
	}

	// Add "add all" option
	indexedOptions = append(indexedOptions, cli.ResponseAll)
	data = append(data, []string{fmt.Sprintf("%d.", len(data)+1),
		"Add all available security groups"})

	// Add "new" option
	indexedOptions = append(indexedOptions, cli.ResponseNew)
	data = append(data, []string{fmt.Sprintf("%d.", len(data)+1),
		"Create a new security group that enables SSH"})

	// Add "done" option, if the added security group slice is not empty
	if len(addedGroups) > 0 {
		question = fmt.Sprintf("If you wish to add additional security group(s), add from the following:\nSecurity Group(s) already selected: %s", addedGroups)
		indexedOptions = append(indexedOptions, cli.ResponseNo)
		data = append(data, []string{fmt.Sprintf("%d.", len(data)+1),
			"Don't add any more security group"})
	}

	optionsText := table.BuildTable(data, []string{"Option", "Security Group", "Description"})

	answer := AskQuestion(&AskQuestionInput{
		QuestionString:    question,
		DefaultOptionRepr: defaultOptionRepr,
		DefaultOption:     defaultOptionValue,
		OptionsString:     &optionsText,
		IndexedOptions:    indexedOptions,
	})

	return answer
}