Chilkat Online Tools

DescribeInstances Swift3 Example

AWS OpsWorks

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 = "opsworks"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(authAws)

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

    // The following code creates the JSON request body.
    // The JSON created by this code is shown below.

    // Use this online tool to generate code from sample JSON:
    // Generate Code to Create JSON

    let json = CkoJsonObject()!
    json.update("InstanceIds[0]", value: "string")
    json.update("LayerId", value: "string")
    json.update("StackId", value: "string")

    // The JSON request body created by the above code:

    // {
    //   "InstanceIds": [
    //     "string"
    //   ],
    //   "LayerId": "string",
    //   "StackId": "string"
    // }

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

    let sbRequestBody = CkoStringBuilder()!
    json.emitSb(sbRequestBody)
    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestSb("POST", uriPath: "/", requestBody: sbRequestBody, responseBody: 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 AgentVersion: String?
    var AmiId: String?
    var Architecture: String?
    var Arn: String?
    var AutoScalingType: String?
    var AvailabilityZone: String?
    var CreatedAt: String?
    var EbsOptimized: Int
    var Ec2InstanceId: String?
    var EcsClusterArn: String?
    var EcsContainerInstanceArn: String?
    var ElasticIp: String?
    var Hostname: String?
    var InfrastructureClass: String?
    var InstallUpdatesOnBoot: Int
    var InstanceId: String?
    var InstanceProfileArn: String?
    var InstanceType: String?
    var LastServiceErrorId: String?
    var Os: String?
    var Platform: String?
    var PrivateDns: String?
    var PrivateIp: String?
    var PublicDns: String?
    var PublicIp: String?
    var RegisteredBy: String?
    var ReportedAgentVersion: String?
    var Family: String?
    var Name: String?
    var Version: String?
    var RootDeviceType: String?
    var RootDeviceVolumeId: String?
    var SshHostDsaKeyFingerprint: String?
    var SshHostRsaKeyFingerprint: String?
    var SshKeyName: String?
    var StackId: String?
    var Status: String?
    var SubnetId: String?
    var Tenancy: String?
    var VirtualizationType: String?
    var j: Int
    var count_j: Int
    var DeviceName: String?
    var DeleteOnTermination: Int
    var Iops: Int
    var SnapshotId: String?
    var VolumeSize: Int
    var VolumeType: String?
    var NoDevice: String?
    var VirtualName: String?
    var strVal: String?

    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "Instances").intValue
    while i < count_i {
        jResp.i = i
        AgentVersion = jResp.string(of: "Instances[i].AgentVersion")
        AmiId = jResp.string(of: "Instances[i].AmiId")
        Architecture = jResp.string(of: "Instances[i].Architecture")
        Arn = jResp.string(of: "Instances[i].Arn")
        AutoScalingType = jResp.string(of: "Instances[i].AutoScalingType")
        AvailabilityZone = jResp.string(of: "Instances[i].AvailabilityZone")
        CreatedAt = jResp.string(of: "Instances[i].CreatedAt")
        EbsOptimized = jResp.int(of: "Instances[i].EbsOptimized").intValue
        Ec2InstanceId = jResp.string(of: "Instances[i].Ec2InstanceId")
        EcsClusterArn = jResp.string(of: "Instances[i].EcsClusterArn")
        EcsContainerInstanceArn = jResp.string(of: "Instances[i].EcsContainerInstanceArn")
        ElasticIp = jResp.string(of: "Instances[i].ElasticIp")
        Hostname = jResp.string(of: "Instances[i].Hostname")
        InfrastructureClass = jResp.string(of: "Instances[i].InfrastructureClass")
        InstallUpdatesOnBoot = jResp.int(of: "Instances[i].InstallUpdatesOnBoot").intValue
        InstanceId = jResp.string(of: "Instances[i].InstanceId")
        InstanceProfileArn = jResp.string(of: "Instances[i].InstanceProfileArn")
        InstanceType = jResp.string(of: "Instances[i].InstanceType")
        LastServiceErrorId = jResp.string(of: "Instances[i].LastServiceErrorId")
        Os = jResp.string(of: "Instances[i].Os")
        Platform = jResp.string(of: "Instances[i].Platform")
        PrivateDns = jResp.string(of: "Instances[i].PrivateDns")
        PrivateIp = jResp.string(of: "Instances[i].PrivateIp")
        PublicDns = jResp.string(of: "Instances[i].PublicDns")
        PublicIp = jResp.string(of: "Instances[i].PublicIp")
        RegisteredBy = jResp.string(of: "Instances[i].RegisteredBy")
        ReportedAgentVersion = jResp.string(of: "Instances[i].ReportedAgentVersion")
        Family = jResp.string(of: "Instances[i].ReportedOs.Family")
        Name = jResp.string(of: "Instances[i].ReportedOs.Name")
        Version = jResp.string(of: "Instances[i].ReportedOs.Version")
        RootDeviceType = jResp.string(of: "Instances[i].RootDeviceType")
        RootDeviceVolumeId = jResp.string(of: "Instances[i].RootDeviceVolumeId")
        SshHostDsaKeyFingerprint = jResp.string(of: "Instances[i].SshHostDsaKeyFingerprint")
        SshHostRsaKeyFingerprint = jResp.string(of: "Instances[i].SshHostRsaKeyFingerprint")
        SshKeyName = jResp.string(of: "Instances[i].SshKeyName")
        StackId = jResp.string(of: "Instances[i].StackId")
        Status = jResp.string(of: "Instances[i].Status")
        SubnetId = jResp.string(of: "Instances[i].SubnetId")
        Tenancy = jResp.string(of: "Instances[i].Tenancy")
        VirtualizationType = jResp.string(of: "Instances[i].VirtualizationType")
        j = 0
        count_j = jResp.size(ofArray: "Instances[i].BlockDeviceMappings").intValue
        while j < count_j {
            jResp.j = j
            DeviceName = jResp.string(of: "Instances[i].BlockDeviceMappings[j].DeviceName")
            DeleteOnTermination = jResp.int(of: "Instances[i].BlockDeviceMappings[j].Ebs.DeleteOnTermination").intValue
            Iops = jResp.int(of: "Instances[i].BlockDeviceMappings[j].Ebs.Iops").intValue
            SnapshotId = jResp.string(of: "Instances[i].BlockDeviceMappings[j].Ebs.SnapshotId")
            VolumeSize = jResp.int(of: "Instances[i].BlockDeviceMappings[j].Ebs.VolumeSize").intValue
            VolumeType = jResp.string(of: "Instances[i].BlockDeviceMappings[j].Ebs.VolumeType")
            NoDevice = jResp.string(of: "Instances[i].BlockDeviceMappings[j].NoDevice")
            VirtualName = jResp.string(of: "Instances[i].BlockDeviceMappings[j].VirtualName")
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "Instances[i].LayerIds").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "Instances[i].LayerIds[j]")
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "Instances[i].SecurityGroupIds").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "Instances[i].SecurityGroupIds[j]")
            j = j + 1
        }

        i = i + 1
    }

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

    // {
    //   "Instances": [
    //     {
    //       "AgentVersion": "string",
    //       "AmiId": "string",
    //       "Architecture": "string",
    //       "Arn": "string",
    //       "AutoScalingType": "string",
    //       "AvailabilityZone": "string",
    //       "BlockDeviceMappings": [
    //         {
    //           "DeviceName": "string",
    //           "Ebs": {
    //             "DeleteOnTermination": boolean,
    //             "Iops": number,
    //             "SnapshotId": "string",
    //             "VolumeSize": number,
    //             "VolumeType": "string"
    //           },
    //           "NoDevice": "string",
    //           "VirtualName": "string"
    //         }
    //       ],
    //       "CreatedAt": "string",
    //       "EbsOptimized": boolean,
    //       "Ec2InstanceId": "string",
    //       "EcsClusterArn": "string",
    //       "EcsContainerInstanceArn": "string",
    //       "ElasticIp": "string",
    //       "Hostname": "string",
    //       "InfrastructureClass": "string",
    //       "InstallUpdatesOnBoot": boolean,
    //       "InstanceId": "string",
    //       "InstanceProfileArn": "string",
    //       "InstanceType": "string",
    //       "LastServiceErrorId": "string",
    //       "LayerIds": [
    //         "string"
    //       ],
    //       "Os": "string",
    //       "Platform": "string",
    //       "PrivateDns": "string",
    //       "PrivateIp": "string",
    //       "PublicDns": "string",
    //       "PublicIp": "string",
    //       "RegisteredBy": "string",
    //       "ReportedAgentVersion": "string",
    //       "ReportedOs": {
    //         "Family": "string",
    //         "Name": "string",
    //         "Version": "string"
    //       },
    //       "RootDeviceType": "string",
    //       "RootDeviceVolumeId": "string",
    //       "SecurityGroupIds": [
    //         "string"
    //       ],
    //       "SshHostDsaKeyFingerprint": "string",
    //       "SshHostRsaKeyFingerprint": "string",
    //       "SshKeyName": "string",
    //       "StackId": "string",
    //       "Status": "string",
    //       "SubnetId": "string",
    //       "Tenancy": "string",
    //       "VirtualizationType": "string"
    //     }
    //   ]
    // }

}