Chilkat Online Tools

GetImageRecipe Swift3 Example

EC2 Image Builder

func chilkatTest() {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let rest = CkoRest()!
    var success: Bool

    let authAws = CkoAuthAws()!
    authAws.accessKey = "AWS_ACCESS_KEY"
    authAws.secretKey = "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.)
    authAws.region = "us-west-2"
    authAws.serviceName = "imagebuilder"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(authAws)

    // URL: https://imagebuilder.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.connect("imagebuilder.us-west-2.amazonaws.com", port: 443, tls: true, autoReconnect: true)
    if success != true {
        print("ConnectFailReason: \(rest.connectFailReason.intValue)")
        print("\(rest.lastErrorText!)")
        return
    }

    rest.addHeader("Content-Type", value: "application/x-amz-json-1.1")
    rest.addHeader("X-Amz-Target", value: "GetImageRecipe")

    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestNoBodySb("GET", uriPath: "/GetImageRecipe", sb: sbResponseBody)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    var respStatusCode: Int = rest.responseStatusCode.intValue
    print("response status code = \(respStatusCode)")
    if respStatusCode != 200 {
        print("Response Header:")
        print("\(rest.responseHeader!)")
        print("Response Body:")
        print("\(sbResponseBody.getAsString()!)")
        return
    }

    let jResp = CkoJsonObject()!
    jResp.loadSb(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

    var deviceName: String?
    var DeleteOnTermination: Int
    var Encrypted: Int
    var Iops: Int
    var KmsKeyId: String?
    var SnapshotId: String?
    var Throughput: Int
    var VolumeSize: Int
    var VolumeType: String?
    var noDevice: String?
    var virtualName: String?
    var componentArn: String?
    var j: Int
    var count_j: Int
    var name: String?
    var k: Int
    var count_k: Int
    var strVal: String?

    var UninstallAfterBuild: Int = jResp.int(of: "imageRecipe.additionalInstanceConfiguration.systemsManagerAgent.uninstallAfterBuild").intValue
    var UserDataOverride: String? = jResp.string(of: "imageRecipe.additionalInstanceConfiguration.userDataOverride")
    var Arn: String? = jResp.string(of: "imageRecipe.arn")
    var DateCreated: String? = jResp.string(of: "imageRecipe.dateCreated")
    var Description: String? = jResp.string(of: "imageRecipe.description")
    var Name: String? = jResp.string(of: "imageRecipe.name")
    var Owner: String? = jResp.string(of: "imageRecipe.owner")
    var ParentImage: String? = jResp.string(of: "imageRecipe.parentImage")
    var Platform: String? = jResp.string(of: "imageRecipe.platform")
    var v_String: String? = jResp.string(of: "imageRecipe.tags.string")
    var v_Type: String? = jResp.string(of: "imageRecipe.type")
    var Version: String? = jResp.string(of: "imageRecipe.version")
    var WorkingDirectory: String? = jResp.string(of: "imageRecipe.workingDirectory")
    var requestId: String? = jResp.string(of: "requestId")
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "imageRecipe.blockDeviceMappings").intValue
    while i < count_i {
        jResp.i = i
        deviceName = jResp.string(of: "imageRecipe.blockDeviceMappings[i].deviceName")
        DeleteOnTermination = jResp.int(of: "imageRecipe.blockDeviceMappings[i].ebs.deleteOnTermination").intValue
        Encrypted = jResp.int(of: "imageRecipe.blockDeviceMappings[i].ebs.encrypted").intValue
        Iops = jResp.int(of: "imageRecipe.blockDeviceMappings[i].ebs.iops").intValue
        KmsKeyId = jResp.string(of: "imageRecipe.blockDeviceMappings[i].ebs.kmsKeyId")
        SnapshotId = jResp.string(of: "imageRecipe.blockDeviceMappings[i].ebs.snapshotId")
        Throughput = jResp.int(of: "imageRecipe.blockDeviceMappings[i].ebs.throughput").intValue
        VolumeSize = jResp.int(of: "imageRecipe.blockDeviceMappings[i].ebs.volumeSize").intValue
        VolumeType = jResp.string(of: "imageRecipe.blockDeviceMappings[i].ebs.volumeType")
        noDevice = jResp.string(of: "imageRecipe.blockDeviceMappings[i].noDevice")
        virtualName = jResp.string(of: "imageRecipe.blockDeviceMappings[i].virtualName")
        i = i + 1
    }

    i = 0
    count_i = jResp.size(ofArray: "imageRecipe.components").intValue
    while i < count_i {
        jResp.i = i
        componentArn = jResp.string(of: "imageRecipe.components[i].componentArn")
        j = 0
        count_j = jResp.size(ofArray: "imageRecipe.components[i].parameters").intValue
        while j < count_j {
            jResp.j = j
            name = jResp.string(of: "imageRecipe.components[i].parameters[j].name")
            k = 0
            count_k = jResp.size(ofArray: "imageRecipe.components[i].parameters[j].value").intValue
            while k < count_k {
                jResp.k = k
                strVal = jResp.string(of: "imageRecipe.components[i].parameters[j].value[k]")
                k = k + 1
            }

            j = j + 1
        }

        i = 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"
    // }

}