Chilkat Online Tools

UpdateLayer Swift3 Example

AWS OpsWorks

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

    let rest = CkoRest()!
    var success: Bool

    let authAws = CkoAuthAws()!
    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: 443, tls: true, autoReconnect: true)
    if success != true {
        print("ConnectFailReason: \(rest.connectFailReason.intValue)")
        print("\(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

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

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

    let sbRequestBody = CkoStringBuilder()!
    json.emitSb(sbRequestBody)
    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestSb("POST", uriPath: "/", requestBody: sbRequestBody, responseBody: sbResponseBody)
    if success != true {
        print("\(rest.lastErrorText!)")
        return
    }

    var respStatusCode: Int = rest.responseStatusCode.intValue
    print("response status code = \(respStatusCode)")
    if respStatusCode != 200 {
        print("Response Header:")
        print("\(rest.responseHeader!)")
        print("Response Body:")
        print("\(sbResponseBody.getAsString()!)")
        return
    }

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

}