Menerapkan CI/CD untuk Azure Stream Analytics pada IoT Edge menggunakan API

Anda dapat mengaktifkan integrasi dan penyebaran berkelanjutan untuk pekerjaan Azure Stream Analytics menggunakan API REST. Artikel ini memberikan contoh API mana yang akan digunakan dan cara menggunakannya. REST API tidak didukung di Azure Cloud Shell.

API Panggilan dari lingkungan yang berbeda

REST API dapat dipanggil dari Linux dan Windows. Perintah berikut menunjukkan sintaks yang tepat untuk panggilan API. Penggunaan API tertentu akan diuraikan di bagian selanjutnya dari artikel ini.

Linux

Untuk Linux, Anda dapat menggunakan perintah Curl atau Wget:

curl -u { <username:password> }  -H "Content-Type: application/json" -X { <method> } -d "{ <request body> }" { <url> }   
wget -q -O- --{ <method> } -data="<request body>" --header=Content-Type:application/json --auth-no-challenge --http-user="<Admin>" --http-password="<password>" <url>

Windows

Untuk Windows, gunakan Powershell:

$user = "<username>" 
$pass = "<password>" 
$encodedCreds = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$pass))) 
$basicAuthValue = "Basic $encodedCreds" 
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" 
$headers.Add("Content-Type", 'application/json') 
$headers.Add("Authorization", $basicAuthValue) 
$content = "<request body>" 
$response = Invoke-RestMethod <url> -Method <method> -Body $content -Headers $Headers 
echo $response 

Membuat pekerjaan ASA di IoT Edge

Untuk membuat pekerjaan Azure Stream Analytics, panggil metode PUT menggunakan API Azure Stream Analytics.

Metode URL Permintaan
PUT https://management.azure.com/subscriptions/{subscription-id}/resourcegroups/{resource-group-name}/providers/Microsoft.StreamAnalytics/streamingjobs/{job-name}?api-version=2017-04-01-preview

Contoh perintah menggunakan curl:

curl -u { <username:password> } -H "Content-Type: application/json" -X { <method> } -d "{ <request body> }" https://management.azure.com/subscriptions/{subscription-id}/resourcegroups/{resource-group-name}/providers/Microsoft.StreamAnalytics/streamingjobs/{jobname}?api-version=2017-04-01-preview  

Contoh isi permintaan di JSON:

{ 
  "location": "West US", 
  "tags": { "key": "value", "ms-suppressjobstatusmetrics": "true" }, 
  "sku": {  
      "name": "Standard" 
    }, 
  "properties": { 
    "sku": {  
      "name": "standard" 
    }, 
       "eventsLateArrivalMaxDelayInSeconds": 1, 
       "jobType": "edge", 
    "inputs": [ 
      { 
        "name": "{inputname}", 
        "properties": { 
         "type": "stream", 
          "serialization": { 
            "type": "JSON", 
            "properties": { 
              "fieldDelimiter": ",", 
              "encoding": "UTF8" 
            } 
          }, 
          "datasource": { 
            "type": "GatewayMessageBus", 
            "properties": { 
            } 
          } 
        } 
      } 
    ], 
    "transformation": { 
      "name": "{queryName}", 
      "properties": { 
        "query": "{query}" 
      } 
    }, 
    "package": { 
      "storageAccount" : { 
        "accountName": "{blobstorageaccountname}", 
        "accountKey": "{blobstorageaccountkey}" 
      }, 
      "container": "{blobcontaine}]" 
    }, 
    "outputs": [ 
      { 
        "name": "{outputname}", 
        "properties": { 
          "serialization": { 
            "type": "JSON", 
            "properties": { 
              "fieldDelimiter": ",", 
              "encoding": "UTF8" 
            } 
          }, 
          "datasource": { 
            "type": "GatewayMessageBus", 
            "properties": { 
            } 
          } 
        } 
      } 
    ] 
  } 
} 

Untuk informasi selengkapnya, lihat dokumentasi API.

Memublikasikan paket IoT Edge

Untuk memublikasikan pekerjaan Azure Stream Analytics di IoT Edge, panggil metode POST menggunakan API Penerbitan Paket IoT Edge.

Metode URL Permintaan
POST https://management.azure.com/subscriptions/{subscriptionid}/resourceGroups/{resourcegroupname}/providers/Microsoft.StreamAnalytics/streamingjobs/{jobname}/publishedgepackage?api-version=2017-04-01-preview

Panggilan sebelumnya ke API Penerbitan Paket IoT Edge memicu operasi asinkron dan menampilkan status 202. Header respons lokasi berisi URI yang digunakan untuk mendapatkan status operasi asinkron tersebut. Panggilan ke URI di header lokasi menampilkan status 202 untuk menunjukkan operasi asinkron masih berlangsung. Ketika operasi selesai, panggilan ke URI di header lokasi menampilkan status 200.

Contoh panggilan penerbitan paket IoT Edge menggunakan curl:

curl -d -X POST https://management.azure.com/subscriptions/{subscriptionid}/resourceGroups/{resourcegroupname}/providers/Microsoft.StreamAnalytics/streamingjobs/{jobname}/publishedgepackage?api-version=2017-04-01-preview

Setelah melakukan panggilan POST, Anda harus mengharapkan respons dengan isi kosong. Cari URI yang terletak di header lokasi respons dan catat untuk digunakan nanti.

Contoh URI dari header lokasi respons:

https://management.azure.com/subscriptions/{subscriptionid}/resourcegroups/{resourcegroupname}/providers/Microsoft.StreamAnalytics/StreamingJobs/{resourcename}/OperationResults/{guidAssignedToTheAsynchronousOperation}?api-version=2017-04-01-preview

Tunggu dari beberapa detik hingga beberapa menit sebelum melakukan panggilan ke API yang URI-nya Anda temukan di header lokasi respons terhadap API Penerbitan Paket IoT Edge, dan ulangi siklus tunggu dan coba kembali hingga Anda mendapatkan respons 200.

Contoh melakukan panggilan API dengan URL yang dihasilkan dengan curl:

curl -d –X GET https://management.azure.com/subscriptions/{subscriptionid}/resourcegroups/{resourcegroupname}/providers/Microsoft.StreamAnalytics/StreamingJobs/{resourcename}/OperationResults/{guidAssignedToTheAsynchronousOperation}?api-version=2017-04-01-preview

Respons menyertakan informasi yang perlu Anda tambahkan ke skrip penyebaran IoT Edge. Contoh di bawah ini menunjukkan informasi apa yang perlu Anda kumpulkan dan di mana menambahkannya dalam manifes penyebaran.

Contoh isi respons setelah penerbitan berhasil:

{ 
  edgePackageUrl : null 
  error : null 
  manifest : "{"supportedPlatforms":[{"os":"linux","arch":"amd64","features":[]},{"os":"linux","arch":"arm","features":[]},{"os":"windows","arch":"amd64","features":[]}],"schemaVersion":"2","name":"{jobname}","version":"1.0.0.0","type":"docker","settings":{"image":"{imageurl}","createOptions":null},"endpoints":{"inputs":["\],"outputs":["{outputnames}"]},"twin":{"contentType":"assignments","content":{"properties.desired":{"ASAJobInfo":"{asajobsasurl}","ASAJobResourceId":"{asajobresourceid}","ASAJobEtag":"{etag}","PublishTimeStamp":"{publishtimestamp}"}}}}" 
  status : "Succeeded" 
} 

Sampel Manifes Penyebaran:

{ 
  "modulesContent": { 
    "$edgeAgent": { 
      "properties.desired": { 
        "schemaVersion": "1.0", 
        "runtime": { 
          "type": "docker", 
          "settings": { 
            "minDockerVersion": "v1.25", 
            "loggingOptions": "", 
            "registryCredentials": {} 
          } 
        }, 
        "systemModules": { 
          "edgeAgent": { 
            "type": "docker", 
            "settings": { 
              "image": "mcr.microsoft.com/azureiotedge-agent:1.0", 
              "createOptions": "{}" 
            } 
          }, 
          "edgeHub": { 
            "type": "docker", 
            "status": "running", 
            "restartPolicy": "always", 
            "settings": { 
              "image": "mcr.microsoft.com/azureiotedge-hub:1.0", 
              "createOptions": "{}" 
            } 
          } 
        }, 
        "modules": { 
          "<asajobname>": { 
            "version": "1.0", 
            "type": "docker", 
            "status": "running", 
            "restartPolicy": "always", 
            "settings": { 
              "image": "<settings.image>", 
              "createOptions": "<settings.createOptions>" 
            } 
            "version": "<version>", 
             "env": { 
              "PlanId": { 
               "value": "stream-analytics-on-iot-edge" 
          } 
        } 
      } 
    }, 
    "$edgeHub": { 
      "properties.desired": { 
        "schemaVersion": "1.0", 
        "routes": { 
            "route": "FROM /* INTO $upstream" 
        }, 
        "storeAndForwardConfiguration": { 
          "timeToLiveSecs": 7200 
        } 
      } 
    }, 
    "<asajobname>": { 
      "properties.desired": {<twin.content.properties.desired>} 
    } 
  } 
} 

Setelah mengonfigurasi manifes penyebaran, lihat Menyebarkan modul Azure IoT Edge dengan Azure CLI untuk penyebaran.

Langkah berikutnya