Skip to content

Commit

Permalink
feat(invitation): fix ci issues
Browse files Browse the repository at this point in the history
  • Loading branch information
romainDavaze authored and manicminer committed Sep 2, 2021
1 parent 1a00931 commit c2f401d
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 192 deletions.
6 changes: 3 additions & 3 deletions docs/resources/invitation.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ resource "azuread_invitation" "example" {
user_message_info {
cc_recipients = ["[email protected]"]
customized_message_body = "Hello there! You are invited to join my Azure tenant !"
customised_message_body = "Hello there! You are invited to join my Azure tenant !"
message_language = "en-US"
}
}
Expand All @@ -42,7 +42,7 @@ resource "azuread_invitation" "example" {
The following arguments are supported:

* `redirect_url` - (Required) URL the user should be redirected to once the invitation is redeemed.
* `send_invitation_message` - (Optional) If `true`, an email wille be sent to the user being invited. Must be set to `true` if a `user_message_info` block is specified. Defaults to `false`.
* `send_invitation_message` - (Optional) If `true`, an email will be sent to the user being invited. Must be set to `true` if a `user_message_info` block is specified. Defaults to `false`.
* `user_display_name` - (Optional) Display name of the user being invited.
* `user_email_address` - (Required) Email address of the user being invited.
* `user_message_info` - (Optional) A `user_message_info` block as documented below, which configures the message being sent to the invited user. `send_invitation_message` must be set to `true` if this block is specified.
Expand All @@ -52,7 +52,7 @@ The following arguments are supported:
`user_message_info` block supports the following:

* `cc_recipients` - (Optional) Additional recipients the invitation message should be sent to. Currently only 1 additional recipient is supported by Azure.
* `customized_message_body` - (Optional) Customized message body you want to send if you don't want the default message.
* `customised_message_body` - (Optional) Customised message body you want to send if you don't want the default message.
* `message_language` - (Optional) Language the message will be sent in. The value specified must be in ISO 639 format. Defaults to `en-US`.


Expand Down
10 changes: 3 additions & 7 deletions internal/services/invitations/client/client.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package client

import (
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/manicminer/hamilton/msgraph"

"github.com/hashicorp/terraform-provider-azuread/internal/common"
)

type Client struct {
AadClient *graphrbac.UsersClient // We set this as a UsersClient for now because configureClient does not support passing nil as autorest client.
MsClient *msgraph.InvitationsClient
MsClient *msgraph.InvitationsClient
}

func NewClient(o *common.ClientOptions) *Client {
aadClient := graphrbac.NewUsersClient(o.TenantID)
msClient := msgraph.NewInvitationsClient(o.TenantID)
o.ConfigureClient(&msClient.BaseClient, &aadClient.Client)
o.ConfigureClient(&msClient.BaseClient)

return &Client{
AadClient: &aadClient,
MsClient: msClient,
MsClient: msClient,
}
}
138 changes: 128 additions & 10 deletions internal/services/invitations/invitation_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package invitations

import (
"context"
"errors"
"fmt"
"log"
"net/http"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/manicminer/hamilton/msgraph"

"github.com/hashicorp/terraform-provider-azuread/internal/clients"
"github.com/hashicorp/terraform-provider-azuread/internal/tf"
"github.com/hashicorp/terraform-provider-azuread/internal/utils"
"github.com/hashicorp/terraform-provider-azuread/internal/validate"
)

Expand Down Expand Up @@ -68,7 +73,7 @@ func invitationResource() *schema.Resource {
Type: schema.TypeString,
},
},
"customized_message_body": {
"customised_message_body": {
Type: schema.TypeString,
Optional: true,
ValidateDiagFunc: validate.NoEmptyStrings,
Expand All @@ -95,22 +100,135 @@ func invitationResource() *schema.Resource {
}

func invitationResourceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if useMsGraph := meta.(*clients.Client).EnableMsGraphBeta; useMsGraph {
return invitationResourceCreateMsGraph(ctx, d, meta)
client := meta.(*clients.Client).Invitations.MsClient

invitedUserEmailAddress := d.Get("user_email_address").(string)
inviteRedirectUrl := d.Get("redirect_url").(string)

properties := msgraph.Invitation{
InvitedUserEmailAddress: utils.String(invitedUserEmailAddress),
InviteRedirectURL: utils.String(inviteRedirectUrl),
}

if v, ok := d.GetOk("user_display_name"); ok {
properties.InvitedUserDisplayName = utils.String(v.(string))
}

if v, ok := d.GetOk("send_invitation_message"); ok {
properties.SendInvitationMessage = utils.Bool(v.(bool))
}
return tf.ErrorDiagF(nil, "azuread_invitation does not support AAD Graph. Please enable `EnableMsGraphBeta`")

if v, ok := d.GetOk("user_message_info"); ok {
// Since ValidateFunc and ValidateDiagFunc are not yet supported on lists or sets we must check for send_invitation_message value here
if properties.SendInvitationMessage == nil || !*properties.SendInvitationMessage {
return tf.ErrorDiagF(errors.New("Wrong value"), "When `user_message_info` is specified, `send_invitation_message` must be set to `true`")
}

properties.InvitedUserMessageInfo = expandInvitedUserMessageInfo(v.([]interface{}))
}

invitation, _, err := client.Create(ctx, properties)
if err != nil {
return tf.ErrorDiagF(err, "Could not create invitation")
}

if invitation.ID == nil || *invitation.ID == "" {
return tf.ErrorDiagF(errors.New("Bad API response"), "Object ID returned for invitation is nil/empty")
}
d.SetId(*invitation.ID)

if invitation.InvitedUser == nil || invitation.InvitedUser.ID == nil || *invitation.InvitedUser.ID == "" {
return tf.ErrorDiagF(errors.New("Bad API response"), "Invited user object ID returned for invitation is nil/empty")
}
d.Set("user_id", invitation.InvitedUser.ID)

if invitation.InviteRedeemURL == nil || *invitation.InviteRedeemURL == "" {
return tf.ErrorDiagF(errors.New("Bad API response"), "Redeem URL returned for invitation is nil/empty")
}
d.Set("redeem_url", invitation.InviteRedeemURL)

if err != nil {
return tf.ErrorDiagF(err, "Waiting for User with object ID: %q", *invitation.InvitedUser.ID)
}

return invitationResourceRead(ctx, d, meta)
}

func invitationResourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if useMsGraph := meta.(*clients.Client).EnableMsGraphBeta; useMsGraph {
return invitationResourceReadMsGraph(ctx, d, meta)
client := meta.(*clients.Client).Users.UsersClient

userID := d.Get("user_id").(string)

user, status, err := client.Get(ctx, userID)
if err != nil {
if status == http.StatusNotFound {
log.Printf("[DEBUG] User with Object ID %q was not found - removing from state!", userID)
d.Set("user_id", "")
return nil
}
return tf.ErrorDiagF(err, "Retrieving user with object ID: %q", userID)
}
return tf.ErrorDiagF(nil, "azuread_invitation does not support AAD Graph. Please enable `EnableMsGraphBeta`")

tf.Set(d, "user_id", user.ID)
tf.Set(d, "user_email_address", user.Mail)

return nil
}

func invitationResourceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if useMsGraph := meta.(*clients.Client).EnableMsGraphBeta; useMsGraph {
return invitationResourceDeleteMsGraph(ctx, d, meta)
client := meta.(*clients.Client).Users.UsersClient

userID := d.Get("user_id").(string)

_, status, err := client.Get(ctx, userID)
if err != nil {
if status == http.StatusNotFound {
return tf.ErrorDiagPathF(fmt.Errorf("User was not found"), "id", "Retrieving user with object ID %q", userID)
}

return tf.ErrorDiagPathF(err, "id", "Retrieving user with object ID %q", userID)
}

status, err = client.Delete(ctx, userID)
if err != nil {
return tf.ErrorDiagPathF(err, "id", "Deleting user with object ID %q, got status %d", userID, status)
}

return nil
}

func expandInvitedUserMessageInfo(in []interface{}) *msgraph.InvitedUserMessageInfo {
if len(in) == 0 {
return nil
}
return tf.ErrorDiagF(nil, "azuread_invitation does not support AAD Graph. Please enable `EnableMsGraphBeta`")

result := msgraph.InvitedUserMessageInfo{}
config := in[0].(map[string]interface{})

ccRecipients := config["cc_recipients"].([]interface{})
messageBody := config["customised_message_body"].(string)
messageLanguage := config["message_language"].(string)

result.CCRecipients = expandRecipients(ccRecipients)
result.CustomizedMessageBody = &messageBody
result.MessageLanguage = &messageLanguage

return &result
}

func expandRecipients(in []interface{}) *[]msgraph.Recipient {
recipients := make([]msgraph.Recipient, 0, len(in))
for _, recipientRaw := range in {
recipient := recipientRaw.(string)

newRecipient := msgraph.Recipient{
EmailAddress: &msgraph.EmailAddress{
Address: &recipient,
},
}

recipients = append(recipients, newRecipient)
}

return &recipients
}
157 changes: 0 additions & 157 deletions internal/services/invitations/invitation_resource_msgraph.go

This file was deleted.

Loading

0 comments on commit c2f401d

Please sign in to comment.