/azurerm/internal/services/lighthouse/tests/resource_arm_lighthouse_definition_test.go

https://github.com/terraform-providers/terraform-provider-azurerm · Go · 283 lines · 251 code · 29 blank · 3 comment · 10 complexity · c8597339a83c088e4941bb847de29e9f MD5 · raw file

  1. package tests
  2. import (
  3. "fmt"
  4. "os"
  5. "testing"
  6. "github.com/google/uuid"
  7. "github.com/hashicorp/terraform-plugin-sdk/helper/resource"
  8. "github.com/hashicorp/terraform-plugin-sdk/terraform"
  9. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate"
  10. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
  11. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
  12. "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
  13. )
  14. func TestAccAzureRMLighthouseDefinition_basic(t *testing.T) {
  15. // Multiple tenants are needed to test this resource.
  16. // Second tenant ID needs to be set as a environment variable ARM_TENANT_ID_ALT.
  17. // ObjectId for user, usergroup or service principal from second Tenant needs to be set as a environment variable ARM_PRINCIPAL_ID_ALT_TENANT.
  18. secondTenantID := os.Getenv("ARM_TENANT_ID_ALT")
  19. principalID := os.Getenv("ARM_PRINCIPAL_ID_ALT_TENANT")
  20. data := acceptance.BuildTestData(t, "azurerm_lighthouse_definition", "test")
  21. resource.ParallelTest(t, resource.TestCase{
  22. PreCheck: func() { acceptance.PreCheck(t) },
  23. Providers: acceptance.SupportedProviders,
  24. CheckDestroy: testCheckAzureRMLighthouseDefinitionDestroy,
  25. Steps: []resource.TestStep{
  26. {
  27. Config: testAccAzureRMLighthouseDefinition_basic(uuid.New().String(), secondTenantID, principalID, data),
  28. Check: resource.ComposeTestCheckFunc(
  29. testCheckAzureRMLighthouseDefinitionExists(data.ResourceName),
  30. resource.TestCheckResourceAttrSet(data.ResourceName, "scope"),
  31. resource.TestMatchResourceAttr(data.ResourceName, "lighthouse_definition_id", validate.UUIDRegExp),
  32. ),
  33. },
  34. },
  35. })
  36. }
  37. func TestAccAzureRMLighthouseDefinition_requiresImport(t *testing.T) {
  38. data := acceptance.BuildTestData(t, "azurerm_lighthouse_definition", "test")
  39. secondTenantID := os.Getenv("ARM_TENANT_ID_ALT")
  40. principalID := os.Getenv("ARM_PRINCIPAL_ID_ALT_TENANT")
  41. id := uuid.New().String()
  42. resource.ParallelTest(t, resource.TestCase{
  43. PreCheck: func() { acceptance.PreCheck(t) },
  44. Providers: acceptance.SupportedProviders,
  45. CheckDestroy: testCheckAzureRMLighthouseDefinitionDestroy,
  46. Steps: []resource.TestStep{
  47. {
  48. Config: testAccAzureRMLighthouseDefinition_basic(id, secondTenantID, principalID, data),
  49. Check: resource.ComposeTestCheckFunc(
  50. testCheckAzureRMLighthouseDefinitionExists(data.ResourceName),
  51. resource.TestCheckResourceAttrSet(data.ResourceName, "scope"),
  52. resource.TestMatchResourceAttr(data.ResourceName, "lighthouse_definition_id", validate.UUIDRegExp),
  53. ),
  54. },
  55. {
  56. Config: testAccAzureRMLighthouseDefinition_requiresImport(id, secondTenantID, principalID, data),
  57. ExpectError: acceptance.RequiresImportError("azurerm_lighthouse_definition"),
  58. },
  59. },
  60. })
  61. }
  62. func TestAccAzureRMLighthouseDefinition_complete(t *testing.T) {
  63. data := acceptance.BuildTestData(t, "azurerm_lighthouse_definition", "test")
  64. secondTenantID := os.Getenv("ARM_TENANT_ID_ALT")
  65. principalID := os.Getenv("ARM_PRINCIPAL_ID_ALT_TENANT")
  66. resource.ParallelTest(t, resource.TestCase{
  67. PreCheck: func() { acceptance.PreCheck(t) },
  68. Providers: acceptance.SupportedProviders,
  69. CheckDestroy: testCheckAzureRMLighthouseDefinitionDestroy,
  70. Steps: []resource.TestStep{
  71. {
  72. Config: testAccAzureRMLighthouseDefinition_complete(uuid.New().String(), secondTenantID, principalID, data),
  73. Check: resource.ComposeTestCheckFunc(
  74. testCheckAzureRMLighthouseDefinitionExists(data.ResourceName),
  75. resource.TestCheckResourceAttrSet(data.ResourceName, "scope"),
  76. resource.TestMatchResourceAttr(data.ResourceName, "lighthouse_definition_id", validate.UUIDRegExp),
  77. resource.TestCheckResourceAttr(data.ResourceName, "description", "Acceptance Test Lighthouse Definition"),
  78. ),
  79. },
  80. data.ImportStep("lighthouse_definition_id"),
  81. },
  82. })
  83. }
  84. func TestAccAzureRMLighthouseDefinition_update(t *testing.T) {
  85. data := acceptance.BuildTestData(t, "azurerm_lighthouse_definition", "test")
  86. secondTenantID := os.Getenv("ARM_TENANT_ID_ALT")
  87. principalID := os.Getenv("ARM_PRINCIPAL_ID_ALT_TENANT")
  88. id := uuid.New().String()
  89. resource.ParallelTest(t, resource.TestCase{
  90. PreCheck: func() { acceptance.PreCheck(t) },
  91. Providers: acceptance.SupportedProviders,
  92. CheckDestroy: testCheckAzureRMLighthouseDefinitionDestroy,
  93. Steps: []resource.TestStep{
  94. {
  95. Config: testAccAzureRMLighthouseDefinition_basic(id, secondTenantID, principalID, data),
  96. Check: resource.ComposeTestCheckFunc(
  97. testCheckAzureRMLighthouseDefinitionExists(data.ResourceName),
  98. resource.TestCheckResourceAttrSet(data.ResourceName, "scope"),
  99. resource.TestMatchResourceAttr(data.ResourceName, "lighthouse_definition_id", validate.UUIDRegExp),
  100. ),
  101. },
  102. {
  103. Config: testAccAzureRMLighthouseDefinition_complete(id, secondTenantID, principalID, data),
  104. Check: resource.ComposeTestCheckFunc(
  105. testCheckAzureRMLighthouseDefinitionExists(data.ResourceName),
  106. resource.TestCheckResourceAttrSet(data.ResourceName, "scope"),
  107. resource.TestMatchResourceAttr(data.ResourceName, "lighthouse_definition_id", validate.UUIDRegExp),
  108. resource.TestCheckResourceAttr(data.ResourceName, "description", "Acceptance Test Lighthouse Definition"),
  109. ),
  110. },
  111. },
  112. })
  113. }
  114. func TestAccAzureRMLighthouseDefinition_emptyID(t *testing.T) {
  115. data := acceptance.BuildTestData(t, "azurerm_lighthouse_definition", "test")
  116. secondTenantID := os.Getenv("ARM_TENANT_ID_ALT")
  117. principalID := os.Getenv("ARM_PRINCIPAL_ID_ALT_TENANT")
  118. resource.ParallelTest(t, resource.TestCase{
  119. PreCheck: func() { acceptance.PreCheck(t) },
  120. Providers: acceptance.SupportedProviders,
  121. CheckDestroy: testCheckAzureRMLighthouseDefinitionDestroy,
  122. Steps: []resource.TestStep{
  123. {
  124. Config: testAccAzureRMLighthouseDefinition_emptyId(secondTenantID, principalID, data),
  125. Check: resource.ComposeTestCheckFunc(
  126. testCheckAzureRMLighthouseDefinitionExists(data.ResourceName),
  127. resource.TestCheckResourceAttrSet(data.ResourceName, "id"),
  128. resource.TestCheckResourceAttrSet(data.ResourceName, "lighthouse_definition_id"),
  129. ),
  130. },
  131. },
  132. })
  133. }
  134. func testCheckAzureRMLighthouseDefinitionExists(resourceName string) resource.TestCheckFunc {
  135. return func(s *terraform.State) error {
  136. client := acceptance.AzureProvider.Meta().(*clients.Client).Lighthouse.DefinitionsClient
  137. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  138. rs, ok := s.RootModule().Resources[resourceName]
  139. if !ok {
  140. return fmt.Errorf("Not found: %q", resourceName)
  141. }
  142. scope := rs.Primary.Attributes["scope"]
  143. lighthouseDefinitionID := rs.Primary.Attributes["lighthouse_definition_id"]
  144. resp, err := client.Get(ctx, scope, lighthouseDefinitionID)
  145. if err != nil {
  146. if utils.ResponseWasNotFound(resp.Response) {
  147. return fmt.Errorf("Bad: Lighthouse Definition %q (Scope: %q) does not exist", lighthouseDefinitionID, scope)
  148. }
  149. return fmt.Errorf("Bad: Get on lighthouseDefinitionsClient: %+v", err)
  150. }
  151. return nil
  152. }
  153. }
  154. func testCheckAzureRMLighthouseDefinitionDestroy(s *terraform.State) error {
  155. client := acceptance.AzureProvider.Meta().(*clients.Client).Lighthouse.DefinitionsClient
  156. ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
  157. for _, rs := range s.RootModule().Resources {
  158. if rs.Type != "azurerm_lighthouse_definition" {
  159. continue
  160. }
  161. scope := rs.Primary.Attributes["scope"]
  162. lighthouseDefinitionID := rs.Primary.Attributes["lighthouse_definition_id"]
  163. resp, err := client.Get(ctx, scope, lighthouseDefinitionID)
  164. if err != nil {
  165. if !utils.ResponseWasNotFound(resp.Response) {
  166. return err
  167. }
  168. }
  169. return nil
  170. }
  171. return nil
  172. }
  173. func testAccAzureRMLighthouseDefinition_basic(id string, secondTenantID string, principalID string, data acceptance.TestData) string {
  174. return fmt.Sprintf(`
  175. provider "azurerm" {
  176. features {}
  177. }
  178. data "azurerm_role_definition" "contributor" {
  179. role_definition_id = "b24988ac-6180-42a0-ab88-20f7382dd24c"
  180. }
  181. resource "azurerm_lighthouse_definition" "test" {
  182. lighthouse_definition_id = "%s"
  183. name = "acctest-LD-%d"
  184. managing_tenant_id = "%s"
  185. authorization {
  186. principal_id = "%s"
  187. role_definition_id = data.azurerm_role_definition.contributor.role_definition_id
  188. }
  189. }
  190. `, id, data.RandomInteger, secondTenantID, principalID)
  191. }
  192. func testAccAzureRMLighthouseDefinition_requiresImport(id string, secondTenantID string, principalID string, data acceptance.TestData) string {
  193. return fmt.Sprintf(`
  194. %s
  195. resource "azurerm_lighthouse_definition" "import" {
  196. name = azurerm_lighthouse_definition.test.name
  197. lighthouse_definition_id = azurerm_lighthouse_definition.test.lighthouse_definition_id
  198. managing_tenant_id = azurerm_lighthouse_definition.test.managing_tenant_id
  199. authorization {
  200. principal_id = azurerm_lighthouse_definition.test.managing_tenant_id
  201. role_definition_id = "b24988ac-6180-42a0-ab88-20f7382dd24c"
  202. }
  203. }
  204. `, testAccAzureRMLighthouseDefinition_basic(id, secondTenantID, principalID, data))
  205. }
  206. func testAccAzureRMLighthouseDefinition_complete(id string, secondTenantID string, principalID string, data acceptance.TestData) string {
  207. return fmt.Sprintf(`
  208. provider "azurerm" {
  209. features {}
  210. }
  211. data "azurerm_role_definition" "contributor" {
  212. role_definition_id = "b24988ac-6180-42a0-ab88-20f7382dd24c"
  213. }
  214. resource "azurerm_lighthouse_definition" "test" {
  215. lighthouse_definition_id = "%s"
  216. name = "acctest-LD-%d"
  217. description = "Acceptance Test Lighthouse Definition"
  218. managing_tenant_id = "%s"
  219. authorization {
  220. principal_id = "%s"
  221. role_definition_id = data.azurerm_role_definition.contributor.role_definition_id
  222. }
  223. }
  224. `, id, data.RandomInteger, secondTenantID, principalID)
  225. }
  226. func testAccAzureRMLighthouseDefinition_emptyId(secondTenantID string, principalID string, data acceptance.TestData) string {
  227. return fmt.Sprintf(`
  228. provider "azurerm" {
  229. features {}
  230. }
  231. data "azurerm_role_definition" "contributor" {
  232. role_definition_id = "b24988ac-6180-42a0-ab88-20f7382dd24c"
  233. }
  234. resource "azurerm_lighthouse_definition" "test" {
  235. name = "acctest-LD-%d"
  236. description = "Acceptance Test Lighthouse Definition"
  237. managing_tenant_id = "%s"
  238. authorization {
  239. principal_id = "%s"
  240. role_definition_id = data.azurerm_role_definition.contributor.role_definition_id
  241. }
  242. }
  243. `, data.RandomInteger, secondTenantID, principalID)
  244. }