Chilkat Online Tools

DescribeConfigRules Swift3 Example

AWS Config

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

    // URL: https://config.us-west-2.amazonaws.com/
    // Use the same region as specified above.
    success = rest.connect("config.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("ConfigRuleNames[0]", value: "string")
    json.update("NextToken", value: "string")

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

    // {
    //   "ConfigRuleNames": [
    //     "string"
    //   ],
    //   "NextToken": "string"
    // }

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

    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 ConfigRuleArn: String?
    var ConfigRuleId: String?
    var ConfigRuleName: String?
    var ConfigRuleState: String?
    var CreatedBy: String?
    var Description: String?
    var InputParameters: String?
    var MaximumExecutionFrequency: String?
    var ComplianceResourceId: String?
    var TagKey: String?
    var TagValue: String?
    var EnableDebugLogDelivery: Int
    var PolicyRuntime: String?
    var PolicyText: String?
    var Owner: String?
    var SourceIdentifier: String?
    var j: Int
    var count_j: Int
    var strVal: String?
    var EventSource: String?
    var MessageType: String?

    var NextToken: String? = jResp.string(of: "NextToken")
    var i: Int = 0
    var count_i: Int = jResp.size(ofArray: "ConfigRules").intValue
    while i < count_i {
        jResp.i = i
        ConfigRuleArn = jResp.string(of: "ConfigRules[i].ConfigRuleArn")
        ConfigRuleId = jResp.string(of: "ConfigRules[i].ConfigRuleId")
        ConfigRuleName = jResp.string(of: "ConfigRules[i].ConfigRuleName")
        ConfigRuleState = jResp.string(of: "ConfigRules[i].ConfigRuleState")
        CreatedBy = jResp.string(of: "ConfigRules[i].CreatedBy")
        Description = jResp.string(of: "ConfigRules[i].Description")
        InputParameters = jResp.string(of: "ConfigRules[i].InputParameters")
        MaximumExecutionFrequency = jResp.string(of: "ConfigRules[i].MaximumExecutionFrequency")
        ComplianceResourceId = jResp.string(of: "ConfigRules[i].Scope.ComplianceResourceId")
        TagKey = jResp.string(of: "ConfigRules[i].Scope.TagKey")
        TagValue = jResp.string(of: "ConfigRules[i].Scope.TagValue")
        EnableDebugLogDelivery = jResp.int(of: "ConfigRules[i].Source.CustomPolicyDetails.EnableDebugLogDelivery").intValue
        PolicyRuntime = jResp.string(of: "ConfigRules[i].Source.CustomPolicyDetails.PolicyRuntime")
        PolicyText = jResp.string(of: "ConfigRules[i].Source.CustomPolicyDetails.PolicyText")
        Owner = jResp.string(of: "ConfigRules[i].Source.Owner")
        SourceIdentifier = jResp.string(of: "ConfigRules[i].Source.SourceIdentifier")
        j = 0
        count_j = jResp.size(ofArray: "ConfigRules[i].Scope.ComplianceResourceTypes").intValue
        while j < count_j {
            jResp.j = j
            strVal = jResp.string(of: "ConfigRules[i].Scope.ComplianceResourceTypes[j]")
            j = j + 1
        }

        j = 0
        count_j = jResp.size(ofArray: "ConfigRules[i].Source.SourceDetails").intValue
        while j < count_j {
            jResp.j = j
            EventSource = jResp.string(of: "ConfigRules[i].Source.SourceDetails[j].EventSource")
            MaximumExecutionFrequency = jResp.string(of: "ConfigRules[i].Source.SourceDetails[j].MaximumExecutionFrequency")
            MessageType = jResp.string(of: "ConfigRules[i].Source.SourceDetails[j].MessageType")
            j = j + 1
        }

        i = i + 1
    }

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

    // {
    //   "ConfigRules": [
    //     {
    //       "ConfigRuleArn": "string",
    //       "ConfigRuleId": "string",
    //       "ConfigRuleName": "string",
    //       "ConfigRuleState": "string",
    //       "CreatedBy": "string",
    //       "Description": "string",
    //       "InputParameters": "string",
    //       "MaximumExecutionFrequency": "string",
    //       "Scope": {
    //         "ComplianceResourceId": "string",
    //         "ComplianceResourceTypes": [
    //           "string"
    //         ],
    //         "TagKey": "string",
    //         "TagValue": "string"
    //       },
    //       "Source": {
    //         "CustomPolicyDetails": {
    //           "EnableDebugLogDelivery": boolean,
    //           "PolicyRuntime": "string",
    //           "PolicyText": "string"
    //         },
    //         "Owner": "string",
    //         "SourceDetails": [
    //           {
    //             "EventSource": "string",
    //             "MaximumExecutionFrequency": "string",
    //             "MessageType": "string"
    //           }
    //         ],
    //         "SourceIdentifier": "string"
    //       }
    //     }
    //   ],
    //   "NextToken": "string"
    // }

}