Chilkat Online Tools

StartMaintenance Swift Example

AWS OpsWorks CM

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

    // URL: https://opsworks-cm.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.Connect("opsworks-cm.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.UpdateString("EngineAttributes[0].Name", value: "string")
    json.UpdateString("EngineAttributes[0].Value", value: "string")
    json.UpdateString("ServerName", value: "string")

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

    // {
    //   "EngineAttributes": [
    //     {
    //       "Name": "string",
    //       "Value": "string"
    //     }
    //   ],
    //   "ServerName": "string"
    // }

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

    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
    }

    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 Name: String?
    var Value: String?
    var strVal: String?

    var AssociatePublicIpAddress: Int = jResp.IntOf("Server.AssociatePublicIpAddress").intValue
    var BackupRetentionCount: Int = jResp.IntOf("Server.BackupRetentionCount").intValue
    var CloudFormationStackArn: String? = jResp.StringOf("Server.CloudFormationStackArn")
    var CreatedAt: Int = jResp.IntOf("Server.CreatedAt").intValue
    var CustomDomain: String? = jResp.StringOf("Server.CustomDomain")
    var DisableAutomatedBackup: Int = jResp.IntOf("Server.DisableAutomatedBackup").intValue
    var Endpoint: String? = jResp.StringOf("Server.Endpoint")
    var Engine: String? = jResp.StringOf("Server.Engine")
    var EngineModel: String? = jResp.StringOf("Server.EngineModel")
    var EngineVersion: String? = jResp.StringOf("Server.EngineVersion")
    var InstanceProfileArn: String? = jResp.StringOf("Server.InstanceProfileArn")
    var InstanceType: String? = jResp.StringOf("Server.InstanceType")
    var KeyPair: String? = jResp.StringOf("Server.KeyPair")
    var MaintenanceStatus: String? = jResp.StringOf("Server.MaintenanceStatus")
    var PreferredBackupWindow: String? = jResp.StringOf("Server.PreferredBackupWindow")
    var PreferredMaintenanceWindow: String? = jResp.StringOf("Server.PreferredMaintenanceWindow")
    var ServerArn: String? = jResp.StringOf("Server.ServerArn")
    var ServerName: String? = jResp.StringOf("Server.ServerName")
    var ServiceRoleArn: String? = jResp.StringOf("Server.ServiceRoleArn")
    var Status: String? = jResp.StringOf("Server.Status")
    var StatusReason: String? = jResp.StringOf("Server.StatusReason")
    var i: Int = 0
    var count_i: Int = jResp.SizeOfArray("Server.EngineAttributes").intValue
    while i < count_i {
        jResp.I = i
        Name = jResp.StringOf("Server.EngineAttributes[i].Name")
        Value = jResp.StringOf("Server.EngineAttributes[i].Value")
        i = i + 1
    }

    i = 0
    count_i = jResp.SizeOfArray("Server.SecurityGroupIds").intValue
    while i < count_i {
        jResp.I = i
        strVal = jResp.StringOf("Server.SecurityGroupIds[i]")
        i = i + 1
    }

    i = 0
    count_i = jResp.SizeOfArray("Server.SubnetIds").intValue
    while i < count_i {
        jResp.I = i
        strVal = jResp.StringOf("Server.SubnetIds[i]")
        i = i + 1
    }

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

    // {
    //   "Server": {
    //     "AssociatePublicIpAddress": boolean,
    //     "BackupRetentionCount": number,
    //     "CloudFormationStackArn": "string",
    //     "CreatedAt": number,
    //     "CustomDomain": "string",
    //     "DisableAutomatedBackup": boolean,
    //     "Endpoint": "string",
    //     "Engine": "string",
    //     "EngineAttributes": [
    //       {
    //         "Name": "string",
    //         "Value": "string"
    //       }
    //     ],
    //     "EngineModel": "string",
    //     "EngineVersion": "string",
    //     "InstanceProfileArn": "string",
    //     "InstanceType": "string",
    //     "KeyPair": "string",
    //     "MaintenanceStatus": "string",
    //     "PreferredBackupWindow": "string",
    //     "PreferredMaintenanceWindow": "string",
    //     "SecurityGroupIds": [
    //       "string"
    //     ],
    //     "ServerArn": "string",
    //     "ServerName": "string",
    //     "ServiceRoleArn": "string",
    //     "Status": "string",
    //     "StatusReason": "string",
    //     "SubnetIds": [
    //       "string"
    //     ]
    //   }
    // }

}