Chilkat Online Tools

GetImageRecipe TCL Example

EC2 Image Builder

load ./chilkat.dll

# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set rest [new_CkRest]

set authAws [new_CkAuthAws]

CkAuthAws_put_AccessKey $authAws "AWS_ACCESS_KEY"
CkAuthAws_put_SecretKey $authAws "AWS_SECRET_KEY"

# Don't forget to change the region to your particular region. (Also make the same change in the call to Connect below.)
CkAuthAws_put_Region $authAws "us-west-2"
CkAuthAws_put_ServiceName $authAws "imagebuilder"
# SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
CkRest_SetAuthAws $rest $authAws

# URL: https://imagebuilder.us-west-2.amazonaws.com/
# Use the same region as specified above.
set success [CkRest_Connect $rest "imagebuilder.us-west-2.amazonaws.com" 443 1 1]
if {$success != 1} then {
    puts "ConnectFailReason: [CkRest_get_ConnectFailReason $rest]"
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    exit
}

CkRest_AddHeader $rest "Content-Type" "application/x-amz-json-1.1"
CkRest_AddHeader $rest "X-Amz-Target" "GetImageRecipe"

set sbResponseBody [new_CkStringBuilder]

set success [CkRest_FullRequestNoBodySb $rest "GET" "/GetImageRecipe" $sbResponseBody]
if {$success != 1} then {
    puts [CkRest_lastErrorText $rest]
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    delete_CkStringBuilder $sbResponseBody
    exit
}

set respStatusCode [CkRest_get_ResponseStatusCode $rest]
puts "response status code = $respStatusCode"
if {$respStatusCode != 200} then {
    puts "Response Header:"
    puts [CkRest_responseHeader $rest]
    puts "Response Body:"
    puts [CkStringBuilder_getAsString $sbResponseBody]
    delete_CkRest $rest
    delete_CkAuthAws $authAws
    delete_CkStringBuilder $sbResponseBody
    exit
}

set jResp [new_CkJsonObject]

CkJsonObject_LoadSb $jResp $sbResponseBody

# The following code parses the JSON response.
# A sample JSON response is shown below the sample code.

# Use this online tool to generate parsing code from sample JSON:
# Generate Parsing Code from JSON

set UninstallAfterBuild [CkJsonObject_IntOf $jResp "imageRecipe.additionalInstanceConfiguration.systemsManagerAgent.uninstallAfterBuild"]
set UserDataOverride [CkJsonObject_stringOf $jResp "imageRecipe.additionalInstanceConfiguration.userDataOverride"]
set Arn [CkJsonObject_stringOf $jResp "imageRecipe.arn"]
set DateCreated [CkJsonObject_stringOf $jResp "imageRecipe.dateCreated"]
set Description [CkJsonObject_stringOf $jResp "imageRecipe.description"]
set Name [CkJsonObject_stringOf $jResp "imageRecipe.name"]
set Owner [CkJsonObject_stringOf $jResp "imageRecipe.owner"]
set ParentImage [CkJsonObject_stringOf $jResp "imageRecipe.parentImage"]
set Platform [CkJsonObject_stringOf $jResp "imageRecipe.platform"]
set v_String [CkJsonObject_stringOf $jResp "imageRecipe.tags.string"]
set v_Type [CkJsonObject_stringOf $jResp "imageRecipe.type"]
set Version [CkJsonObject_stringOf $jResp "imageRecipe.version"]
set WorkingDirectory [CkJsonObject_stringOf $jResp "imageRecipe.workingDirectory"]
set requestId [CkJsonObject_stringOf $jResp "requestId"]
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "imageRecipe.blockDeviceMappings"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set deviceName [CkJsonObject_stringOf $jResp "imageRecipe.blockDeviceMappings[i].deviceName"]
    set DeleteOnTermination [CkJsonObject_IntOf $jResp "imageRecipe.blockDeviceMappings[i].ebs.deleteOnTermination"]
    set Encrypted [CkJsonObject_IntOf $jResp "imageRecipe.blockDeviceMappings[i].ebs.encrypted"]
    set Iops [CkJsonObject_IntOf $jResp "imageRecipe.blockDeviceMappings[i].ebs.iops"]
    set KmsKeyId [CkJsonObject_stringOf $jResp "imageRecipe.blockDeviceMappings[i].ebs.kmsKeyId"]
    set SnapshotId [CkJsonObject_stringOf $jResp "imageRecipe.blockDeviceMappings[i].ebs.snapshotId"]
    set Throughput [CkJsonObject_IntOf $jResp "imageRecipe.blockDeviceMappings[i].ebs.throughput"]
    set VolumeSize [CkJsonObject_IntOf $jResp "imageRecipe.blockDeviceMappings[i].ebs.volumeSize"]
    set VolumeType [CkJsonObject_stringOf $jResp "imageRecipe.blockDeviceMappings[i].ebs.volumeType"]
    set noDevice [CkJsonObject_stringOf $jResp "imageRecipe.blockDeviceMappings[i].noDevice"]
    set virtualName [CkJsonObject_stringOf $jResp "imageRecipe.blockDeviceMappings[i].virtualName"]
    set i [expr $i + 1]
}
set i 0
set count_i [CkJsonObject_SizeOfArray $jResp "imageRecipe.components"]
while {$i < $count_i} {
    CkJsonObject_put_I $jResp $i
    set componentArn [CkJsonObject_stringOf $jResp "imageRecipe.components[i].componentArn"]
    set j 0
    set count_j [CkJsonObject_SizeOfArray $jResp "imageRecipe.components[i].parameters"]
    while {$j < $count_j} {
        CkJsonObject_put_J $jResp $j
        set name [CkJsonObject_stringOf $jResp "imageRecipe.components[i].parameters[j].name"]
        set k 0
        set count_k [CkJsonObject_SizeOfArray $jResp "imageRecipe.components[i].parameters[j].value"]
        while {$k < $count_k} {
            CkJsonObject_put_K $jResp $k
            set strVal [CkJsonObject_stringOf $jResp "imageRecipe.components[i].parameters[j].value[k]"]
            set k [expr $k + 1]
        }
        set j [expr $j + 1]
    }
    set i [expr $i + 1]
}

# A sample JSON response body parsed by the above code:

# {
#   "imageRecipe": {
#     "additionalInstanceConfiguration": {
#       "systemsManagerAgent": {
#         "uninstallAfterBuild": boolean
#       },
#       "userDataOverride": "string"
#     },
#     "arn": "string",
#     "blockDeviceMappings": [
#       {
#         "deviceName": "string",
#         "ebs": {
#           "deleteOnTermination": boolean,
#           "encrypted": boolean,
#           "iops": number,
#           "kmsKeyId": "string",
#           "snapshotId": "string",
#           "throughput": number,
#           "volumeSize": number,
#           "volumeType": "string"
#         },
#         "noDevice": "string",
#         "virtualName": "string"
#       }
#     ],
#     "components": [
#       {
#         "componentArn": "string",
#         "parameters": [
#           {
#             "name": "string",
#             "value": [
#               "string"
#             ]
#           }
#         ]
#       }
#     ],
#     "dateCreated": "string",
#     "description": "string",
#     "name": "string",
#     "owner": "string",
#     "parentImage": "string",
#     "platform": "string",
#     "tags": {
#       "string": "string"
#     },
#     "type": "string",
#     "version": "string",
#     "workingDirectory": "string"
#   },
#   "requestId": "string"
# }

delete_CkRest $rest
delete_CkAuthAws $authAws
delete_CkStringBuilder $sbResponseBody
delete_CkJsonObject $jResp