|
| 1 | +package ovh |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 8 | +) |
| 9 | + |
| 10 | +func TestAccMeIdentityProviderDataSource_basic(t *testing.T) { |
| 11 | + groupAttributeName := "http://schemas.xmlsoap.org/claims/Group" |
| 12 | + disableUsers := "false" |
| 13 | + reqAttributeRequired := "false" |
| 14 | + reqAttributeName := "identity" |
| 15 | + reqAttributeNameFormat := "urn:oasis:names:tc:SAML:2.0:attrname-format:basic" |
| 16 | + reqAttributeValue := "foobar" |
| 17 | + |
| 18 | + preSetup := fmt.Sprintf( |
| 19 | + testAccMeIdentityProviderDataSourceConfig_preSetup, |
| 20 | + groupAttributeName, |
| 21 | + samlIDPMetadata, |
| 22 | + disableUsers, |
| 23 | + reqAttributeRequired, |
| 24 | + reqAttributeName, |
| 25 | + reqAttributeNameFormat, |
| 26 | + reqAttributeValue, |
| 27 | + ) |
| 28 | + config := fmt.Sprintf( |
| 29 | + testAccMeIdentityProviderDataSourceConfig_keys, |
| 30 | + groupAttributeName, |
| 31 | + samlIDPMetadata, |
| 32 | + disableUsers, |
| 33 | + reqAttributeRequired, |
| 34 | + reqAttributeName, |
| 35 | + reqAttributeNameFormat, |
| 36 | + reqAttributeValue, |
| 37 | + ) |
| 38 | + |
| 39 | + userAttributeName := "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn" |
| 40 | + ssoServiceUrl := "https://ovhcloud.com/" |
| 41 | + certificateExpiration := "2033-11-06T10:06:24Z" |
| 42 | + certificateSubject := "CN=ovhcloud.com,O=OVHcloud,L=RBX,ST=Some-State,C=FR" |
| 43 | + |
| 44 | + requestedAttributes := map[string]string{ |
| 45 | + "is_required": reqAttributeRequired, |
| 46 | + "name": reqAttributeName, |
| 47 | + "name_format": reqAttributeNameFormat, |
| 48 | + "values": reqAttributeValue, |
| 49 | + } |
| 50 | + |
| 51 | + checks := checkIdentityProviderResourceAttr("ovh_me_identity_provider.sso", groupAttributeName, disableUsers, samlIDPMetadata, requestedAttributes) |
| 52 | + dataSourceChecks := checkIdentityProviderDataSourceAttr("data.ovh_me_identity_provider.sso", groupAttributeName, userAttributeName, ssoServiceUrl, disableUsers, certificateExpiration, certificateSubject, requestedAttributes) |
| 53 | + dataSourceChecks = append(dataSourceChecks, resource.TestCheckOutput("keys_present", "true")) |
| 54 | + |
| 55 | + resource.Test(t, resource.TestCase{ |
| 56 | + PreCheck: func() { testAccPreCheckCredentials(t) }, |
| 57 | + Providers: testAccProviders, |
| 58 | + Steps: []resource.TestStep{ |
| 59 | + { |
| 60 | + Config: preSetup, |
| 61 | + Check: resource.ComposeTestCheckFunc(checks...), |
| 62 | + }, { |
| 63 | + Config: config, |
| 64 | + Check: resource.ComposeTestCheckFunc(dataSourceChecks...), |
| 65 | + }, |
| 66 | + }, |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func checkIdentityProviderDataSourceAttr(name, group_attribute, user_attribute, sso_service_url, disable_users, certificateExpiration, certificateSubject string, requestedAttributes map[string]string) []resource.TestCheckFunc { |
| 71 | + checks := []resource.TestCheckFunc{} |
| 72 | + checks = append(checks, resource.TestCheckResourceAttr(name, "group_attribute_name", group_attribute)) |
| 73 | + checks = append(checks, resource.TestCheckResourceAttr(name, "user_attribute_name", user_attribute)) |
| 74 | + checks = append(checks, resource.TestCheckResourceAttr(name, "sso_service_url", sso_service_url)) |
| 75 | + checks = append(checks, resource.TestCheckResourceAttr(name, "disable_users", disable_users)) |
| 76 | + checks = append(checks, resource.TestCheckResourceAttr(name, "idp_signing_certificates.0.expiration", certificateExpiration)) |
| 77 | + checks = append(checks, resource.TestCheckResourceAttr(name, "idp_signing_certificates.0.subject", certificateSubject)) |
| 78 | + if requestedAttributes != nil { |
| 79 | + checks = append(checks, resource.TestCheckResourceAttr(name, "requested_attributes.0.is_required", requestedAttributes["is_required"])) |
| 80 | + checks = append(checks, resource.TestCheckResourceAttr(name, "requested_attributes.0.name", requestedAttributes["name"])) |
| 81 | + checks = append(checks, resource.TestCheckResourceAttr(name, "requested_attributes.0.name_format", requestedAttributes["name_format"])) |
| 82 | + checks = append(checks, resource.TestCheckResourceAttr(name, "requested_attributes.0.values.0", requestedAttributes["values"])) |
| 83 | + } |
| 84 | + return checks |
| 85 | +} |
| 86 | + |
| 87 | +const testAccMeIdentityProviderDataSourceConfig_preSetup = ` |
| 88 | +resource "ovh_me_identity_provider" "sso" { |
| 89 | + group_attribute_name = "%s" |
| 90 | + metadata = <<EOT |
| 91 | +%s |
| 92 | +EOT |
| 93 | + disable_users = %s |
| 94 | + requested_attributes { |
| 95 | + is_required = %s |
| 96 | + name = "%s" |
| 97 | + name_format = "%s" |
| 98 | + values = ["%s"] |
| 99 | + } |
| 100 | +}` |
| 101 | + |
| 102 | +const testAccMeIdentityProviderDataSourceConfig_keys = ` |
| 103 | +resource "ovh_me_identity_provider" "sso" { |
| 104 | + group_attribute_name = "%s" |
| 105 | + metadata = <<EOT |
| 106 | +%s |
| 107 | +EOT |
| 108 | + disable_users = %s |
| 109 | + requested_attributes { |
| 110 | + is_required = %s |
| 111 | + name = "%s" |
| 112 | + name_format = "%s" |
| 113 | + values = ["%s"] |
| 114 | + } |
| 115 | +} |
| 116 | +
|
| 117 | +data "ovh_me_identity_provider" "sso" {} |
| 118 | +
|
| 119 | +output "keys_present" { |
| 120 | + value = tostring( |
| 121 | + data.ovh_me_identity_provider.sso.group_attribute_name == ovh_me_identity_provider.sso.group_attribute_name && |
| 122 | + data.ovh_me_identity_provider.sso.requested_attributes.0.name == ovh_me_identity_provider.sso.requested_attributes.0.name |
| 123 | + ) |
| 124 | +} |
| 125 | +` |
0 commit comments