|
| 1 | +//go:build acceptance |
| 2 | +// +build acceptance |
| 3 | + |
| 4 | +package provider |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 14 | + "github.com/xanzy/go-gitlab" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAccGitlabGroupSamlLink_basic(t *testing.T) { |
| 18 | + rInt := acctest.RandInt() |
| 19 | + resourceName := "gitlab_group_saml_link.foo" |
| 20 | + |
| 21 | + // PreCheck runs after Config so load test data here |
| 22 | + var samlLink gitlab.SAMLGroupLink |
| 23 | + testSamlLink := gitlab.SAMLGroupLink{ |
| 24 | + Name: "test_saml_group" |
| 25 | + } |
| 26 | + |
| 27 | + resource.ParallelTest(t, resource.TestCase{ |
| 28 | + ProviderFactories: providerFactories, |
| 29 | + CheckDestroy: testAccCheckGitlabGroupSamlLinkDestroy, |
| 30 | + Steps: []resource.TestStep{ |
| 31 | + |
| 32 | + // Create a group SAML link as a developer (uses testAccGitlabGroupLdapSamlCreateConfig for Config) |
| 33 | + { |
| 34 | + SkipFunc: isRunningInCE, |
| 35 | + Config: testAccGitlabGroupSamlLinkCreateConfig(rInt, &testSamlLink), |
| 36 | + Check: resource.ComposeTestCheckFunc( |
| 37 | + testAccCheckGitlabGroupSamlLinkExists(resourceName, &samlLink)), |
| 38 | + }, |
| 39 | + |
| 40 | + // Import the group SAML link (re-uses testAccGitlabGroupSamlLinkCreateConfig for Config) |
| 41 | + { |
| 42 | + SkipFunc: isRunningInCE, |
| 43 | + ResourceName: resourceName, |
| 44 | + ImportStateIdFunc: getGitlabGroupSamlLinkImportID(resourceName), |
| 45 | + ImportState: true, |
| 46 | + ImportStateVerify: true, |
| 47 | + }, |
| 48 | + |
| 49 | + // Update the group SAML link to change the access level (uses testAccGitlabGroupSamlLinkUpdateConfig for Config) |
| 50 | + { |
| 51 | + SkipFunc: isRunningInCE, |
| 52 | + Config: testAccGitlabGroupSamlLinkUpdateConfig(rInt, &testSamlLink), |
| 53 | + Check: resource.ComposeTestCheckFunc( |
| 54 | + testAccCheckGitlabGroupSamlLinkExists(resourceName, &samlLink)) |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func getGitlabGroupSamlLinkImportID(resourceName string) resource.ImportStateIdFunc { |
| 61 | + return func(s *terraform.State) (string, error) { |
| 62 | + rs, ok := s.RootModule().Resources[resourceName] |
| 63 | + if !ok { |
| 64 | + return "", fmt.Errorf("Not Found: %s", resourceName) |
| 65 | + } |
| 66 | + |
| 67 | + groupID := rs.Primary.Attributes["group_id"] |
| 68 | + if groupID == "" { |
| 69 | + return "", fmt.Errorf("No group ID is set") |
| 70 | + } |
| 71 | + samlGroupName := rs.Primary.Attributes["saml_group_name"] |
| 72 | + if samlGroupName == "" { |
| 73 | + return "", fmt.Errorf("No SAML group name is set") |
| 74 | + } |
| 75 | + |
| 76 | + return fmt.Sprintf("%s:%s", groupID, samlGroupName), nil |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func testAccCheckGitlabGroupSamlLinkExists(resourceName string, samlLink *gitlab.SAMLGroupLink) resource.TestCheckFunc { |
| 81 | + return func(s *terraform.State) error { |
| 82 | + // Clear the "found" SAML link before checking for existence |
| 83 | + *samlLink = gitlab.SAMLGroupLink{} |
| 84 | + |
| 85 | + resourceState, ok := s.RootModule().Resources[resourceName] |
| 86 | + if !ok { |
| 87 | + return fmt.Errorf("Not found: %s", resourceName) |
| 88 | + } |
| 89 | + |
| 90 | + err := testAccGetGitlabGroupSamlLink(samlLink, resourceState) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func testAccCheckGitlabGroupSamlLinkDestroy(s *terraform.State) error { |
| 100 | + // Can't check for links if the group is destroyed so make sure all groups are destroyed instead |
| 101 | + for _, resourceState := range s.RootModule().Resources { |
| 102 | + if resourceState.Type != "gitlab_group" { |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + group, _, err := testGitlabClient.Groups.GetGroup(resourceState.Primary.ID, nil) |
| 107 | + if err == nil { |
| 108 | + if group != nil && fmt.Sprintf("%d", group.ID) == resourceState.Primary.ID { |
| 109 | + if group.MarkedForDeletionOn == nil { |
| 110 | + return fmt.Errorf("Group still exists") |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + if !is404(err) { |
| 115 | + return err |
| 116 | + } |
| 117 | + return nil |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func testAccGetGitlabGroupSamlLink(samlLink *gitlab.SAMLGroupLink, resourceState *terraform.ResourceState) error { |
| 123 | + groupId := resourceState.Primary.Attributes["group_id"] |
| 124 | + if groupId == "" { |
| 125 | + return fmt.Errorf("No group ID is set") |
| 126 | + } |
| 127 | + |
| 128 | + // Construct our desired SAML Link from the config values |
| 129 | + desiredSamlLink := gitlab.SAMLGroupLink{ |
| 130 | + AccessLevel: resourceState.Primary.Attributes["access_level"], |
| 131 | + Name: resourceState.Primary.Attributes["saml_group_name"], |
| 132 | + } |
| 133 | + |
| 134 | + desiredSamlLinkId := buildTwoPartID(&groupId, &desiredSamlLink.Name) |
| 135 | + |
| 136 | + // Try to fetch all group links from GitLab |
| 137 | + currentSamlLinks, _, err := testGitlabClient.Groups.ListGroupSamlLinks(groupId, nil) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + found := false |
| 143 | + |
| 144 | + // Check if the SAML link exists in the returned list of links |
| 145 | + for _, currentSamlLink := range currentSamlLinks { |
| 146 | + if buildTwoPartID(&groupId, ¤tSamlLink.Name) == desiredSamlLinkId { |
| 147 | + found = true |
| 148 | + *samlLink = *currentSamlLink |
| 149 | + break |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + if !found { |
| 154 | + return errors.New(fmt.Sprintf("SamlLink %s does not exist.", desiredSamlLinkId)) // nolint // TODO: Resolve this golangci-lint issue: S1028: should use fmt.Errorf(...) instead of errors.New(fmt.Sprintf(...)) (gosimple) |
| 155 | + } |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +func testAccGitlabGroupSamlLinkCreateConfig(rInt int, testSamlLink *gitlab.SAMLGroupLink) string { |
| 161 | + return fmt.Sprintf(` |
| 162 | +resource "gitlab_group" "foo" { |
| 163 | + name = "foo%d" |
| 164 | + path = "foo%d" |
| 165 | + description = "Terraform acceptance test - Group SAML Links 1" |
| 166 | +} |
| 167 | +
|
| 168 | +resource "gitlab_group_saml_link" "foo" { |
| 169 | + group_id = "${gitlab_group.foo.id}" |
| 170 | + access_level = "Developer" |
| 171 | + saml_group_name = "%s" |
| 172 | +
|
| 173 | +}`, rInt, rInt, testSamlLink.Name) |
| 174 | +} |
| 175 | + |
| 176 | +func testAccGitlabGroupSamlLinkUpdateConfig(rInt int, testSamlLink *gitlab.SAMLGroupLink) string { |
| 177 | + return fmt.Sprintf(` |
| 178 | +resource "gitlab_group" "foo" { |
| 179 | + name = "foo%d" |
| 180 | + path = "foo%d" |
| 181 | + description = "Terraform acceptance test - Group SAML Links 2" |
| 182 | +} |
| 183 | +
|
| 184 | +resource "gitlab_group_saml_link" "foo" { |
| 185 | + group_id = "${gitlab_group.foo.id}" |
| 186 | + access_level = "Maintainer" |
| 187 | + saml_group_name = "%s" |
| 188 | +}`, rInt, rInt, testSamlLink.Name) |
| 189 | +} |
0 commit comments