Chilkat Online Tools

UpdateComputeEnvironment Swift3 Example

AWS Batch

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 = "batch"
    // SetAuthAws causes Chilkat to automatically add the following headers: Authorization, X-Amz-Date
    rest.setAuthAws(authAws)

    // URL: https://batch.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.connect("batch.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("computeEnvironment", value: "string")
    json.update("computeResources.allocationStrategy", value: "string")
    json.updateInt("computeResources.bidPercentage", value: 123)
    json.updateInt("computeResources.desiredvCpus", value: 123)
    json.update("computeResources.ec2Configuration[0].imageIdOverride", value: "string")
    json.update("computeResources.ec2Configuration[0].imageType", value: "string")
    json.update("computeResources.ec2KeyPair", value: "string")
    json.update("computeResources.imageId", value: "string")
    json.update("computeResources.instanceRole", value: "string")
    json.update("computeResources.instanceTypes[0]", value: "string")
    json.update("computeResources.launchTemplate.launchTemplateId", value: "string")
    json.update("computeResources.launchTemplate.launchTemplateName", value: "string")
    json.update("computeResources.launchTemplate.version", value: "string")
    json.updateInt("computeResources.maxvCpus", value: 123)
    json.updateInt("computeResources.minvCpus", value: 123)
    json.update("computeResources.placementGroup", value: "string")
    json.update("computeResources.securityGroupIds[0]", value: "string")
    json.update("computeResources.subnets[0]", value: "string")
    json.update("computeResources.tags.string", value: "string")
    json.update("computeResources.type", value: "string")
    json.updateInt("computeResources.updateToLatestImageVersion", value: 123)
    json.update("serviceRole", value: "string")
    json.update("state", value: "string")
    json.updateInt("unmanagedvCpus", value: 123)
    json.updateInt("updatePolicy.jobExecutionTimeoutMinutes", value: 123)
    json.updateInt("updatePolicy.terminateJobsOnUpdate", value: 123)

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

    // {
    //   "computeEnvironment": "string",
    //   "computeResources": {
    //     "allocationStrategy": "string",
    //     "bidPercentage": number,
    //     "desiredvCpus": number,
    //     "ec2Configuration": [
    //       {
    //         "imageIdOverride": "string",
    //         "imageType": "string"
    //       }
    //     ],
    //     "ec2KeyPair": "string",
    //     "imageId": "string",
    //     "instanceRole": "string",
    //     "instanceTypes": [
    //       "string"
    //     ],
    //     "launchTemplate": {
    //       "launchTemplateId": "string",
    //       "launchTemplateName": "string",
    //       "version": "string"
    //     },
    //     "maxvCpus": number,
    //     "minvCpus": number,
    //     "placementGroup": "string",
    //     "securityGroupIds": [
    //       "string"
    //     ],
    //     "subnets": [
    //       "string"
    //     ],
    //     "tags": {
    //       "string": "string"
    //     },
    //     "type": "string",
    //     "updateToLatestImageVersion": boolean
    //   },
    //   "serviceRole": "string",
    //   "state": "string",
    //   "unmanagedvCpus": number,
    //   "updatePolicy": {
    //     "jobExecutionTimeoutMinutes": number,
    //     "terminateJobsOnUpdate": boolean
    //   }
    // }

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

    let sbRequestBody = CkoStringBuilder()!
    json.emitSb(sbRequestBody)
    let sbResponseBody = CkoStringBuilder()!
    success = rest.fullRequestSb("POST", uriPath: "/v1/updatecomputeenvironment", 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
    }

    let jResp = CkoJsonObject()!
    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

    var computeEnvironmentArn: String? = jResp.string(of: "computeEnvironmentArn")
    var computeEnvironmentName: String? = jResp.string(of: "computeEnvironmentName")

    // A sample JSON response body parsed by the above code:

    // {
    //   "computeEnvironmentArn": "string",
    //   "computeEnvironmentName": "string"
    // }

}