|
| 1 | +package bandwidth |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/softlayer/softlayer-go/datatypes" |
| 5 | + "github.com/spf13/cobra" |
| 6 | + |
| 7 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/errors" |
| 8 | + . "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n" |
| 9 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/managers" |
| 10 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata" |
| 11 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/utils" |
| 12 | +) |
| 13 | + |
| 14 | +var LOCATION_GROUPS = map[string]string{ |
| 15 | + "SJC/DAL/WDC/TOR/MON": "US/Canada", |
| 16 | + "AMS/LON/MAD/PAR": "AMS/LON/MAD/PAR", |
| 17 | + "SNG/HKG/OSA/TOK": "SNG/HKG/JPN", |
| 18 | + "SYD": "AUS", |
| 19 | + "MEX": "MEX", |
| 20 | + "SAO": "BRA", |
| 21 | + "CHE": "IND", |
| 22 | + "MIL": "ITA", |
| 23 | + "SEO": "KOR", |
| 24 | + "FRA": "FRA", |
| 25 | +} |
| 26 | + |
| 27 | +type PoolsCreateCommand struct { |
| 28 | + *metadata.SoftlayerCommand |
| 29 | + BandwidthManager managers.BandwidthManager |
| 30 | + Command *cobra.Command |
| 31 | + Name string |
| 32 | + Region string |
| 33 | +} |
| 34 | + |
| 35 | +func NewPoolsCreateCommand(sl *metadata.SoftlayerCommand) *PoolsCreateCommand { |
| 36 | + |
| 37 | + thisCmd := &PoolsCreateCommand{ |
| 38 | + SoftlayerCommand: sl, |
| 39 | + BandwidthManager: managers.NewBandwidthManager(sl.Session), |
| 40 | + } |
| 41 | + cobraCmd := &cobra.Command{ |
| 42 | + Use: "pools-create", |
| 43 | + Short: T("Create a bandwidth pool."), |
| 44 | + Args: metadata.NoArgs, |
| 45 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | + return thisCmd.Run(args) |
| 47 | + }, |
| 48 | + } |
| 49 | + cobraCmd.Flags().StringVar(&thisCmd.Name, "name", "", T("Pool name.")) |
| 50 | + cobraCmd.Flags().StringVar(&thisCmd.Region, "region", "", T("Region selected. Permit:[SJC/DAL/WDC/TOR/MON, AMS/LON/MAD/PAR, SNG/HKG/OSA/TOK, SYD, MEX, SAO, CHE, MIL, SEO, FRA]")) |
| 51 | + |
| 52 | + //#nosec G104 -- This is a false positive |
| 53 | + cobraCmd.MarkFlagRequired("name") |
| 54 | + //#nosec G104 -- This is a false positive |
| 55 | + cobraCmd.MarkFlagRequired("region") |
| 56 | + |
| 57 | + thisCmd.Command = cobraCmd |
| 58 | + return thisCmd |
| 59 | +} |
| 60 | + |
| 61 | +func (cmd *PoolsCreateCommand) Run(args []string) error { |
| 62 | + |
| 63 | + outputFormat := cmd.GetOutputFlag() |
| 64 | + |
| 65 | + locationGroup, err := cmd.BandwidthManager.GetLocationGroup() |
| 66 | + if err != nil { |
| 67 | + return errors.NewAPIError(T("Failed to get Location Group."), err.Error(), 2) |
| 68 | + } |
| 69 | + idLocationGroup := finIdLocationGroup(locationGroup, LOCATION_GROUPS[cmd.Region]) |
| 70 | + poolCreated, err := cmd.BandwidthManager.CreatePool(cmd.Name, idLocationGroup) |
| 71 | + if err != nil { |
| 72 | + return errors.NewAPIError(T("Failed to get Create Bandwidth Pool."), err.Error(), 2) |
| 73 | + } |
| 74 | + |
| 75 | + table := cmd.UI.Table([]string{T("Name"), T("Value")}) |
| 76 | + table.Add("Id", utils.FormatIntPointer(poolCreated.Id)) |
| 77 | + table.Add("Name Pool", utils.FormatStringPointer(poolCreated.Name)) |
| 78 | + table.Add("Region", cmd.Region) |
| 79 | + table.Add("Created Date", utils.FormatSLTimePointer(poolCreated.CreateDate)) |
| 80 | + |
| 81 | + utils.PrintTable(cmd.UI, table, outputFormat) |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func finIdLocationGroup(locations []datatypes.Location_Group, regionName string) int { |
| 86 | + for _, location := range locations { |
| 87 | + if utils.FormatStringPointer(location.Name) == regionName { |
| 88 | + return *location.Id |
| 89 | + } |
| 90 | + } |
| 91 | + return 0 |
| 92 | +} |
0 commit comments