<commit_before>package brightbox

import (
	"fmt"
	"testing"

	"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccBrightboxOrbitContainer_Basic(t *testing.T) {
	resourceName := "brightbox_orbit_container.foobar"

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckBrightboxOrbitContainerDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_basic,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists(resourceName),
					resource.TestCheckResourceAttr(
						resourceName, "name", "test-acc-initial"),
				),
			},
			{
				ResourceName:            resourceName,
				ImportState:             true,
				ImportStateVerify:       true,
				ImportStateVerifyIgnore: []string{"created_at"},
			},
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_updated,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists(resourceName),
					resource.TestCheckResourceAttr(
						resourceName, "name", "test-acc-updated"),
				),
			},
		},
	})
}

func TestAccBrightboxOrbitContainer_metadata(t *testing.T) {
	resourceName := "brightbox_orbit_container.foobar"

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckBrightboxOrbitContainerDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_metadata,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists(resourceName),
					resource.TestCheckResourceAttr(
						resourceName, "name", "initial"),
				),
			},
			{
				ResourceName:            resourceName,
				ImportState:             true,
				ImportStateVerify:       true,
				ImportStateVerifyIgnore: []string{"created_at"},
			},
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_metadata_add,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists("brightbox_orbit_container.foobar"),
					resource.TestCheckResourceAttr(
						"brightbox_orbit_container.foobar", "name", "test-acc-initial"),
				),
			},
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_metadata,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists("brightbox_orbit_container.foobar"),
					resource.TestCheckResourceAttr(
						"brightbox_orbit_container.foobar", "name", "test-acc-initial"),
				),
			},
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_metadata_delete,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists("brightbox_orbit_container.foobar"),
					resource.TestCheckResourceAttr(
						"brightbox_orbit_container.foobar", "name", "test-acc-initial"),
				),
			},
		},
	})
}

func testAccCheckBrightboxOrbitContainerDestroy(s *terraform.State) error {
	client := testAccProvider.Meta().(*CompositeClient).OrbitClient

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "brightbox_orbit_container" {
			continue
		}

		// Try to find the container
		getresult := containers.Get(client, rs.Primary.ID, nil)

		// Wait

		err := getresult.Err
		if err != nil && err.Error() != "Resource not found" {
			return fmt.Errorf(
				"Error waiting for container %s to be destroyed: %s",
				rs.Primary.ID, getresult.Err)
		}
	}

	return nil
}

func testAccCheckBrightboxOrbitContainerExists(n string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		_, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}
		return nil
	}
}

const testAccCheckBrightboxOrbitContainerConfig_basic = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-initial"
}
`

const testAccCheckBrightboxOrbitContainerConfig_updated = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-updated"
}
`

const testAccCheckBrightboxOrbitContainerConfig_metadata = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-initial"
	container_read = [ "acc-testy", "acc-12345"]
	metadata = {
		"foo"= "bar"
		"bar"= "baz" 
		"uni" = "€uro"
	}
}
`

const testAccCheckBrightboxOrbitContainerConfig_metadata_add = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-initial"
	metadata = {
		"foo"= "bar"
		"bar"= "foo"
		"uni" = "€uro"
	}
	container_read = [ "acc-testy", "acc-12345", "acc-98765" ]
}
`

const testAccCheckBrightboxOrbitContainerConfig_metadata_delete = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-initial"
	metadata = {
		"bar"= "foo"
		"uni" = "€uro"
	}
	container_read = [ "acc-testy", "acc-12345", "acc-98765" ]
}
`
<commit_msg>Check for correct name in orbit tests<commit_after>package brightbox

import (
	"fmt"
	"testing"

	"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/containers"
	"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
	"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccBrightboxOrbitContainer_Basic(t *testing.T) {
	resourceName := "brightbox_orbit_container.foobar"

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckBrightboxOrbitContainerDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_basic,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists(resourceName),
					resource.TestCheckResourceAttr(
						resourceName, "name", "test-acc-initial"),
				),
			},
			{
				ResourceName:            resourceName,
				ImportState:             true,
				ImportStateVerify:       true,
				ImportStateVerifyIgnore: []string{"created_at"},
			},
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_updated,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists(resourceName),
					resource.TestCheckResourceAttr(
						resourceName, "name", "test-acc-updated"),
				),
			},
		},
	})
}

func TestAccBrightboxOrbitContainer_metadata(t *testing.T) {
	resourceName := "brightbox_orbit_container.foobar"

	resource.Test(t, resource.TestCase{
		PreCheck:     func() { testAccPreCheck(t) },
		Providers:    testAccProviders,
		CheckDestroy: testAccCheckBrightboxOrbitContainerDestroy,
		Steps: []resource.TestStep{
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_metadata,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists(resourceName),
					resource.TestCheckResourceAttr(
						resourceName, "name", "test-acc-initial"),
				),
			},
			{
				ResourceName:            resourceName,
				ImportState:             true,
				ImportStateVerify:       true,
				ImportStateVerifyIgnore: []string{"created_at"},
			},
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_metadata_add,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists("brightbox_orbit_container.foobar"),
					resource.TestCheckResourceAttr(
						"brightbox_orbit_container.foobar", "name", "test-acc-initial"),
				),
			},
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_metadata,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists("brightbox_orbit_container.foobar"),
					resource.TestCheckResourceAttr(
						"brightbox_orbit_container.foobar", "name", "test-acc-initial"),
				),
			},
			{
				Config: testAccCheckBrightboxOrbitContainerConfig_metadata_delete,
				Check: resource.ComposeTestCheckFunc(
					testAccCheckBrightboxOrbitContainerExists("brightbox_orbit_container.foobar"),
					resource.TestCheckResourceAttr(
						"brightbox_orbit_container.foobar", "name", "test-acc-initial"),
				),
			},
		},
	})
}

func testAccCheckBrightboxOrbitContainerDestroy(s *terraform.State) error {
	client := testAccProvider.Meta().(*CompositeClient).OrbitClient

	for _, rs := range s.RootModule().Resources {
		if rs.Type != "brightbox_orbit_container" {
			continue
		}

		// Try to find the container
		getresult := containers.Get(client, rs.Primary.ID, nil)

		// Wait

		err := getresult.Err
		if err != nil && err.Error() != "Resource not found" {
			return fmt.Errorf(
				"Error waiting for container %s to be destroyed: %s",
				rs.Primary.ID, getresult.Err)
		}
	}

	return nil
}

func testAccCheckBrightboxOrbitContainerExists(n string) resource.TestCheckFunc {
	return func(s *terraform.State) error {
		_, ok := s.RootModule().Resources[n]
		if !ok {
			return fmt.Errorf("Not found: %s", n)
		}
		return nil
	}
}

const testAccCheckBrightboxOrbitContainerConfig_basic = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-initial"
}
`

const testAccCheckBrightboxOrbitContainerConfig_updated = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-updated"
}
`

const testAccCheckBrightboxOrbitContainerConfig_metadata = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-initial"
	container_read = [ "acc-testy", "acc-12345"]
	metadata = {
		"foo"= "bar"
		"bar"= "baz" 
		"uni" = "€uro"
	}
}
`

const testAccCheckBrightboxOrbitContainerConfig_metadata_add = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-initial"
	metadata = {
		"foo"= "bar"
		"bar"= "foo"
		"uni" = "€uro"
	}
	container_read = [ "acc-testy", "acc-12345", "acc-98765" ]
}
`

const testAccCheckBrightboxOrbitContainerConfig_metadata_delete = `

resource "brightbox_orbit_container" "foobar" {
	name = "test-acc-initial"
	metadata = {
		"bar"= "foo"
		"uni" = "€uro"
	}
	container_read = [ "acc-testy", "acc-12345", "acc-98765" ]
}
`

{'fraction_non_alphanumeric': 0.11655011655011654, 'fraction_numerical': 0.005270092226613966, 'mean_word_length': 4.395297977036632, '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': '6434541', 'n_tokens_mistral': 3817, 'n_tokens_neox': 3236, 'n_words': 490}