# Path: repokid/exceptions.py
# class IAMError(Exception):
#     pass


import datetime
import json
import logging
import re
import botocore
from typing import Any
from typing import Dict
from cloudaux.aws.iam import delete_role_policy
from cloudaux.aws.iam import put_role_policy
from cloudaux.aws.sts import boto3_cached_conn
from mypy_boto3_iam.client import IAMClient
from repokid.exceptions import IAMError
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.



LOGGER = logging.getLogger("repokid")
MAX_AWS_POLICY_SIZE = 10240


def update_repoed_description(role_name: str, conn_details: Dict[str, Any]) -> None:
    client: IAMClient = boto3_cached_conn("iam", **conn_details)
    try:
        description = client.get_role(RoleName=role_name)["Role"].get("Description", "")
    except KeyError:
        return
    date_string = datetime.datetime.now(tz=datetime.timezone.utc).strftime("%m/%d/%y")
    if "; Repokid repoed" in description:
        new_description = re.sub(
            r"; Repokid repoed [0-9]{2}\/[0-9]{2}\/[0-9]{2}",
            f"; Repokid repoed {date_string}",
            description,
        )
    else:
        new_description = description + " ; Repokid repoed {}".format(date_string)
    # IAM role descriptions have a max length of 1000, if our new length would be longer, skip this
    if len(new_description) < 1000:
        client.update_role_description(RoleName=role_name, Description=new_description)

# Path: repokid/exceptions.py
# class IAMError(Exception):
#     pass


# Path: repokid/utils/iam.py
import datetime
import json
import logging
import re
import botocore
from typing import Any
from typing import Dict
from cloudaux.aws.iam import delete_role_policy
from cloudaux.aws.iam import put_role_policy
from cloudaux.aws.sts import boto3_cached_conn
from mypy_boto3_iam.client import IAMClient
from repokid.exceptions import IAMError

#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.



LOGGER = logging.getLogger("repokid")
MAX_AWS_POLICY_SIZE = 10240


def update_repoed_description(role_name: str, conn_details: Dict[str, Any]) -> None:
    client: IAMClient = boto3_cached_conn("iam", **conn_details)
    try:
        description = client.get_role(RoleName=role_name)["Role"].get("Description", "")
    except KeyError:
        return
    date_string = datetime.datetime.now(tz=datetime.timezone.utc).strftime("%m/%d/%y")
    if "; Repokid repoed" in description:
        new_description = re.sub(
            r"; Repokid repoed [0-9]{2}\/[0-9]{2}\/[0-9]{2}",
            f"; Repokid repoed {date_string}",
            description,
        )
    else:
        new_description = description + " ; Repokid repoed {}".format(date_string)
    # IAM role descriptions have a max length of 1000, if our new length would be longer, skip this
    if len(new_description) < 1000:
        client.update_role_description(RoleName=role_name, Description=new_description)

    else: