2
votes

I need to add existing (deployed) API APP to API management service through Powershell command. When I am trying it using 'New-AzureRmApiManagementApi' command it will create new API but it did not import the operations. There are many ways to import API from file or URL but I need to add existing API I followed https://docs.microsoft.com/en-us/powershell/resourcemanager/apimanagement.servicemanagement/v1.1.4/new-azurermapimanagementapi link Is there any way to add existing API App to API management service?

The request is -

https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxxxxxx/providers/Microsoft.ApiManag
ement/service/xxxxxxxxxx/apis/xxxxxxxxxxxxxxxxxxxxx?api-version=2016-10-10

Headers:

Body:
{
  "name": "test",
  "serviceUrl": "https://test.azure-api.net/",
  "path": "test",
  "protocols": [
    "Http",
    "Https"
  ],
  "type": "Http"
}

I am looking for command which takes API URL suffix,API APP,Name as parameters

1
well, this is definitely possible using powershell, can you post the http request that the powershell is performing? you can get it using your powershell command with a -debug switch (it will be somewhere in the middle of all the output) - 4c74356b41
@4c74356b41 mentioned in question - Rohi_Dev_1.0
well, unless there is some other powershell command you are out of luck, you can use rest to do that, if you cant figure this, I can create an answer in an hour (gotta rush somewhere now, sorry) - 4c74356b41

1 Answers

1
votes

So you can do that with a REST call described here.

To get the bearer token use the following powershell code:

$body = @{
    client_id= '{Azure AD Application GUID}'
    client_secret = '{Azure AD Application secret}'
    grant_Type = 'clientcredentials'
    resource = 'https://graph.windows.net/'
}
$result = iwr https://login.microsoftonline.com/{Azure_AD_Tenant_GUID}/oauth2/token -Method Post -Body $body
$BearerToken = ($result.Content | ConvertFrom-Json).access_token

Azure AD Application Service Principal should have the rights to API Management And issue the following REST call:

$url = 'https://management.azure.com/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/xxxxxxxxx/providers/Microsoft.ApiManagement/service/xxxxxxxxxx/apis/xxxxxxxxxxxxxxxxxxxxx?api-version=2016-10-10&import=true&path=xxx'
$headers = @{'Content-Type' = 'application/vnd.swagger.link+json'; 'Authorization' = $BearerToken}
$body = {
  "name": "test",
##### serviceUrl needs a link to SWAGGER.JSON, not to the root of the api #####
  "serviceUrl": "https://test.azure-api.net/swagger.json", 
  "path": "test",
  "protocols": [
    "Http",
    "Https"
  ],
  "type": "Http"
}

The path query parameter in the URL is the name of the API Management backend service associated with this API.

If you want to add API schema in the request you can do that but substituting the body of the request with your swagger schema and substituting 'Content-Type' header with vnd.swagger.doc+json

ps. maybe for the Authorization header you need this: "Bearer " + $BearerToken