<commit_before>package test_helpers

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"

	slclient "github.com/maximilien/softlayer-go/client"
	datatypes "github.com/maximilien/softlayer-go/data_types"
	softlayer "github.com/maximilien/softlayer-go/softlayer"
)

var (
	TIMEOUT          time.Duration
	POLLING_INTERVAL time.Duration
)

const (
	TEST_NOTES_PREFIX = "TEST:softlayer-go"
	TEST_LABEL_PREFIX = "TEST:softlayer-go"

	MAX_WAIT_RETRIES = 10
	WAIT_TIME        = 5
)

func ReadJsonTestFixtures(packageName, fileName string) ([]byte, error) {
	wd, _ := os.Getwd()
	return ioutil.ReadFile(filepath.Join(wd, "..", "test_fixtures", packageName, fileName))
}

func FindTestVirtualGuests() ([]datatypes.SoftLayer_Virtual_Guest, error) {
	accountService, err := CreateAccountService()
	if err != nil {
		return []datatypes.SoftLayer_Virtual_Guest{}, err
	}

	virtualGuests, err := accountService.GetVirtualGuests()
	if err != nil {
		return []datatypes.SoftLayer_Virtual_Guest{}, err
	}

	testVirtualGuests := []datatypes.SoftLayer_Virtual_Guest{}
	for _, vGuest := range virtualGuests {
		if strings.Contains(vGuest.Notes, TEST_NOTES_PREFIX) {
			testVirtualGuests = append(testVirtualGuests, vGuest)
		}
	}

	return testVirtualGuests, nil
}

func FindTestVirtualDiskImages() ([]datatypes.SoftLayer_Virtual_Disk_Image, error) {
	accountService, err := CreateAccountService()
	if err != nil {
		return []datatypes.SoftLayer_Virtual_Disk_Image{}, err
	}

	virtualDiskImages, err := accountService.GetVirtualDiskImages()
	if err != nil {
		return []datatypes.SoftLayer_Virtual_Disk_Image{}, err
	}

	testVirtualDiskImages := []datatypes.SoftLayer_Virtual_Disk_Image{}
	for _, vDI := range virtualDiskImages {
		if strings.Contains(vDI.Description, TEST_NOTES_PREFIX) {
			testVirtualDiskImages = append(testVirtualDiskImages, vDI)
		}
	}

	return testVirtualDiskImages, nil
}

func FindTestNetworkStorage() ([]datatypes.SoftLayer_Network_Storage, error) {
	accountService, err := CreateAccountService()
	if err != nil {
		return []datatypes.SoftLayer_Network_Storage{}, err
	}

	networkStorageArray, err := accountService.GetNetworkStorage()
	if err != nil {
		return []datatypes.SoftLayer_Network_Storage{}, err
	}

	testNetworkStorageArray := []datatypes.SoftLayer_Network_Storage{}
	for _, storage := range networkStorageArray {
		if strings.Contains(storage.Notes, TEST_NOTES_PREFIX) {
			testNetworkStorageArray = append(testNetworkStorageArray, storage)
		}
	}

	return testNetworkStorageArray, nil
}

func FindTestSshKeys() ([]datatypes.SoftLayer_Security_Ssh_Key, error) {
	accountService, err := CreateAccountService()
	if err != nil {
		return []datatypes.SoftLayer_Security_Ssh_Key{}, err
	}

	sshKeys, err := accountService.GetSshKeys()
	if err != nil {
		return []datatypes.SoftLayer_Security_Ssh_Key{}, err
	}

	testSshKeys := []datatypes.SoftLayer_Security_Ssh_Key{}
	for _, key := range sshKeys {
		if key.Notes == TEST_NOTES_PREFIX {
			testSshKeys = append(testSshKeys, key)
		}
	}

	return testSshKeys, nil
}

func GetUsernameAndApiKey() (string, string, error) {
	username := os.Getenv("SL_USERNAME")
	if username == "" {
		return "", "", errors.New("SL_USERNAME environment must be set")
	}

	apiKey := os.Getenv("SL_API_KEY")
	if apiKey == "" {
		return username, "", errors.New("SL_API_KEY environment must be set")
	}

	return username, apiKey, nil
}

func CreateAccountService() (softlayer.SoftLayer_Account_Service, error) {
	username, apiKey, err := GetUsernameAndApiKey()
	if err != nil {
		return nil, err
	}

	client := slclient.NewSoftLayerClient(username, apiKey)
	accountService, err := client.GetSoftLayer_Account_Service()
	if err != nil {
		return nil, err
	}

	return accountService, nil
}

func CreateVirtualGuestService() (softlayer.SoftLayer_Virtual_Guest_Service, error) {
	username, apiKey, err := GetUsernameAndApiKey()
	if err != nil {
		return nil, err
	}

	client := slclient.NewSoftLayerClient(username, apiKey)
	virtualGuestService, err := client.GetSoftLayer_Virtual_Guest_Service()
	if err != nil {
		return nil, err
	}

	return virtualGuestService, nil
}

func CreateSecuritySshKeyService() (softlayer.SoftLayer_Security_Ssh_Key_Service, error) {<commit_msg>Added CreateVirtualGuestBlockDeviceTemplateGroupService function to test_helpers<commit_after>package test_helpers

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"time"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	. "github.com/onsi/gomega/gexec"

	slclient "github.com/maximilien/softlayer-go/client"
	datatypes "github.com/maximilien/softlayer-go/data_types"
	softlayer "github.com/maximilien/softlayer-go/softlayer"
)

var (
	TIMEOUT          time.Duration
	POLLING_INTERVAL time.Duration
)

const (
	TEST_NOTES_PREFIX = "TEST:softlayer-go"
	TEST_LABEL_PREFIX = "TEST:softlayer-go"

	MAX_WAIT_RETRIES = 10
	WAIT_TIME        = 5
)

func ReadJsonTestFixtures(packageName, fileName string) ([]byte, error) {
	wd, _ := os.Getwd()
	return ioutil.ReadFile(filepath.Join(wd, "..", "test_fixtures", packageName, fileName))
}

func FindTestVirtualGuests() ([]datatypes.SoftLayer_Virtual_Guest, error) {
	accountService, err := CreateAccountService()
	if err != nil {
		return []datatypes.SoftLayer_Virtual_Guest{}, err
	}

	virtualGuests, err := accountService.GetVirtualGuests()
	if err != nil {
		return []datatypes.SoftLayer_Virtual_Guest{}, err
	}

	testVirtualGuests := []datatypes.SoftLayer_Virtual_Guest{}
	for _, vGuest := range virtualGuests {
		if strings.Contains(vGuest.Notes, TEST_NOTES_PREFIX) {
			testVirtualGuests = append(testVirtualGuests, vGuest)
		}
	}

	return testVirtualGuests, nil
}

func FindTestVirtualDiskImages() ([]datatypes.SoftLayer_Virtual_Disk_Image, error) {
	accountService, err := CreateAccountService()
	if err != nil {
		return []datatypes.SoftLayer_Virtual_Disk_Image{}, err
	}

	virtualDiskImages, err := accountService.GetVirtualDiskImages()
	if err != nil {
		return []datatypes.SoftLayer_Virtual_Disk_Image{}, err
	}

	testVirtualDiskImages := []datatypes.SoftLayer_Virtual_Disk_Image{}
	for _, vDI := range virtualDiskImages {
		if strings.Contains(vDI.Description, TEST_NOTES_PREFIX) {
			testVirtualDiskImages = append(testVirtualDiskImages, vDI)
		}
	}

	return testVirtualDiskImages, nil
}

func FindTestNetworkStorage() ([]datatypes.SoftLayer_Network_Storage, error) {
	accountService, err := CreateAccountService()
	if err != nil {
		return []datatypes.SoftLayer_Network_Storage{}, err
	}

	networkStorageArray, err := accountService.GetNetworkStorage()
	if err != nil {
		return []datatypes.SoftLayer_Network_Storage{}, err
	}

	testNetworkStorageArray := []datatypes.SoftLayer_Network_Storage{}
	for _, storage := range networkStorageArray {
		if strings.Contains(storage.Notes, TEST_NOTES_PREFIX) {
			testNetworkStorageArray = append(testNetworkStorageArray, storage)
		}
	}

	return testNetworkStorageArray, nil
}

func FindTestSshKeys() ([]datatypes.SoftLayer_Security_Ssh_Key, error) {
	accountService, err := CreateAccountService()
	if err != nil {
		return []datatypes.SoftLayer_Security_Ssh_Key{}, err
	}

	sshKeys, err := accountService.GetSshKeys()
	if err != nil {
		return []datatypes.SoftLayer_Security_Ssh_Key{}, err
	}

	testSshKeys := []datatypes.SoftLayer_Security_Ssh_Key{}
	for _, key := range sshKeys {
		if key.Notes == TEST_NOTES_PREFIX {
			testSshKeys = append(testSshKeys, key)
		}
	}

	return testSshKeys, nil
}

func GetUsernameAndApiKey() (string, string, error) {
	username := os.Getenv("SL_USERNAME")
	if username == "" {
		return "", "", errors.New("SL_USERNAME environment must be set")
	}

	apiKey := os.Getenv("SL_API_KEY")
	if apiKey == "" {
		return username, "", errors.New("SL_API_KEY environment must be set")
	}

	return username, apiKey, nil
}

func CreateAccountService() (softlayer.SoftLayer_Account_Service, error) {
	username, apiKey, err := GetUsernameAndApiKey()
	if err != nil {
		return nil, err
	}

	client := slclient.NewSoftLayerClient(username, apiKey)
	accountService, err := client.GetSoftLayer_Account_Service()
	if err != nil {
		return nil, err
	}

	return accountService, nil
}

func CreateVirtualGuestService() (softlayer.SoftLayer_Virtual_Guest_Service, error) {
	username, apiKey, err := GetUsernameAndApiKey()
	if err != nil {
		return nil, err
	}

	client := slclient.NewSoftLayerClient(username, apiKey)
	virtualGuestService, err := client.GetSoftLayer_Virtual_Guest_Service()
	if err != nil {
		return nil, err
	}

	return virtualGuestService, nil
}

func CreateVirtualGuestBlockDeviceTemplateGroupService() (softlayer.SoftLayer_Virtual_Guest_Block_Device_Template_Group_Service, error) {
	username, apiKey, err := GetUsernameAndApiKey()
	if err != nil {
		return nil, err
	}

	client := slclient.NewSoftLayerClient(username, apiKey)
	vgbdtgService, err := client.GetSoftLayer_Virtual_Guest_Block_Device_Template_Group_Service()
	if err != nil {
		return nil, err
	}

	return vgbdtgService, nil
}

func CreateSecuritySshKeyService() (softlayer.SoftLayer_Security_Ssh_Key_Service, error) {
{'fraction_non_alphanumeric': 0.11587199312714777, 'fraction_numerical': 0.001288659793814433, 'mean_word_length': 5.40949759119064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6585889', 'n_tokens_mistral': 3294, 'n_tokens_neox': 3178, 'n_words': 615}