Googleアナリティクス

2018年11月23日金曜日

Check Flow Connector Error

Original post is here.

There is a thing that many people feel using Microsoft Flow, connection of the created connector becomes a connection error periodically. Because it is due to the expiration of the access token used at the time of connection, it may be an error, but checking from the portal every time was troublesome.

I tried to automate this check with Flow this time.

Since it is quite long as an entry, those who want to check in advance have prepared packages exported to github, so please use here as well.

Logic Apps is managed as an API connection, but in Flow you can check from the connection menu. If there is a connection error, it is a mechanism to notice that problems are starting to appear as errors on the list. Therefore, one day it suddenly falls into the situation that Flow has stopped moving.

image

When actually checking the connection status, it may be displayed like this. LogicFlow using this connection can not operate properly.

image

Connection information you are using can be obtained by using the Flow Management connector. List My Connections (ListConnections) action specifies the environment to be acquired in this way and uses it.

image

When you execute the action, you can obtain information in the following form.

{
   "name": "shared-approvals-zzzzzzz-xxxx-4883-bef8-1167d33409c9",
   "id": "/providers/Microsoft.PowerApps/apis/shared_approvals/connections/shared-approvals-zzzzzzz-xxxx-4883-bef8-1167d33409c9",
   "type": "Microsoft.PowerApps/apis/connections",
   "properties": {
     "apiId": "/providers/Microsoft.PowerApps/apis/shared_approvals",
     "displayName": "Approvals",
     "iconUri": "https://psux.azureedge.net/Content/Images/Connectors/Approvals3.svg",
     "statuses": [
       {
         "status": "Connected"
       }
     ],
     "connectionParameters": {
       "sku": "Enterprise"
     },
     "keywordsRemaining": 38,
     "createdBy": {
       "id": "zzzzzzz-xxxx-4447-b638-0b3aabfcae18",
       "displayName": "クマー",
       "email": "ahf@hogehoge.onmicrosoft.com",
       "type": "User",
       "tenantId": "zzzzzzz-6ace-4629-b910-6dc08bc4b453",
       "userPrincipalName": "ahf@hogehoge.onmicrosoft.com"
     },
     "createdTime": "2018-04-10T03:59:34.5393925Z",
     "lastModifiedTime": "2018-04-10T03:59:34.5393925Z",
     "environment": {
       "id": "/providers/Microsoft.PowerApps/environments/zzzzzzz-xxxx-4ef6-a237-0a357b9910de",
       "name": "zzzzzzz-xxxx-4ef6-a237-0a357b9910de"
     }
   }
}

The important thing is that statuses is an array, and the connection state in it has the name is status. It is normal if status is Connected, otherwise a connection error occurs.

Whole diagram of LogicFlow created looks something like this.

image

Actually, if you only check it, it will not be a big LogicFlow so far. Since we are about to send e-mails about connections that are in error this time, we are doing various processing in combination.

  1. Acquire connection information in the environment
  2. Obtain a list of Flows in the environment
  3. Filtering from connection information, to Connect Error
  4. Confirm from Flow list whether non-Connected connection is still being used
  5. Record the connector name if it is still being used
  6. Remove duplicates from recorded results and notify by email

First of all, since the connection information acquired by the Flow Management connector has a large and complicated structure as a value, it is adjusted to a form that is easy to use at a later time. Using selected actions, we rebuild the array to the required items.

image

Necessary in this process is "name" and "connection status". Regarding the name, we use the acquired value as it is, but this value is the name named inside Flow, which is not open to the user side.

The other connection state is made easy to use by specifying the following function.

@first(item()['properties']['statuses'])['status']

The connection information of the sample is described at the very beginning, so please compare it with that.

The point is that the connection status is recorded Status is set in properties.statuses by [array]. Therefore, using the first function, we use the first value of the statuses array. Since it was an array as a definition, I was wondering if other values will come, but at this time it is not possible to confirm the case where multiple connection states are set.

image

Look at the whole LogicFlow, you will notice that you are acquiring the list of created Flows at the same time you acquire the connection information. There are reasons for this, as Flow's connection information contains that are not currently in use, and those that do not require checking are also included. In addition, dust information not displayed on Flow 's portal may remain, so it is necessary to omit extra processing.

Therefore, we obtain a list of currently existing Flows and make a judgment as to whether it is being used by one of them.

"value": [
   {
     "name": "zzzzzzzz-xxxx-478b-b279-f16ef9ab1e80",
     "id": "/providers/Microsoft.Flow/environments/zzzzzzzz-df04-4ef6-a237-0a357b9910de/flows/zzzzzzzz-xxxx-478b-b279-f16ef9ab1e80",
     "type": "Microsoft.Flow/environments/flows",
     "properties": {
       "apiId": "/providers/Microsoft.PowerApps/apis/shared_logicflows",
       "displayName": "Get an email report of approvals waiting for my response",
       "userType": "Owner",
       "state": "Stopped",
       "connectionReferences": {
         "shared_commondataservice": {
           "connectionName": "shared-commondataser-zzzzzzzz-d7b6-4703-ae28-91074b9e1f96",
           "source": "Embedded",
           "id": "/providers/Microsoft.PowerApps/apis/shared_commondataservice",
           "displayName": "Common Data Service",
           "iconUri": "https://connectoricons-prod.azureedge.net/commondataservice/icon_1.0.1002.1175.png",
           "brandColor": "#742775",
           "tier": "NotSpecified"
         },
         "shared_flowpush": {
           "connectionName": "shared-flowpush-zzzzzzzz-cacb-432b-b285-6157e8d10791",
           "source": "Embedded",
           "id": "/providers/Microsoft.PowerApps/apis/shared_flowpush",
           "displayName": "Notifications",
           "iconUri": "https://psux.azureedge.net/Content/Images/Connectors/FlowNotification.svg",
           "brandColor": "#FF3B30",
           "tier": "NotSpecified"
         }
       },
       "createdTime": "2018-04-10T04:25:58.2803477Z",
       "lastModifiedTime": "2018-04-11T02:05:13.2155405Z",
       "templateName": "33d7ad77f610418d8cf3d61fe39fd507",
       "environment": {
         "name": "zzzzzzzz-df04-4ef6-a237-0a357b9910de",
         "type": "Microsoft.Flow/environments",
         "id": "/providers/Microsoft.Flow/environments/zzzzzzzz-df04-4ef6-a237-0a357b9910de"
       },
       "definitionSummary": {
         "triggers": [   ...以下省略

Flow information can be obtained in this way. This is the definition of LogicFlow itself. This includes information on the connection you are using.

That connection information is described in properties.connectionReferences, but this was the most difficult point in this work. Within connectionReference, detailed information is recorded with the connection name as a key. In other words, what kind of name (shared_commondataservice or shared_flowpush) the value is recorded in this is indeterminate. With the current function of LogicFlow, it is not possible to get the key name, so what to do with this part was the biggest problem.

This time we are dealing with the following way.

image

Connection information recorded in Flow is recorded in the specified format. If it can be limited only to connectionReferences, it will be easier to handle subsequent processing.

@split(replace(string(items('Check_Flow_Connection')['properties']['connectionReferences']),'},','}},'),'},')

As you can see, this is a very powerful technique. Since the value of connectReference is held in the form of "connectReference": {~}, "connectReference" deletes when converting it to a character string. Convert it so that it is only {~} and value. For example, if multiple values are set, adjust to the form {~}, {~} ... and create only an array of values (as in the function above, delete here It will not be done but in subsequent processing)

By performing the conversion like this, it is possible to make a decision based on this created array using the ForEach loop in subsequent processing.

image

In the loop, to make it easy to use, the value converted to {~} earlier is set to the JSON value by the json function. At that time, I deleted the part of connectReference which I had before.

@json(substring(item(), add(indexOf(item(), ':'), 1), sub(length(item()), add(indexOf(item(), ':'), 1))))

The position of the key name and value is separated by indexOf function, it extracts from the next character to the end, and the result is converted to JSON value by JSON function.

There is no need to do "Set Condition" especially. It is intended to make confirmation easier when testing.

image

Connection information of flow information. We filter the value to see if the same name as the connection that caused the error exists.

image

Since it is often that the value you want to use on the dialog side does not come out, direct description using the detailed setting mode becomes convenient.

image

Judgmentfrom the filtered result is described like this.

@greater(length(body('Filter_Using_Connection’)), 0)

If there are one or more filtered results, you are using the connection that is in error with this Flow. If it is 0 here, it will not be used in this flow.

image

If there was more than one case, this time we will determine if connection is an error at this timing. This area is a way of assembling the processing, if you do processing before narrowing down to errors only beforehand, judgment here is unnecessary.

@not(equals(first(first(actionBody('Filter_Using_Connection'))['properties']?['statuses'])?['status'], 'Connected'))

image

If the judgment result is wrong, it is added to the variable created by the array to send the result later. Arrays are used because more than one connection can occur because multiple connections can be created with the same connector. It will be added to the array once and duplicates will be deleted later.

image

To remove duplicates, we use the union function. Union function needs to specify two arrays, but here we specify the same variable. By doing so you can eliminate duplicates.

image

As a result of eliminating duplicates, if the number of cases is not 0, you can see that a connection error has occurred.

image

Finally, I will create a notification mail. Convert array in which error connection is recorded into one character string by join function. In that case, <br> and make it a line break on the text of the mail.

image

Sent result is like this. By doing this, you can automate the confirmation itself by going to the Flow portal only when this mail arrives and correcting the connection information.

0 件のコメント:

コメントを投稿