This topic describes how to use the Operation Orchestration Service (OOS) SDK for C# to perform common operations, such as creating a template, executing a template, and checking the execution result.
Create a template
Run the following code to create a template:
using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.oos.Model.V20190601;
namespace oosDemo
{
class Program
{
static void Main(string[] args)
{
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
DefaultAcsClient client = new DefaultAcsClient(profile);
// Create a template.
var request = new CreateTemplateRequest();
request.TemplateName = "MyTemplate";
request.Content = "{\n"
+ " \"FormatVersion\": \"OOS-2019-06-01\",\n"
+ " \"Description\": \"Descirbe instances of given status\",\n"
+ " \"Parameters\": {\n"
+ " \"Status\": {\n"
+ " \"Type\": \"String\",\n"
+ " \"Description\": \"(Required) The status of the Ecs instance.\"\n"
+ " }\n"
+ " },\n"
+ " \"Tasks\": [\n"
+ " {\n"
+ " \"Properties\": {\n"
+ " \"Parameters\": { \"Status\": \"{{ Status }}\" },\n"
+ " \"API\": \"DescribeInstances\",\n"
+ " \"Service\": \"Ecs\"\n"
+ " },\n"
+ " \"Name\": \"foo\",\n"
+ " \"Action\": \"ACS::ExecuteApi\"\n"
+ " }\n"
+ " ]\n"
+ "}";
// Initiate a request and handle the response or exception.
try
{
var response = client.GetAcsResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}
Execute a template
Run the following code to execute a template:
using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.oos.Model.V20190601;
namespace oosDemo
{
class Program
{
static void Main(string[] args)
{
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
DefaultAcsClient client = new DefaultAcsClient(profile);
// Execute a template.
var request = new StartExecutionRequest();
request.Parameters = "{\"Status\": \"Running\"}";
request.TemplateName = "MyTemplate";
// Initiate a request and handle the response or exception.
try
{
var response = client.GetAcsResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}
Check the execution result
Run the following code to check the execution result:
using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.oos.Model.V20190601;
namespace oosDemo
{
class Program
{
static void Main(string[] args)
{
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
DefaultAcsClient client = new DefaultAcsClient(profile);
// Check the execution result.
var request = new ListExecutionsRequest();
request.ExecutionId = "<ExecutionId>";
// Initiate a request and handle the response or exception.
try
{
var response = client.GetAcsResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}