Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax updates for Terraform 0.12, support application_id argument on data.azuread_application #274

Merged
merged 3 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions azuread/data_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,27 @@ func dataApplication() *schema.Resource {

Schema: map[string]*schema.Schema{
"object_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.UUID,
ConflictsWith: []string{"name"},
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"application_id", "name", "object_id"},
ValidateFunc: validate.UUID,
},

"application_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"application_id", "name", "object_id"},
ValidateFunc: validate.UUID,
},

"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.NoEmptyStrings,
ConflictsWith: []string{"object_id"},
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"application_id", "name", "object_id"},
ValidateFunc: validate.NoEmptyStrings,
},

"homepage": {
Expand Down Expand Up @@ -69,11 +77,6 @@ func dataApplication() *schema.Resource {
Computed: true,
},

"application_id": {
Type: schema.TypeString,
Computed: true,
},

"group_membership_claims": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -162,8 +165,19 @@ func dataApplicationRead(d *schema.ResourceData, meta interface{}) error {
}

app = resp
} else if name, ok := d.Get("name").(string); ok {
filter := fmt.Sprintf("displayName eq '%s'", name)
} else {
var fieldName, fieldValue string
if applicationId, ok := d.Get("application_id").(string); ok && applicationId != "" {
fieldName = "appId"
fieldValue = applicationId
} else if name, ok := d.Get("name").(string); ok && name != "" {
fieldName = "displayName"
fieldValue = name
} else {
return fmt.Errorf("one of `object_id` or `name` must be supplied")
}

filter := fmt.Sprintf("%s eq '%s'", fieldName, fieldValue)

resp, err := client.ListComplete(ctx, filter)
if err != nil {
Expand All @@ -182,18 +196,26 @@ func dataApplicationRead(d *schema.ResourceData, meta interface{}) error {
}

app = (*values)[0]
if app.DisplayName == nil {
return fmt.Errorf("nil DisplayName for AD Applications matching %q", filter)
}
if *app.DisplayName != name {
return fmt.Errorf("displayname for AD Applications matching %q does is does not match(%q!=%q)", filter, *app.DisplayName, name)
switch fieldName {
case "appId":
if app.AppID == nil {
return fmt.Errorf("nil AppID for AD Applications matching %q", filter)
}
if *app.AppID != fieldValue {
return fmt.Errorf("AppID for AD Applications matching %q does is does not match(%q!=%q)", filter, *app.AppID, fieldValue)
}
case "displayName":
if app.DisplayName == nil {
return fmt.Errorf("nil DisplayName for AD Applications matching %q", filter)
}
if *app.DisplayName != fieldValue {
return fmt.Errorf("DisplayName for AD Applications matching %q does is does not match(%q!=%q)", filter, *app.DisplayName, fieldValue)
}
}
} else {
return fmt.Errorf("one of `object_id` or `name` must be supplied")
}

if app.ObjectID == nil {
return fmt.Errorf("Application objectId is nil")
return fmt.Errorf("Application ObjectId is nil")
}
d.SetId(*app.ObjectID)

Expand Down
47 changes: 44 additions & 3 deletions azuread/data_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,36 @@ func TestAccAzureADApplicationDataSource_byObjectIdComplete(t *testing.T) {
})
}

func TestAccAzureADApplicationDataSource_byApplicationId(t *testing.T) {
dataSourceName := "data.azuread_application.test"
ri := tf.AccRandTimeInt()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckADApplicationDestroy,
Steps: []resource.TestStep{
{
Config: testAccADApplication_basic(ri),
},
{
Config: testAccAzureADApplicationDataSource_applicationId(ri),
Check: resource.ComposeTestCheckFunc(
testCheckADApplicationExists(dataSourceName),
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest-APP-%d", ri)),
resource.TestCheckResourceAttr(dataSourceName, "homepage", fmt.Sprintf("https://acctest-APP-%d", ri)),
resource.TestCheckResourceAttr(dataSourceName, "identifier_uris.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "reply_urls.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "optional_claims.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "required_resource_access.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "oauth2_allow_implicit_flow", "false"),
resource.TestCheckResourceAttrSet(dataSourceName, "application_id"),
),
},
},
})
}

func TestAccAzureADApplicationDataSource_byName(t *testing.T) {
dataSourceName := "data.azuread_application.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -114,7 +144,7 @@ func testAccAzureADApplicationDataSource_objectId(ri int) string {
%s

data "azuread_application" "test" {
object_id = "${azuread_application.test.object_id}"
object_id = azuread_application.test.object_id
}
`, template)
}
Expand All @@ -125,7 +155,18 @@ func testAccAzureADApplicationDataSource_objectIdComplete(ri int, pw string) str
%s

data "azuread_application" "test" {
object_id = "${azuread_application.test.object_id}"
object_id = azuread_application.test.object_id
}
`, template)
}

func testAccAzureADApplicationDataSource_applicationId(ri int) string {
template := testAccADApplication_basic(ri)
return fmt.Sprintf(`
%s

data "azuread_application" "test" {
application_id = azuread_application.test.application_id
}
`, template)
}
Expand All @@ -136,7 +177,7 @@ func testAccAzureADApplicationDataSource_name(ri int) string {
%s

data "azuread_application" "test" {
name = "${azuread_application.test.name}"
name = azuread_application.test.name
}
`, template)
}
8 changes: 4 additions & 4 deletions azuread/data_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func testAccDataSourceAzureADGroup_name(id int) string {
%s

data "azuread_group" "test" {
name = "${azuread_group.test.name}"
name = azuread_group.test.name
}
`, testAccAzureADGroup_basic(id))
}
Expand All @@ -109,7 +109,7 @@ func testAccDataSourceAzureADGroup_objectId(id int) string {
%s

data "azuread_group" "test" {
object_id = "${azuread_group.test.object_id}"
object_id = azuread_group.test.object_id
}
`, testAccAzureADGroup_basic(id))
}
Expand All @@ -119,7 +119,7 @@ func testAccDataSourceAzureADGroup_members(id int, password string) string {
%s

data "azuread_group" "test" {
object_id = "${azuread_group.test.object_id}"
object_id = azuread_group.test.object_id
}
`, testAccAzureADGroupWithThreeMembers(id, password))
}
Expand All @@ -129,7 +129,7 @@ func testAccDataSourceAzureADGroup_owners(id int, password string) string {
%s

data "azuread_group" "test" {
object_id = "${azuread_group.test.object_id}"
object_id = azuread_group.test.object_id
}
`, testAccAzureADGroupWithThreeOwners(id, password))
}
4 changes: 2 additions & 2 deletions azuread/data_groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func testAccAzureADGroupsDataSource_byDisplayNames(id int) string {
%s

data "azuread_groups" "test" {
names = ["${azuread_group.testA.name}", "${azuread_group.testB.name}"]
names = [azuread_group.testA.name, azuread_group.testB.name]
}
`, testAccAzureADGroup_multiple(id))
}
Expand All @@ -94,7 +94,7 @@ func testAccAzureADGroupsDataSource_byObjectIds(id int) string {
%s

data "azuread_groups" "test" {
object_ids = ["${azuread_group.testA.object_id}", "${azuread_group.testB.object_id}"]
object_ids = [azuread_group.testA.object_id, azuread_group.testB.object_id]
}
`, testAccAzureADGroup_multiple(id))
}
Expand Down
6 changes: 3 additions & 3 deletions azuread/data_service_principal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func testAccAzureADServicePrincipalDataSource_byApplicationId(id string) string
%s

data "azuread_service_principal" "test" {
application_id = "${azuread_service_principal.test.application_id}"
application_id = azuread_service_principal.test.application_id
}
`, template)
}
Expand All @@ -94,7 +94,7 @@ func testAccAzureADServicePrincipalDataSource_byDisplayName(id string) string {
%s

data "azuread_service_principal" "test" {
display_name = "${azuread_service_principal.test.display_name}"
display_name = azuread_service_principal.test.display_name
}
`, template)
}
Expand All @@ -105,7 +105,7 @@ func testAccAzureADServicePrincipalDataSource_byObjectId(id string) string {
%s

data "azuread_service_principal" "test" {
object_id = "${azuread_service_principal.test.object_id}"
object_id = azuread_service_principal.test.object_id
}
`, template)
}
6 changes: 3 additions & 3 deletions azuread/data_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func testAccAzureADUserDataSource_byUserPrincipalName(id int, password string) s
%s

data "azuread_user" "test" {
user_principal_name = "${azuread_user.test.user_principal_name}"
user_principal_name = azuread_user.test.user_principal_name
}
`, testAccADUser_basic(id, password))
}
Expand All @@ -91,7 +91,7 @@ func testAccAzureADUserDataSource_byObjectId(id int, password string) string {
%s

data "azuread_user" "test" {
object_id = "${azuread_user.test.object_id}"
object_id = azuread_user.test.object_id
}
`, testAccADUser_basic(id, password))
}
Expand All @@ -101,7 +101,7 @@ func testAccAzureADUserDataSource_byMailNickname(id int, password string) string
%s

data "azuread_user" "test" {
mail_nickname = "${azuread_user.test.mail_nickname}"
mail_nickname = azuread_user.test.mail_nickname
}
`, testAccADUser_basic(id, password))
}
6 changes: 3 additions & 3 deletions azuread/data_users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func testAccAzureADUsersDataSource_byUserPrincipalNames(id int, password string)
%s

data "azuread_users" "test" {
user_principal_names = ["${azuread_user.testA.user_principal_name}", "${azuread_user.testB.user_principal_name}"]
user_principal_names = [azuread_user.testA.user_principal_name, azuread_user.testB.user_principal_name]
}
`, testAccADUser_threeUsersABC(id, password))
}
Expand All @@ -105,7 +105,7 @@ func testAccAzureADUsersDataSource_byObjectIds(id int, password string) string {
%s

data "azuread_users" "test" {
object_ids = ["${azuread_user.testA.object_id}", "${azuread_user.testB.object_id}"]
object_ids = [azuread_user.testA.object_id, azuread_user.testB.object_id]
}
`, testAccADUser_threeUsersABC(id, password))
}
Expand All @@ -115,7 +115,7 @@ func testAccAzureADUsersDataSource_byMailNicknames(id int, password string) stri
%s

data "azuread_users" "test" {
mail_nicknames = ["${azuread_user.testA.mail_nickname}", "${azuread_user.testB.mail_nickname}"]
mail_nicknames = [azuread_user.testA.mail_nickname, azuread_user.testB.mail_nickname]
}
`, testAccADUser_threeUsersABC(id, password))
}
Expand Down
16 changes: 8 additions & 8 deletions azuread/resource_application_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func testAccADObjectCertificateApplication_basic(ri int, keyType, endDate, value
%s

resource "azuread_application_certificate" "test" {
application_object_id = "${azuread_application.test.id}"
application_object_id = azuread_application.test.id
type = "%s"
end_date = "%s"
value = <<EOT
Expand All @@ -251,7 +251,7 @@ func testAccADApplicationCertificate_complete(ri int, keyId, keyType, startDate,
%s

resource "azuread_application_certificate" "test" {
application_object_id = "${azuread_application.test.id}"
application_object_id = azuread_application.test.id
key_id = "%s"
type = "%s"
start_date = "%s"
Expand All @@ -268,7 +268,7 @@ func testAccADApplicationCertificate_relativeEndDate(ri int, keyType, value stri
%s

resource "azuread_application_certificate" "test" {
application_object_id = "${azuread_application.test.id}"
application_object_id = azuread_application.test.id
end_date_relative = "4320h"
type = "%s"
value = <<EOT
Expand All @@ -284,11 +284,11 @@ func testAccADApplicationCertificate_requiresImport(ri int, keyType, endDate, va
%s

resource "azuread_application_certificate" "import" {
application_object_id = "${azuread_application_certificate.test.application_object_id}"
key_id = "${azuread_application_certificate.test.key_id}"
type = "${azuread_application_certificate.test.type}"
end_date = "${azuread_application_certificate.test.end_date}"
value = "${azuread_application_certificate.test.value}"
application_object_id = azuread_application_certificate.test.application_object_id
key_id = azuread_application_certificate.test.key_id
type = azuread_application_certificate.test.type
end_date = azuread_application_certificate.test.end_date
value = azuread_application_certificate.test.value
}
`, template)
}
Loading