Chilkat Online Tools

UpdateLayer unicodeC Example

AWS OpsWorks

#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    HCkAuthAwsW authAws;
    HCkJsonObjectW json;
    HCkStringBuilderW sbRequestBody;
    HCkStringBuilderW sbResponseBody;
    int respStatusCode;

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

    rest = CkRestW_Create();

    authAws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
    CkAuthAwsW_putSecretKey(authAws,L"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.)
    CkAuthAwsW_putRegion(authAws,L"us-west-2");
    CkAuthAwsW_putServiceName(authAws,L"opsworks");
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    CkRestW_SetAuthAws(rest,authAws);

    // URL: https://opsworks.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = CkRestW_Connect(rest,L"opsworks.us-west-2.amazonaws.com",443,TRUE,TRUE);
    if (success != TRUE) {
        wprintf(L"ConnectFailReason: %d\n",CkRestW_getConnectFailReason(rest));
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        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

    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"Attributes.string",L"string");
    CkJsonObjectW_UpdateInt(json,L"AutoAssignElasticIps",123);
    CkJsonObjectW_UpdateInt(json,L"AutoAssignPublicIps",123);
    CkJsonObjectW_UpdateInt(json,L"CloudWatchLogsConfiguration.Enabled",123);
    CkJsonObjectW_UpdateInt(json,L"CloudWatchLogsConfiguration.LogStreams[0].BatchCount",123);
    CkJsonObjectW_UpdateInt(json,L"CloudWatchLogsConfiguration.LogStreams[0].BatchSize",123);
    CkJsonObjectW_UpdateInt(json,L"CloudWatchLogsConfiguration.LogStreams[0].BufferDuration",123);
    CkJsonObjectW_UpdateString(json,L"CloudWatchLogsConfiguration.LogStreams[0].DatetimeFormat",L"string");
    CkJsonObjectW_UpdateString(json,L"CloudWatchLogsConfiguration.LogStreams[0].Encoding",L"string");
    CkJsonObjectW_UpdateString(json,L"CloudWatchLogsConfiguration.LogStreams[0].File",L"string");
    CkJsonObjectW_UpdateString(json,L"CloudWatchLogsConfiguration.LogStreams[0].FileFingerprintLines",L"string");
    CkJsonObjectW_UpdateString(json,L"CloudWatchLogsConfiguration.LogStreams[0].InitialPosition",L"string");
    CkJsonObjectW_UpdateString(json,L"CloudWatchLogsConfiguration.LogStreams[0].LogGroupName",L"string");
    CkJsonObjectW_UpdateString(json,L"CloudWatchLogsConfiguration.LogStreams[0].MultiLineStartPattern",L"string");
    CkJsonObjectW_UpdateString(json,L"CloudWatchLogsConfiguration.LogStreams[0].TimeZone",L"string");
    CkJsonObjectW_UpdateString(json,L"CustomInstanceProfileArn",L"string");
    CkJsonObjectW_UpdateString(json,L"CustomJson",L"string");
    CkJsonObjectW_UpdateString(json,L"CustomRecipes.Configure[0]",L"string");
    CkJsonObjectW_UpdateString(json,L"CustomRecipes.Deploy[0]",L"string");
    CkJsonObjectW_UpdateString(json,L"CustomRecipes.Setup[0]",L"string");
    CkJsonObjectW_UpdateString(json,L"CustomRecipes.Shutdown[0]",L"string");
    CkJsonObjectW_UpdateString(json,L"CustomRecipes.Undeploy[0]",L"string");
    CkJsonObjectW_UpdateString(json,L"CustomSecurityGroupIds[0]",L"string");
    CkJsonObjectW_UpdateInt(json,L"EnableAutoHealing",123);
    CkJsonObjectW_UpdateInt(json,L"InstallUpdatesOnBoot",123);
    CkJsonObjectW_UpdateString(json,L"LayerId",L"string");
    CkJsonObjectW_UpdateInt(json,L"LifecycleEventConfiguration.Shutdown.DelayUntilElbConnectionsDrained",123);
    CkJsonObjectW_UpdateInt(json,L"LifecycleEventConfiguration.Shutdown.ExecutionTimeout",123);
    CkJsonObjectW_UpdateString(json,L"Name",L"string");
    CkJsonObjectW_UpdateString(json,L"Packages[0]",L"string");
    CkJsonObjectW_UpdateString(json,L"Shortname",L"string");
    CkJsonObjectW_UpdateInt(json,L"UseEbsOptimizedInstances",123);
    CkJsonObjectW_UpdateInt(json,L"VolumeConfigurations[0].Encrypted",123);
    CkJsonObjectW_UpdateInt(json,L"VolumeConfigurations[0].Iops",123);
    CkJsonObjectW_UpdateString(json,L"VolumeConfigurations[0].MountPoint",L"string");
    CkJsonObjectW_UpdateInt(json,L"VolumeConfigurations[0].NumberOfDisks",123);
    CkJsonObjectW_UpdateInt(json,L"VolumeConfigurations[0].RaidLevel",123);
    CkJsonObjectW_UpdateInt(json,L"VolumeConfigurations[0].Size",123);
    CkJsonObjectW_UpdateString(json,L"VolumeConfigurations[0].VolumeType",L"string");

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

    // {
    //   "Attributes": {
    //     "string": "string"
    //   },
    //   "AutoAssignElasticIps": boolean,
    //   "AutoAssignPublicIps": boolean,
    //   "CloudWatchLogsConfiguration": {
    //     "Enabled": boolean,
    //     "LogStreams": [
    //       {
    //         "BatchCount": number,
    //         "BatchSize": number,
    //         "BufferDuration": number,
    //         "DatetimeFormat": "string",
    //         "Encoding": "string",
    //         "File": "string",
    //         "FileFingerprintLines": "string",
    //         "InitialPosition": "string",
    //         "LogGroupName": "string",
    //         "MultiLineStartPattern": "string",
    //         "TimeZone": "string"
    //       }
    //     ]
    //   },
    //   "CustomInstanceProfileArn": "string",
    //   "CustomJson": "string",
    //   "CustomRecipes": {
    //     "Configure": [
    //       "string"
    //     ],
    //     "Deploy": [
    //       "string"
    //     ],
    //     "Setup": [
    //       "string"
    //     ],
    //     "Shutdown": [
    //       "string"
    //     ],
    //     "Undeploy": [
    //       "string"
    //     ]
    //   },
    //   "CustomSecurityGroupIds": [
    //     "string"
    //   ],
    //   "EnableAutoHealing": boolean,
    //   "InstallUpdatesOnBoot": boolean,
    //   "LayerId": "string",
    //   "LifecycleEventConfiguration": {
    //     "Shutdown": {
    //       "DelayUntilElbConnectionsDrained": boolean,
    //       "ExecutionTimeout": number
    //     }
    //   },
    //   "Name": "string",
    //   "Packages": [
    //     "string"
    //   ],
    //   "Shortname": "string",
    //   "UseEbsOptimizedInstances": boolean,
    //   "VolumeConfigurations": [
    //     {
    //       "Encrypted": boolean,
    //       "Iops": number,
    //       "MountPoint": "string",
    //       "NumberOfDisks": number,
    //       "RaidLevel": number,
    //       "Size": number,
    //       "VolumeType": "string"
    //     }
    //   ]
    // }

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

    sbRequestBody = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(json,sbRequestBody);
    sbResponseBody = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"POST",L"/",sbRequestBody,sbResponseBody);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    wprintf(L"response status code = %d\n",respStatusCode);
    if (respStatusCode != 200) {
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponseBody));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkJsonObjectW_Dispose(json);
        CkStringBuilderW_Dispose(sbRequestBody);
        CkStringBuilderW_Dispose(sbResponseBody);
        return;
    }

    // If the action is successful, the service sends back an HTTP 200 response with an empty HTTP body.


    CkRestW_Dispose(rest);
    CkAuthAwsW_Dispose(authAws);
    CkJsonObjectW_Dispose(json);
    CkStringBuilderW_Dispose(sbRequestBody);
    CkStringBuilderW_Dispose(sbResponseBody);

    }