Skip to content

Commit

Permalink
Azure Devops pipeline to migrate APIM dev portal from one instance to…
Browse files Browse the repository at this point in the history
… another instance of APIM (Azure#1362)
  • Loading branch information
SSanjeevi authored Jul 19, 2021
1 parent b128ad2 commit 0c618f7
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
28 changes: 28 additions & 0 deletions Pipelines/Pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: CD-ApiMDeveloperPortal-Build

trigger: none

variables:
- name: poolName
value: "agentpoolName"
- name: location
value: West US

jobs:

# All tasks on APIM Developer portal pipeline
- job: Deploy_APIM_Developer_Portal
displayName: Deploy APIM Developer portal from one APIM instance to another APIM instance Migration
pool:
name: $(poolName)
timeoutInMinutes: 90
steps:
- task: Npm@1
displayName: Npm Install command
inputs:
command: "install"

- pwsh: |
node ./migrate --sourceSubscriptionId "$(sourceSubscriptionId)" --sourceResourceGroupName "$(sourceResourceGroupName)" --sourceServiceName "$(sourceAPIMName)" --destServiceName "$(destinationAPIMName)" --destSubscriptionId "$(destSubscriptionId)" --destResourceGroupName "$(destResourceGroupName)" --sourceTenantid "$(sourceAzure_Tenant)" --sourceServiceprincipal "$(sourceServicePrincipal)" --sourceSecret "$(sourceAzureDevOps-ServicePrincipal-Secret)" --destTenantid "$(Azure_Tenant)" --destServiceprincipal "$(ServicePrincipal)" --destSecret "$(AzureDevOps-ServicePrincipal-Secret)" --existingEnvUrls "$(existingEnvUrls)" --destEnvUrls "$(destEnvUrls)"
workingDirectory: "$(System.DefaultWorkingDirectory)/scripts.v3"
displayName: Run Migrate cmd from $(sourceAPIMName) to $(destinationAPIMName)
18 changes: 17 additions & 1 deletion scripts.v3/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* --destTenantid "< optional (needed if source and destination is in different subscription) destination tenantid >"
* --destServiceprincipal "< optional (needed if source and destination is in different subscription)destination serviceprincipal or user name. >"
* --destSecret "< optional (needed if source and destination is in different subscription) secret or password for service principal or az login for the destination. >"
* --existingEnvUrls "< optional (urls used in the developer portal from source apim to replace - if we have multiple urls then comma separated values to be given.) >"
* --destEnvUrls "< optional (urls to be replaced in the developer portal in destination apim in same order - if we have multiple urls then comma separated values to be given.) >"
*
* Auto-publishing is not supported for self-hosted versions, so make sure you publish the portal (for example, locally)
* and upload the generated static files to your hosting after the migration is completed.
Expand All @@ -45,7 +47,9 @@ const yargs = require('yargs')
* --destServiceName "< your service name > \r
* --destTenantid "< optional (needed if source and destination is in different subscription) destination tenantid > \r
* --destServiceprincipal "< optional (needed if source and destination is in different subscription) destination serviceprincipal or user name. > \r
* --destSecret "< optional (needed if source and destination is in different subscription) secret or password for service principal or az login for the destination. >\n`)
* --destSecret "< optional (needed if source and destination is in different subscription) secret or password for service principal or az login for the destination. >\r
* --existingEnvUrls "< optional (urls used in the developer portal from source apim to replace - if we have multiple urls then comma separated values to be given.) > \r
* --destEnvUrls "< optional (urls to be replaced in the developer portal in destination apim in same order - if we have multiple urls then comma separated values to be given.) > \n`)
.option('sourceSubscriptionId', {
type: 'string',
description: 'Azure subscription ID.',
Expand Down Expand Up @@ -106,6 +110,16 @@ const yargs = require('yargs')
description: 'secret or password for service principal or az login for the destination.',
demandOption: false
})
.option('existingEnvUrls', {
type: 'string',
description: 'urls used in the developer portal from source apim to replace - if we have multiple urls then comma separated values to be given.',
demandOption: true
})
.option('destEnvUrls', {
type: 'string',
description: 'urls to be replaced in the developer portal in destination apim in same order - if we have multiple urls then comma separated values to be given.',
demandOption: true
})
.help()
.argv;

Expand All @@ -117,6 +131,8 @@ async function migrate() {
const destIimporterExporter = new ImporterExporter(yargs.destSubscriptionId, yargs.destResourceGroupName, yargs.destServiceName, yargs.destTenantid, yargs.destServiceprincipal, yargs.destSecret);
await destIimporterExporter.cleanup();
await destIimporterExporter.import();
await destIUpdateUrl.updateContentUrl(yargs.existingEnvUrls.split(','), yargs.destEnvUrls.split(','));

await destIimporterExporter.publish();
}
catch (error) {
Expand Down
33 changes: 33 additions & 0 deletions scripts.v3/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,39 @@ class ImporterExporter {
throw new Error(`Unable to schedule website publishing. ${error.message}`);
}
}

/**
* Updates the url links of specified API Management service into updated url in destination.
*/
async updateContentUrl(existingUrls, replaceableUrls) {
try {
const contentItems = await this.getContentItems("url");

console.log("urls found in portal: " + contentItems.length);

for (const contentItem of contentItems) {
var count = 0;
console.log("url found in portal: " + contentItem.properties.permalink);

for (const existingUrl of existingUrls) {
if (contentItem.properties.permalink == existingUrl) {
contentItem.properties.permalink = replaceableUrls[count];
contentItem.properties.title = replaceableUrls[count];
console.log("updating url content..." + contentItem.properties.permalink);
console.log(" updated url content : " + JSON.stringify(contentItem));
const response = await this.httpClient.sendRequest("PUT", contentItem.id + "?api-version=2019-12-01", contentItem);

console.log(" resp: " + JSON.stringify(response));
}
count++;
};
};

}
catch (error) {
throw new Error(`Unable to update url content. ${error.message}`);
}
}
}

module.exports = {
Expand Down

0 comments on commit 0c618f7

Please sign in to comment.