-
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.
New resource & data source 'azuread_user' (#18)
- Loading branch information
Showing
65 changed files
with
19,038 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,79 @@ | ||
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/ar" | ||
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate" | ||
) | ||
|
||
func dataSourceUser() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceUserRead, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"user_principal_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
|
||
"account_enabled": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"display_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"mail": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"mail_nickname": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceUserRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).usersClient | ||
ctx := meta.(*ArmClient).StopContext | ||
|
||
var user graphrbac.User | ||
|
||
queryString := d.Get("user_principal_name").(string) | ||
|
||
log.Printf("[DEBUG] Using Get with the following query string: %q", queryString) | ||
user, err := client.Get(ctx, queryString) | ||
if err != nil { | ||
if ar.ResponseWasNotFound(user.Response) { | ||
return fmt.Errorf("Error: No AzureAD User found with the following query string: %q", queryString) | ||
} | ||
return fmt.Errorf("Error making Read request on AzureAD User the following query string: %q: %+v", queryString, err) | ||
} | ||
|
||
if user.ObjectID == nil { | ||
return fmt.Errorf("User objectId is nil") | ||
} | ||
|
||
d.SetId(*user.ObjectID) | ||
d.Set("user_principal_name", user.UserPrincipalName) | ||
d.Set("account_enabled", user.AccountEnabled) | ||
d.Set("display_name", user.DisplayName) | ||
d.Set("mail", user.Mail) | ||
d.Set("mail_nickname", user.MailNickname) | ||
|
||
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,44 @@ | ||
package azuread | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAzureADUser_byUserPrincipalName(t *testing.T) { | ||
dataSourceName := "data.azuread_user.test" | ||
id := acctest.RandStringFromCharSet(7, acctest.CharSetAlphaNum) | ||
password := id + "p@$$wR2" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAzureADUserDataSource_byUserPrincipalName(id, password), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet(dataSourceName, "user_principal_name"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "account_enabled"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "display_name"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "mail_nickname"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccAzureADUserDataSource_byUserPrincipalName(id, password string) string { | ||
template := testAccADUser_basic(id, password) | ||
return fmt.Sprintf(` | ||
%s | ||
data "azuread_user" "test" { | ||
user_principal_name = "${azuread_user.test.user_principal_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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package validate | |
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
|
@@ -18,3 +19,18 @@ func NoEmptyStrings(i interface{}, k string) ([]string, []error) { | |
|
||
return nil, nil | ||
} | ||
|
||
// StringIsEmailAddress validates that the given string is a valid email address ([email protected]) | ||
func StringIsEmailAddress(i interface{}, k string) ([]string, []error) { | ||
v, ok := i.(string) | ||
if !ok { | ||
return nil, []error{fmt.Errorf("expected type of %q to be string", k)} | ||
} | ||
|
||
regExIsEmailAddress := regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") | ||
if !regExIsEmailAddress.MatchString(v) { | ||
return nil, []error{fmt.Errorf("%q must be in email address format", k)} | ||
} | ||
|
||
return nil, 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 |
---|---|---|
|
@@ -97,3 +97,37 @@ func TestNoEmptyStrings(t *testing.T) { | |
}) | ||
} | ||
} | ||
|
||
func TestStringIsEmailAddress(t *testing.T) { | ||
cases := []struct { | ||
Value string | ||
TestName string | ||
ErrCount int | ||
}{ | ||
{ | ||
Value: "[email protected]", | ||
TestName: "Valid_EmailAddress", | ||
ErrCount: 0, | ||
}, | ||
{ | ||
Value: "john.doehashicorp.com", | ||
TestName: "Invalid_EmailAddress_NoAtChar", | ||
ErrCount: 1, | ||
}, | ||
{ | ||
Value: "john/doe@ha$hicorp.com", | ||
TestName: "Invalid_EmailAddress_InvalidChars", | ||
ErrCount: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.TestName, func(t *testing.T) { | ||
_, errors := StringIsEmailAddress(tc.Value, tc.TestName) | ||
|
||
if len(errors) != tc.ErrCount { | ||
t.Fatalf("Expected StringIsEmailAddress to have %d not %d errors for %q", tc.ErrCount, len(errors), tc.TestName) | ||
} | ||
}) | ||
} | ||
} |
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.