DescribeInstances Java Example
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkRest rest = new CkRest();
boolean success;
CkAuthAws authAws = new CkAuthAws();
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) {
System.out.println("ConnectFailReason: " + rest.get_ConnectFailReason());
System.out.println(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
CkJsonObject json = new CkJsonObject();
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 = new CkStringBuilder();
json.EmitSb(sbRequestBody);
CkStringBuilder sbResponseBody = new CkStringBuilder();
success = rest.FullRequestSb("POST","/",sbRequestBody,sbResponseBody);
if (success != true) {
System.out.println(rest.lastErrorText());
return;
}
int respStatusCode = rest.get_ResponseStatusCode();
System.out.println("response status code = " + respStatusCode);
if (respStatusCode != 200) {
System.out.println("Response Header:");
System.out.println(rest.responseHeader());
System.out.println("Response Body:");
System.out.println(sbResponseBody.getAsString());
return;
}
CkJsonObject jResp = new CkJsonObject();
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
String AgentVersion;
String AmiId;
String Architecture;
String Arn;
String AutoScalingType;
String AvailabilityZone;
String CreatedAt;
int EbsOptimized;
String Ec2InstanceId;
String EcsClusterArn;
String EcsContainerInstanceArn;
String ElasticIp;
String Hostname;
String InfrastructureClass;
int InstallUpdatesOnBoot;
String InstanceId;
String InstanceProfileArn;
String InstanceType;
String LastServiceErrorId;
String Os;
String Platform;
String PrivateDns;
String PrivateIp;
String PublicDns;
String PublicIp;
String RegisteredBy;
String ReportedAgentVersion;
String Family;
String Name;
String Version;
String RootDeviceType;
String RootDeviceVolumeId;
String SshHostDsaKeyFingerprint;
String SshHostRsaKeyFingerprint;
String SshKeyName;
String StackId;
String Status;
String SubnetId;
String Tenancy;
String VirtualizationType;
int j;
int count_j;
String DeviceName;
int DeleteOnTermination;
int Iops;
String SnapshotId;
int VolumeSize;
String VolumeType;
String NoDevice;
String VirtualName;
String strVal;
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"
// }
// ]
// }
}
}