Skip to content

Commit

Permalink
azuread_service_principal: add tags property (#31)
Browse files Browse the repository at this point in the history
fixes #5
  • Loading branch information
katbyte authored Jan 25, 2019
1 parent 3a0d164 commit 1fea7e0
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
2 changes: 0 additions & 2 deletions azuread/data_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func dataApplicationRead(d *schema.ResourceData, meta interface{}) error {
filter := fmt.Sprintf("displayName eq '%s'", name)

resp, err := client.ListComplete(ctx, filter)

if err != nil {
return fmt.Errorf("Error listing Azure AD Applications: %+v", err)
}
Expand All @@ -118,7 +117,6 @@ func dataApplicationRead(d *schema.ResourceData, meta interface{}) error {
if app == nil {
return fmt.Errorf("Couldn't locate an Azure AD Application with a name of %q", name)
}

application = *app
}

Expand Down
40 changes: 36 additions & 4 deletions azuread/resource_service_principal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"log"

"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/tf"

"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate"

"github.com/hashicorp/go-azure-helpers/response"
Expand Down Expand Up @@ -33,6 +35,16 @@ func resourceServicePrincipal() *schema.Resource {
ValidateFunc: validate.UUID,
},

"tags": {
Type: schema.TypeSet,
Optional: true,
Set: schema.HashString,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"display_name": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -53,18 +65,28 @@ 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("tags"); ok {
properties.Tags = tf.ExpandStringArrayPtr(v.(*schema.Set).List())
}

app, err := client.Create(ctx, properties)
sp, err := client.Create(ctx, properties)
if err != nil {
return fmt.Errorf("Error creating Service Principal %q: %+v", applicationId, err)
return fmt.Errorf("Error creating Service Principal for application %q: %+v", applicationId, err)
}

if sp.ObjectID == nil {
return fmt.Errorf("Create returned a nil object id for application %q", applicationId)
}
objectId := *sp.ObjectID

objectId := *app.ObjectID
resp, err := client.Get(ctx, objectId)
if err != nil {
return fmt.Errorf("Error retrieving Service Principal ID %q: %+v", objectId, err)
return fmt.Errorf("Error retrieving Service Principal with ID %q: %+v", objectId, err)
}

if resp.ObjectID == nil {
return fmt.Errorf("Get returned a nil object ID for %q", objectId)
}
d.SetId(*resp.ObjectID)

return resourceServicePrincipalRead(d, meta)
Expand All @@ -75,6 +97,7 @@ func resourceServicePrincipalRead(d *schema.ResourceData, meta interface{}) erro
ctx := meta.(*ArmClient).StopContext

objectId := d.Id()

app, err := client.Get(ctx, objectId)
if err != nil {
if ar.ResponseWasNotFound(app.Response) {
Expand All @@ -88,6 +111,15 @@ func resourceServicePrincipalRead(d *schema.ResourceData, meta interface{}) erro
d.Set("application_id", app.AppID)
d.Set("display_name", app.DisplayName)

// tags doesn't exist as a property, so extract it
if iTags, ok := app.AdditionalProperties["tags"]; ok {
if tags, ok := iTags.([]interface{}); ok {
if err := d.Set("tags", tf.ExpandStringArrayPtr(tags)); err != nil {
return fmt.Errorf("Error setting `tags`: %+v", err)
}
}
}

return nil
}

Expand Down
42 changes: 40 additions & 2 deletions azuread/resource_service_principal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ import (
func TestAccAzureADServicePrincipal_basic(t *testing.T) {
resourceName := "azuread_service_principal.test"
id := uuid.New().String()
config := testAccADServicePrincipal_basic(id)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckADServicePrincipalDestroy,
Steps: []resource.TestStep{
{
Config: config,
Config: testAccADServicePrincipal_basic(id),
Check: resource.ComposeTestCheckFunc(
testCheckADServicePrincipalExists(resourceName),
resource.TestCheckResourceAttrSet(resourceName, "display_name"),
Expand All @@ -38,6 +37,31 @@ func TestAccAzureADServicePrincipal_basic(t *testing.T) {
})
}

func TestAccAzureADServicePrincipal_complete(t *testing.T) {
resourceName := "azuread_service_principal.test"
id := uuid.New().String()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckADServicePrincipalDestroy,
Steps: []resource.TestStep{
{
Config: testAccADServicePrincipal_complete(id),
Check: resource.ComposeTestCheckFunc(
testCheckADServicePrincipalExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "tags.#", "3"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckADServicePrincipalExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -95,3 +119,17 @@ resource "azuread_service_principal" "test" {
}
`, id)
}

func testAccADServicePrincipal_complete(id string) string {
return fmt.Sprintf(`
resource "azuread_application" "test" {
name = "acctestspa%s"
}
resource "azuread_service_principal" "test" {
application_id = "${azuread_application.test.application_id}"
tags = ["test", "multiple", "CapitalS"]
}
`, id)
}
4 changes: 4 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,8 @@ resource "azuread_application" "test" {
resource "azuread_service_principal" "test" {
application_id = "${azuread_application.test.application_id}"
tags = ["example", "tags", "here"]
}
```

Expand All @@ -36,6 +38,8 @@ The following arguments are supported:

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

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

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit 1fea7e0

Please sign in to comment.