Skip to content

Commit

Permalink
implement oauth2_permission attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
janschumann committed May 14, 2019
1 parent d8e6074 commit a7d331d
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
52 changes: 52 additions & 0 deletions azuread/data_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,54 @@ func dataApplication() *schema.Resource {
Computed: true,
},

"oauth2_permissions": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"admin_consent_description": {
Type: schema.TypeString,
Computed: true,
},

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

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

"is_enabled": {
Type: schema.TypeBool,
Computed: true,
},

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

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

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

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

"required_resource_access": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -175,5 +223,9 @@ func dataApplicationRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error setting `required_resource_access`: %+v", err)
}

if oauth2Permissions, ok := application.AdditionalProperties["oauth2Permissions"].([]interface{}); ok {
d.Set("oauth2_permissions", flattenADApplicationOauth2Permissions(oauth2Permissions))
}

return nil
}
2 changes: 2 additions & 0 deletions azuread/data_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestAccAzureADApplicationDataSource_byObjectId(t *testing.T) {
resource.TestCheckResourceAttr(dataSourceName, "reply_urls.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "required_resource_access.#", "0"),
resource.TestCheckResourceAttr(dataSourceName, "oauth2_allow_implicit_flow", "false"),
resource.TestCheckResourceAttr(dataSourceName, "oauth2_permissions.#", "1"),
resource.TestCheckResourceAttr(dataSourceName, "oauth2_permissions.0.admin_consent_description", fmt.Sprintf("Allow the application to access %s on behalf of the signed-in user.", fmt.Sprintf("acctest%s", id))),
resource.TestCheckResourceAttrSet(dataSourceName, "application_id"),
),
},
Expand Down
94 changes: 94 additions & 0 deletions azuread/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,55 @@ func resourceApplication() *schema.Resource {
Computed: true,
},

"oauth2_permissions": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"admin_consent_description": {
Type: schema.TypeString,
Computed: true,
},

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

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

"is_enabled": {
Type: schema.TypeBool,
Computed: true,
},

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

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

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

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

"required_resource_access": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -121,6 +170,7 @@ func resourceApplicationCreate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)

properties := graphrbac.ApplicationCreateParameters{
AdditionalProperties: make(map[string]interface{}),
DisplayName: &name,
Homepage: expandADApplicationHomepage(d, name),
IdentifierUris: tf.ExpandStringArrayPtr(d.Get("identifier_uris").([]interface{})),
Expand Down Expand Up @@ -224,6 +274,10 @@ func resourceApplicationRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error setting `required_resource_access`: %+v", err)
}

if oauth2Permissions, ok := resp.AdditionalProperties["oauth2Permissions"].([]interface{}); ok {
d.Set("oauth2_permissions", flattenADApplicationOauth2Permissions(oauth2Permissions))
}

return nil
}

Expand Down Expand Up @@ -340,3 +394,43 @@ func flattenADApplicationResourceAccess(in *[]graphrbac.ResourceAccess) []interf

return accesses
}

func flattenADApplicationOauth2Permissions(in []interface{}) []map[string]interface{} {
if in == nil {
return []map[string]interface{}{}
}

result := make([]map[string]interface{}, 0, len(in))
for _, oauth2Permissions := range in {
rawPermission := oauth2Permissions.(map[string]interface{})
permission := make(map[string]interface{})
if rawPermission["adminConsentDescription"] != nil {
permission["admin_consent_description"] = rawPermission["adminConsentDescription"]
}
if rawPermission["adminConsentDisplayName"] != nil {
permission["admin_consent_description"] = rawPermission["adminConsentDescription"]
}
if rawPermission["id"] != nil {
permission["id"] = rawPermission["id"]
}
if rawPermission["isEnabled"] != nil {
permission["is_enabled"] = rawPermission["isEnabled"].(bool)
}
if rawPermission["type"] != nil {
permission["type"] = rawPermission["type"]
}
if rawPermission["userConsentDescription"] != nil {
permission["user_consent_description"] = rawPermission["userConsentDescription"]
}
if rawPermission["userConsentDisplayName"] != nil {
permission["user_consent_display_name"] = rawPermission["userConsentDisplayName"]
}
if rawPermission["value"] != nil {
permission["value"] = rawPermission["value"]
}

result = append(result, permission)
}

return result
}
2 changes: 2 additions & 0 deletions azuread/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func TestAccAzureADApplication_basic(t *testing.T) {
testCheckADApplicationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("acctest%s", id)),
resource.TestCheckResourceAttr(resourceName, "homepage", fmt.Sprintf("https://acctest%s", id)),
resource.TestCheckResourceAttr(resourceName, "oauth2_permissions.#", "1"),
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("acctest%s", id))),
resource.TestCheckResourceAttrSet(resourceName, "application_id"),
),
},
Expand Down

0 comments on commit a7d331d

Please sign in to comment.