func getAuthenticationParameters(t *testing.T) common.AuthenticationParameters {
	ap, err := common.FromEnvVars()
	if err != nil {
		t.Fatal("Integration test cannot proceed because env vars are not set.")
	}

	return ap
}
func TestFromFromEnvVarsShouldErrorWithNoAuthURLUserNameAndPassword(t *testing.T) {
	savedParams, _ := common.FromEnvVars()
	if savedParams.AuthURL != "" {
		t.Skip("Skipping testing as open stack environments are set.")
	}

	os.Setenv("OS_AUTH_URL", "")
	os.Setenv("OS_TENANT_ID", "")
	os.Setenv("OS_TENANT_NAME", "")
	os.Setenv("OS_USERNAME", "")
	os.Setenv("OS_PASSWORD", "")
	os.Setenv("OS_REGION_NAME", "")

	_, err := common.FromEnvVars()
	testUtil.Assert(t, err != nil, "Expected an error")
	testUtil.Equals(t, "No value provided for env variable 'OS_AUTH_URL'\nNo value provided for env variable 'OS_REGION_NAME'\nEither AuthToken or Username-Password should be provided", err.Error())

	applyParameter(savedParams)
}
func TestNetworkServiceScenarios(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}
	if os.Getenv("OS_AUTH_URL") == "" {
		t.Skip("No openstack auth configured so skipping test")
	}

	authParameters, err := common.FromEnvVars()
	if err != nil {
		t.Fatal("There was an error getting authentication env variables:", err)
	}

	authenticator := identity.Authenticate(authParameters)
	authenticator.SetFunction(requester.DebugRequestMakerGenerator(nil, nil, testing.Verbose()))

	networkService := network.NewService(&authenticator)

	networkName := "OtherNetwork"

	createdNetwork, err := networkService.CreateNetwork(
		network.CreateNetworkParameters{
			AdminStateUp: true,
			Shared:       false,
			Name:         networkName,
			TenantID:     authParameters.TenantID})
	if err != nil {
		t.Fatal("There was an error creating a network:", err)
	}

	networks, err := networkService.Networks()
	if err != nil {
		t.Fatal("There was an error getting a list of networks:", err)
	}

	networkIDs, err := networkService.NetworkIDsByName(networkName)
	if err != nil {
		t.Fatal("There was an error getting a list of networks ids by name", err)
	}

	if len(networkIDs) > 1 {
		t.Fatalf("Wrong number of networks with the name %s: %v", networkName, err)
	}

	if networkIDs[0] != createdNetwork.ID {
		t.Fatalf("Didn't find the correct networkID by its name %s: %v", networkName, err)
	}

	foundCreatedNetwork := false
	for _, networkFound := range networks {
		if reflect.DeepEqual(createdNetwork, networkFound) {
			foundCreatedNetwork = true
		}
	}

	if !foundCreatedNetwork {
		t.Fatal("Cannot find network called OtherNetwork when getting a list of networks.")
	}

	// Might be nice to have some sugar api that can do this easily for a developer...
	// Keep iterating until active or until more than 10 tries has been exceeded.
	numTries := 0
	activeNetwork := createdNetwork
	for numTries < 10 || activeNetwork.Status != "ACTIVE" {
		activeNetwork, _ = networkService.Network(createdNetwork.ID)
		numTries++
		fmt.Println("Sleeping 50ms on try:" + string(numTries) + " with status currently " + activeNetwork.Status)
		sleepDuration, _ := time.ParseDuration("50ms")
		time.Sleep(sleepDuration)
	}

	foundNetworksByName, err := networkService.QueryNetworks(network.QueryParameters{
		Name: networkName,
	})

	if err != nil {
		t.Fatal("There was an error getting a list of networks by name", err)
	}

	if len(foundNetworksByName) > 1 {
		t.Fatalf("Wrong number of networks with the name %s: %v", networkName, err)
	}

	if foundNetworksByName[0].Name != networkName && foundNetworksByName[0].ID != createdNetwork.ID {
		t.Fatalf("Didn't find the correct network by its name %s: %v", networkName, err)
	}

	err = networkService.DeleteNetwork(activeNetwork.ID)
	if err != nil {
		t.Fatal("Error in deleting OtherNetwork:", err)
	}

	networks, err = networkService.Networks()
	if err != nil {
		t.Fatal("There was an error getting a list of networks to verify the network was deleted:", err)
	}

	networkDeleted := true
	for _, networkFound := range networks {
		if reflect.DeepEqual(createdNetwork, networkFound) {
			networkDeleted = false
		}
	}

	if !networkDeleted {
		t.Fatal("Delete of 'NewNetwork' did not occur")
	}
}
func TestSecurityGroupScenarios(t *testing.T) {
	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}

	authParameters, err := common.FromEnvVars()
	if err != nil {
		t.Fatal("There was an error getting authentication env variables:", err)
	}

	authenticator := identity.Authenticate(authParameters)
	authenticator.SetFunction(requester.DebugRequestMakerGenerator(nil, nil, testing.Verbose()))

	networkService := network.NewService(&authenticator)

	sampleSecurityGroup := network.CreateSecurityGroupParameters{
		Description: "Description",
		Name:        "NewCustomSecurityGroup",
	}

	securityGroup, err := networkService.CreateSecurityGroup(sampleSecurityGroup)
	if err != nil {
		t.Fatal("Cannot create security group:", err)
	}

	tcp := network.TCP
	cidr := "0.0.0.0/0"
	sampleRule := network.CreateSecurityGroupRuleParameters{
		PortRangeMin:    intPointer(80),
		PortRangeMax:    intPointer(80),
		IPProtocol:      &tcp,
		RemoteIPPrefix:  &cidr,
		SecurityGroupID: securityGroup.ID,
		Direction:       "ingress",
	}

	securityGroupRule, err := networkService.CreateSecurityGroupRule(sampleRule)
	if err != nil {
		t.Error("Cannot create security group rule:", err)
	}

	testUtil.Equals(t, 80, *securityGroupRule.PortRangeMin)
	testUtil.Equals(t, 80, *securityGroupRule.PortRangeMax)
	testUtil.Equals(t, cidr, *securityGroupRule.RemoteIPPrefix)

	sampleRule2 := network.CreateSecurityGroupRuleParameters{
		PortRangeMin:    nil,
		PortRangeMax:    nil,
		IPProtocol:      &tcp,
		SecurityGroupID: securityGroup.ID,
		Direction:       "ingress",
		RemoteGroupID:   &securityGroup.ID,
	}

	securityGroupRule2, err := networkService.CreateSecurityGroupRule(sampleRule2)
	if err != nil {
		t.Error("Cannot create security group rule:", err)
	}

	testUtil.Equals(t, (*int)(nil), securityGroupRule2.PortRangeMin)
	testUtil.Equals(t, (*int)(nil), securityGroupRule2.PortRangeMax)
	testUtil.Equals(t, securityGroup.ID, *securityGroupRule2.RemoteGroupID)
	testUtil.Equals(t, (*string)(nil), securityGroupRule2.RemoteIPPrefix)

	queriedSecurityGroup, err := networkService.SecurityGroup(securityGroup.ID)
	if err != nil {
		t.Error("Cannot requery security group:", err)
	}

	testUtil.Equals(t, 4, len(queriedSecurityGroup.SecurityGroupRules))

	err = networkService.DeleteSecurityGroupRule(securityGroupRule.ID)
	if err != nil {
		t.Error("Cannot delete security group rule:", err)
	}

	err = networkService.DeleteSecurityGroup(queriedSecurityGroup.ID)
	if err != nil {
		t.Error("Cannot delete security group:", err)
	}
}