|
| 1 | +function Invoke-AddTenant { |
| 2 | + <# |
| 3 | + .FUNCTIONALITY |
| 4 | + Entrypoint,AnyTenant |
| 5 | + .ROLE |
| 6 | + Tenant.Config.ReadWrite |
| 7 | + #> |
| 8 | + [CmdletBinding()] |
| 9 | + param($Request, $TriggerMetadata) |
| 10 | + |
| 11 | + $APIName = $Request.Params.CIPPEndpoint |
| 12 | + Write-LogMessage -headers $Request.Headers -API $APINAME -message 'Accessed this API' -Sev 'Debug' |
| 13 | + |
| 14 | + switch ($Request.Body.Action) { |
| 15 | + 'ValidateDomain' { |
| 16 | + # Validate the onmicrosoft.com domain |
| 17 | + $Domain = "$($Request.Body.TenantName).onmicrosoft.com" |
| 18 | + $DomainCheckUri = "https://api.partnercenter.microsoft.com/v1/domains/$Domain" |
| 19 | + try { |
| 20 | + $DomainCheckResponse = New-GraphPOSTRequest -type HEAD -uri $DomainCheckUri -scope 'https://api.partnercenter.microsoft.com/.default' -NoAuthCheck $true |
| 21 | + } catch { |
| 22 | + return @{ |
| 23 | + Status = 'Error' |
| 24 | + Message = "The domain '$Domain' is already in use." |
| 25 | + } |
| 26 | + } |
| 27 | + return @{ |
| 28 | + Status = 'Success' |
| 29 | + Message = "The domain '$Domain' is available." |
| 30 | + } |
| 31 | + } |
| 32 | + 'AddTenant' { |
| 33 | + # Fetch the organization id for Tier 2 CSPs |
| 34 | + if ($Request.Body.ResellerType -eq 'Tier2') { |
| 35 | + $OrganizationProfileUri = 'https://api.partnercenter.microsoft.com/v1/profiles/organization' |
| 36 | + try { |
| 37 | + $OrgResponse = New-GraphPOSTRequest -type GET -uri $OrganizationProfileUri -scope 'https://api.partnercenter.microsoft.com/.default' -NoAuthCheck $true |
| 38 | + $Request.Body.AssociatedPartnerId = $OrgResponse.id |
| 39 | + } catch { |
| 40 | + $Body = @{ |
| 41 | + state = 'Error' |
| 42 | + resultText = "Failed to retrieve organization profile: $($_.Exception.Message)" |
| 43 | + } |
| 44 | + break |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + $Payload = @{ |
| 49 | + enableGDAPByDefault = $false |
| 50 | + Id = $null |
| 51 | + CommerceId = $null |
| 52 | + CompanyProfile = @{ |
| 53 | + TenantId = $null |
| 54 | + Domain = '{0}.onmicrosoft.com' -f $Request.Body.TenantName |
| 55 | + CompanyName = $Request.Body.CompanyName |
| 56 | + Attributes = @{ ObjectType = 'CustomerCompanyProfile' } |
| 57 | + } |
| 58 | + BillingProfile = @{ |
| 59 | + Id = $null |
| 60 | + FirstName = $Request.Body.FirstName |
| 61 | + LastName = $Request.Body.LastName |
| 62 | + Email = $Request.Body.Email |
| 63 | + Culture = 'EN-US' |
| 64 | + Language = 'En' |
| 65 | + CompanyName = $Request.Body.CompanyName |
| 66 | + DefaultAddress = @{ |
| 67 | + Country = $Request.Body.Country |
| 68 | + Region = $null |
| 69 | + City = $Request.Body.City |
| 70 | + State = $Request.Body.State |
| 71 | + AddressLine1 = $Request.Body.AddressLine1 |
| 72 | + AddressLine2 = $Request.Body.AddressLine2 |
| 73 | + PostalCode = $Request.Body.PostalCode |
| 74 | + FirstName = $Request.Body.FirstName |
| 75 | + LastName = $Request.Body.LastName |
| 76 | + PhoneNumber = $Request.Body.PhoneNumber |
| 77 | + } |
| 78 | + Attributes = @{ ObjectType = 'CustomerBillingProfile' } |
| 79 | + } |
| 80 | + RelationshipToPartner = 'none' |
| 81 | + AllowDelegatedAccess = $null |
| 82 | + UserCredentials = $null |
| 83 | + CustomDomains = $null |
| 84 | + Attributes = @{ ObjectType = 'Customer' } |
| 85 | + } |
| 86 | + |
| 87 | + if ($Request.Body.ResellerType -eq 'Tier2' -and $Request.Body.AssociatedPartnerId) { |
| 88 | + $Payload.AssociatedPartnerId = $Request.Body.AssociatedPartnerId |
| 89 | + } |
| 90 | + |
| 91 | + $CustomerCreationUri = 'https://api.partnercenter.microsoft.com/v1/customers' |
| 92 | + try { |
| 93 | + $Response = New-GraphPOSTRequest -type POST -uri $CustomerCreationUri -scope 'https://api.partnercenter.microsoft.com/.default' -Body ($Payload | ConvertTo-Json -Depth 10) -NoAuthCheck $true |
| 94 | + |
| 95 | + $Body = @{ |
| 96 | + state = 'Success' |
| 97 | + resultText = "Tenant created successfully. 'Username is $($Response.userCredentials.userName)@{0}.onmicrosoft.com'. Click copy to retrieve the password." -f $Request.Body.TenantName |
| 98 | + copyField = $Response.userCredentials.password |
| 99 | + } |
| 100 | + } catch { |
| 101 | + $Body = @{ |
| 102 | + state = 'Error' |
| 103 | + resultText = "Failed to create tenant: $($_.Exception.Message)" |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + 'ValidateAddress' { |
| 108 | + $AddressPayload = @{ |
| 109 | + AddressLine1 = $Request.Body.AddressLine1 |
| 110 | + AddressLine2 = $Request.Body.AddressLine2 |
| 111 | + City = $Request.Body.City |
| 112 | + State = $Request.Body.State |
| 113 | + PostalCode = $Request.Body.PostalCode |
| 114 | + Country = $Request.Body.Country |
| 115 | + } |
| 116 | + |
| 117 | + $AddressValidationUri = 'https://api.partnercenter.microsoft.com/v1/validations/address' |
| 118 | + try { |
| 119 | + $Response = New-GraphPOSTRequest -type POST -uri $AddressValidationUri -scope 'https://api.partnercenter.microsoft.com/.default' -Body ($AddressPayload | ConvertTo-Json -Depth 10) -NoAuthCheck $true |
| 120 | + |
| 121 | + return @{ |
| 122 | + Status = 'Success' |
| 123 | + OriginalAddress = $Response.originalAddress |
| 124 | + SuggestedAddresses = $Response.suggestedAddresses |
| 125 | + ValidationStatus = $Response.status |
| 126 | + } |
| 127 | + } catch { |
| 128 | + return @{ |
| 129 | + state = 'Error' |
| 130 | + resultText = "Address validation failed: $($_.Exception.Message)" |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + default { |
| 135 | + return @{ |
| 136 | + state = 'Error' |
| 137 | + resultText = "Invalid action specified: $($Request.Body.Action)" |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ |
| 143 | + StatusCode = [HttpStatusCode]::InternalServerError |
| 144 | + Body = $Body |
| 145 | + }) |
| 146 | +} |
0 commit comments