# Simple fucntion to enable all availiable offering items for child (partner or customer) tenant function Enable-All-OfferingItems{ [CmdletBinding()] Param( [parameter(Mandatory=$true)] [string] $ParentTenantID, [parameter(Mandatory=$true)] [string] $TenantID, [parameter(Mandatory=$true)] [System.Collections.IDictionary] $AuthHeader, [parameter(Mandatory=$false)] [string] $Edition = "standard", [parameter(Mandatory=$false)] [string] $Kind = "customer" ) $queryParameters = @{ edition = $Edition; kind = $Kind } # Get all availiable services for root tenant $applications = Invoke-RestMethod -Method Get -Uri "${base_url}api/2/tenants/${ParentTenantID}/applications" -Headers $AuthHeader # Enable all avaiable services for the partner tenant # It's preferrable to use Batch API, but here an enumeration are used # to give you possibility to customize applications enablement for a partner foreach($application_id in $applications.items){ Invoke-RestMethod -Method Post -Uri "${base_url}api/2/applications/${application_id}/bindings/tenants/${TenantID}" -Headers $headers } # Get Offergin Items Availiable for the child tentnts $offering_items = Invoke-RestMethod -Method Get -Uri "${base_url}api/2/tenants/${ParentTenantID}/offering_items/available_for_child" -Headers $headers -Body $queryParameters # The next API expeced to have offering_items root, but the previous API returns items # So we create an alias property ans use them to generate JSON for the next API request $offering_items | Add-Member -MemberType AliasProperty -Name offering_items -Value items | Select-Object offering_items $offering_items_json = $offering_items | Select-Object offering_items| ConvertTo-Json -Depth 5 $offering_items_json| Out-File "${TenantID}_offering_items.json" # Enable all offerting items for the partner Invoke-RestMethod -Method Put -Uri "${base_url}api/2/tenants/${TenantID}/offering_items" -Headers $headers -Body $offering_items_json -ContentType "application/json" } # Read an API Client info from a file and store client_id and client_secret in variables $token = Get-Content "api_token.json" | Out-String | ConvertFrom-Json $access_token = $token.access_token # Manualy construct Bearer $bearerAuthValue = "Bearer $access_token" $headers = @{ "Authorization" = $bearerAuthValue } # Base URL for all requests -- replace with your own # Here we exepected that you are in the sandbox availaible fron Acronis Developer Network Portal $base_url = "https://dev-cloud.acronis.com/" # Get Root tenant_id from Self info $my_info = Invoke-RestMethod -Method Get -Uri "${base_url}api/2/users/me" -Headers $headers $tenant_id = $my_info.tenant_id # Body JSON, to create a partner tenant $json = @" { "name": "MyFirstPartner", "parent_id": "${tenant_id}", "kind": "partner" } "@ # The response will be JSON $headers.Add("Content-Type","application/json") # Create a partner $partner = Invoke-RestMethod -Method Post -Uri "${base_url}api/2/tenants" -Headers $headers -Body $json $partner_id = $partner.id Enable-All-OfferingItems -ParentTenantID $tenant_id -TenantID $partner_id -AuthHeader $headers -Kind "partner" # Body JSON, to create a customer tenant $json = @" { "name": "MyCustomer", "parent_id": "${partner_id}", "kind": "customer" } "@ # Create a customer in a trial mode $customer = Invoke-RestMethod -Method Post -Uri "${base_url}api/2/tenants" -Headers $headers -Body $json $customer_id = $customer.id Enable-All-OfferingItems -ParentTenantID $partner_id -TenantID $customer_id -AuthHeader $headers # Switching customer tenant to porduction mode $customer_pricing = Invoke-RestMethod -Method Get -Uri "${base_url}api/2/tenants/${customer_id}/pricing" -Headers $headers $customer_pricing.mode = "production" $customer_pricing_json = $customer_pricing | ConvertTo-Json Invoke-RestMethod -Method Put -Uri "${base_url}api/2/tenants/${customer_id}/pricing" -Headers $headers -Body $customer_pricing_json -ContentType "application/json" $user_login = "MyFirstUser" $user_login_param = @{username=$user_login} $response = Invoke-WebRequest -Method Get -Uri "${base_url}api/2/users/check_login" -Headers $headers -Body $user_login_param # Check if login name is free if ($response.StatusCode -eq 204){ # Body JSON, to create a user $json = @" { "tenant_id": "${customer_id}", "login": "${user_login}", "contact": { "email": "${user_login}@example.com", "firstname": "Firstname", "lastname": "Lastname" } } "@ $user = Invoke-RestMethod -Method Post -Uri "${base_url}api/2/users" -Headers $headers -Body $json -ContentType "application/json" $user_id = $user.id # Body JSON, to assign a password and activate the user # NEVER STORE A PASSWEOR IN PLAIN TEXT FILE # THIS CODE IS FOR API DEMO PURPOSES ONLY # AS IT USES FAKE E-MAIL AND ACTIVATION E-MAIL CAN'T BE SENT $json = @" { "password": "MyStrongP@ssw0rd" } "@ $response = Invoke-WebRequest -Method Post -Uri "${base_url}api/2/users/${user_id}/password" -Headers $headers -Body $json -ContentType "application/json" }