全部產品
Search
文件中心

CloudOps Orchestration Service:ACS::ExecuteHttpRequest

更新時間:Aug 02, 2024

用途

使用該動作可以在營運任務中調用外部HTTP/HTTPS請求,以實現雲資源營運任務與外部HTTP服務的互動。

以下是 ACS::ExecuteHttpRequest 動作的一些可能的情境:

  1. API 整合:您可以使用 ACS::ExecuteHttpRequest 動作調用第三方服務API,從而查詢資訊、執行操作或整合第三方服務。

  2. 自訂通知:如果您需要在指令碼執行的特定階段發送通知,可以通過 ACS::ExecuteHttpRequest 發送 HTTP 要求到自訂的Webhook或通知服務,實現諸如發送郵件、簡訊或推播通知等功能。

  3. 資料收集與報告:藉助 ACS::ExecuteHttpRequest 動作,您可以將營運任務執行資訊發送到遠程伺服器或資料平台,以用於審計、監控或報告。

  4. 自動化流程觸發:您可以利用 ACS::ExecuteHttpRequest 在 OOS 模板執行時觸發其他自動化工具或指令碼的執行,如調用 CI/CD 系統中的部署流程,或者啟動其他外部系統的營運任務。

  5. 外部驗證與授權:在營運過程中,您可能需要對操作進行驗證。您可以通過 ACS::ExecuteHttpRequest 向外部認證系統發送請求以驗證操作者的身份或校正操作許可權。

文法

Tasks:
  - Name: executeHttpRequestExample
    Action: ACS::ExecuteHttpRequest
    Properties:
      Method: POST # 可選,HTTP方法,支援POST和GET,預設為POST
      URL: 'https://example.com' # 必填,HTTP請求的統一資源定位器
      Headers: # 可選,HTTP要求標頭資訊
        Content-Type: 'application/json'
      Query: # 可選,HTTP請求參數資訊
        Parameter1: value1
        Parameter2: value2
      Body: # 可選,HTTP請求體,當Method為POST時有效
        Parameter3: value3
        Parameter4: value4
    Outputs:
      OutputParameter1:
        ValueSelector: 'jq selector' # jq的選取器文法,以OpenAPI的返回作為JSON輸入,jq的文法請參考 https://stedolan.github.io/jq/
        Type: String/Boolean/List/Number/Object
{
  "Tasks": [
    {
      "Name": "executeHttpRequestExample",
      "Action": "ACS::ExecuteHttpRequest",
      "Properties": {
        "Method": "POST",
        "URL": "https://example.com",
        "Headers": {
          "Content-Type": "application/json"
        },
        "Query": {
          "Parameter1": "Value1",
          "Parameter2": "Value2"
        },
        "Body": {
          "Parameter3": "Value3",
          "Parameter4": "Value4"
        }
      },
      "Outputs": {
        "OutputParameter1": {
          "ValueSelector": "jq selector",
          "Type": "String/Boolean/List(Array)/Number/Object"
        }
      }
    }
  ]
}

樣本

以下為通過調用HTTP請求觸發github action將分支代碼部署到指定執行個體的樣本模板。

FormatVersion: OOS-2019-06-01
Description: 
    en: OOS template for deploying ECS instances using GitHub Actions.
    zh-cn: OOS模板,用於使用GitHub Actions部署ECS執行個體
Parameters:
  regionId:
    Type: String
    Label:
      en: RegionId
      zh-cn: 地區ID
    AssociationProperty: RegionId
    Default: '{{ ACS::RegionId }}'
  targets:
    Type: Json
    Label:
      en: TargetInstance
      zh-cn: 目標執行個體
    AssociationProperty: Targets
    AssociationPropertyMetadata:
      ResourceType: ALIYUN::ECS::Instance
      RegionId: regionId
      Status: Running
  gitHubBranch:
    Type: String
    Description:
        en: Branch where the deployment will occur.
        zh-cn: 要部署的git倉庫分支
Tasks:
  - Name: getInstance
    Description:
      en: Views the ECS instances
      zh-cn: 擷取ECS執行個體
    Action: ACS::SelectTargets
    Properties:
      ResourceType: ALIYUN::ECS::Instance
      RegionId: '{{ regionId }}'
      Filters:
        - '{{ targets }}'
    Outputs:
      instanceIds:
        Type: List
        ValueSelector: Instances.Instance[].InstanceId
  - Name: DeployCodeByGitHubAction
    Action: ACS::ExecuteHttpRequest
    Properties:
      Method: POST
      URL: "https://api.github.com/repos/<YOUR-GITHUB-ACCOUNT>/<YOUR-GITHUB-REPO>/actions/workflows/<YOUR-WORKFLOW-ID>/dispatches"
      Headers:
        Accept: application/vnd.github+json
        Authorization: "Bearer <YOUR-TOKEN>"
        X-GitHub-Api-Version: 2022-11-28
      Body: 
        ref: "{{gitHubBranch}}"
        inputs:
          instance_ids: "{{getInstance.instanceIds}}"
{
  "FormatVersion": "OOS-2019-06-01",
  "Description": {
    "en": "OOS template for deploying ECS instances using GitHub Actions.",
    "zh-cn": "OOS模板,用於使用GitHub Actions部署ECS執行個體"
  },
  "Parameters": {
    "regionId": {
      "Type": "String",
      "Label": {
        "en": "RegionId",
        "zh-cn": "地區ID"
      },
      "AssociationProperty": "RegionId",
      "Default": "{{ ACS::RegionId }}"
    },
    "targets": {
      "Type": "Json",
      "Label": {
        "en": "TargetInstance",
        "zh-cn": "目標執行個體"
      },
      "AssociationProperty": "Targets",
      "AssociationPropertyMetadata": {
        "ResourceType": "ALIYUN::ECS::Instance",
        "RegionId": "regionId",
        "Status": "Running"
      }
    },
    "gitHubBranch": {
      "Type": "String",
      "Description": {
        "en": "Branch where the deployment will occur.",
        "zh-cn": "要部署的git倉庫分支"
      }
    }
  },
  "Tasks": [
    {
      "Name": "getInstance",
      "Description": {
        "en": "Views the ECS instances",
        "zh-cn": "擷取ECS執行個體"
      },
      "Action": "ACS::SelectTargets",
      "Properties": {
        "ResourceType": "ALIYUN::ECS::Instance",
        "RegionId": "{{ regionId }}",
        "Filters": [
          "{{ targets }}"
        ]
      },
      "Outputs": {
        "instanceIds": {
          "Type": "List",
          "ValueSelector": "Instances.Instance[].InstanceId"
        }
      }
    },
    {
      "Name": "DeployCodeByGitHubAction",
      "Action": "ACS::ExecuteHttpRequest",
      "Properties": {
        "Method": "POST",
        "URL": "https://api.github.com/repos/<YOUR-GITHUB-ACCOUNT>/<YOUR-GITHUB-REPO>/actions/workflows/<YOUR-WORKFLOW-ID>/dispatches",
        "Headers": {
          "Accept": "application/vnd.github+json",
          "Authorization": "Bearer <YOUR-TOKEN>",
          "X-GitHub-Api-Version": "2022-11-28T00:00:00.000Z"
        },
        "Body": {
          "ref": "{{gitHubBranch}}",
          "inputs": {
            "instance_ids": "{{getInstance.instanceIds}}"
          }
        },
        "Query": {}
      }
    }
  ]
}