Chilkat Online Tools

DescribeInstances C++ Example

AWS OpsWorks

#include <CkRest.h>
#include <CkAuthAws.h>
#include <CkJsonObject.h>
#include <CkStringBuilder.h>

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

    CkRest rest;
    bool success;

    CkAuthAws authAws;
    authAws.put_AccessKey("AWS_ACCESS_KEY");
    authAws.put_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.put_Region("us-west-2");
    authAws.put_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",443,true,true);
    if (success != true) {
        std::cout << "ConnectFailReason: " << rest.get_ConnectFailReason() << "\r\n";
        std::cout << rest.lastErrorText() << "\r\n";
        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

    CkJsonObject json;
    json.UpdateString("InstanceIds[0]","string");
    json.UpdateString("LayerId","string");
    json.UpdateString("StackId","string");

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

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

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

    CkStringBuilder sbRequestBody;
    json.EmitSb(sbRequestBody);
    CkStringBuilder sbResponseBody;
    success = rest.FullRequestSb("POST","/",sbRequestBody,sbResponseBody);
    if (success != true) {
        std::cout << rest.lastErrorText() << "\r\n";
        return;
    }

    int respStatusCode = rest.get_ResponseStatusCode();
    std::cout << "response status code = " << respStatusCode << "\r\n";
    if (respStatusCode != 200) {
        std::cout << "Response Header:" << "\r\n";
        std::cout << rest.responseHeader() << "\r\n";
        std::cout << "Response Body:" << "\r\n";
        std::cout << sbResponseBody.getAsString() << "\r\n";
        return;
    }

    CkJsonObject jResp;
    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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
    // See this example explaining how this memory should be used: const char * functions.

    const char *AgentVersion = 0;
    const char *AmiId = 0;
    const char *Architecture = 0;
    const char *Arn = 0;
    const char *AutoScalingType = 0;
    const char *AvailabilityZone = 0;
    const char *CreatedAt = 0;
    int EbsOptimized;
    const char *Ec2InstanceId = 0;
    const char *EcsClusterArn = 0;
    const char *EcsContainerInstanceArn = 0;
    const char *ElasticIp = 0;
    const char *Hostname = 0;
    const char *InfrastructureClass = 0;
    int InstallUpdatesOnBoot;
    const char *InstanceId = 0;
    const char *InstanceProfileArn = 0;
    const char *InstanceType = 0;
    const char *LastServiceErrorId = 0;
    const char *Os = 0;
    const char *Platform = 0;
    const char *PrivateDns = 0;
    const char *PrivateIp = 0;
    const char *PublicDns = 0;
    const char *PublicIp = 0;
    const char *RegisteredBy = 0;
    const char *ReportedAgentVersion = 0;
    const char *Family = 0;
    const char *Name = 0;
    const char *Version = 0;
    const char *RootDeviceType = 0;
    const char *RootDeviceVolumeId = 0;
    const char *SshHostDsaKeyFingerprint = 0;
    const char *SshHostRsaKeyFingerprint = 0;
    const char *SshKeyName = 0;
    const char *StackId = 0;
    const char *Status = 0;
    const char *SubnetId = 0;
    const char *Tenancy = 0;
    const char *VirtualizationType = 0;
    int j;
    int count_j;
    const char *DeviceName = 0;
    int DeleteOnTermination;
    int Iops;
    const char *SnapshotId = 0;
    int VolumeSize;
    const char *VolumeType = 0;
    const char *NoDevice = 0;
    const char *VirtualName = 0;
    const char *strVal = 0;

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

        j = 0;
        count_j = jResp.SizeOfArray("Instances[i].LayerIds");
        while (j < count_j) {
            jResp.put_J(j);
            strVal = jResp.stringOf("Instances[i].LayerIds[j]");
            j = j + 1;
        }

        j = 0;
        count_j = jResp.SizeOfArray("Instances[i].SecurityGroupIds");
        while (j < count_j) {
            jResp.put_J(j);
            strVal = jResp.stringOf("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"
    //     }
    //   ]
    // }
    }