|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAccMeIdentityGroupsDataSource_basic(t *testing.T) { |
| 12 | + desc := "Identity group created by Terraform Acc." |
| 13 | + role1 := "NONE" |
| 14 | + role2 := "ADMIN" |
| 15 | + groupName1 := acctest.RandomWithPrefix(test_prefix) |
| 16 | + groupName2 := acctest.RandomWithPrefix(test_prefix) |
| 17 | + |
| 18 | + preSetup := fmt.Sprintf( |
| 19 | + testAccMeIdentityGroupsDatasourceConfig_preSetup, |
| 20 | + desc, |
| 21 | + groupName1, |
| 22 | + role1, |
| 23 | + desc, |
| 24 | + groupName2, |
| 25 | + role2, |
| 26 | + ) |
| 27 | + config := fmt.Sprintf( |
| 28 | + testAccMeIdentityGroupsDatasourceConfig_keys, |
| 29 | + desc, |
| 30 | + groupName1, |
| 31 | + role1, |
| 32 | + desc, |
| 33 | + groupName2, |
| 34 | + role2, |
| 35 | + ) |
| 36 | + |
| 37 | + checks := checkIdentityGroupResourceAttr("ovh_me_identity_group.group_1", groupName1, desc, role1) |
| 38 | + checks = append(checks, checkIdentityGroupResourceAttr("ovh_me_identity_group.group_2", groupName2, desc, role2)...) |
| 39 | + |
| 40 | + resource.Test(t, resource.TestCase{ |
| 41 | + PreCheck: func() { testAccPreCheckCredentials(t) }, |
| 42 | + Providers: testAccProviders, |
| 43 | + Steps: []resource.TestStep{ |
| 44 | + { |
| 45 | + Config: preSetup, |
| 46 | + Check: resource.ComposeTestCheckFunc(checks...), |
| 47 | + }, { |
| 48 | + Config: config, |
| 49 | + Check: resource.ComposeTestCheckFunc( |
| 50 | + resource.TestCheckOutput( |
| 51 | + "keys_present", "true"), |
| 52 | + ), |
| 53 | + }, |
| 54 | + }, |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +func checkIdentityGroupResourceAttr(name, g_name, desc, role string) []resource.TestCheckFunc { |
| 59 | + return []resource.TestCheckFunc{ |
| 60 | + resource.TestCheckResourceAttr(name, "name", g_name), |
| 61 | + resource.TestCheckResourceAttr(name, "description", desc), |
| 62 | + resource.TestCheckResourceAttr(name, "role", role), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +const testAccMeIdentityGroupsDatasourceConfig_preSetup = ` |
| 67 | +resource "ovh_me_identity_group" "group_1" { |
| 68 | + description = "%s" |
| 69 | + name = "%s" |
| 70 | + role = "%s" |
| 71 | +} |
| 72 | +
|
| 73 | +resource "ovh_me_identity_group" "group_2" { |
| 74 | + description = "%s" |
| 75 | + name = "%s" |
| 76 | + role = "%s" |
| 77 | +} |
| 78 | +` |
| 79 | + |
| 80 | +const testAccMeIdentityGroupsDatasourceConfig_keys = ` |
| 81 | +resource "ovh_me_identity_group" "group_1" { |
| 82 | + description = "%s" |
| 83 | + name = "%s" |
| 84 | + role = "%s" |
| 85 | +} |
| 86 | +
|
| 87 | +resource "ovh_me_identity_group" "group_2" { |
| 88 | + description = "%s" |
| 89 | + name = "%s" |
| 90 | + role = "%s" |
| 91 | +} |
| 92 | +
|
| 93 | +data "ovh_me_identity_groups" "groups" {} |
| 94 | +
|
| 95 | +output "keys_present" { |
| 96 | + value = tostring(contains(data.ovh_me_identity_groups.groups.groups, ovh_me_identity_group.group_1.name) && contains(data.ovh_me_identity_groups.groups.groups, ovh_me_identity_group.group_2.name)) |
| 97 | +} |
| 98 | +` |
0 commit comments