|
| 1 | +/* |
| 2 | +Copyright © LiquidWeb |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/spf13/cast" |
| 23 | + "github.com/spf13/cobra" |
| 24 | + |
| 25 | + "github.com/liquidweb/liquidweb-cli/instance" |
| 26 | +) |
| 27 | + |
| 28 | +var cloudTemplateListCmd = &cobra.Command{ |
| 29 | + Use: "list", |
| 30 | + Short: "Displays a list of cloud VPS templates", |
| 31 | + Long: `Displays a list of cloud VPS templates.`, |
| 32 | + Run: func(cmd *cobra.Command, args []string) { |
| 33 | + zoneFlag, _ := cmd.Flags().GetInt("zone") |
| 34 | + filterOsFlag, _ := cmd.Flags().GetString("os") |
| 35 | + filterManageLevelFlag, _ := cmd.Flags().GetString("manage-level") |
| 36 | + |
| 37 | + templateList, err := lwCliInst.AllPaginatedResults(&instance.AllPaginatedResultsArgs{ |
| 38 | + Method: "bleed/storm/template/list", |
| 39 | + ResultsPerPage: 100, |
| 40 | + }) |
| 41 | + if err != nil { |
| 42 | + lwCliInst.Die(err) |
| 43 | + } |
| 44 | + |
| 45 | + zonesResults, err := lwCliInst.LwCliApiClient.Call("bleed/network/zone/list", nil) |
| 46 | + if err != nil { |
| 47 | + lwCliInst.Die(err) |
| 48 | + } |
| 49 | + |
| 50 | + type ZoneInfo struct { |
| 51 | + Id int |
| 52 | + Name string |
| 53 | + RegionName string |
| 54 | + } |
| 55 | + zones := make(map[int]*ZoneInfo) |
| 56 | + zoneMap := zonesResults.(map[string]interface{}) |
| 57 | + |
| 58 | + for _, item := range zoneMap["items"].([]interface{}) { |
| 59 | + z := item.(map[string]interface{}) |
| 60 | + |
| 61 | + zone := &ZoneInfo{ |
| 62 | + Id: cast.ToInt(z["id"]), |
| 63 | + Name: cast.ToString(z["name"]), |
| 64 | + RegionName: cast.ToString(z["region"].(map[string]interface{})["name"]), |
| 65 | + } |
| 66 | + zones[zone.Id] = zone |
| 67 | + } |
| 68 | + |
| 69 | + for _, template := range templateList.Items { |
| 70 | + if cast.ToBool(template["deprecated"]) { |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + if !strings.HasPrefix(strings.ToLower(cast.ToString(template["os"])), strings.ToLower(filterOsFlag)) { |
| 75 | + continue |
| 76 | + } |
| 77 | + |
| 78 | + if filterManageLevelFlag != "" { |
| 79 | + if strings.ToLower(filterManageLevelFlag) != strings.ToLower(cast.ToString(template["manage_level"])) { |
| 80 | + continue |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if zoneFlag != -1 { |
| 85 | + var skip bool = true |
| 86 | + |
| 87 | + for templateZoneStr, _ := range template["zone_availability"].(map[string]interface{}) { |
| 88 | + templateZone := cast.ToInt(templateZoneStr) |
| 89 | + if templateZone == zoneFlag { |
| 90 | + skip = false |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if skip { |
| 95 | + continue |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + fmt.Println("name:", template["name"]) |
| 100 | + fmt.Println(" description: ", template["description"]) |
| 101 | + fmt.Print(" os: ", template["os"]) |
| 102 | + fmt.Println(", manage-level:", template["manage_level"]) |
| 103 | + fmt.Println(" Zone Availibility:") |
| 104 | + |
| 105 | + for templateZoneStr, _ := range template["zone_availability"].(map[string]interface{}) { |
| 106 | + templateZone := cast.ToInt(templateZoneStr) |
| 107 | + if z, ok := zones[templateZone]; ok { |
| 108 | + fmt.Printf(" %5d - %s - %s\n", z.Id, z.Name, z.RegionName) |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + fmt.Println("") |
| 114 | + } |
| 115 | + }, |
| 116 | +} |
| 117 | + |
| 118 | +func init() { |
| 119 | + cloudTemplateCmd.AddCommand(cloudTemplateListCmd) |
| 120 | + cloudTemplateListCmd.Flags().Int("zone", -1, "id of zone to filter by") |
| 121 | + cloudTemplateListCmd.Flags().String("os", "", "filter if os begins with string (i.e. linux, win)") |
| 122 | + cloudTemplateListCmd.Flags().String("manage-level", "", "filter list by management level") |
| 123 | +} |
0 commit comments