-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trying out adding some Iter() style functions to make this SDK faster #…
- Loading branch information
1 parent
967fb9b
commit 88ce3a9
Showing
5 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"github.com/softlayer/softlayer-go/filter" | ||
"github.com/softlayer/softlayer-go/services" | ||
"github.com/softlayer/softlayer-go/session" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(listVirtCmd) | ||
} | ||
|
||
var listVirtCmd = &cobra.Command{ | ||
Use: "virt-list", | ||
Short: "Lists all VSI on the account", | ||
Long: `Lists all VSI on the account using an iterative aproach.`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return RunListVirtCmd(cmd, args) | ||
}, | ||
} | ||
|
||
func RunListVirtCmd(cmd *cobra.Command, args []string) error { | ||
|
||
objectMask := "mask[id,hostname,domain,primaryIpAddress,primaryBackendIpAddress]" | ||
// When using a result Limit to break up your API request, its important to include an orderBy objectFilter | ||
// to enforce an order on the query, as the database might not always return results in the same order between | ||
// queries otherwise | ||
filters := filter.New() | ||
filters = append(filters, filter.Path("virtualGuests.id").OrderBy("ASC")) | ||
objectFilter := filters.Build() | ||
// Sets up the session with authentication headers. | ||
sess := session.New() | ||
// uncomment to output API calls as they are made. | ||
sess.Debug = true | ||
|
||
// creates a reference to the service object (SoftLayer_Account) | ||
service := services.GetAccountService(sess) | ||
|
||
// Sets the mask, filter, result limit, and then makes the API call SoftLayer_Account::getHardware() | ||
servers, err := service.Mask(objectMask).Filter(objectFilter).GetVirtualGuestsIter() | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Id, Hostname, Domain, IP Address\n") | ||
|
||
for _, server := range servers { | ||
ipAddress := "-" | ||
// Servers with a private only connection will not have a primary IP | ||
if server.PrimaryIpAddress != nil { | ||
ipAddress = *server.PrimaryIpAddress | ||
} | ||
fmt.Printf("%v, %v, %v, %v\n", *server.Id, *server.Hostname, *server.Domain, ipAddress) | ||
} | ||
|
||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters