Skip to content

Commit 5c4fe80

Browse files
committed
Add domain management entrypoints for tenants
Introduces Invoke-AddDomain.ps1 for adding domains and Invoke-ExecDomainAction.ps1 for verifying, deleting, or setting a domain as default for a tenant. These entrypoints interact with Microsoft Graph API and include input validation, error handling, and logging.
1 parent 7424e39 commit 5c4fe80

File tree

2 files changed

+141
-0
lines changed

2 files changed

+141
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
function Invoke-AddDomain {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
Tenant.Administration.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$APIName = $Request.Params.CIPPEndpoint
12+
$TenantFilter = $Request.Body.tenantFilter
13+
$DomainName = $Request.Body.domain
14+
15+
# Interact with query parameters or the body of the request.
16+
try {
17+
if ([string]::IsNullOrWhiteSpace($DomainName)) {
18+
throw 'Domain name is required'
19+
}
20+
21+
# Validate domain name format
22+
if ($DomainName -notmatch '^[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])?)*$') {
23+
throw 'Invalid domain name format'
24+
}
25+
26+
Write-Information "Adding domain $DomainName to tenant $TenantFilter"
27+
28+
$Body = @{
29+
id = $DomainName
30+
} | ConvertTo-Json -Compress
31+
32+
$GraphRequest = New-GraphPOSTRequest -uri 'https://graph.microsoft.com/beta/domains' -tenantid $TenantFilter -type POST -body $Body -AsApp $true
33+
34+
$Result = "Successfully added domain $DomainName to tenant $TenantFilter. Please verify the domain to complete setup."
35+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Added domain $DomainName" -Sev 'Info'
36+
$StatusCode = [HttpStatusCode]::OK
37+
} catch {
38+
$ErrorMessage = Get-CippException -Exception $_
39+
$Result = "Failed to add domain $DomainName`: $($ErrorMessage.NormalizedError)"
40+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Failed to add domain $DomainName`: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
41+
$StatusCode = [HttpStatusCode]::Forbidden
42+
}
43+
44+
return ([HttpResponseContext]@{
45+
StatusCode = $StatusCode
46+
Body = @{'Results' = $Result }
47+
})
48+
49+
}
50+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
function Invoke-ExecDomainAction {
2+
<#
3+
.FUNCTIONALITY
4+
Entrypoint
5+
.ROLE
6+
Tenant.Administration.ReadWrite
7+
#>
8+
[CmdletBinding()]
9+
param($Request, $TriggerMetadata)
10+
11+
$APIName = $Request.Params.CIPPEndpoint
12+
$TenantFilter = $Request.Body.tenantFilter
13+
$DomainName = $Request.Body.domain
14+
$Action = $Request.Body.Action
15+
16+
try {
17+
if ([string]::IsNullOrWhiteSpace($DomainName)) {
18+
throw 'Domain name is required'
19+
}
20+
21+
if ([string]::IsNullOrWhiteSpace($Action)) {
22+
throw 'Action is required'
23+
}
24+
25+
switch ($Action) {
26+
'verify' {
27+
Write-Information "Verifying domain $DomainName for tenant $TenantFilter"
28+
29+
$Body = @{
30+
verificationDnsRecordCollection = @()
31+
} | ConvertTo-Json -Compress
32+
33+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName/verify" -tenantid $TenantFilter -type POST -body $Body -AsApp $true
34+
35+
$Result = @{
36+
resultText = "Domain $DomainName has been verified successfully."
37+
state = 'success'
38+
}
39+
40+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Verified domain $DomainName" -Sev 'Info'
41+
$StatusCode = [HttpStatusCode]::OK
42+
}
43+
'delete' {
44+
Write-Information "Deleting domain $DomainName from tenant $TenantFilter"
45+
46+
$null = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName" -tenantid $TenantFilter -type DELETE -AsApp $true
47+
48+
$Result = @{
49+
resultText = "Domain $DomainName has been deleted successfully."
50+
state = 'success'
51+
}
52+
53+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Deleted domain $DomainName" -Sev 'Info'
54+
$StatusCode = [HttpStatusCode]::OK
55+
}
56+
'setDefault' {
57+
Write-Information "Setting domain $DomainName as default for tenant $TenantFilter"
58+
59+
$Body = @{
60+
isDefault = $true
61+
} | ConvertTo-Json -Compress
62+
63+
$GraphRequest = New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/domains/$DomainName" -tenantid $TenantFilter -type PATCH -body $Body -AsApp $true
64+
65+
$Result = @{
66+
resultText = "Domain $DomainName has been set as the default domain successfully."
67+
state = 'success'
68+
}
69+
70+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Set domain $DomainName as default" -Sev 'Info'
71+
$StatusCode = [HttpStatusCode]::OK
72+
}
73+
default {
74+
throw "Invalid action: $Action"
75+
}
76+
}
77+
} catch {
78+
$ErrorMessage = Get-CippException -Exception $_
79+
$Result = @{
80+
resultText = "Failed to perform action on domain $DomainName`: $($ErrorMessage.NormalizedError)"
81+
state = 'error'
82+
}
83+
Write-LogMessage -headers $Request.Headers -API $APIName -tenant $TenantFilter -message "Failed to perform action on domain $DomainName`: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
84+
$StatusCode = [HttpStatusCode]::Forbidden
85+
}
86+
87+
return ([HttpResponseContext]@{
88+
StatusCode = $StatusCode
89+
Body = @{'Results' = $Result }
90+
})
91+
}

0 commit comments

Comments
 (0)