|
| 1 | +package search |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/IBM-Cloud/ibm-cloud-cli-sdk/plugin" |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/softlayer/softlayer-go/datatypes" |
| 10 | + . "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n" |
| 11 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/managers" |
| 12 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata" |
| 13 | + |
| 14 | + "github.ibm.com/SoftLayer/softlayer-cli/plugin/utils" |
| 15 | +) |
| 16 | + |
| 17 | +type SearchCommand struct { |
| 18 | + *metadata.SoftlayerCommand |
| 19 | + SearchManager managers.SearchManager |
| 20 | + Command *cobra.Command |
| 21 | + Query string |
| 22 | +} |
| 23 | + |
| 24 | +func SetupCobraCommands(sl *metadata.SoftlayerCommand) *cobra.Command { |
| 25 | + thisCmd := &SearchCommand{ |
| 26 | + SoftlayerCommand: sl, |
| 27 | + SearchManager: managers.NewSearchManager(sl.Session), |
| 28 | + } |
| 29 | + cobraCmd := &cobra.Command{ |
| 30 | + Use: "search", |
| 31 | + Short: T("Perform a query against the SoftLayer search database."), |
| 32 | + Long: T(`Read More: https://sldn.softlayer.com/reference/services/SoftLayer_Search/search/ |
| 33 | +Examples:: |
| 34 | +
|
| 35 | + sl search --query 'test.com' |
| 36 | + sl search --query '_objectType:SoftLayer_Virtual_Guest test.com' |
| 37 | +`), |
| 38 | + Args: metadata.NoArgs, |
| 39 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 40 | + return thisCmd.Run(args) |
| 41 | + }, |
| 42 | + } |
| 43 | + cobraCmd.Flags().StringVarP(&thisCmd.Query, "query", "q", "", T("The search query you want to use.")) |
| 44 | + cobraCmd.AddCommand(NewSearchTypesCommand(sl).Command) |
| 45 | + return cobraCmd |
| 46 | +} |
| 47 | + |
| 48 | +func SearchNamespace() plugin.Namespace { |
| 49 | + return plugin.Namespace{ |
| 50 | + ParentName: "sl", |
| 51 | + Name: "search", |
| 52 | + Description: T("Perform a query against the SoftLayer search database."), |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func (cmd *SearchCommand) Run(args []string) error { |
| 57 | + |
| 58 | + results, err := cmd.SearchManager.AdvancedSearch("", cmd.Query) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + if cmd.GetOutputFlag() == "JSON" { |
| 63 | + return utils.PrintPrettyJSON(cmd.UI, results) |
| 64 | + } |
| 65 | + table := cmd.UI.Table([]string{"Type", "Matched Terms", "Resource"}) |
| 66 | + for _, result := range results { |
| 67 | + resource_string := "" |
| 68 | + switch *result.ResourceType { |
| 69 | + case "SoftLayer_Virtual_Guest": |
| 70 | + resource_object := result.Resource.(*datatypes.Virtual_Guest) |
| 71 | + resource_string = parseVirtual_Guest(*resource_object) |
| 72 | + case "SoftLayer_Event_Log": |
| 73 | + resource_object := result.Resource.(*datatypes.Event_Log) |
| 74 | + resource_string = parseEvent_Log(*resource_object) |
| 75 | + case "SoftLayer_Virtual_DedicatedHost": |
| 76 | + resource_object := result.Resource.(*datatypes.Virtual_DedicatedHost) |
| 77 | + resource_string = parseVirtual_DedicatedHost(*resource_object) |
| 78 | + case "SoftLayer_Hardware": |
| 79 | + resource_object := result.Resource.(*datatypes.Hardware) |
| 80 | + resource_string = parseHardware(*resource_object) |
| 81 | + case "SoftLayer_Network_Application_Delivery_Controller": |
| 82 | + resource_object := result.Resource.(*datatypes.Network_Application_Delivery_Controller) |
| 83 | + resource_string = parseNetwork_Application_Delivery_Controller(*resource_object) |
| 84 | + case "SoftLayer_Network_Subnet_IpAddress": |
| 85 | + resource_object := result.Resource.(*datatypes.Network_Subnet_IpAddress) |
| 86 | + resource_string = parseNetwork_Subnet_IpAddress(*resource_object) |
| 87 | + case "SoftLayer_Network_Vlan": |
| 88 | + resource_object := result.Resource.(*datatypes.Network_Vlan) |
| 89 | + resource_string = parseNetwork_Vlan(*resource_object) |
| 90 | + case "SoftLayer_Network_Vlan_Firewall": |
| 91 | + resource_object := result.Resource.(*datatypes.Network_Vlan_Firewall) |
| 92 | + resource_string = parseNetwork_Vlan_Firewall(*resource_object) |
| 93 | + case "SoftLayer_Ticket": |
| 94 | + resource_object := result.Resource.(*datatypes.Ticket) |
| 95 | + resource_string = parseTicket(*resource_object) |
| 96 | + } |
| 97 | + table.Add(*result.ResourceType, parseMatchedTerms(result), resource_string) |
| 98 | + } |
| 99 | + table.Print() |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func parseMatchedTerms(searchResult datatypes.Container_Search_Result) string { |
| 104 | + return strings.Join(searchResult.MatchedTerms, "\n") |
| 105 | +} |
| 106 | +func parseVirtual_Guest(resource datatypes.Virtual_Guest) string { |
| 107 | + return fmt.Sprintf(T("ID")+": %d\n"+T("FQDN")+": %s\n", *resource.Id, *resource.FullyQualifiedDomainName) |
| 108 | +} |
| 109 | +func parseEvent_Log(resource datatypes.Event_Log) string { |
| 110 | + return fmt.Sprintf(T("ID")+": %s\n"+T("Event")+": %s\n", *resource.TraceId, *resource.EventName) |
| 111 | +} |
| 112 | +func parseVirtual_DedicatedHost(resource datatypes.Virtual_DedicatedHost) string { |
| 113 | + return fmt.Sprintf(T("ID")+": %d\n"+T("Name")+": %s\n", *resource.Id, *resource.Name) |
| 114 | +} |
| 115 | +func parseHardware(resource datatypes.Hardware) string { |
| 116 | + return fmt.Sprintf(T("ID")+": %d\n"+T("FQDN")+": %s\n", *resource.Id, *resource.FullyQualifiedDomainName) |
| 117 | +} |
| 118 | +func parseNetwork_Application_Delivery_Controller(resource datatypes.Network_Application_Delivery_Controller) string { |
| 119 | + return fmt.Sprintf(T("ID")+": %d\n"+T("Name")+": %s\n", *resource.Id, *resource.Name) |
| 120 | +} |
| 121 | +func parseNetwork_Subnet_IpAddress(resource datatypes.Network_Subnet_IpAddress) string { |
| 122 | + return fmt.Sprintf(T("ID")+": %d\n"+T("Ip Address")+": %s\n", *resource.Id, *resource.IpAddress) |
| 123 | +} |
| 124 | +func parseNetwork_Vlan(resource datatypes.Network_Vlan) string { |
| 125 | + return fmt.Sprintf(T("ID")+": %d\n"+T("VLAN")+": %d\n", *resource.Id, *resource.VlanNumber) |
| 126 | +} |
| 127 | +func parseNetwork_Vlan_Firewall(resource datatypes.Network_Vlan_Firewall) string { |
| 128 | + return fmt.Sprintf(T("ID")+": %d\n"+T("Ip Address")+": %s\n", *resource.Id, *resource.PrimaryIpAddress) |
| 129 | +} |
| 130 | +func parseTicket(resource datatypes.Ticket) string { |
| 131 | + return fmt.Sprintf(T("ID")+": %d\n"+T("Subject")+": %s\n", *resource.Id, *resource.Title) |
| 132 | +} |
0 commit comments