-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from tiwood/ds_r_azuread_group
New resource & data source 'azuread_group'
- Loading branch information
Showing
9 changed files
with
432 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package azuread | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate" | ||
) | ||
|
||
func dataGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceActiveDirectoryGroupRead, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceActiveDirectoryGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).groupsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
var adgroup graphrbac.ADGroup | ||
var groupObj *graphrbac.ADGroup | ||
|
||
// use the name to find the Azure AD group | ||
name := d.Get("name").(string) | ||
filter := fmt.Sprintf("displayName eq '%s'", name) | ||
log.Printf("[DEBUG] Using filter %q", filter) | ||
|
||
resp, err := client.ListComplete(ctx, filter) | ||
if err != nil { | ||
return fmt.Errorf("Error listing Azure AD groups: %+v", err) | ||
} | ||
|
||
for _, v := range *resp.Response().Value { | ||
if v.DisplayName == nil { | ||
//no DisplayName returned, continue with the next iteration | ||
continue | ||
} else { | ||
if *v.DisplayName == name { | ||
log.Printf("[DEBUG] %q (API result) matches %q (given value). The group has the objectId: %q", *v.DisplayName, name, *v.ObjectID) | ||
groupObj = &v | ||
break | ||
} else { | ||
log.Printf("[DEBUG] %q (API result) does not match %q (given value)", *v.DisplayName, name) | ||
} | ||
} | ||
} | ||
|
||
if groupObj == nil { | ||
return fmt.Errorf("Couldn't locate a Azure AD group with a name of %q", name) | ||
} | ||
|
||
adgroup = *groupObj | ||
|
||
d.SetId(*adgroup.ObjectID) | ||
d.Set("object_id", adgroup.ObjectID) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package azuread | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAzureADGroup_byName(t *testing.T) { | ||
dataSourceName := "data.azuread_group.test" | ||
id, err := uuid.GenerateUUID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
config := testAccDataSourceAzureADGroup_name(id) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureADGroupDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureADGroup(id), | ||
}, | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureADGroupExists(dataSourceName), | ||
resource.TestCheckResourceAttr(dataSourceName, "name", fmt.Sprintf("acctest%s", id)), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAzureADGroup_name(id string) string { | ||
template := testAccAzureADGroup(id) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azuread_group" "test" { | ||
name = "${azuread_group.test.name}" | ||
} | ||
`, template) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package azuread | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/ar" | ||
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/p" | ||
) | ||
|
||
func resourceGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceGroupCreate, | ||
Read: resourceGroupRead, | ||
Delete: resourceGroupDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceGroupCreate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).groupsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
name := d.Get("name").(string) | ||
|
||
properties := graphrbac.GroupCreateParameters{ | ||
DisplayName: &name, | ||
MailEnabled: p.Bool(false), //we're defaulting to false, as the API currently only supports the creation of non-mail enabled security groups. | ||
MailNickname: &name, | ||
SecurityEnabled: p.Bool(true), //we're defaulting to true, as the API currently only supports the creation of non-mail enabled security groups. | ||
} | ||
|
||
group, err := client.Create(ctx, properties) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(*group.ObjectID) | ||
|
||
return resourceGroupRead(d, meta) | ||
} | ||
|
||
func resourceGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).groupsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
resp, err := client.Get(ctx, d.Id()) | ||
if err != nil { | ||
if ar.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[DEBUG] Azure AD group with id %q was not found - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error retrieving Azure AD Group with ID %q: %+v", d.Id(), err) | ||
} | ||
|
||
d.Set("name", resp.DisplayName) | ||
|
||
return nil | ||
} | ||
|
||
func resourceGroupDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).groupsClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
if resp, err := client.Delete(ctx, d.Id()); err != nil { | ||
if !ar.ResponseWasNotFound(resp) { | ||
return fmt.Errorf("Error Deleting Azure AD Group with ID %q: %+v", d.Id(), err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package azuread | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/ar" | ||
) | ||
|
||
func TestAccAzureADGroup_basic(t *testing.T) { | ||
resourceName := "azuread_group.test" | ||
id, err := uuid.GenerateUUID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
config := testAccAzureADGroup(id) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureADGroupDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureADGroupExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("acctest%s", id)), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureADGroup_complete(t *testing.T) { | ||
resourceName := "azuread_group.test" | ||
id, err := uuid.GenerateUUID() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
config := testAccAzureADGroup(id) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureADGroupDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureADGroupExists(resourceName), | ||
resource.TestCheckResourceAttr(resourceName, "name", fmt.Sprintf("acctest%s", id)), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureADGroupExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %q", name) | ||
} | ||
|
||
client := testAccProvider.Meta().(*ArmClient).groupsClient | ||
ctx := testAccProvider.Meta().(*ArmClient).StopContext | ||
resp, err := client.Get(ctx, rs.Primary.ID) | ||
|
||
if err != nil { | ||
if ar.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Bad: Azure AD Group %q does not exist", rs.Primary.ID) | ||
} | ||
return fmt.Errorf("Bad: Get on Azure AD groupsClient: %+v", err) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testCheckAzureADGroupDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azuread_group" { | ||
continue | ||
} | ||
|
||
client := testAccProvider.Meta().(*ArmClient).groupsClient | ||
ctx := testAccProvider.Meta().(*ArmClient).StopContext | ||
resp, err := client.Get(ctx, rs.Primary.ID) | ||
|
||
if err != nil { | ||
if ar.ResponseWasNotFound(resp.Response) { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
return fmt.Errorf("Azure AD group still exists:\n%#v", resp) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccAzureADGroup(id string) string { | ||
return fmt.Sprintf(` | ||
resource "azuread_group" "test" { | ||
name = "acctest%s" | ||
} | ||
`, id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.