Chilkat Online Tools

DescribeStacks 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("StackIds[0]", value: "string")

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

    // {
    //   "StackIds": [
    //     "string"
    //   ]
    // }

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

    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 AgentVersion: String?
    var Arn: String?
    var v_String: String?
    var BerkshelfVersion: String?
    var ManageBerkshelf: Int
    var Name: String?
    var Version: String?
    var CreatedAt: String?
    var Password: String?
    var Revision: String?
    var v_SshKey: String?
    var v_Type: String?
    var v_Url: String?
    var Username: String?
    var CustomJson: String?
    var DefaultAvailabilityZone: String?
    var DefaultInstanceProfileArn: String?
    var DefaultOs: String?
    var DefaultRootDeviceType: String?
    var DefaultSshKeyName: String?
    var DefaultSubnetId: String?
    var HostnameTheme: String?
    var Region: String?
    var ServiceRoleArn: String?
    var StackId: String?
    var UseCustomCookbooks: Int
    var UseOpsworksSecurityGroups: Int
    var VpcId: String?

    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "Stacks").intValue
    while i < count_i {
        jResp.i = i
        AgentVersion = jResp.string(of: "Stacks[i].AgentVersion")
        Arn = jResp.string(of: "Stacks[i].Arn")
        v_String = jResp.string(of: "Stacks[i].Attributes.string")
        BerkshelfVersion = jResp.string(of: "Stacks[i].ChefConfiguration.BerkshelfVersion")
        ManageBerkshelf = jResp.int(of: "Stacks[i].ChefConfiguration.ManageBerkshelf").intValue
        Name = jResp.string(of: "Stacks[i].ConfigurationManager.Name")
        Version = jResp.string(of: "Stacks[i].ConfigurationManager.Version")
        CreatedAt = jResp.string(of: "Stacks[i].CreatedAt")
        Password = jResp.string(of: "Stacks[i].CustomCookbooksSource.Password")
        Revision = jResp.string(of: "Stacks[i].CustomCookbooksSource.Revision")
        v_SshKey = jResp.string(of: "Stacks[i].CustomCookbooksSource.SshKey")
        v_Type = jResp.string(of: "Stacks[i].CustomCookbooksSource.Type")
        v_Url = jResp.string(of: "Stacks[i].CustomCookbooksSource.Url")
        Username = jResp.string(of: "Stacks[i].CustomCookbooksSource.Username")
        CustomJson = jResp.string(of: "Stacks[i].CustomJson")
        DefaultAvailabilityZone = jResp.string(of: "Stacks[i].DefaultAvailabilityZone")
        DefaultInstanceProfileArn = jResp.string(of: "Stacks[i].DefaultInstanceProfileArn")
        DefaultOs = jResp.string(of: "Stacks[i].DefaultOs")
        DefaultRootDeviceType = jResp.string(of: "Stacks[i].DefaultRootDeviceType")
        DefaultSshKeyName = jResp.string(of: "Stacks[i].DefaultSshKeyName")
        DefaultSubnetId = jResp.string(of: "Stacks[i].DefaultSubnetId")
        HostnameTheme = jResp.string(of: "Stacks[i].HostnameTheme")
        Name = jResp.string(of: "Stacks[i].Name")
        Region = jResp.string(of: "Stacks[i].Region")
        ServiceRoleArn = jResp.string(of: "Stacks[i].ServiceRoleArn")
        StackId = jResp.string(of: "Stacks[i].StackId")
        UseCustomCookbooks = jResp.int(of: "Stacks[i].UseCustomCookbooks").intValue
        UseOpsworksSecurityGroups = jResp.int(of: "Stacks[i].UseOpsworksSecurityGroups").intValue
        VpcId = jResp.string(of: "Stacks[i].VpcId")
        i = i + 1
    }

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

    // {
    //   "Stacks": [
    //     {
    //       "AgentVersion": "string",
    //       "Arn": "string",
    //       "Attributes": {
    //         "string": "string"
    //       },
    //       "ChefConfiguration": {
    //         "BerkshelfVersion": "string",
    //         "ManageBerkshelf": boolean
    //       },
    //       "ConfigurationManager": {
    //         "Name": "string",
    //         "Version": "string"
    //       },
    //       "CreatedAt": "string",
    //       "CustomCookbooksSource": {
    //         "Password": "string",
    //         "Revision": "string",
    //         "SshKey": "string",
    //         "Type": "string",
    //         "Url": "string",
    //         "Username": "string"
    //       },
    //       "CustomJson": "string",
    //       "DefaultAvailabilityZone": "string",
    //       "DefaultInstanceProfileArn": "string",
    //       "DefaultOs": "string",
    //       "DefaultRootDeviceType": "string",
    //       "DefaultSshKeyName": "string",
    //       "DefaultSubnetId": "string",
    //       "HostnameTheme": "string",
    //       "Name": "string",
    //       "Region": "string",
    //       "ServiceRoleArn": "string",
    //       "StackId": "string",
    //       "UseCustomCookbooks": boolean,
    //       "UseOpsworksSecurityGroups": boolean,
    //       "VpcId": "string"
    //     }
    //   ]
    // }

}