DescribeInstances Objective-C Example
#import <CkoRest.h>
#import <CkoAuthAws.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>
#import <NSString.h>
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoRest *rest = [[CkoRest alloc] init];
BOOL success;
CkoAuthAws *authAws = [[CkoAuthAws alloc] init];
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: [NSNumber numberWithInt: 443] tls: YES autoReconnect: YES];
if (success != YES) {
NSLog(@"%@%d",@"ConnectFailReason: ",[rest.ConnectFailReason intValue]);
NSLog(@"%@",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
CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json UpdateString: @"InstanceIds[0]" value: @"string"];
[json UpdateString: @"LayerId" value: @"string"];
[json UpdateString: @"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"];
CkoStringBuilder *sbRequestBody = [[CkoStringBuilder alloc] init];
[json EmitSb: sbRequestBody];
CkoStringBuilder *sbResponseBody = [[CkoStringBuilder alloc] init];
success = [rest FullRequestSb: @"POST" uriPath: @"/" requestBody: sbRequestBody responseBody: sbResponseBody];
if (success != YES) {
NSLog(@"%@",rest.LastErrorText);
return;
}
int respStatusCode = [rest.ResponseStatusCode intValue];
NSLog(@"%@%d",@"response status code = ",respStatusCode);
if (respStatusCode != 200) {
NSLog(@"%@",@"Response Header:");
NSLog(@"%@",rest.ResponseHeader);
NSLog(@"%@",@"Response Body:");
NSLog(@"%@",[sbResponseBody GetAsString]);
return;
}
CkoJsonObject *jResp = [[CkoJsonObject alloc] init];
[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
NSString *AgentVersion = 0;
NSString *AmiId = 0;
NSString *Architecture = 0;
NSString *Arn = 0;
NSString *AutoScalingType = 0;
NSString *AvailabilityZone = 0;
NSString *CreatedAt = 0;
int EbsOptimized;
NSString *Ec2InstanceId = 0;
NSString *EcsClusterArn = 0;
NSString *EcsContainerInstanceArn = 0;
NSString *ElasticIp = 0;
NSString *Hostname = 0;
NSString *InfrastructureClass = 0;
int InstallUpdatesOnBoot;
NSString *InstanceId = 0;
NSString *InstanceProfileArn = 0;
NSString *InstanceType = 0;
NSString *LastServiceErrorId = 0;
NSString *Os = 0;
NSString *Platform = 0;
NSString *PrivateDns = 0;
NSString *PrivateIp = 0;
NSString *PublicDns = 0;
NSString *PublicIp = 0;
NSString *RegisteredBy = 0;
NSString *ReportedAgentVersion = 0;
NSString *Family = 0;
NSString *Name = 0;
NSString *Version = 0;
NSString *RootDeviceType = 0;
NSString *RootDeviceVolumeId = 0;
NSString *SshHostDsaKeyFingerprint = 0;
NSString *SshHostRsaKeyFingerprint = 0;
NSString *SshKeyName = 0;
NSString *StackId = 0;
NSString *Status = 0;
NSString *SubnetId = 0;
NSString *Tenancy = 0;
NSString *VirtualizationType = 0;
int j;
int count_j;
NSString *DeviceName = 0;
int DeleteOnTermination;
int Iops;
NSString *SnapshotId = 0;
int VolumeSize;
NSString *VolumeType = 0;
NSString *NoDevice = 0;
NSString *VirtualName = 0;
NSString *strVal = 0;
int i = 0;
int count_i = [[jResp SizeOfArray: @"Instances"] intValue];
while (i < count_i) {
jResp.I = [NSNumber numberWithInt: 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"] intValue];
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"] intValue];
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"] intValue];
while (j < count_j) {
jResp.J = [NSNumber numberWithInt: j];
DeviceName = [jResp StringOf: @"Instances[i].BlockDeviceMappings[j].DeviceName"];
DeleteOnTermination = [[jResp IntOf: @"Instances[i].BlockDeviceMappings[j].Ebs.DeleteOnTermination"] intValue];
Iops = [[jResp IntOf: @"Instances[i].BlockDeviceMappings[j].Ebs.Iops"] intValue];
SnapshotId = [jResp StringOf: @"Instances[i].BlockDeviceMappings[j].Ebs.SnapshotId"];
VolumeSize = [[jResp IntOf: @"Instances[i].BlockDeviceMappings[j].Ebs.VolumeSize"] intValue];
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"] intValue];
while (j < count_j) {
jResp.J = [NSNumber numberWithInt: j];
strVal = [jResp StringOf: @"Instances[i].LayerIds[j]"];
j = j + 1;
}
j = 0;
count_j = [[jResp SizeOfArray: @"Instances[i].SecurityGroupIds"] intValue];
while (j < count_j) {
jResp.J = [NSNumber numberWithInt: 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"
// }
// ]
// }