Chilkat Online Tools

UpdateLayer PureBasic Example

AWS OpsWorks

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkRest.pb"
IncludeFile "CkAuthAws.pb"

Procedure ChilkatExample()

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

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    authAws.i = CkAuthAws::ckCreate()
    If authAws.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAuthAws::setCkAccessKey(authAws, "AWS_ACCESS_KEY")
    CkAuthAws::setCkSecretKey(authAws, "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.)
    CkAuthAws::setCkRegion(authAws, "us-west-2")
    CkAuthAws::setCkServiceName(authAws, "opsworks")
    ; SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    CkRest::ckSetAuthAws(rest,authAws)

    ; URL: https://opsworks.us-west-2.amazonaws.com/
    ; Use the same region as specified above.
    success = CkRest::ckConnect(rest,"opsworks.us-west-2.amazonaws.com",443,1,1)
    If success <> 1
        Debug "ConnectFailReason: " + Str(CkRest::ckConnectFailReason(rest))
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        ProcedureReturn
    EndIf

    ; 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.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"Attributes.string","string")
    CkJsonObject::ckUpdateInt(json,"AutoAssignElasticIps",123)
    CkJsonObject::ckUpdateInt(json,"AutoAssignPublicIps",123)
    CkJsonObject::ckUpdateInt(json,"CloudWatchLogsConfiguration.Enabled",123)
    CkJsonObject::ckUpdateInt(json,"CloudWatchLogsConfiguration.LogStreams[0].BatchCount",123)
    CkJsonObject::ckUpdateInt(json,"CloudWatchLogsConfiguration.LogStreams[0].BatchSize",123)
    CkJsonObject::ckUpdateInt(json,"CloudWatchLogsConfiguration.LogStreams[0].BufferDuration",123)
    CkJsonObject::ckUpdateString(json,"CloudWatchLogsConfiguration.LogStreams[0].DatetimeFormat","string")
    CkJsonObject::ckUpdateString(json,"CloudWatchLogsConfiguration.LogStreams[0].Encoding","string")
    CkJsonObject::ckUpdateString(json,"CloudWatchLogsConfiguration.LogStreams[0].File","string")
    CkJsonObject::ckUpdateString(json,"CloudWatchLogsConfiguration.LogStreams[0].FileFingerprintLines","string")
    CkJsonObject::ckUpdateString(json,"CloudWatchLogsConfiguration.LogStreams[0].InitialPosition","string")
    CkJsonObject::ckUpdateString(json,"CloudWatchLogsConfiguration.LogStreams[0].LogGroupName","string")
    CkJsonObject::ckUpdateString(json,"CloudWatchLogsConfiguration.LogStreams[0].MultiLineStartPattern","string")
    CkJsonObject::ckUpdateString(json,"CloudWatchLogsConfiguration.LogStreams[0].TimeZone","string")
    CkJsonObject::ckUpdateString(json,"CustomInstanceProfileArn","string")
    CkJsonObject::ckUpdateString(json,"CustomJson","string")
    CkJsonObject::ckUpdateString(json,"CustomRecipes.Configure[0]","string")
    CkJsonObject::ckUpdateString(json,"CustomRecipes.Deploy[0]","string")
    CkJsonObject::ckUpdateString(json,"CustomRecipes.Setup[0]","string")
    CkJsonObject::ckUpdateString(json,"CustomRecipes.Shutdown[0]","string")
    CkJsonObject::ckUpdateString(json,"CustomRecipes.Undeploy[0]","string")
    CkJsonObject::ckUpdateString(json,"CustomSecurityGroupIds[0]","string")
    CkJsonObject::ckUpdateInt(json,"EnableAutoHealing",123)
    CkJsonObject::ckUpdateInt(json,"InstallUpdatesOnBoot",123)
    CkJsonObject::ckUpdateString(json,"LayerId","string")
    CkJsonObject::ckUpdateInt(json,"LifecycleEventConfiguration.Shutdown.DelayUntilElbConnectionsDrained",123)
    CkJsonObject::ckUpdateInt(json,"LifecycleEventConfiguration.Shutdown.ExecutionTimeout",123)
    CkJsonObject::ckUpdateString(json,"Name","string")
    CkJsonObject::ckUpdateString(json,"Packages[0]","string")
    CkJsonObject::ckUpdateString(json,"Shortname","string")
    CkJsonObject::ckUpdateInt(json,"UseEbsOptimizedInstances",123)
    CkJsonObject::ckUpdateInt(json,"VolumeConfigurations[0].Encrypted",123)
    CkJsonObject::ckUpdateInt(json,"VolumeConfigurations[0].Iops",123)
    CkJsonObject::ckUpdateString(json,"VolumeConfigurations[0].MountPoint","string")
    CkJsonObject::ckUpdateInt(json,"VolumeConfigurations[0].NumberOfDisks",123)
    CkJsonObject::ckUpdateInt(json,"VolumeConfigurations[0].RaidLevel",123)
    CkJsonObject::ckUpdateInt(json,"VolumeConfigurations[0].Size",123)
    CkJsonObject::ckUpdateString(json,"VolumeConfigurations[0].VolumeType","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"
    ;     }
    ;   ]
    ; }

    CkRest::ckAddHeader(rest,"Content-Type","application/x-amz-json-1.1")
    CkRest::ckAddHeader(rest,"X-Amz-Target","OpsWorks_20130218.UpdateLayer")

    sbRequestBody.i = CkStringBuilder::ckCreate()
    If sbRequestBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckEmitSb(json,sbRequestBody)
    sbResponseBody.i = CkStringBuilder::ckCreate()
    If sbResponseBody.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkRest::ckFullRequestSb(rest,"POST","/",sbRequestBody,sbResponseBody)
    If success <> 1
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

    respStatusCode.i = CkRest::ckResponseStatusCode(rest)
    Debug "response status code = " + Str(respStatusCode)
    If respStatusCode <> 200
        Debug "Response Header:"
        Debug CkRest::ckResponseHeader(rest)
        Debug "Response Body:"
        Debug CkStringBuilder::ckGetAsString(sbResponseBody)
        CkRest::ckDispose(rest)
        CkAuthAws::ckDispose(authAws)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbRequestBody)
        CkStringBuilder::ckDispose(sbResponseBody)
        ProcedureReturn
    EndIf

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


    CkRest::ckDispose(rest)
    CkAuthAws::ckDispose(authAws)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbRequestBody)
    CkStringBuilder::ckDispose(sbResponseBody)


    ProcedureReturn
EndProcedure