Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS PHP SDK Filter Aws\Result Object

I am using AWS SDK PHP V3. If i run following code

$result = $client->getAccountPasswordPolicy([]);
$result = $result->toArray();

It returns result below

Array
(
    [PasswordPolicy] => Array
        (
            [MinimumPasswordLength] => 6
            [RequireSymbols] => 
            [RequireNumbers] => 
            [RequireUppercaseCharacters] => 1
            [RequireLowercaseCharacters] => 
            [AllowUsersToChangePassword] => 
            [ExpirePasswords] => 
        )

    [@metadata] => Array
        (
            [statusCode] => 200
            [effectiveUri] => https://iam.amazonaws.com
            [headers] => Array
                (
                    [x-amzn-requestid] => 437f79e9-9ee0-11e7-8a82-47e702eaf8eb
                    [content-type] => text/xml
                    [content-length] => 740
                    [date] => Thu, 21 Sep 2017 15:19:29 GMT
                    [connection] => close
                )

            [transferStats] => Array
                (
                    [http] => Array
                        (
                            [0] => Array
                                (
                                )

                        )

                )

        )

)

How to get result of only RequireUppercaseCharacters or RequireLowercaseCharacters using AWS SDK or do i have to use array functions to filter data?

Reference Link

like image 815
niteshd22 Avatar asked Mar 08 '23 00:03

niteshd22


2 Answers

ah..you are asking about JMESPath. you can achieve what are you are trying to do by

$result = $client->getAccountPasswordPolicy([]);
$data = $result->search('PasswordPolicy.RequireUppercaseCharacters');

you can read more about JMESPath here

like image 119
Gaurav Singh Faujdar Avatar answered Mar 19 '23 05:03

Gaurav Singh Faujdar


This seems like it's not possible with the current API. Although it might seem like the method parameter is open to indicate which specific items you need (because it's an array), there is no signal whatsoever of this being the case anywhere in the documentation.

Compare the documentation for getAccountPasswordPolicy() with, for example, ListPolicies(). The latter explicitly says:

You can filter the list of policies that is returned using the optional OnlyAttached, Scope, and PathPrefix parameters.

In contrast, the docs for getAccountPasswordPolicy() make no mention of this. Not even a mention of the parameter. The parameter is mentioned however in the method signature, which makes me think this might come in a future version of the API.

The CLI documentation doesn't have any parameters of the like either, so this definitely seems like something you can't do at the moment.

I did some tests, just in case, with no success:

$result = $client->getAccountPasswordPolicy(["RequireUppercaseCharacters"]);

returns

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Found 1 error while validating the input provided for the GetAccountPasswordPolicy operation: must be an associative array. Found array(2)' in /Applications/MAMP/htdocs/awss3/vendor/aws/aws-sdk-php/src/Api/Validator.php:65

And specifying an associative array does not filter anything:

$result = $client->getAccountPasswordPolicy(["PasswordPolicy" => ["RequireUppercaseCharacters"]]);

Aws\Result Object
(
    [data:Aws\Result:private] => Array
        (
            [PasswordPolicy] => Array
                (
                    [MinimumPasswordLength] => 6
                    [RequireSymbols] => 
                    [RequireNumbers] => 
                    [RequireUppercaseCharacters] => 1
                    [RequireLowercaseCharacters] => 
                    [AllowUsersToChangePassword] => 1
                    [ExpirePasswords] => 
                    [HardExpiry] => 
                )

            [@metadata] => Array
                (
                    [statusCode] => 200
                    [effectiveUri] => https://iam.amazonaws.com
                    [headers] => Array
                        (
                            // redacted
                        )

                    [transferStats] => Array
                        (
                            [http] => Array
                                (
                                    [0] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)

So, you're only option for now is going through the array's keys, as always:

$result["PasswordPolicy"]["RequireUppercaseCharacters"];
like image 25
ishegg Avatar answered Mar 19 '23 07:03

ishegg