11package search
22
33import (
4-
4+ "fmt"
5+ "strings"
56 "github.com/IBM-Cloud/ibm-cloud-cli-sdk/plugin"
67 "github.com/spf13/cobra"
78
9+ "github.com/softlayer/softlayer-go/datatypes"
810 . "github.ibm.com/SoftLayer/softlayer-cli/plugin/i18n"
911 "github.ibm.com/SoftLayer/softlayer-cli/plugin/managers"
1012 "github.ibm.com/SoftLayer/softlayer-cli/plugin/metadata"
13+
1114 "github.ibm.com/SoftLayer/softlayer-cli/plugin/utils"
1215)
1316
@@ -54,6 +57,74 @@ func (cmd *SearchCommand) Run(args []string) error {
5457
5558 results , err := cmd .SearchManager .AdvancedSearch ("" , cmd .Query )
5659 if err != nil { return err }
60+ if cmd .GetOutputFlag () == "JSON" {
61+ return utils .PrintPrettyJSON (cmd .UI , results )
62+ }
63+ table := cmd .UI .Table ([]string {"Type" , "Matched Terms" , "Resource" })
64+ for _ , result := range results {
65+ resource_string := ""
66+ switch * result .ResourceType {
67+ case "SoftLayer_Virtual_Guest" :
68+ resource_object := result .Resource .(* datatypes.Virtual_Guest )
69+ resource_string = parseVirtual_Guest (* resource_object )
70+ case "SoftLayer_Event_Log" :
71+ resource_object := result .Resource .(* datatypes.Event_Log )
72+ resource_string = parseEvent_Log (* resource_object )
73+ case "SoftLayer_Virtual_DedicatedHost" :
74+ resource_object := result .Resource .(* datatypes.Virtual_DedicatedHost )
75+ resource_string = parseVirtual_DedicatedHost (* resource_object )
76+ case "SoftLayer_Hardware" :
77+ resource_object := result .Resource .(* datatypes.Hardware )
78+ resource_string = parseHardware (* resource_object )
79+ case "SoftLayer_Network_Application_Delivery_Controller" :
80+ resource_object := result .Resource .(* datatypes.Network_Application_Delivery_Controller )
81+ resource_string = parseNetwork_Application_Delivery_Controller (* resource_object )
82+ case "SoftLayer_Network_Subnet_IpAddress" :
83+ resource_object := result .Resource .(* datatypes.Network_Subnet_IpAddress )
84+ resource_string = parseNetwork_Subnet_IpAddress (* resource_object )
85+ case "SoftLayer_Network_Vlan" :
86+ resource_object := result .Resource .(* datatypes.Network_Vlan )
87+ resource_string = parseNetwork_Vlan (* resource_object )
88+ case "SoftLayer_Network_Vlan_Firewall" :
89+ resource_object := result .Resource .(* datatypes.Network_Vlan_Firewall )
90+ resource_string = parseNetwork_Vlan_Firewall (* resource_object )
91+ case "SoftLayer_Ticket" :
92+ resource_object := result .Resource .(* datatypes.Ticket )
93+ resource_string = parseTicket (* resource_object )
94+ }
95+ table .Add (* result .ResourceType , parseMatchedTerms (result ), resource_string )
96+ }
97+ table .Print ()
98+ return nil
99+ }
57100
58- return utils .PrintPrettyJSON (cmd .UI , results )
101+ func parseMatchedTerms (searchResult datatypes.Container_Search_Result ) string {
102+ return strings .Join (searchResult .MatchedTerms , "\n " )
103+ }
104+ func parseVirtual_Guest (resource datatypes.Virtual_Guest ) string {
105+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("FQDN" ) + ": %s\n " , * resource .Id , * resource .FullyQualifiedDomainName )
106+ }
107+ func parseEvent_Log (resource datatypes.Event_Log ) string {
108+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("Event" ) + ": %s\n " , * resource .TraceId , * resource .EventName )
109+ }
110+ func parseVirtual_DedicatedHost (resource datatypes.Virtual_DedicatedHost ) string {
111+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("Name" ) + ": %s\n " , * resource .Id , * resource .Name )
112+ }
113+ func parseHardware (resource datatypes.Hardware ) string {
114+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("FQDN" ) + ": %s\n " , * resource .Id , * resource .FullyQualifiedDomainName )
115+ }
116+ func parseNetwork_Application_Delivery_Controller (resource datatypes.Network_Application_Delivery_Controller ) string {
117+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("Name" ) + ": %s\n " , * resource .Id , * resource .Name )
118+ }
119+ func parseNetwork_Subnet_IpAddress (resource datatypes.Network_Subnet_IpAddress ) string {
120+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("Ip Address" ) + ": %s\n " , * resource .Id , * resource .IpAddress )
121+ }
122+ func parseNetwork_Vlan (resource datatypes.Network_Vlan ) string {
123+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("VLAN" ) + ": %s\n " , * resource .Id , * resource .VlanNumber )
124+ }
125+ func parseNetwork_Vlan_Firewall (resource datatypes.Network_Vlan_Firewall ) string {
126+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("Ip Address" ) + ": %s\n " , * resource .Id , * resource .PrimaryIpAddress )
127+ }
128+ func parseTicket (resource datatypes.Ticket ) string {
129+ return fmt .Sprintf (T ("ID" ) + ": %d\n " + T ("Subject" ) + ": %s\n " , * resource .Id , * resource .Title )
59130}
0 commit comments