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

azuread_service_principal - support for the AppRoleAssignmentRequired property #127

Merged
merged 4 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions azuread/resource_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func resourceServicePrincipal() *schema.Resource {
return &schema.Resource{
Create: resourceServicePrincipalCreate,
Read: resourceServicePrincipalRead,
Update: resourceServicePrincipalUpdate,
Delete: resourceServicePrincipalDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -44,6 +45,11 @@ func resourceServicePrincipal() *schema.Resource {
Computed: true,
},

"app_role_assignment_required": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this above the computed properties?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. What about the oauth2_permissions and tags, would you like me to move those above the computed properties as well, for consistency's sake?

Type: schema.TypeBool,
Optional: true,
},

"oauth2_permissions": graph.SchemaOauth2Permissions(),

"tags": {
Expand Down Expand Up @@ -71,6 +77,11 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er
// given there's no way to change it - we'll just default this to true
AccountEnabled: p.Bool(true),
}

if v, ok := d.GetOk("app_role_assignment_required"); ok {
properties.AppRoleAssignmentRequired = p.Bool(v.(bool))
}

if v, ok := d.GetOk("tags"); ok {
properties.Tags = tf.ExpandStringSlicePtr(v.(*schema.Set).List())
}
Expand All @@ -94,6 +105,23 @@ func resourceServicePrincipalCreate(d *schema.ResourceData, meta interface{}) er
return resourceServicePrincipalRead(d, meta)
}

func resourceServicePrincipalUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).servicePrincipalsClient
ctx := meta.(*ArmClient).StopContext

var properties graphrbac.ServicePrincipalUpdateParameters

if d.HasChange("app_role_assignment_required") {
properties.AppRoleAssignmentRequired = p.Bool(d.Get("app_role_assignment_required").(bool))
}

if _, err := client.Update(ctx, d.Id(), properties); err != nil {
return fmt.Errorf("Error patching Azure AD Service Principal with ID %q: %+v", d.Id(), err)
}

return resourceServicePrincipalRead(d, meta)
}

func resourceServicePrincipalRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).servicePrincipalsClient
ctx := meta.(*ArmClient).StopContext
Expand All @@ -113,6 +141,7 @@ func resourceServicePrincipalRead(d *schema.ResourceData, meta interface{}) erro
d.Set("application_id", app.AppID)
d.Set("display_name", app.DisplayName)
d.Set("object_id", app.ObjectID)
d.Set("app_role_assignment_required", app.AppRoleAssignmentRequired)

// tags doesn't exist as a property, so extract it
if err := d.Set("tags", app.Tags); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion azuread/resource_service_principal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestAccAzureADServicePrincipal_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "display_name"),
resource.TestCheckResourceAttrSet(resourceName, "application_id"),
resource.TestCheckResourceAttr(resourceName, "oauth2_permissions.#", "1"),
resource.TestCheckResourceAttr(resourceName, "app_role_assignment_required", "false"),
resource.TestCheckResourceAttr(resourceName, "oauth2_permissions.0.admin_consent_description", fmt.Sprintf("Allow the application to access %s on behalf of the signed-in user.", fmt.Sprintf("acctestApp-%s", id))),
resource.TestCheckResourceAttrSet(resourceName, "object_id"),
),
Expand All @@ -53,6 +54,7 @@ func TestAccAzureADServicePrincipal_complete(t *testing.T) {
Config: testAccADServicePrincipal_complete(id),
Check: resource.ComposeTestCheckFunc(
testCheckADServicePrincipalExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "app_role_assignment_required", "true"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "3"),
resource.TestCheckResourceAttrSet(resourceName, "object_id"),
),
Expand Down Expand Up @@ -131,7 +133,8 @@ resource "azuread_application" "test" {
}

resource "azuread_service_principal" "test" {
application_id = "${azuread_application.test.application_id}"
application_id = "${azuread_application.test.application_id}"
app_role_assignment_required = true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COuld we align these assingments here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oauth2_permissions would be great, tags however we always leave at the end. Thanks!


tags = ["test", "multiple", "CapitalS"]
}
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/service_principal.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ resource "azuread_application" "example" {

resource "azuread_service_principal" "example" {
application_id = "${azuread_application.example.application_id}"
app_role_assignment_required = false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we aling these assingments here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


tags = ["example", "tags", "here"]
}
Expand All @@ -38,6 +39,8 @@ The following arguments are supported:

* `application_id` - (Required) The ID of the Azure AD Application for which to create a Service Principal.

* `app_role_assignment_required` - (Optional) Does this Service Principal require an AppRoleAssignment to a user or group before Azure AD will issue a user or access token to the application? Defaults to `false`.

* `tags` - (Optional) A list of tags to apply to the Service Principal.

## Attributes Reference
Expand All @@ -52,6 +55,8 @@ The following attributes are exported:

* `display_name` - The Display Name of the Azure Active Directory Application associated with this Service Principal.

* `app_role_assignment_required` - Whether this Service Principal requires an AppRoleAssignment to a user or group before Azure AD will issue a user or access token to the application.

* `oauth2_permissions` - A collection of OAuth 2.0 permissions exposed by the associated application. Each permission is covered by a `oauth2_permission` block as documented below.

---
Expand Down